class ElevatorContractTests(ElevatorTests): def setUp(self): with open('docs/examples/elevator_contract.yaml') as f: self.sc = io.import_from_yaml(f) self.interpreter = Interpreter(self.sc) # Stabilization self.interpreter.execute_once()
class BindTests(unittest.TestCase): def setUp(self): with open('tests/yaml/simple.yaml') as f: sc = io.import_from_yaml(f) self.interpreter = Interpreter(sc) # Stabilization self.interpreter.execute_once() def test_bind(self): with open('tests/yaml/simple.yaml') as f: other_sc = io.import_from_yaml(f) other_interpreter = Interpreter(other_sc) self.interpreter.bind(other_interpreter) self.assertIn(other_interpreter.queue, self.interpreter._bound) self.interpreter.raise_event(InternalEvent('test')) self.assertTrue(self.interpreter._internal_events.pop(), Event('test')) self.assertTrue(other_interpreter._external_events.pop(), Event('test')) def test_bind_callable(self): with open('tests/yaml/simple.yaml') as f: other_sc = io.import_from_yaml(f) other_interpreter = Interpreter(other_sc) self.interpreter.bind(other_interpreter.queue) self.assertIn(other_interpreter.queue, self.interpreter._bound) self.interpreter.raise_event(InternalEvent('test')) self.assertTrue(self.interpreter._internal_events.pop(), Event('test')) self.assertTrue(other_interpreter._external_events.pop(), Event('test'))
def interpreter(self, nondeterministic_statechart): interpreter = Interpreter(nondeterministic_statechart, evaluator_klass=DummyEvaluator) # Stabilization interpreter.execute_once() return interpreter
class InfiniteExecutionTests(unittest.TestCase): def setUp(self): self.sc = io.import_from_yaml(open('tests/yaml/infinite.yaml')) self.interpreter = Interpreter(self.sc) def test_three_steps(self): self.assertEqual(self.interpreter.configuration, ['s1']) self.interpreter.execute_once() self.assertEqual(self.interpreter.configuration, ['s2']) self.interpreter.execute_once() self.assertEqual(self.interpreter.configuration, ['s1']) self.interpreter.execute_once() self.assertEqual(self.interpreter.configuration, ['s2']) self.assertEqual(self.interpreter.context['x'], 2) # x is incremented in s1.on_entry def test_auto_three_steps(self): self.interpreter.execute(max_steps=3) self.assertEqual(self.interpreter.configuration, ['s2']) self.assertEqual(self.interpreter.context['x'], 2) # x is incremented in s1.on_entry def test_auto_stop(self): self.interpreter.execute() self.assertTrue(self.interpreter.final) self.assertEqual(self.interpreter.context['x'], 100)
class RacerComponent(Component): def __init__(self, race_distance: int, sensor_distance: str, racer_distance: str, events: str): super().__init__() self._racer_distance_topic = racer_distance self._racer = Racer() with open(SCRIPT_DIR / 'racer.yaml') as f: statechart = sismic.io.import_from_yaml(f) context = {'race_distance': race_distance, 'racer': self._racer} self._interpreter = Interpreter(statechart, initial_context=context) self._interpreter.execute() self._io.subscribe(sensor_distance, self._on_sensor_distance) self._io.subscribe(events, self._on_event) def _on_sensor_distance(self, distance: int): """ :param distance: meters since sensor start """ self._racer.sensor_distance = distance self._io.publish(self._racer_distance_topic, distance=self._racer.distance) steps = self._interpreter.execute() if steps: print(steps) def _on_event(self, name: str, **kwargs): self._interpreter.queue(name, **kwargs)
def setUp(self): self.sc = import_from_yaml(""" statechart: name: test contract root state: name: root on entry: x = 1 initial: s0 states: - name: s0 initial: s1 transitions: - event: end target: root states: - name: s1 transitions: - target: s2 action: x = 2 event: e - name: s2 """) self.root = self.sc.state_for('root') # Will never be exited self.s0 = self.sc.state_for('s0') # Will be exited on "end" self.s1 = self.sc.state_for('s1') # Entered, and then exited on e. self.s2 = self.sc.state_for('s2') # Entered when e self.intp = Interpreter(self.sc)
def __init__(self, master, statechart, contracts, properties): super().__init__(master) # Initialize widgets self.create_widgets() # Create a Stopwatch interpreter statechart = import_from_yaml(statechart) self.interpreter = Interpreter(statechart, ignore_contract=not contracts) # Bind interpreter events to the GUI self.interpreter.bind(self.event_handler) # Bind execution watchers self.watcher = ExecutionWatcher(self.interpreter) for prop in properties if properties else []: self.watcher.watch_with(import_from_yaml(prop), fails_fast=True) self.watcher.start() # Hide disabled widgets for widget_name in DISABLED_WIDGETS: widget = getattr(self, widget_name) widget.pack_forget() self.on_autotick() self.execute()
def interpreter(self, nested_parallel_statechart): interpreter = Interpreter(nested_parallel_statechart, evaluator_klass=DummyEvaluator) # Stabilization interpreter.execute_once() return interpreter
class InternalTests(unittest.TestCase): def setUp(self): with open('tests/yaml/internal.yaml') as f: self.sc = io.import_from_yaml(f) self.interpreter = Interpreter(self.sc) # Stabilization self.interpreter.execute_once() def testInternalSent(self): step = self.interpreter.execute_once() self.assertEqual(step.event.name, 'next') def testInternalBeforeExternal(self): self.interpreter.queue(Event('not_next')) step = self.interpreter.execute_once() self.assertEqual(step.event.name, 'next') step = self.interpreter.execute_once() self.assertEqual(step.event, None) self.assertEqual(step.entered_states, ['s2']) step = self.interpreter.execute_once() self.assertEqual(step.event.name, 'not_next') def testActiveGuard(self): self.interpreter.execute() self.assertTrue(self.interpreter.final)
class ElevatorContractTests(ElevatorTests): def setUp(self): with open('docs/examples/elevator/elevator_contract.yaml') as f: self.sc = io.import_from_yaml(f) self.interpreter = Interpreter(self.sc) # Stabilization self.interpreter.execute_once()
def test_elevator_moves_after_10s(self): stories = [ Story([Event('floorSelected', floor=4)]), Story([Event('floorSelected', floor=0)]), Story([Event('floorSelected', floor=4), Pause(10)]), Story([Event('floorSelected', floor=0), Pause(10)]), Story([Event('floorSelected', floor=4), Pause(9)]), Story([Event('floorSelected', floor=0), Pause(9)]), ] for story in stories: with self.subTest(story=story): # Reopen because we need to reset it with open('docs/examples/elevator.yaml') as f: sc = io.import_from_yaml(f) tested = Interpreter(sc) test_story = teststory_from_trace(story.tell(tested)) with open('docs/examples/tester_elevator_moves_after_10s.yaml' ) as f: tester = Interpreter(io.import_from_yaml(f)) test_story.tell(tester) self.assertFalse(tester.final)
class WriterExecutionTests(unittest.TestCase): def setUp(self): with open('docs/examples/writer_options.yaml') as f: self.sc = io.import_from_yaml(f) self.interpreter = Interpreter(self.sc) def test_output(self): scenario = [ Event('keyPress', key='bonjour '), Event('toggle'), Event('keyPress', key='a '), Event('toggle'), Event('toggle_bold'), Event('keyPress', key='tous !'), Event('leave') ] for event in scenario: self.interpreter.queue(event) self.interpreter.execute() self.assertTrue(self.interpreter.final) self.assertEqual(self.interpreter.context['output'], [ 'bonjour ', '[b]', '[i]', 'a ', '[/b]', '[/i]', '[b]', 'tous !', '[/b]' ])
def interpreter(self, simple_statechart): interpreter = Interpreter(simple_statechart, evaluator_klass=DummyEvaluator) # Stabilization interpreter.execute_once() return interpreter
def setUp(self): with open('tests/yaml/nested_parallel.yaml') as f: sc = io.import_from_yaml(f) self.interpreter = Interpreter(sc) # Stabilization self.interpreter.execute_once() self.common_states = ['root', 's1', 'p1', 'p2', 'r1', 'r2', 'r3', 'r4']
def interpreter(self, internal_statechart): interpreter = Interpreter(internal_statechart) # Stabilization interpreter.execute_once() return interpreter
def __init__(self): with open('statecharts/state_machine.yaml') as f: self.statechart = import_from_yaml(f) assert isinstance(self.statechart, Statechart) self.interpreter = Interpreter(self.statechart) self.interpreter.execute_once()
def test_statechart_postcondition(self): sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) sc.postconditions.append('False') interpreter = Interpreter(sc) interpreter.send(Event('goto s2')).send(Event('goto final')) with self.assertRaises(PostconditionFailed) as cm: interpreter.execute() self.assertTrue(isinstance(cm.exception.obj, StateChart))
def test_floor_selection(self): sc = io.import_from_yaml(open('docs/examples/elevator.yaml')) interpreter = Interpreter(sc) interpreter.send(Event('floorSelected', floor=4)).execute_once() self.assertEqual(interpreter._evaluator.context['destination'], 4) interpreter.execute_once() self.assertEqual(sorted(interpreter.configuration), ['active', 'doorsClosed', 'floorListener', 'floorSelecting', 'movingElevator'])
def test_run_in_background(self): sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) intp = Interpreter(sc) task = run_in_background(intp, 0.001) intp.send(Event('goto s2')) intp.send(Event('goto final')) task.join() self.assertTrue(intp.final)
def setUp(self): with open('docs/examples/elevator/elevator.yaml') as f: elevator = io.import_from_yaml(f) with open('docs/examples/elevator/elevator_buttons.yaml') as f: buttons = io.import_from_yaml(f) self.elevator = Interpreter(elevator) self.buttons = Interpreter(buttons) self.buttons.bind(self.elevator)
def test_run_in_background(self): with open('tests/yaml/simple.yaml') as f: sc = io.import_from_yaml(f) interpreter = Interpreter(sc) task = run_in_background(interpreter, 0.001) interpreter.queue(Event('goto s2')) interpreter.queue(Event('goto final')) task.join() self.assertTrue(interpreter.final)
def test_nondeterminism(self): with open('tests/yaml/nondeterministic.yaml') as f: sc = io.import_from_yaml(f) interpreter = Interpreter(sc, evaluator_klass=DummyEvaluator) # Stabilization interpreter.execute_once() with self.assertRaises(exceptions.NonDeterminismError): interpreter.execute_once()
def setUp(self): with open('docs/examples/elevator.yaml') as f: elevator = io.import_from_yaml(f) with open('docs/examples/elevator_buttons.yaml') as f: buttons = io.import_from_yaml(f) self.elevator = Interpreter(elevator) self.buttons = Interpreter(buttons) self.buttons.bind(self.elevator)
def test_memory(self): sc = io.import_from_yaml(open('tests/yaml/history.yaml')) interpreter = Interpreter(sc, DummyEvaluator) interpreter.send(Event('next')).execute_once() self.assertEqual(sorted(interpreter.configuration), ['loop', 's2']) step = interpreter.send(Event('pause')).execute_once() self.assertEqual(step.exited_states, ['s2', 'loop']) self.assertEqual(sorted(interpreter.configuration), ['pause'])
def test_resume_memory(self): sc = io.import_from_yaml(open('tests/yaml/history.yaml')) interpreter = Interpreter(sc, DummyEvaluator) interpreter.send(Event('next')).send(Event('pause')).send(Event('continue')) steps = interpreter.execute() step = steps[-1] self.assertEqual(step.entered_states, ['loop', 'loop.H', 's2']) self.assertEqual(step.exited_states, ['pause', 'loop.H']) self.assertEqual(sorted(interpreter.configuration), ['loop', 's2'])
def generic_temporal_test(self, expression: TemporalExpression, story: list, accept_after: bool): # Todo: convert the story list into a 'real' story that can be told to an interpreter statechart = expression.generate_statechart() interpreter = Interpreter(statechart) for event in story: interpreter.queue(event) interpreter.execute() self.assertEqual(len(interpreter.configuration) == 0, accept_after)
def __init__(self, view, model): with open(get_resource_path('statechart/watch.yml')) as f: statechart = yaml.load(f, Loader=IncludeLoader) statechart = import_from_yaml(text=yaml.dump(statechart)) self.it = Interpreter(statechart) self.it.context["pr"] = self self.it.context["model"] = model self.it.clock.start() self._view = view self._view.schedule_once(self._run_interpreter, 0.1) self._view.set_event_callback(self.it.queue)
def test_simple_configuration(self): sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) interpreter = Interpreter(sc, DummyEvaluator) interpreter.execute_once() # Should do nothing! self.assertEqual(interpreter.configuration, ['s1']) interpreter.send(Event('goto s2')) interpreter.execute_once() self.assertEqual(interpreter.configuration, ['s2']) interpreter.execute_once() self.assertEqual(interpreter.configuration, ['s3'])
class BindTests(unittest.TestCase): def setUp(self): sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) self.interpreter = Interpreter(sc) def test_bind(self): other_sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) other_interpreter = Interpreter(other_sc) self.interpreter.bind(other_interpreter) self.assertEqual(self.interpreter._bound, [other_interpreter.send]) self.interpreter.send(Event('test'), internal=True) self.assertTrue(self.interpreter._events.pop(), Event('test')) self.assertTrue(other_interpreter._events.pop(), Event('test')) def test_bind_callable(self): other_sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) other_interpreter = Interpreter(other_sc) self.interpreter.bind(other_interpreter.send) self.assertEqual(self.interpreter._bound, [other_interpreter.send]) self.interpreter.send(Event('test'), internal=True) self.assertTrue(self.interpreter._events.pop(), Event('test')) self.assertTrue(other_interpreter._events.pop(), Event('test'))
def __init__(self, master=None): super().__init__(master) # Initialize widgets self.create_widgets() # Create a Stopwatch interpreter with open('microwave.yaml') as f: statechart = import_from_yaml(f) self.interpreter = Interpreter(statechart) # Bind interpreter events to the GUI self.interpreter.bind(self.event_handler) self.execute()
class PythonEvaluatorNestedContextTests(unittest.TestCase): def setUp(self): statechart = """ statechart: name: test preamble: x = y = 1 root state: name: root initial: s1 states: - name: s1 on entry: x, z = 2, 1 transitions: - target: s2 guard: y == 1 action: a, z, y = 2, 2, 2 - name: s2 """ sc = import_from_yaml(statechart) self.intp = Interpreter(sc) def test_initialization(self): self.assertEqual(self.intp.context.get('x'), 1) self.assertEqual(self.intp.context.get('y'), 1) with self.assertRaises(KeyError): _ = self.intp.context['z'] with self.assertRaises(KeyError): _ = self.intp.context['a'] def test_global_context(self): self.intp.execute() self.assertEqual(self.intp.context.get('x'), 2) self.assertEqual(self.intp.context.get('y'), 2) with self.assertRaises(KeyError): _ = self.intp.context['z'] with self.assertRaises(KeyError): _ = self.intp.context['a'] def test_nested_context(self): self.intp.execute() s1 = self.intp._evaluator.context_for('s1') self.assertEqual(s1['x'], 2) self.assertEqual(s1['y'], 2) self.assertEqual(s1['z'], 2) with self.assertRaises(KeyError): _ = s1['a']
class Validator(object): '''Verify that messages adhere to my protocol ''' def validate(self, ril_msg): ''' Run through verifier ''' call = type(ril_msg).__name__[3:] command = ril_msg.command # Transfer to function calls if call == 'Request': event = 'onRequest(' + REQUEST[command] + ')' elif call == 'SolicitedResponse': event = 'OnRequestComplete(' + REQUEST[command] + ')' elif call == 'UnsolicitedResponse': event = 'OnUnsolicitedResponse(' + UNSOL[command] + ')' elif call == 'Message': info('\tValidator: message ignored') return else: error('\tValidator: %s not supported by validator', call) return if event not in self.statechart.events_for(): raise ExecutionError( 'Event ' + event + ' is not in state machine nor explicitly ignored.') step = self.interpreter.queue(Event(event)).execute_once() while step: if step.transitions: transition = step.transitions[0] info('\tValidator: %s - %s -> %s', transition.source, transition.event, transition.target) else: raise ExecutionError('No valid transition for ' + event + 'from this state.') step = self.interpreter.execute_once() def __init__(self): with open('statecharts/state_machine.yaml') as f: self.statechart = import_from_yaml(f) assert isinstance(self.statechart, Statechart) self.interpreter = Interpreter(self.statechart) self.interpreter.execute_once()
def __init__(self, race_distance: int, sensor_distance: str, racer_distance: str, events: str): super().__init__() self._racer_distance_topic = racer_distance self._racer = Racer() with open(SCRIPT_DIR / 'racer.yaml') as f: statechart = sismic.io.import_from_yaml(f) context = {'race_distance': race_distance, 'racer': self._racer} self._interpreter = Interpreter(statechart, initial_context=context) self._interpreter.execute() self._io.subscribe(sensor_distance, self._on_sensor_distance) self._io.subscribe(events, self._on_event)
def __init__(self, master=None): super().__init__(master) # Initialize widgets self.create_widgets() # Create a Stopwatch interpreter with open('stopwatch.yaml') as f: statechart = import_from_yaml(f) self.interpreter = Interpreter(statechart) self.interpreter.time = time.time() # Bind interpreter events to the GUI self.interpreter.bind(self.event_handler) # Run the interpreter self.run()
class WatchElevatorTests(unittest.TestCase): def setUp(self): with open('docs/examples/elevator.yaml') as f: sc = io.import_from_yaml(f) self.tested = Interpreter(sc) self.watcher = ExecutionWatcher(self.tested) def test_7th_floor_never_reached(self): with open('docs/examples/tester_elevator_7th_floor_never_reached.yaml' ) as f: tester_sc = io.import_from_yaml(f) tester = self.watcher.watch_with(tester_sc) self.watcher.start() # Send elevator to 4th self.tested.queue(Event('floorSelected', floor=4)).execute() self.watcher.stop() self.assertFalse(tester.final) def test_7th_floor_never_reached_fails(self): with open('docs/examples/tester_elevator_7th_floor_never_reached.yaml' ) as f: tester_sc = io.import_from_yaml(f) tester = self.watcher.watch_with(tester_sc) self.watcher.start() # Send elevator to 7th self.tested.queue(Event('floorSelected', floor=7)).execute() self.watcher.stop() self.assertTrue(tester.final) def test_destination_reached(self): with open( 'docs/examples/tester_elevator_destination_reached.yaml') as f: tester_statechart = io.import_from_yaml(f) # Create the interpreter and the watcher watcher = ExecutionWatcher(self.tested) # Add the tester and start watching tester = watcher.watch_with(tester_statechart) watcher.start() # Send the elevator to 4th self.tested.queue(Event('floorSelected', floor=4)).execute(max_steps=2) self.assertEqual(tester.context['destinations'], [4]) self.tested.execute() self.assertEqual(tester.context['destinations'], []) # Stop watching. The tester must be in a final state watcher.stop() self.assertFalse(tester.final)
def elevator(request): if request.param: sc = import_from_yaml( filepath='docs/examples/elevator/elevator_contract.yaml') else: sc = import_from_yaml(filepath='docs/examples/elevator/elevator.yaml') return Interpreter(sc)
def microwave(request): if request.param: sc = import_from_yaml( filepath='docs/examples/microwave/microwave_with_contracts.yaml') else: sc = import_from_yaml( filepath='docs/examples/microwave/microwave.yaml') return Interpreter(sc)
def generic_test(self, condition: Condition, success_expected: bool, failure_expected: bool, delay: int = 0): statechart = Statechart('test') parallel_state = OrthogonalState('parallel_state') statechart.add_state(parallel_state, parent=None) initial_state = CompoundState('initial_state', initial='Cond') statechart.add_state(initial_state, "parallel_state") statechart.add_state(BasicState('success'), 'initial_state') statechart.add_state(BasicState('failure'), 'initial_state') condition.add_to(statechart=statechart, id='Cond', parent_id='initial_state', status_id=parallel_state, success_id='success', failure_id='failure') interpreter = Interpreter(statechart) self.assertFalse('success' in interpreter.configuration) self.assertFalse('failure' in interpreter.configuration) interpreter.execute() interpreter.time += delay interpreter.queue(Event(Condition.STEP_ENDED_EVENT)) interpreter.queue(Event(Condition.STEP_ENDED_EVENT)) interpreter.execute() self.assertEqual(success_expected, 'success' in interpreter.configuration) self.assertEqual(failure_expected, 'failure' in interpreter.configuration)
class LogTraceTests(unittest.TestCase): def setUp(self): with open('docs/examples/elevator/elevator.yaml') as f: sc = io.import_from_yaml(f) self.tested = Interpreter(sc) self.steps = log_trace(self.tested) def test_empty_trace(self): self.assertEqual(self.steps, []) def test_nonempty_trace(self): self.tested.queue(Event('floorSelected', floor=4)).execute() self.assertTrue(len(self.steps) > 0) def test_log_content(self): self.tested.queue(Event('floorSelected', floor=4)) steps = self.tested.execute() self.assertSequenceEqual(self.steps, steps)
def load_statechart(context, path): with open(path) as f: context._statechart = import_from_yaml(f) context._interpreter = Interpreter(context._statechart) context._steps = [] context._events = [] context._automatic_execution = True context._interpreter.bind(context._events.append) context._interpreter.execute_once() # init
class WatchElevatorTests(unittest.TestCase): def setUp(self): with open('docs/examples/elevator.yaml') as f: sc = io.import_from_yaml(f) self.tested = Interpreter(sc) self.watcher = ExecutionWatcher(self.tested) def test_7th_floor_never_reached(self): with open('docs/examples/tester_elevator_7th_floor_never_reached.yaml') as f: tester_sc = io.import_from_yaml(f) tester = self.watcher.watch_with(tester_sc) self.watcher.start() # Send elevator to 4th self.tested.queue(Event('floorSelected', floor=4)).execute() self.watcher.stop() self.assertFalse(tester.final) def test_7th_floor_never_reached_fails(self): with open('docs/examples/tester_elevator_7th_floor_never_reached.yaml') as f: tester_sc = io.import_from_yaml(f) tester = self.watcher.watch_with(tester_sc) self.watcher.start() # Send elevator to 7th self.tested.queue(Event('floorSelected', floor=7)).execute() self.watcher.stop() self.assertTrue(tester.final) def test_destination_reached(self): with open('docs/examples/tester_elevator_destination_reached.yaml') as f: tester_statechart = io.import_from_yaml(f) # Create the interpreter and the watcher watcher = ExecutionWatcher(self.tested) # Add the tester and start watching tester = watcher.watch_with(tester_statechart) watcher.start() # Send the elevator to 4th self.tested.queue(Event('floorSelected', floor=4)).execute(max_steps=2) self.assertEqual(tester.context['destinations'], [4]) self.tested.execute() self.assertEqual(tester.context['destinations'], []) # Stop watching. The tester must be in a final state watcher.stop() self.assertFalse(tester.final)
def tell(self, interpreter: Interpreter, *args, **kwargs) -> List[MacroStep]: """ Tells the whole story to the interpreter. :param interpreter: an interpreter instance :param args: additional positional arguments that are passed to *interpreter.execute*. :param kwargs: additional keywords arguments that are passed to *interpreter.execute*. :return: the resulting trace of execution (a list of *MacroStep*) """ trace = [] # type: List[MacroStep] for item in self: if isinstance(item, Event): interpreter.queue(item) elif isinstance(item, Pause): interpreter.time += item.duration step = interpreter.execute(*args, **kwargs) if step: trace.extend(step) return trace
def setUp(self): statechart = """ statechart: name: test preamble: x = y = 1 root state: name: root initial: s1 states: - name: s1 on entry: x, z = 2, 1 transitions: - target: s2 guard: y == 1 action: a, z, y = 2, 2, 2 - name: s2 """ sc = import_from_yaml(statechart) self.intp = Interpreter(sc)
def test_tell(self): story = Story([Event('goto s2'), Pause(5), Event('goto final')]) with open('tests/yaml/simple.yaml') as f: sc = io.import_from_yaml(f) interpreter = Interpreter(sc) trace = story.tell(interpreter) self.assertTrue(interpreter.final) self.assertEqual(interpreter.time, 5) self.assertEqual(len(trace), 4)
def test_bind_callable(self): with open('tests/yaml/simple.yaml') as f: other_sc = io.import_from_yaml(f) other_interpreter = Interpreter(other_sc) self.interpreter.bind(other_interpreter.queue) self.assertEqual(self.interpreter._bound, [other_interpreter.queue]) self.interpreter.queue(InternalEvent('test')) self.assertTrue(self.interpreter._internal_events.pop(), Event('test')) self.assertTrue(other_interpreter._external_events.pop(), Event('test'))
def test_simple_entered(self): sc = io.import_from_yaml(open('tests/yaml/simple.yaml')) interpreter = Interpreter(sc, DummyEvaluator) interpreter.send(Event('goto s2')) self.assertEqual(interpreter.execute_once().entered_states, ['s2']) interpreter.send(Event('goto final')) self.assertEqual(interpreter.execute_once().entered_states, ['s3']) self.assertEqual(interpreter.execute_once().entered_states, ['final']) self.assertEqual(interpreter.configuration, []) self.assertTrue(interpreter.final)
def test_bind(self): with open('tests/yaml/simple.yaml') as f: other_sc = io.import_from_yaml(f) other_interpreter = Interpreter(other_sc) self.interpreter.bind(other_interpreter) self.assertIn(other_interpreter.queue, self.interpreter._bound) self.interpreter.raise_event(InternalEvent('test')) self.assertTrue(self.interpreter._internal_events.pop(), Event('test')) self.assertTrue(other_interpreter._external_events.pop(), Event('test'))
def generic_test(self, tested_statechart: Statechart, events: list, property: Condition, expected_success: bool, expected_failure: bool): from sismic.interpreter import log_trace tester_statechart = Statechart('tester_statechart') tester_statechart.add_state(OrthogonalState('parallel_state'), None) tester_statechart.add_state(CompoundState('testing_area', initial='property'), parent='parallel_state') tester_statechart.add_state(BasicState('success_state'), parent='testing_area') tester_statechart.add_state(BasicState('failure_state'), parent='testing_area') property.add_to(tester_statechart, id='property', parent_id='testing_area', status_id='parallel_state', success_id='success_state', failure_id='failure_state') tester_interpreter = Interpreter(tester_statechart) self.assertFalse('success_state' in tester_interpreter.configuration) self.assertFalse('failure_state' in tester_interpreter.configuration) tested_interpreter = Interpreter(tested_statechart) trace = log_trace(tested_interpreter) for event in events: tested_interpreter.queue(event) tested_interpreter.execute() story = teststory_from_trace(trace) story.tell(tester_interpreter) self.assertEqual(expected_success, 'success_state' in tester_interpreter.configuration) self.assertEqual(expected_failure, 'failure_state' in tester_interpreter.configuration)
def test_check_guard_failure(self): statechart = Statechart('statechart') statechart.add_state(OrthogonalState('parallel_state'), parent=None) initial_state = CompoundState('initial_state', initial='condition') statechart.add_state(initial_state, parent='parallel_state') statechart.add_state(BasicState('success'), parent='initial_state') statechart.add_state(BasicState('failure'), parent='initial_state') CheckGuard('x == 1').add_to(statechart=statechart, id='condition', parent_id='initial_state', status_id='parallel_state', success_id='success', failure_id='failure') interpreter = Interpreter(statechart) interpreter.context['x'] = 42 interpreter.execute() self.assertFalse('success' in interpreter.configuration) self.assertTrue('failure' in interpreter.configuration)
def test_doorsOpen(self): sc = io.import_from_yaml(open('docs/examples/elevator.yaml')) interpreter = Interpreter(sc) interpreter.send(Event('floorSelected', floor=4)) interpreter.execute() self.assertEqual(interpreter._evaluator.context['current'], 4) interpreter.time += 10 interpreter.execute() self.assertTrue('doorsOpen' in interpreter.configuration) self.assertEqual(interpreter._evaluator.context['current'], 0)
class WriterExecutionTests(unittest.TestCase): def setUp(self): self.sc = io.import_from_yaml(open('docs/examples/writer_options.yaml')) self.interpreter = Interpreter(self.sc) def test_output(self): scenario = [ Event('keyPress', key='bonjour '), Event('toggle'), Event('keyPress', key='a '), Event('toggle'), Event('toggle_bold'), Event('keyPress', key='tous !'), Event('leave') ] for event in scenario: self.interpreter.send(event) self.interpreter.execute() self.assertTrue(self.interpreter.final) self.assertEqual(self.interpreter.context['output'], ['bonjour ', '[b]', '[i]', 'a ', '[/b]', '[/i]', '[b]', 'tous !', '[/b]'])
def __init__(self, master=None): super().__init__(master) # Initialize widgets self.create_widgets() # Create a Stopwatch interpreter with open('stopwatch.yaml') as f: statechart = import_from_yaml(f) self.interpreter = Interpreter(statechart, initial_time=time.time()) # Bind interpreter events to the GUI self.interpreter.bind(self.event_handler) # Run the interpreter self.run()
def test_exited_order(self): sc = io.import_from_yaml(open('tests/yaml/deep_history.yaml')) interpreter = Interpreter(sc, DummyEvaluator) interpreter.send(Event('next1')).send(Event('next2')).send(Event('pause')) step = interpreter.execute()[-1] self.assertEqual(step.exited_states, ['s12', 's22', 'process_1', 'process_2', 'concurrent_processes', 'active']) self.assertEqual(sorted(interpreter.configuration), ['pause']) step = interpreter.send(Event('continue')).execute_once() self.assertEqual(step.exited_states, ['pause', 'active.H*']) interpreter.send(Event('next1')).send(Event('next2')).execute() self.assertTrue(interpreter.final)