Bläddra i källkod

Merge remote-tracking branch 'upstream/master'

Yentl Van Tendeloo 7 år sedan
förälder
incheckning
ec38c08aca

+ 154 - 0
examples/my-tests/python/create_and_start.py

@@ -0,0 +1,154 @@
+"""
+Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
+
+Model name:   sourcechildbug
+
+"""
+
+from sccd.runtime.statecharts_core import *
+
+# package "sourcechildbug"
+
+class A(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
+        A.user_defined_constructor(self)
+    
+    def user_defined_constructor(self):
+        pass
+    
+    def user_defined_destructor(self):
+        pass
+    
+    
+    # builds Statechart structure
+    def build_statechart_structure(self):
+        
+        # state <root>
+        self.states[""] = State(0, "", self)
+        
+        # state /running
+        self.states["/running"] = State(1, "/running", self)
+        self.states["/running"].setEnter(self._running_enter)
+        
+        # state /created
+        self.states["/created"] = State(2, "/created", self)
+        
+        # state /stopped
+        self.states["/stopped"] = State(3, "/stopped", self)
+        
+        # add children
+        self.states[""].addChild(self.states["/running"])
+        self.states[""].addChild(self.states["/created"])
+        self.states[""].addChild(self.states["/stopped"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/running"]
+        
+        # transition /running
+        _running_0 = Transition(self, self.states["/running"], [self.states["/created"]])
+        _running_0.setAction(self._running_0_exec)
+        _running_0.setTrigger(Event("instance_created", None))
+        self.states["/running"].addTransition(_running_0)
+        
+        # transition /created
+        _created_0 = Transition(self, self.states["/created"], [self.states["/stopped"]])
+        _created_0.setAction(self._created_0_exec)
+        _created_0.setTrigger(Event("instance_started", None))
+        self.states["/created"].addTransition(_created_0)
+    
+    def _running_enter(self):
+        self.big_step.outputEventOM(Event("create_and_start_instance", None, [self, 'to_B']))
+    
+    def _running_0_exec(self, parameters):
+        association_name = parameters[0]
+        print 'A got instance_created [%s]' % association_name
+    
+    def _created_0_exec(self, parameters):
+        association_name = parameters[0]
+        print 'A got instance_started [%s]' % association_name
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/running"].getEffectiveTargetStates()
+        RuntimeClassBase.initializeStatechart(self)
+
+class B(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
+        B.user_defined_constructor(self)
+    
+    def user_defined_constructor(self):
+        pass
+    
+    def user_defined_destructor(self):
+        pass
+    
+    
+    # builds Statechart structure
+    def build_statechart_structure(self):
+        
+        # state <root>
+        self.states[""] = State(0, "", self)
+        
+        # state /running
+        self.states["/running"] = State(1, "/running", self)
+        self.states["/running"].setEnter(self._running_enter)
+        
+        # add children
+        self.states[""].addChild(self.states["/running"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/running"]
+    
+    def _running_enter(self):
+        print 'B instance created!'
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/running"].getEffectiveTargetStates()
+        RuntimeClassBase.initializeStatechart(self)
+
+class ObjectManager(ObjectManagerBase):
+    def __init__(self, controller):
+        ObjectManagerBase.__init__(self, controller)
+    
+    def instantiate(self, class_name, construct_params):
+        if class_name == "A":
+            instance = A(self.controller)
+            instance.associations = {}
+            instance.associations["to_B"] = Association("B", 0, 1)
+        elif class_name == "B":
+            instance = B(self.controller)
+            instance.associations = {}
+        else:
+            raise Exception("Cannot instantiate class " + class_name)
+        return instance
+
+class Controller(ThreadsControllerBase):
+    def __init__(self, keep_running = None, behind_schedule_callback = None):
+        if keep_running == None: keep_running = True
+        if behind_schedule_callback == None: behind_schedule_callback = None
+        ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
+        self.addInputPort("input")
+        self.object_manager.createInstance("A", [])

+ 44 - 0
examples/my-tests/python/create_and_start.xml

@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<diagram name="sourcechildbug">
+    <inport name="input" />
+    <class name="A" default="true">
+        <relationships>
+            <association name="to_B" class="B" min="0" max="1" />
+        </relationships>
+        <scxml initial="running">
+            <state id="running">
+                <onentry>
+                    <raise event="create_and_start_instance" scope="cd">                        
+                        <parameter expr="'to_B'"/>
+                    </raise>
+                </onentry>
+                <transition target="../created" event="instance_created">
+                    <parameter name="association_name" />
+                    <script>
+                        print 'A got instance_created [%s]' % association_name
+                    </script>
+                </transition>
+            </state>
+            <state id="created">
+                <transition target="../stopped" event="instance_started">
+                    <parameter name="association_name" />
+                    <script>
+                        print 'A got instance_started [%s]' % association_name
+                    </script>
+                </transition>
+            </state>
+            <state id="stopped" />
+        </scxml>
+    </class>
+    <class name="B">
+        <scxml initial="running">
+            <state id="running">
+                <onentry>
+                    <script>
+                        print 'B instance created!'
+                    </script>
+                </onentry>
+            </state>
+        </scxml>
+    </class>
+</diagram>

+ 162 - 0
examples/my-tests/python/else_transition.py

@@ -0,0 +1,162 @@
+"""
+Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
+
+Model name:   else_transition_test
+
+"""
+
+from sccd.runtime.statecharts_core import *
+
+# package "else_transition_test"
+
+class A(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
+        A.user_defined_constructor(self)
+    
+    def user_defined_constructor(self):
+        self.x = 3
+    
+    def user_defined_destructor(self):
+        pass
+    
+    
+    # builds Statechart structure
+    def build_statechart_structure(self):
+        
+        # state <root>
+        self.states[""] = State(0, "", self)
+        
+        # state /main
+        self.states["/main"] = ParallelState(1, "/main", self)
+        
+        # state /main/one
+        self.states["/main/one"] = State(2, "/main/one", self)
+        
+        # state /main/one/A
+        self.states["/main/one/A"] = State(3, "/main/one/A", self)
+        
+        # state /main/one/B
+        self.states["/main/one/B"] = State(4, "/main/one/B", self)
+        self.states["/main/one/B"].setEnter(self._main_one_B_enter)
+        self.states["/main/one/B"].setExit(self._main_one_B_exit)
+        
+        # state /main/one/C
+        self.states["/main/one/C"] = State(5, "/main/one/C", self)
+        self.states["/main/one/C"].setEnter(self._main_one_C_enter)
+        self.states["/main/one/C"].setExit(self._main_one_C_exit)
+        
+        # state /main/one/D
+        self.states["/main/one/D"] = State(6, "/main/one/D", self)
+        self.states["/main/one/D"].setEnter(self._main_one_D_enter)
+        
+        # state /main/two
+        self.states["/main/two"] = State(7, "/main/two", self)
+        
+        # state /main/two/A
+        self.states["/main/two/A"] = State(8, "/main/two/A", self)
+        
+        # state /main/two/B
+        self.states["/main/two/B"] = State(9, "/main/two/B", self)
+        
+        # add children
+        self.states[""].addChild(self.states["/main"])
+        self.states["/main"].addChild(self.states["/main/one"])
+        self.states["/main"].addChild(self.states["/main/two"])
+        self.states["/main/one"].addChild(self.states["/main/one/A"])
+        self.states["/main/one"].addChild(self.states["/main/one/B"])
+        self.states["/main/one"].addChild(self.states["/main/one/C"])
+        self.states["/main/one"].addChild(self.states["/main/one/D"])
+        self.states["/main/two"].addChild(self.states["/main/two/A"])
+        self.states["/main/two"].addChild(self.states["/main/two/B"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/main"]
+        self.states["/main/one"].default_state = self.states["/main/one/A"]
+        self.states["/main/two"].default_state = self.states["/main/two/A"]
+        
+        # transition /main/one/A
+        _main_one_A_0 = Transition(self, self.states["/main/one/A"], [self.states["/main/one/B"]])
+        _main_one_A_0.setTrigger(Event("a", None))
+        _main_one_A_0.setGuard(self._main_one_A_0_guard)
+        self.states["/main/one/A"].addTransition(_main_one_A_0)
+        _main_one_A_1 = Transition(self, self.states["/main/one/A"], [self.states["/main/one/C"]])
+        _main_one_A_1.setTrigger(Event("a", None))
+        _main_one_A_1.setGuard(self._main_one_A_1_guard)
+        self.states["/main/one/A"].addTransition(_main_one_A_1)
+        
+        # transition /main/one/B
+        _main_one_B_0 = Transition(self, self.states["/main/one/B"], [self.states["/main/one/A"]])
+        _main_one_B_0.setTrigger(Event("_0after"))
+        self.states["/main/one/B"].addTransition(_main_one_B_0)
+        
+        # transition /main/one/C
+        _main_one_C_0 = Transition(self, self.states["/main/one/C"], [self.states["/main/one/A"]])
+        _main_one_C_0.setTrigger(Event("_1after"))
+        self.states["/main/one/C"].addTransition(_main_one_C_0)
+        
+        # transition /main/two/A
+        _main_two_A_0 = Transition(self, self.states["/main/two/A"], [self.states["/main/two/B"]])
+        _main_two_A_0.setAction(self._main_two_A_0_exec)
+        _main_two_A_0.setTrigger(None)
+        self.states["/main/two/A"].addTransition(_main_two_A_0)
+    
+    def _main_one_B_enter(self):
+        print "in B"
+        self.addTimer(0, 0.05)
+    
+    def _main_one_B_exit(self):
+        self.removeTimer(0)
+    
+    def _main_one_C_enter(self):
+        print "in C"
+        self.addTimer(1, 0.05)
+    
+    def _main_one_C_exit(self):
+        self.removeTimer(1)
+    
+    def _main_one_D_enter(self):
+        print "in D"
+    
+    def _main_one_A_0_guard(self, parameters):
+        return self.x == 5
+    
+    def _main_one_A_1_guard(self, parameters):
+        return "ELSE_GUARD"
+    
+    def _main_two_A_0_exec(self, parameters):
+        self.raiseInternalEvent(Event("a", None, []))
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/main"].getEffectiveTargetStates()
+        RuntimeClassBase.initializeStatechart(self)
+
+class ObjectManager(ObjectManagerBase):
+    def __init__(self, controller):
+        ObjectManagerBase.__init__(self, controller)
+    
+    def instantiate(self, class_name, construct_params):
+        if class_name == "A":
+            instance = A(self.controller)
+            instance.associations = {}
+        else:
+            raise Exception("Cannot instantiate class " + class_name)
+        return instance
+
+class Controller(ThreadsControllerBase):
+    def __init__(self, keep_running = None, behind_schedule_callback = None):
+        if keep_running == None: keep_running = True
+        if behind_schedule_callback == None: behind_schedule_callback = None
+        ThreadsControllerBase.__init__(self, ObjectManager(self), keep_running, behind_schedule_callback)
+        self.object_manager.createInstance("A", [])

+ 51 - 0
examples/my-tests/python/else_transition.xml

@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<diagram name="else_transition_test">
+    <class name="A" default="true">
+        <constructor>
+            <body>
+                self.x = 3
+            </body>
+        </constructor>
+        <scxml initial="main">
+            <parallel id="main">
+                <state id="one" initial="A">
+                    <state id="A">
+                        <transition target="../B" event="a" cond="self.x == 5" />
+                        <transition target="../C" event="a" cond="ELSE" />
+                    </state>
+                    <state id="B">
+                        <onentry>
+                            <script>
+                                print "in B"
+                            </script>
+                        </onentry>
+                        <transition target="../A" after="0.05" />
+                    </state>
+                    <state id="C">
+                        <onentry>
+                            <script>
+                                print "in C"
+                            </script>
+                        </onentry>
+                        <transition target="../A" after="0.05" />
+                    </state>
+                    <state id="D">
+                        <onentry>
+                            <script>
+                                print "in D"
+                            </script>
+                        </onentry>
+                    </state>
+                </state>
+                <state id="two" initial="A">
+                    <state id="A">
+                        <transition target="../B">
+                            <raise event="a" />
+                        </transition>
+                    </state>
+                    <state id="B" />
+                </state>
+            </parallel>
+        </scxml>
+    </class>
+</diagram>

+ 3 - 0
examples/my-tests/python/runner_create_and_start.py

@@ -0,0 +1,3 @@
+import create_and_start
+controller = create_and_start.Controller(False)
+controller.start()

+ 3 - 0
examples/my-tests/python/runner_else_transition.py

@@ -0,0 +1,3 @@
+import else_transition
+controller = else_transition.Controller(False)
+controller.start()

+ 7 - 4
src/python_sccd/python_sccd_compiler/generic_generator.py

@@ -28,9 +28,7 @@ class GenericGenerator(Visitor):
         return self.writer.get()
 
     def visit_ClassDiagram(self, class_diagram):
-        header = ("Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)\n"
-                  "\n"
-                  "Date:   " + time.asctime() + "\n")
+        header = ("Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)\n")
         if class_diagram.name or class_diagram.author or class_diagram.description:
             header += "\n"
         if class_diagram.author:
@@ -515,7 +513,7 @@ class GenericGenerator(Visitor):
             if s.transitions:
                 self.writer.addVSpace()
                 self.writer.addComment("transition %s" % s.new_full_name)
-            for (i, t) in enumerate(s.transitions):
+            for (i, t) in enumerate(s.transitions + s.else_transitions):
                 # instantiate new Transition instance
                 self.writer.addAssignment(
                     GLC.LocalVariableDeclaration(
@@ -773,6 +771,11 @@ class GenericGenerator(Visitor):
                 ]
             )
         )
+        
+    def visit_ElseGuard(self, else_guard):
+        self.writer.add(
+            GLC.String("ELSE_GUARD")
+        )
 
     def visit_Expression(self, expression):
         self.writer.startRecordingExpression()

+ 14 - 3
src/python_sccd/python_sccd_compiler/sccd_constructs.py

@@ -18,6 +18,7 @@ reserved = ["__init__", "__del__", "init", "transition", "microstep", "step", "i
 
 SELF_REFERENCE_SEQ = 'SELF'
 INSTATE_SEQ = 'INSTATE'
+ELSE_SEQ = 'ELSE'
 
 ##################################
 
@@ -42,6 +43,9 @@ class ExpressionPartString(ExpressionPart):
     
 class SelfReference(ExpressionPart):        
     pass
+
+class ElseGuard(ExpressionPart):
+    pass
     
 class InStateCall(ExpressionPart):
     def __init__(self, state_strings):
@@ -72,6 +76,8 @@ class Expression(Visitable):
                     raise CompilerException("Macro \"" + token.val + "\" not allowed here.")
                 elif token.val == SELF_REFERENCE_SEQ :
                     created_object = SelfReference()
+                elif token.val == ELSE_SEQ :
+                    created_object = ElseGuard()
                 elif token.val == INSTATE_SEQ :
                     created_object = self.parseInStateCall()
                     if created_object is None :
@@ -412,7 +418,7 @@ class StateChartTransition(Visitable):
         return self.guard is not None
     
     def getAction(self):
-        return self.action        
+        return self.action
 
 ##################################  
 
@@ -481,12 +487,17 @@ class StateChartNode(Visitable):
         
         #transitions
         self.transitions = []
+        self.else_transitions = []
         for transition_xml in xml_element.findall("transition"):
             transition = StateChartTransition(transition_xml, self)
-            self.transitions.append(transition)
+            if isinstance(transition.guard, ElseGuard):
+                self.else_transitions.append(transition)
+            else:
+                self.transitions.append(transition)
             if transition.trigger.isAfter():
                 self.has_timers = True
-            
+        
+        #TODO: Remove this...
         self.optimizeTransitions()
         self.generateChildren(xml_element)    
         self.calculateDefaults(xml_element)

+ 15 - 6
src/python_sccd/python_sccd_runtime/statecharts_core.py

@@ -30,6 +30,7 @@ from sccd.runtime.accurate_time import AccurateTime
 from time import time
 
 DEBUG = False
+ELSE_GUARD = "ELSE_GUARD"
 
 def print_debug(msg):
     if DEBUG:
@@ -138,7 +139,8 @@ class ObjectManagerBase(object):
                          "associate_instance": self.handleAssociateEvent,
                          "disassociate_instance": self.handleDisassociateEvent,
                          "start_instance": self.handleStartInstanceEvent,
-                         "delete_instance": self.handleDeleteInstanceEvent}
+                         "delete_instance": self.handleDeleteInstanceEvent,
+                         "create_and_start_instance": self.handleCreateAndStartEvent}
         self.lock = threading.Condition()
         
     def addEvent(self, event, time_offset = 0):
@@ -248,8 +250,15 @@ class ObjectManagerBase(object):
             if p:
                 p.addInstance(source)
             source.addEvent(Event("instance_created", None, [association_name+"["+str(index)+"]"]))
+            return [source, association_name+"["+str(index)+"]"]
         else:
             source.addEvent(Event("instance_creation_error", None, [association_name]))
+            return []
+
+    def handleCreateAndStartEvent(self, parameters):
+        params = self.handleCreateEvent(parameters)
+        if params:
+            self.handleStartInstanceEvent(params)
 
     def handleDeleteInstanceEvent(self, parameters):
         if len(parameters) < 2:
@@ -876,13 +885,13 @@ class Transition:
         self.enabled_event = None # the event that enabled this transition
         self.optimize()
     
-    def isEnabled(self, events):
+    def isEnabled(self, events, enabled_transitions):
         if self.trigger is None:
             self.enabled_event = None
-            return (self.guard is None) or self.guard([])
+            return (self.guard is None) or (self.guard == ELSE_GUARD and not enabled_transitions) or self.guard([])
         else:
             for event in events:
-                if (self.trigger.name == event.name and (not self.trigger.port or self.trigger.port == event.port)) and ((self.guard is None) or self.guard(event.parameters)):
+                if (self.trigger.name == event.name and (not self.trigger.port or self.trigger.port == event.port)) and ((self.guard is None) or (self.guard == ELSE_GUARD and not enabled_transitions) or self.guard(event.parameters)):
                     self.enabled_event = event
                     return True
     
@@ -1141,8 +1150,8 @@ class RuntimeClassBase(object):
         enabledEvents = self.getEnabledEvents()
         enabledTransitions = []
         for t in transitions:
-            if t.isEnabled(enabledEvents):
-                enabledTransitions.append(t)
+            if t.isEnabled(enabledEvents, enabledTransitions):
+				enabledTransitions.append(t)
         return enabledTransitions
 
     # @profile