Skip to content

Commit

Permalink
Fix contract in elevator example
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Feb 12, 2016
1 parent f4fae23 commit 17fc19b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 39 deletions.
19 changes: 4 additions & 15 deletions docs/examples/elevator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ statechart:
preamble: |
current = 0
destination = 0
class Doors:
def __init__(self):
self.opened = True
def open(self):
self.opened = True
def close(self):
self.opened = False
doors = Doors()
doors_open = True
root state:
name: active
parallel states:
Expand All @@ -25,12 +14,12 @@ statechart:
transitions:
- target: doorsClosed
guard: destination != current
action: doors.close()
action: doors_open = False
- target: doorsClosed
guard: after(10) and current > 0
action: |
destination = 0
doors.close()
doors_open = False
- name: doorsClosed
transitions:
- target: movingUp
Expand All @@ -41,7 +30,7 @@ statechart:
transitions:
- target: doorsOpen
guard: destination == current
action: doors.open()
action: doors_open = True
states:
- name: movingUp
on entry: current = current + 1
Expand Down
29 changes: 9 additions & 20 deletions docs/examples/elevator_contract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ statechart:
preamble: |
current = 0
destination = 0
class Doors:
def __init__(self):
self.opened = True
def open(self):
self.opened = True
def close(self):
self.opened = False
doors = Doors()
doors_open = True
root state:
name: active
contract:
Expand All @@ -29,15 +18,15 @@ statechart:
transitions:
- target: doorsClosed
guard: destination != current
action: doors.close()
action: doors_open = False
- target: doorsClosed
guard: after(10) and current > 0
action: |
destination = 0
doors.close()
doors_open = False
contract:
- before: current > 0
- after: destination = 0
- after: destination == 0
- name: doorsClosed
transitions:
- target: movingUp
Expand All @@ -46,17 +35,17 @@ statechart:
guard: destination < current and destination >= 0
- name: moving
contract:
- always: not doors.opened # Keep doors closed while moving
- always: not doors_open # Keep doors closed while moving
- before: destination != current # Move only if destination is not reached
- after: destination == current # Destination should be reached
- after: current != __old__.current # Current changed (redundant)
- after: current != __old__.current # Current changed (redundant given the two last conditions)
transitions:
- target: doorsOpen
guard: destination == current
action: doors.open()
action: doors_open = True
contract:
- before: not doors.opened # Doors are closed
- after: doors.opened # Doors are opened
- before: not doors_open # Doors are closed
- after: doors_open # Doors are opened
states:
- name: movingUp
on entry: current = current + 1
Expand Down
15 changes: 11 additions & 4 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sismic.model import Event


class SimulatorElevatorTests(unittest.TestCase):
class ElevatorTests(unittest.TestCase):
def setUp(self):
with open('docs/examples/elevator.yaml') as f:
self.sc = io.import_from_yaml(f)
Expand All @@ -15,19 +15,26 @@ def test_init(self):

def test_floor_selection(self):
self.interpreter.queue(Event('floorSelected', floor=4)).execute_once()
self.assertEqual(self.interpreter._evaluator.context['destination'], 4)
self.assertEqual(self.interpreter.context['destination'], 4)
self.interpreter.execute_once()
self.assertEqual(sorted(self.interpreter.configuration), ['active', 'doorsClosed', 'floorListener', 'floorSelecting', 'movingElevator'])

def test_doorsOpen(self):
self.interpreter.queue(Event('floorSelected', floor=4))
self.interpreter.execute()
self.assertEqual(self.interpreter._evaluator.context['current'], 4)
self.assertEqual(self.interpreter.context['current'], 4)
self.interpreter.time += 10
self.interpreter.execute()

self.assertTrue('doorsOpen' in self.interpreter.configuration)
self.assertEqual(self.interpreter._evaluator.context['current'], 0)
self.assertEqual(self.interpreter.context['current'], 0)


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)


class WriterExecutionTests(unittest.TestCase):
Expand Down

0 comments on commit 17fc19b

Please sign in to comment.