Package: pylstar
About pylstarpylstar is a free and open source Python implementation of theLSTARGrammatical 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.Sample usageOne that wants to usepylstarmust 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 subclassingpylstar.ActiveKnowledgeBase.ActiveKnowledgeBase. If the targeted process is a network server, one can solely subclasspylstar.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 frompylstar.NetworkActiveKnowledgeBasewhich 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 apylstar.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 intest/src/test_pylstar/coffee_machine_example.InstallationPylstar is a typical python library. It relies on asetup.pyfile to describe its installation process.The following command can be use to install pylstar on your system:$pythonsetup.pyinstallMain FeaturesPlaying with AutomataThe implementation of automata in pylstar follows the definition ofMealy Machines. An automaton is made of a unique initial state, states and transitions.StatesA 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.TransitionsA 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)AutomatonAn 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 messageswstarting from states(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>)).TestsThis project uses DocTests for testing and documentation purposes.
To trigger the tests, please use the following command:$pythonsetup.pytestReferencesThe 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},
 }Bugs and enhancementsI’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.AuthorGeorges Bossert <gbossert@miskin.fr>LicenseThis software is licensed under the GPLv3 License. See theCOPYING.txtfile
in the top distribution directory for the full license text.Changelog0.1.2 - 2017-10-05Small bug fixes0.1.1 - 2017-05-29Small bug fixes0.1 - 2017-05-14Initial versionCopyright (C) 2015-2017 Georges Bossert
Copying and distribution of this file, with or without modification, are
permitted provided the copyright notice and this notice are preserved.