- Notifications
You must be signed in to change notification settings - Fork12
An implementation of the LSTAR Grammatical Inference Algorithm
License
gbossert/pylstar
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
pylstar is a free and open source Python implementation of theLSTAR Grammatical inference algorithm. It can be use to automaticaly infer the state machine that best describe the internal of a deterministic black box. To achieve this, pylstar observes the behavior of the target when stimulated with sequence of messages.
It has succesfully been used to infer various protocols such as Botnet protocols, Smart Cards protocols, Cryptographic protocols and Web Servers.
One that wants to usepylstar must write a class that communicates with the targeted black box (i.e. it exposes the Minimaly Adequate Teacher of the targeted reactive System). This can be done by subclassing pylstar.ActiveKnowledgeBase.ActiveKnowledgeBase. If the targeted process is a network server, one can solely subclass pylstar.NetworkActiveKnowledgeBase.
For example, the following class can be use to start and stop a fake coffee machine (coffeemachine.py) through it API (localhost:3000). This class inherits from pylstar.NetworkActiveKnowledgeBase which exposes methods that can send (and read) network messages to (and by) the coffee machine API.
importtimeimportsubprocessfrompylstar.NetworkActiveKnowledgeBaseimportNetworkActiveKnowledgeBaseclassCoffeeMachineKnowledgeBase(NetworkActiveKnowledgeBase):def__init__(self):super(CoffeeMachineKnowledgeBase,self).__init__("localhost",3000)self.__sp=Nonedefstart(self):"""This methods starts the coffee machine (to be triggered before the learning process)."""self.__sp=subprocess.Popen("/usr/bin/python coffeemachine.py",shell=True)# lets wait 5 seconds for the coffee machine to starttime.sleep(5)defstop(self):"""This method stops the coffee machine (to be triggered after the learning process)."""ifself.__spisnotNone:self.__sp.kill()
Given this wrapper, the following snippet can be used to trigger the automatic inference of the coffee machine. This code declares the messages accepted by the API, an instance of our wrapper and returns a pylstar.automata.Automata.Automata (and prints its DOT code) that best describes the behavior of the coffee machine.
frompylstar.LSTARimportLSTARfromCoffeeMachineKnowledgeBaseimportCoffeeMachineKnowledgeBase# list of messages accepted by the coffee machineinput_vocabulary= ["REFILL_WATER","REFILL_COFFEE","PRESS_BUTTON_A","PRESS_BUTTON_B","PRESS_BUTTON_C"]# instanciates our CoffeeMachine MATcoffeeBase=CoffeeMachineKnowledgeBase()try:# starts the coffee machinecoffeeBase.start()# learns its grammarlstar=LSTAR(input_vocabulary,coffeeBase,max_states=10)# stores the coffee machine state machinecoffee_state_machine=lstar.learn()# displays the DOT code of the state machineprint(coffee_state_machine.build_dot_code())finally:coffeeBase.stop()
The execution of this sample returns the state machine illustrated below:
A runnable example of the coffee machine inference is available in test/src/test_pylstar/coffee_machine_example.
Pylstar is a typical python library. It relies on a setup.py file to describe its installation process.The following command can be use to install pylstar on your system:
$ python setup.py install
The implementation of automata in pylstar follows the definition ofMealy Machines. An automaton is made of a unique initial state, states and transitions.
A state (pylstar.automata.state.State) is defined by its name (str) and some transitions (list<pylstar.automata.transition.Transition>). Per default, a state has no transition.
frompylstar.automata.StateimportStateq0=State(name="Example state")q1=State("Another state")
N.B: Two states are said equivalent if their name equals.
A transition (pylstar.automata.transition.Transition) denotes a directed edge between two states. An edge is attached to a source state and is defined by a triplet:
- a name (str),
- an input letter (pylstar.Letter.Letter),
- an output letter (pylstar.Letter.Letter),
- a destination state (pylstar.automata.State.State).
The following snippet defines a transition (t0) that can be use to reach "destination state" (q1) from "origin state" (q0) if input letter "a" (la) is received. Executing this transition triggers the emission of letter "0" (l0).
frompylstar.letterimportLetterfrompylstar.automata.StateimportStatefrompylstar.automata.TransitionimportTransitionla=Letter("a")l0=Letter("0")q0=State("origin state")q1=State("destination state")t0=Transition("Example Transition",q1,la,l0)q0.transitions.append(t0)
An automaton (pylstar.automata.Automata.Automata) is defined by its initial state (pylstar.automata.State.State) and an optional name (str). For example, the following snippet illustrates the creation of an automaton:
frompylstar.automata.AutomataimportAutomatafrompylstar.automata.StateimportStateq0=State(name="Initial State")simple_automata=Automata(initial_state=q0,name="Simple Automata")
An automaton exposes the following methods:
- build_dot_code() - Returns the DOT code (str) that represents the automaton.
- get_states() - Returns all the states (list<pylstar.automata.State.State>) that can be reached from the initial state of the automaton.
- play_word(`pylstar.Word.Word` w, `pylstar.automata.State.State` s = None) - Visits the automaton according to the specified sequence of input messages w starting from state s (if None, it starts from the initial state). It returns a tupple made of the produced messages and the states reached while visiting the automaton ( (pylstar.Word.Word, list<pylstar.automata.State.State>)).
This project uses DocTests for testing and documentation purposes.To trigger the tests, please use the following command:
$ python setup.pytestThe LSTAR algorithm was introduced by Dana Angluin in the article
@article{Angluin:1987, author = {Angluin, Dana}, title = {Learning Regular Sets from Queries and Counterexamples}, journal = {Inf. Comput.}, issue_date = {November 1, 1987}, publisher = {Academic Press, Inc.},}This implementation also relies on the description of LSTAR provided by Colin de la Higuera in the book
@book{ColindelaHiguera, author = {de la Higuera, Colin}, title = {Grammatical Inference: Learning Automata and Grammars}, year = {2010}, isbn = {0521763169, 9780521763165}, publisher = {Cambridge University Press}, address = {New York, NY, USA}, }I'm almost certain this code contains bugs. Please, report any bug found by opening a ticket and/or by submiting a pull requests.Obvisouly, the projet is opened to any minor and major enhancements.
- Georges Bossert <gbossert@miskin.fr>
This software is licensed under the GPLv3 License. See theCOPYING.txt filein the top distribution directory for the full license text.
About
An implementation of the LSTAR Grammatical Inference Algorithm
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.