Module code

class sismic.code.Evaluator(interpreter=None, *, initial_context=None)

Bases: object

Abstract base class for any evaluator.

An instance of this class defines what can be done with piece of codes contained in a statechart (condition, action, etc.).

Notice that the execute_* methods are called at each step, even if there is no code to execute. This allows the evaluator to keep track of the states that are entered or exited, and of the transitions that are processed.

Parameters
  • interpreter – the interpreter that will use this evaluator, is expected to be an Interpreter instance

  • initial_context (Optional[Mapping[str, Any]]) – an optional dictionary to populate the context

abstract property context: Mapping[str, Any]

The context of this evaluator. A context is a dict-like mapping between variables and values that is expected to be exposed when the code is evaluated.

Return type

Mapping[str, Any]

execute_statechart(statechart)

Execute the initial code of a statechart. This method is called at the very beginning of the execution.

Parameters

statechart (Statechart) – statechart to consider

evaluate_guard(transition, event=None)

Evaluate the guard for given transition.

Parameters
  • transition (Transition) – the considered transition

  • event (Optional[Event]) – instance of Event if any

Return type

Optional[bool]

Returns

truth value of code

execute_action(transition, event=None)

Execute the action for given transition. This method is called for every transition that is processed, even those with no action.

Parameters
  • transition (Transition) – the considered transition

  • event (Optional[Event]) – instance of Event if any

Return type

List[Event]

Returns

a list of sent events

execute_on_entry(state)

Execute the on entry action for given state. This method is called for every state that is entered, even those with no on_entry.

Parameters

state (StateMixin) – the considered state

Return type

List[Event]

Returns

a list of sent events

execute_on_exit(state)

Execute the on exit action for given state. This method is called for every state that is exited, even those with no on_exit.

Parameters

state (StateMixin) – the considered state

Return type

List[Event]

Returns

a list of sent events

evaluate_preconditions(obj, event=None)

Evaluate the preconditions for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterable[str]

Returns

list of unsatisfied conditions

evaluate_invariants(obj, event=None)

Evaluate the invariants for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterable[str]

Returns

list of unsatisfied conditions

evaluate_postconditions(obj, event=None)

Evaluate the postconditions for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterable[str]

Returns

list of unsatisfied conditions

class sismic.code.DummyEvaluator(interpreter=None, *, initial_context=None)

Bases: Evaluator

A dummy evaluator that does nothing and evaluates every condition to True.

property context

The context of this evaluator. A context is a dict-like mapping between variables and values that is expected to be exposed when the code is evaluated.

evaluate_guard(transition, event=None)

Evaluate the guard for given transition.

Parameters
  • transition (Transition) – the considered transition

  • event (Optional[Event]) – instance of Event if any

Return type

Optional[bool]

Returns

truth value of code

evaluate_invariants(obj, event=None)

Evaluate the invariants for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterable[str]

Returns

list of unsatisfied conditions

evaluate_postconditions(obj, event=None)

Evaluate the postconditions for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterable[str]

Returns

list of unsatisfied conditions

evaluate_preconditions(obj, event=None)

Evaluate the preconditions for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterable[str]

Returns

list of unsatisfied conditions

execute_action(transition, event=None)

Execute the action for given transition. This method is called for every transition that is processed, even those with no action.

Parameters
  • transition (Transition) – the considered transition

  • event (Optional[Event]) – instance of Event if any

Return type

List[Event]

Returns

a list of sent events

execute_on_entry(state)

Execute the on entry action for given state. This method is called for every state that is entered, even those with no on_entry.

Parameters

state (StateMixin) – the considered state

Return type

List[Event]

Returns

a list of sent events

execute_on_exit(state)

Execute the on exit action for given state. This method is called for every state that is exited, even those with no on_exit.

Parameters

state (StateMixin) – the considered state

Return type

List[Event]

Returns

a list of sent events

execute_statechart(statechart)

Execute the initial code of a statechart. This method is called at the very beginning of the execution.

Parameters

statechart (Statechart) – statechart to consider

class sismic.code.PythonEvaluator(interpreter=None, *, initial_context=None)

