Skip to content

Commit

Permalink
Remove "cancel"
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Aug 24, 2018
1 parent 9778993 commit 13ee515
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 48 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Unreleased

- (Added) Some documentation about running multiple statecharts.
- (Added) An ``unbind`` method for an ``Interpreter``.
- (Added) A ``remove`` method on ``EventQueue`` and a corresponding ``cancel`` method on ``Interpreter``.
- (Added) A ``setdefault`` function that can be used in the preamble and actions of a statechart to assign default values to variables.
- (Changed) Meta-Event *step started* has a ``time`` attribute.
- (Changed) The current event queue can be consulted as a list using ``interpreter._event_queue``.
Expand Down
11 changes: 5 additions & 6 deletions docs/execution.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,11 @@ This can be checked by looking at the current event queue of the interpreter.
clack
clock

Queued events can be removed from the queue by calling the :py:meth:`~sismic.interpreter.cancel` method of
the interpreter. This method accepts both the name of an event or an event instance, and remove the
first corresponding event from the queue.

.. testcode:: interpreter

..
Queued events can be removed from the queue by calling the :py:meth:`~sismic.interpreter.cancel` method of
the interpreter. This method accepts both the name of an event or an event instance, and remove the
first corresponding event from the queue.
.. testcode:: interpreter
interpreter.cancel('clock')
assert interpreter._event_queue == [(0, Event('clack'))]
Expand Down
2 changes: 1 addition & 1 deletion sismic/code/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PythonEvaluator(Evaluator):
- 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 *default(name:str, value: Any) -> Any* function that defines and returns variable *name* in
- 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, the *event: Event* that fires the transition is exposed.
Expand Down
32 changes: 16 additions & 16 deletions sismic/interpreter/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,19 @@ def queue(self, event_or_name: Union[str, Event], *events_or_names: Union[str, E

return self

def cancel(self, event_or_name: Union[str, Event]) -> bool:
"""
Remove first occurrence of given event (or name) from the interpreter queue.
:param event_or_name: an *Event* instance of the name of an event to cancel.
:return: True if the event was found and removed, False otherwise.
"""
event = Event(event_or_name) if isinstance(event_or_name, str) else event_or_name
for i, (time, queued_event) in enumerate(self._event_queue):
if queued_event == event:
self._event_queue.pop(i)
return True
return False
# def cancel(self, event_or_name: Union[str, Event]) -> bool:
# """
# Remove first occurrence of given event (or name) from the interpreter queue.

# :param event_or_name: an *Event* instance of the name of an event to cancel.
# :return: True if the event was found and removed, False otherwise.
# """
# event = Event(event_or_name) if isinstance(event_or_name, str) else event_or_name
# for i, (time, queued_event) in enumerate(self._event_queue):
# if queued_event == event:
# self._event_queue.pop(i)
# return True
# return False

def execute(self, max_steps: int = -1) -> List[MacroStep]:
"""
Expand Down Expand Up @@ -524,7 +524,7 @@ def _compute_steps(self) -> Optional[List[MicroStep]]:
# Initialization
if not self._initialized:
self._initialized = True
return [MicroStep(entered_states=[self._statechart.root])]
return [MicroStep(entered_states=[cast(str, self._statechart.root)])]

# Select transitions
event = self._select_event(consume=False)
Expand Down Expand Up @@ -620,9 +620,9 @@ def _create_stabilization_step(self, names: Iterable[str]) -> Optional[MicroStep

for leaf in leaves:
if isinstance(leaf, FinalState) and self._statechart.parent_for(leaf.name) == self._statechart.root:
return MicroStep(exited_states=[leaf.name, self._statechart.root])
return MicroStep(exited_states=[leaf.name, cast(str, self._statechart.root)])
if isinstance(leaf, (ShallowHistoryState, DeepHistoryState)):
states_to_enter = self._memory.get(leaf.name, [leaf.memory])
states_to_enter = cast(List[str], self._memory.get(leaf.name, [leaf.memory]))
states_to_enter.sort(key=lambda x: (self._statechart.depth_for(x), x))
return MicroStep(entered_states=states_to_enter, exited_states=[leaf.name])
elif isinstance(leaf, OrthogonalState) and self._statechart.children_for(leaf.name):
Expand Down
48 changes: 24 additions & 24 deletions tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,30 +725,30 @@ def test_internal_first(self, interpreter):
event = interpreter._select_event(consume=True)
assert isinstance(event, DelayedEvent) and event == Event('test3')

def test_cancel_event(self, interpreter):
interpreter.queue('test1')
interpreter.queue(Event('test2', x=1))
interpreter.queue('test3')
# def test_cancel_event(self, interpreter):
# interpreter.queue('test1')
# interpreter.queue(Event('test2', x=1))
# interpreter.queue('test3')

# Normal cancellation
assert interpreter.cancel('test1')
assert interpreter._select_event(consume=False) == Event('test2', x=1)
# # Normal cancellation
# assert interpreter.cancel('test1')
# assert interpreter._select_event(consume=False) == Event('test2', x=1)

# Cancellation of unknown event
assert not interpreter.cancel('test4')
# # Cancellation of unknown event
# assert not interpreter.cancel('test4')

# Cancellation satisfies event parameters
assert not interpreter.cancel('test2')
assert interpreter._select_event(consume=False) == Event('test2', x=1)
assert not interpreter.cancel(Event('test2', x=2))
assert interpreter._select_event(consume=False) == Event('test2', x=1)
assert interpreter.cancel(Event('test2', x=1))
assert interpreter._select_event(consume=False) == Event('test3')

# Cancellation only cancels first occurrence
interpreter.queue('test3')
interpreter.queue('test3')
interpreter.cancel('test3')
assert interpreter._select_event(consume=True) == Event('test3')
assert interpreter._select_event(consume=True) == Event('test3')
assert interpreter._select_event(consume=True) is None
# # Cancellation satisfies event parameters
# assert not interpreter.cancel('test2')
# assert interpreter._select_event(consume=False) == Event('test2', x=1)
# assert not interpreter.cancel(Event('test2', x=2))
# assert interpreter._select_event(consume=False) == Event('test2', x=1)
# assert interpreter.cancel(Event('test2', x=1))
# assert interpreter._select_event(consume=False) == Event('test3')

# # Cancellation only cancels first occurrence
# interpreter.queue('test3')
# interpreter.queue('test3')
# interpreter.cancel('test3')
# assert interpreter._select_event(consume=True) == Event('test3')
# assert interpreter._select_event(consume=True) == Event('test3')
# assert interpreter._select_event(consume=True) is None

0 comments on commit 13ee515

Please sign in to comment.