Skip to content

Commit

Permalink
Force boolean evaluation of guards/contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Aug 20, 2019
1 parent d135e62 commit 901b3cc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions sismic/code/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class PythonEvaluator(Evaluator):
- 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.
- 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.
Expand Down Expand Up @@ -138,7 +139,7 @@ def _evaluate_code(self, code: Optional[str], *, additional_context: Mapping[str
exposed_context.update(additional_context if additional_context is not None else {})

try:
return eval(compiled_code, exposed_context, self._context) # type: ignore
return bool(eval(compiled_code, exposed_context, self._context))
except Exception as e:
raise CodeEvaluationError('"{}" occurred while evaluating "{}"'.format(e, code)) from e

Expand Down
2 changes: 1 addition & 1 deletion sismic/model/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Event:
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 delay has elapsed).
executed until given delay has elapsed).
When two events are compared, they are considered equal if their names
and their data are equal.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def test_condition_on_event(self, evaluator):
assert evaluator._evaluate_code('event.data[\'a\'] == 1', additional_context={'event': Event('test', a=1)})
assert evaluator._evaluate_code('event.name == \'test\'', additional_context={'event': Event('test')})

assert evaluator._evaluate_code('event', additional_context={'event': Event('a')}) is True
assert evaluator._evaluate_code('not event', additional_context={'event': None}) is True

def test_setdefault(self, evaluator):
evaluator._execute_code('setdefault("x", 2)')
assert evaluator.context['x'] == 1
Expand Down
4 changes: 4 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def test_empty_event(self):
with pytest.raises(TypeError):
Event()

def test_truth_value(self):
assert bool(Event('a'))
assert (not Event('a')) is False

def test_parametrized_events(self):
event = Event('test', a=1, b=2, c=3)

Expand Down

0 comments on commit 901b3cc

Please sign in to comment.