Bases: Evaluator

A code evaluator that understands Python.

This evaluator exposes some additional functions/variables:

  • On both code execution and code evaluation:
    • A time: float value that represents the current time exposed by interpreter clock.

    • An active(name: str) -> bool Boolean function that takes a state name and return True if and only if this state is currently active, ie. it is in the active configuration of the Interpreter instance that makes use of this evaluator.

  • On code execution:
    • A send(name: str, **kwargs) -> None function that takes an event name and additional keyword parameters and raises an internal event with it. Raised events are propagated to bound statecharts as external events and to the current statechart as internal event. If delay is provided, a delayed event is created.

    • A notify(name: str, **kwargs) -> None function that takes an event name and additional keyword parameters and raises a meta-event with it. Meta-events are only sent to bound property statecharts.

    • If the code is related to a transition, the event: Event that fires the transition is exposed.

    • A setdefault(name:str, value: Any) -> Any function that defines and returns variable name in the global scope if it is not yet defined.

  • On guard or contract evaluation:
    • If the code is related to a transition, an event: Optional[Event] variable is exposed. This variable contains the currently considered event, or None.

  • On guard or contract (except preconditions) evaluation:
    • An after(sec: float) -> bool Boolean function that returns True if and only if the source state was entered more than sec seconds ago. The time is evaluated according to Interpreter’s clock.

    • A idle(sec: float) -> bool Boolean function that returns True if and only if the source state did not fire a transition for more than sec ago. The time is evaluated according to Interpreter’s clock.

  • On contract (except preconditions) evaluation:
    • A variable __old__ that has an attribute x for every x in the context when either the state was entered (if the condition involves a state) or the transition was processed (if the condition involves a transition). The value of __old__.x is a shallow copy of x at that time.

  • On contract evaluation:
    • A sent(name: str) -> bool function that takes an event name and return True if an event with the same name was sent during the current step.

    • A received(name: str) -> bool function that takes an event name and return True if an event with the same name is currently processed in this step.

If an exception occurred while executing or evaluating a piece of code, it is propagated by the evaluator.

Parameters
  • interpreter – the interpreter that will use this evaluator, is expected to be an Interpreter instance

  • initial_context (Optional[Mapping[str, Any]]) – a dictionary that will be used as __locals__

property context: Mapping

The context of this evaluator. A context is a dict-like mapping between variables and values that is expected to be exposed when the code is evaluated.

Return type

Mapping

evaluate_guard(transition, event=None)

Evaluate the guard for given transition.

Parameters
  • transition (Transition) – the considered transition

  • event (Optional[Event]) – instance of Event if any

Return type

bool

Returns

truth value of code

evaluate_preconditions(obj, event=None)

Evaluate the preconditions for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterator[str]

Returns

list of unsatisfied conditions

evaluate_invariants(obj, event=None)

Evaluate the invariants for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterator[str]

Returns

list of unsatisfied conditions

evaluate_postconditions(obj, event=None)

Evaluate the postconditions for given object (either a StateMixin or a Transition) and return a list of conditions that are not satisfied.

Parameters
  • obj – the considered state or transition

  • event (Optional[Event]) – an optional Event instance, if any

Return type

Iterator[str]

Returns

list of unsatisfied conditions

execute_action(transition, event=None)

Execute the action for given transition. This method is called for every transition that is processed, even those with no action.

Parameters
  • transition (Transition) – the considered transition

  • event (Optional[Event]) – instance of Event if any

Return type

List[Event]

Returns

a list of sent events

execute_on_entry(state)

Execute the on entry action for given state. This method is called for every state that is entered, even those with no on_entry.

Parameters

state (StateMixin) – the considered state

Return type

List[Event]

Returns

a list of sent events

execute_on_exit(state)

Execute the on exit action for given state. This method is called for every state that is exited, even those with no on_exit.

Parameters

state (StateMixin) – the considered state

Return type

List[Event]

Returns

a list of sent events

execute_statechart(statechart)

Execute the initial code of a statechart. This method is called at the very beginning of the execution.

Parameters

statechart (Statechart) – statechart to consider