| Tutorial |
![]() |
![]() |
![]() |
Here we will make our first working project using FSME. There's (at least in Russia) a classic joke-problem, which sounds like following dialog:
First of all, we should create a refrigerator. From the previous page you know how to run FSME and how to create entities in it. So, we should now create a simple refrigerator.
This refrigerator will have two states: "the door is opened" and "the door is closed". Also, there will be 2 events: "Open" and "Close"
FrigeFSM)
Opened and
Closed, and events Open and
Close. Set Initial State to Closed
|
| Two states and Two events |
Closed, press Ins key. You are now in
the new transition editor. Give new transition a name, for example,
open
close transition in the
Opened state, putting Close to the
transition condition.
|
| Final FSM |
Now it is time to make source from this automata. In this tutorial we use Python, but C++ way will be nearly the same. As our FSM does not interact anyhow yet, we would include automatic trace information to see that it works.
frige.fsm
pyfsmc frige.fsm -o frige.py -d
main.py), which contains the
following:
import frige
f = frige.FrigeFSM()
f.A(frige.FrigeFSM.Open)
f.A(frige.FrigeFSM.Close)
FrigeFSM:event:'Open' FrigeFSM:state:'Closed' FrigeFSM:transition:'open' FrigeFSM:new state:'Opened' FrigeFSM:event:'Close' FrigeFSM:state:'Opened' FrigeFSM:transition:'close' FrigeFSM:new state:'Closed'
All good refrigerators turn light on opening, and turn it off on closing. Our frige is not an exception. Let's extend it a bit.
turnLightOff and turnLightOn
Closed and Opened states respectively.
This makes FSM to call these outputs on entering appropriate state.
Note: Adding outputs to outpart makes
them to be executed on leaving the state (details)
|
| A FSM with outputs |
frige.FrigeFSM to
implement created outputs.
class LightFrige( frige.FrigeFSM ):
def turnLightOn(self):
print "Turning on the light"
def turnLightOff(self):
print "Turning off the light"
f = LightFrige()
f.A( frige.FrigeFSM.Open )
f.A( frige.FrigeFSM.Close )
Output will be the following (if you run pyfsmc without -d):
Turning on the light Turning off the light
Let's solve our problem at last! Draw FSM implementing the logic by yourself :). or take ready FSM from here
|
| A sample FSM look |
This FSM will have the following events:
|
Implement your logic:
from frige import *
from frige_logic1 import *
class LightFrige( FrigeFSM ):
def turnLightOn(self):
print "Turning on the light"
def turnLightOff(self):
print "Turning off the light"
class FrigeLogic( FrigeLogicFSM ):
def __init__(self, frige):
super(FrigeLogic, self).__init__()
self.frige = frige
def putHippopotamus( self ):
print "Putting Hippopotamus to the frige"
def getGiraffe( self ):
print "Getting Giraffe from the frige"
def putGiraffe( self ):
print "Putting Giraffe to the frige"
def openFrige( self ):
print "Opening frige"
self.frige.A( FrigeFSM.Open )
print "Frige opened"
def closeFrige( self ):
print "Closing frige"
self.frige.A( FrigeFSM.Close )
print "Frige closed"
def getHippopotamus( self ):
print "Getting Hippopotamus from the frige"
if __name__=="__main__":
f = LightFrige()
l = FrigeLogic( f )
print "\n*** Putting Giraffe to Frige"
l.A( FrigeLogicFSM.PutGiraffe )
print "\n*** Putting Hippopotamus to Frige"
l.A( FrigeLogicFSM.PutHippopotamus )
print "\n*** Freeing Frige"
l.A( FrigeLogicFSM.FreeFrige )