123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- """
- Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
- Date: Mon Aug 08 09:49:24 2016
- Model author: Herr Joeri Exelmans
- Model name: no_statechart
- Model description:
- Test to see what happens when a class with no statechart is defined.
- """
- from sccd.runtime.statecharts_core import *
- # package "no_statechart"
- class my_struct:
- def __init__(self, x, y):
-
- # user defined attributes
- self.x = None
- self.y = None
-
- # call user defined constructor
- my_struct.user_defined_constructor(self, x, y)
-
- def user_defined_constructor(self, x, y):
- self.x = x
- self.y = y
-
- def user_defined_destructor(self):
- pass
- class my_class(RuntimeClassBase):
- def __init__(self, controller):
- RuntimeClassBase.__init__(self, controller)
-
- self.semantics.big_step_maximality = StatechartSemantics.TakeMany
- self.semantics.internal_event_lifeline = StatechartSemantics.Queue
- self.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep
- self.semantics.priority = StatechartSemantics.SourceParent
- self.semantics.concurrency = StatechartSemantics.Single
-
- # build Statechart structure
- self.build_statechart_structure()
-
- # call user defined constructor
- my_class.user_defined_constructor(self)
-
- def user_defined_constructor(self):
- self.data = my_struct(0, 0)
-
- def user_defined_destructor(self):
- pass
-
-
- # builds Statechart structure
- def build_statechart_structure(self):
-
- # state <root>
- self.states[""] = State(0, self)
-
- # state /a
- self.states["/a"] = State(1, self)
- self.states["/a"].setEnter(self._a_enter)
- self.states["/a"].setExit(self._a_exit)
-
- # state /b
- self.states["/b"] = State(2, self)
- self.states["/b"].setEnter(self._b_enter)
- self.states["/b"].setExit(self._b_exit)
-
- # add children
- self.states[""].addChild(self.states["/a"])
- self.states[""].addChild(self.states["/b"])
- self.states[""].fixTree()
- self.states[""].default_state = self.states["/a"]
-
- # transition /a
- _a_0 = Transition(self, self.states["/a"], [self.states["/b"]])
- _a_0.setAction(self._a_0_exec)
- _a_0.setTrigger(Event("_0after"))
- _a_0.setGuard(self._a_0_guard)
- self.states["/a"].addTransition(_a_0)
-
- # transition /b
- _b_0 = Transition(self, self.states["/b"], [self.states["/a"]])
- _b_0.setAction(self._b_0_exec)
- _b_0.setTrigger(Event("_1after"))
- _b_0.setGuard(self._b_0_guard)
- self.states["/b"].addTransition(_b_0)
-
- def _a_enter(self):
- self.addTimer(0, 0.1)
-
- def _a_exit(self):
- self.removeTimer(0)
-
- def _b_enter(self):
- self.addTimer(1, 0.1)
-
- def _b_exit(self):
- self.removeTimer(1)
-
- def _a_0_exec(self, parameters):
- self.big_step.outputEvent(Event("to_b", "out", [self.data.x]))
- self.data.x += 1
-
- def _a_0_guard(self, parameters):
- return self.data.x < 2
-
- def _b_0_exec(self, parameters):
- self.data.y += 1
- self.big_step.outputEvent(Event("to_a", "out", [self.data.y]))
-
- def _b_0_guard(self, parameters):
- return self.data.y < 2
-
- def initializeStatechart(self):
- # enter default state
- states = self.states["/a"].getEffectiveTargetStates()
- self.updateConfiguration(states)
- for state in states:
- if state.enter:
- state.enter()
- class ObjectManager(ObjectManagerBase):
- def __init__(self, controller):
- ObjectManagerBase.__init__(self, controller)
-
- def instantiate(self, class_name, construct_params):
- if class_name == "my_struct":
- instance = my_struct(self.controller, construct_params[0], construct_params[1])
- instance.associations = {}
- elif class_name == "my_class":
- instance = my_class(self.controller)
- instance.associations = {}
- else:
- raise Exception("Cannot instantiate class " + class_name)
- return instance
- class Controller(ThreadsControllerBase):
- def __init__(self, keep_running = None):
- if keep_running == None: keep_running = True
- ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running)
- self.addInputPort("in")
- self.addOutputPort("out")
- self.object_manager.createInstance("my_class", [])
- class InputEvent:
- def __init__(self, name, port, parameters, time_offset):
- self.name = name
- self.port = port
- self.parameters = parameters
- self.time_offset = time_offset
- class Test:
- def __init__(self):
- pass
- input_events = []
- expected_events = [[Event("to_b", "out", [0])], [Event("to_a", "out", [1])], [Event("to_b", "out", [1])], [Event("to_a", "out", [2])]]
|