wiki:backends:python_lang
Generating Python Code
To generate Python code call the code generator with the command line flag ’-l python’
.
The generator generates just a Python class which implements the complete state machine. This has the benefit that your Python project does not become bloated with all kinds of helper classes. If required an optional base class can be specified in the configuration file. To get started the microwave oven project from the introduction section is is available on GitHub. It is a fully running example. A GUI realized with Tkinker allows you to send events to the state machine and observe the state machine reaction.
Example Code:
''' Command line options: -l PYTHON -p ssc -o oven oven.xml ''' ''' This file is generated from oven.xml - do not edit manually ''' ''' Generated on: Tue Jan 01 19:08:22 CET 2019 / version 3.7.3''' from oven_base import * class oven(oven_base): def __init__(self, errorhandler=None): ''' Set state vars to default states ''' self.stateVar = "Super" # set init state of top state self.stateVarSuper = "Idle" # set init state of Super ''' call on entry code of default states ''' self.radiator.ovenOff(); self.events = { 'evPwrLow', 'evTimeout', 'evDec', 'evDoorOpen', 'evDoorClosed', 'evPwrHigh', 'evInc', 'OVEN_NO_MSG' } self.eventConsumed=False if errorhandler is None: self.ehlr=self.errorhandler else: self.ehlr=errorhandler def errorhandler(self, msg): print ("Error:" + msg) def processEvent(self, event): self.eventConsumed=False # action code # just a comment if self.stateVar == "Super": if self.stateVarSuper == "Completed": if event=="evDoorOpen": ''' Transition from Completed to Idle''' self.eventConsumed=True ''' OnEntry code of state Idle''' self.radiator.ovenOff(); ''' adjust state variables ''' self.stateVarSuper = "Idle"; else: pass #Intentionally left blank elif self.stateVarSuper == "Cooking": if event=="evDoorOpen": ''' Transition from Cooking to CookingPause''' self.eventConsumed=True ''' Action code for transition ''' self.radiator.ovenOff(); self.tm.pause(); ''' adjust state variables ''' self.stateVarSuper = "CookingPause"; elif event=="evTimeout": ''' Transition from Cooking to Completed''' self.eventConsumed=True ''' Action code for transition ''' self.radiator.ovenOff(); self.tm.clear(); ''' adjust state variables ''' self.stateVarSuper = "Completed"; else: pass #Intentionally left blank elif self.stateVarSuper == "CookingPause": if event=="evDoorClosed": ''' Transition from CookingPause to Cooking''' self.eventConsumed=True ''' Action code for transition ''' self.tm.cont(); ''' OnEntry code of state Cooking''' self.radiator.ovenOn(); ''' adjust state variables ''' self.stateVarSuper = "Cooking"; else: pass #Intentionally left blank elif self.stateVarSuper == "Idle": if event=="evDoorClosed": if self.tm.preset()>0: ''' Transition from Idle to Cooking''' self.eventConsumed=True ''' Action code for transition ''' self.tm.start(); ''' OnEntry code of state Cooking''' self.radiator.ovenOn(); ''' adjust state variables ''' self.stateVarSuper = "Cooking"; else: pass #Intentionally left blank else: pass #Intentionally left blank ''' Check if event was already processed ''' if self.eventConsumed !=True: if event=="evDec": ''' Transition from Super to Super''' self.eventConsumed=True ''' Action code for transition ''' self.tm.dec(); self.stateVar = "Super";''' entry chain ''' if self.stateVarSuper== "Cooking": self.radiator.ovenOn(); elif self.stateVarSuper== "Idle": self.radiator.ovenOff(); ### elif event=="evInc": ''' Transition from Super to Super''' self.eventConsumed=True ''' Action code for transition ''' self.tm.inc(); self.stateVar = "Super";''' entry chain ''' if self.stateVarSuper== "Cooking": self.radiator.ovenOn(); elif self.stateVarSuper== "Idle": self.radiator.ovenOff(); ### elif event=="evPwrHigh": ''' Transition from Super to Super''' self.eventConsumed=True ''' Action code for transition ''' self.radiator.ovenSetPwrHigh(); self.stateVar = "Super";''' entry chain ''' if self.stateVarSuper== "Cooking": self.radiator.ovenOn(); elif self.stateVarSuper== "Idle": self.radiator.ovenOff(); ### elif event=="evPwrLow": ''' Transition from Super to Super''' self.eventConsumed=True ''' Action code for transition ''' self.radiator.ovenSetPwrLow(); self.stateVar = "Super";''' entry chain ''' if self.stateVarSuper== "Cooking": self.radiator.ovenOn(); elif self.stateVarSuper== "Idle": self.radiator.ovenOff(); ### else: pass #Intentionally left blank return self.eventConsumed
wiki/backends/python_lang.txt · Last modified: 2024/06/15 18:08 by webmin