Module interpreter

class sismic.interpreter.Interpreter(statechart, *, evaluator_klass=<class 'sismic.code.python.PythonEvaluator'>, initial_context=None, clock=None, ignore_contract=False)

Bases: object

A discrete interpreter that executes a statechart according to a semantic close to SCXML (eventless transitions first, inner-first/source state semantics).

Parameters
  • statechart (Statechart) – statechart to interpret

  • evaluator_klass (Callable[..., Evaluator]) – An optional callable (e.g. a class) that takes an interpreter and an optional initial context as input and returns an Evaluator instance that will be used to initialize the interpreter. By default, the PythonEvaluator class will be used.

  • initial_context (Optional[Mapping[str, Any]]) – an optional initial context that will be provided to the evaluator. By default, an empty context is provided

  • clock (Optional[Clock]) – A BaseClock instance that will be used to set this interpreter internal time. By default, a SimulatedClock is used.

  • ignore_contract (bool) – set to True to ignore contract checking during the execution.

property time: float

Time of the latest execution.

Return type

float

property configuration: List[str]

List of active states names, ordered by depth. Ties are broken according to the lexicographic order on the state name.

Return type

List[str]

property context: Mapping[str, Any]

The context of execution.

Return type

Mapping[str, Any]

property final: bool

Boolean indicating whether this interpreter is in a final configuration.

Return type

bool

property statechart: Statechart

Embedded statechart

Return type

Statechart

attach(listener)

Attach given listener to the current interpreter.

The listener is called each time a meta-event is emitted by current interpreter. Emitted meta-events are:

  • step started: when a (possibly empty) macro step starts. The current time of the step is available through the time attribute.

  • step ended: when a (possibly empty) macro step ends.

  • event consumed: when an event is consumed. The consumed event is exposed through the event attribute.

  • event sent: when an event is sent. The sent event is exposed through the event attribute.

  • state exited: when a state is exited. The exited state is exposed through the state attribute.

  • state entered: when a state is entered. The entered state is exposed through the state attribute.

  • transition processed: when a transition is processed. The source state, target state and the event ard exposed respectively through the source, target and event attributes.

  • Every meta-event that is sent from within the statechart.

This is a low-level interface for self.bind and self.bind_property_statechart.

Consult sismic.interpreter.listener for common listeners/wrappers.

Parameters

listener (Callable[[MetaEvent], Any]) – A callable that accepts meta-event instances.

Return type

None

detach(listener)

Remove given listener from the ones that are currently attached to this interpreter.

Parameters

listener (Callable[[MetaEvent], Any]) – A previously attached listener.

Return type

None

bind(interpreter_or_callable)

Bind an interpreter (or a callable) to the current interpreter.

Internal events sent by this interpreter will be propagated as external events. If interpreter_or_callable is an Interpreter instance, its queue method is called. This is, if i1 and i2 are interpreters, i1.bind(i2) is equivalent to i1.bind(i2.queue).

This method is a higher-level interface for self.attach. If x = interpreter.bind(...), use interpreter.detach(x) to unbind a previously bound interpreter.

Parameters

interpreter_or_callable (Union[Interpreter, Callable[[Event], Any]]) – interpreter or callable to bind.

Return type

Callable[[MetaEvent], Any]

Returns

the resulting attached listener.

bind_property_statechart(statechart, *, interpreter_klass=None)

Bind a property statechart to the current interpreter.

A property statechart receives meta-events from the current interpreter depending on what happens. See attach method for a full list of meta-events.

The internal clock of all property statecharts is synced with the one of the current interpreter. As soon as a property statechart reaches a final state, a PropertyStatechartError will be raised, meaning that the property expressed by the corresponding property statechart is not satisfied. Property statecharts are automatically executed when they are bound to an interpreter.

Since Sismic 1.4.0: passing an interpreter as first argument is deprecated.

This method is a higher-level interface for self.attach. If x = interpreter.bind_property_statechart(...), use interpreter.detach(x) to unbind a previously bound property statechart.

Parameters
  • statechart (Statechart) – A statechart instance.

  • interpreter_klass (Optional[Callable]) – An optional callable that accepts a statechart as first parameter and a named parameter clock. Default to Interpreter.

Return type

Callable[[MetaEvent], Any]

Returns

the resulting attached listener.

queue(event_or_name, *event_or_names, **parameters)

Create and queue given events to the external event queue.

If an event has a delay parameter, it will be processed by the first call to execute_once as soon as self.clock.time exceeds current self.time + event.delay.

If named parameters are provided, they will be added to all events that are provided by name.

Parameters
  • event_or_name (Union[str, Event]) – name of the event or Event instance

  • event_or_names (Union[str, Event]) – additional events

  • parameters – event parameters.

Return type

Interpreter

Returns

self so it can be chained.

execute(max_steps=-1)

Repeatedly calls execute_once and return a list containing the returned values of execute_once.

Notice that this does NOT return an iterator but computes the whole list first before returning it.

Parameters

max_steps (int) – An upper bound on the number steps that are computed and returned. Default is -1, no limit. Set to a positive integer to avoid infinite loops in the statechart execution.

Return type

List[MacroStep]

Returns

A list of MacroStep instances

execute_once()

Select transitions that can be fired based on available queued events, process them and stabilize the interpreter. When multiple transitions are selected, they are atomically processed: states are exited, transition is processed, states are entered, statechart is stabilized and only after that, the next transition is processed.

Return type

Optional[MacroStep]

Returns

a macro step or None if nothing happened

class sismic.interpreter.Event(name, **additional_parameters)

Bases: object

An event with a name and (optionally) some data passed as named parameters.

The list of parameters can be obtained using dir(event). Notice that name and data are reserved names. If a delay parameter is provided, then this event will be considered as a delayed event (and won’t be executed until given delay has elapsed).

When two events are compared, they are considered equal if their names and their data are equal.

Parameters
  • name (str) – name of the event.

  • data – additional data passed as named parameters.

class sismic.interpreter.InternalEvent(name, **additional_parameters)

Bases: Event

Subclass of Event that represents an internal event.

class sismic.interpreter.MetaEvent(name, **additional_parameters)

Bases: Event

Subclass of Event that represents a MetaEvent, as used in property statecharts.