Quickstart¶
Installation¶
PySS can be installed using pip
as usual: pip install pyss
.
This will install the latest stable version.
You can also install PySS from this repository by cloning it. The development occurs in the master branch, the latest stable distributed version is in the stable branch.
PySS requires Python >=3.4 but should also work with Python 3.3. You can isolate PySS installation by using virtual environments:
- Get the tool to create environment:
pip install virtualenv
- Create the environment:
virtualenv -p python3.4 env
- Jump into:
source env/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Test PySS:
python -m unittest discover
Statechart in YAML¶
Example of a YAML definition of a statechart for an elevator:
statechart:
name: Elevator
initial: active
on entry: |
current = 0
destination = 0
class Doors:
def __init__(self):
self.opened = True
def open(self):
print('Opening doors')
self.opened = True
def close(self):
print('Closing doors')
self.opened = False
doors = Doors()
states:
- name: active
parallel states:
- name: movingElevator
initial: doorsOpen
states:
- name: doorsOpen
transitions:
- target: doorsClosed
guard: destination != current
action: doors.close()
- target: doorsClosed
event: after10s
guard: current > 0
action: destination = 0
- name: doorsClosed
transitions:
- target: movingUp
guard: destination > current
- target: movingDown
guard: destination < current and destination >= 0
- name: moving
transitions:
- target: doorsOpen
guard: destination == current
action: doors.open()
states:
- name: movingUp
on entry: current = current + 1
transitions:
- target: movingUp
guard: destination > current
- name: movingDown
on entry: current = current - 1
transitions:
- target: movingDown
guard: destination < current
- name: floorListener
initial: floorSelecting
states:
- name: floorSelecting
transitions:
- target: floorSelecting
event: floorSelected
action: destination = event.data['floor']
More examples are available in the examples directory.
Statechart execution¶
You can execute this example using the command-line interface or programmatically or using the command-line interface, see Subcommand: execute.
import pyss
statechart = pyss.io.import_from_yaml(open('examples/concrete/elevator.yaml'))
simulator = pyss.simulator.Simulator(statechart)
simulator.send(pyss.model.Event('floorSelected', data={'floor': 4}))
for step in simulator.execute():
print('{}: {}'.format(step.transitions, simulator.configuration))
The output should be:
[floorSelecting+floorSelected -> floorSelecting]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']
[doorsOpen -> doorsClosed]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']
[doorsClosed -> movingUp]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']
[movingUp -> movingUp]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']
[movingUp -> movingUp]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']
[movingUp -> movingUp]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']
[moving -> doorsOpen]: ['active', 'movingElevator', 'floorListener', 'doorsOpen', 'floorSelecting']