Skip to content

Commit

Permalink
_select_transitions accepts a list of transitions as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Jun 15, 2018
1 parent ddeff5f commit 3507295
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions sismic/interpreter/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ def execute_once(self) -> Optional[MacroStep]:
self._initialized = True
event = None # type: Optional[Event]
else:
event, transitions = self._select_transitions()
# Consider transitions of active states
# TODO: "fallback" transitions should be considered separately
event, transitions = self._select_transitions(
[tr for tr in self._statechart.transitions if tr.source in self._configuration]
)


# If there is no event and no transition, return "no step"
if event is None and len(transitions) == 0:
Expand All @@ -243,7 +248,7 @@ def execute_once(self) -> Optional[MacroStep]:
if len(transitions) == 0:
computed_steps = [MicroStep(event=event)]
else:
# Select the transitions that will be performed
# Select the triggerable transitions that will be performed
transitions = self._sort_transitions(
self._filter_transitions(transitions)
)
Expand Down Expand Up @@ -357,40 +362,38 @@ def _select_event(self, consume=True) -> Optional[Event]:
else:
return None

def _select_transitions(self) -> Tuple[Optional[Event], List[Transition]]:
def _select_transitions(self, transitions: List[Transition]) -> Tuple[Optional[Event], List[Transition]]:
"""
Select the transitions that could be triggered and the corresponding (optional) event.
If automatic transitions (ie. ones without event) are found, return them and do not look for
transitions with event. Otherwise, consume next event and return a possibly empty list of
transitions that could be fired with this event.
:param transitions: list of transitions to consider (i.e. the ones of active states)
:return: a couple (event instance, list of *Transition* instances)
"""
transitions = []

# Transitions of active states
activable_transitions = [tr for tr in self._statechart.transitions if tr.source in self._configuration]
triggerable_transitions = []

# Eventless transitions are considered first
for transition in activable_transitions:
for transition in transitions:
if (transition.event is None and
(transition.guard is None or self._evaluator.evaluate_guard(transition))):
transitions.append(transition)
triggerable_transitions.append(transition)

# If an eventless transition can be triggered, return it
if len(transitions) > 0:
return None, transitions
if len(triggerable_transitions) > 0:
return None, triggerable_transitions

# Otherwise, take and consume next event
event = self._select_event()
if event is None:
return None, []

for transition in activable_transitions:
for transition in transitions:
if (transition.event == event.name and
(transition.guard is None or self._evaluator.evaluate_guard(transition, event))):
transitions.append(transition)
return event, transitions
triggerable_transitions.append(transition)
return event, triggerable_transitions

def _filter_transitions(self, transitions: List[Transition]) -> List[Transition]:
"""
Expand Down

0 comments on commit 3507295

Please sign in to comment.