Browse Source

fixed yentl's eventloop bug

Simon Van Mierlo 7 years ago
parent
commit
f54e60ab1e

+ 95 - 0
examples/my-tests/python/eventloop_bug.py

@@ -0,0 +1,95 @@
+"""
+Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
+
+Date:   Wed Sep 27 15:08:22 2017
+
+Model name:   multiple-raises-parallel
+
+"""
+
+from sccd.runtime.statecharts_core import *
+
+# package "multiple-raises-parallel"
+
+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 /listening
+        self.states["/listening"] = State(1, "/listening", self)
+        self.states["/listening"].setEnter(self._listening_enter)
+        self.states["/listening"].setExit(self._listening_exit)
+        
+        # add children
+        self.states[""].addChild(self.states["/listening"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/listening"]
+        
+        # transition /listening
+        _listening_0 = Transition(self, self.states["/listening"], [self.states["/listening"]])
+        _listening_0.setAction(self._listening_0_exec)
+        _listening_0.setTrigger(Event("input", "input"))
+        self.states["/listening"].addTransition(_listening_0)
+        _listening_1 = Transition(self, self.states["/listening"], [self.states["/listening"]])
+        _listening_1.setTrigger(Event("_0after"))
+        self.states["/listening"].addTransition(_listening_1)
+    
+    def _listening_enter(self):
+        self.addTimer(0, 1)
+    
+    def _listening_exit(self):
+        self.removeTimer(0)
+    
+    def _listening_0_exec(self, parameters):
+        value = parameters[0]
+        print(value)
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/listening"].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(EventLoopControllerBase):
+    def __init__(self, event_loop_callbacks, finished_callback = None, behind_schedule_callback = None):
+        if finished_callback == None: finished_callback = None
+        if behind_schedule_callback == None: behind_schedule_callback = None
+        EventLoopControllerBase.__init__(self, ObjectManager(self), event_loop_callbacks, finished_callback, behind_schedule_callback)
+        self.addInputPort("input")
+        self.object_manager.createInstance("A", [])

+ 17 - 0
examples/my-tests/python/eventloop_bug.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<diagram name="multiple-raises-parallel">
+    <inport name="input" />
+    <class name="A" default="true">
+        <scxml initial="listening">
+            <state id="listening">
+                <transition target="." event="input" port="input">
+                    <parameter name="value" />
+                    <script>
+                        print(value)
+                    </script>
+                </transition>
+                <transition target="." after="1" />
+            </state>
+        </scxml>
+    </class>
+</diagram>

+ 28 - 0
examples/my-tests/python/runner_eventloop_bug.py

@@ -0,0 +1,28 @@
+'''
+Created on 27-jul.-2014
+
+@author: Simon
+'''
+
+import Tkinter as tk
+import threading, time
+import eventloop_bug
+from sccd.runtime.statecharts_core import Event
+from sccd.runtime.tkinter_eventloop import *
+
+if __name__ == '__main__':
+    window = tk.Tk()
+    window.withdraw()
+    controller = eventloop_bug.Controller(TkEventLoop(window))
+    
+    def inputter():
+        time.sleep(1)
+        while 1:
+            controller.addInput(Event("hello world", "input", []))
+    for _ in range(2):
+        thread = threading.Thread(target=inputter)
+        thread.daemon = True
+        thread.start()
+    
+    controller.start()
+    window.mainloop()

+ 11 - 9
src/python_sccd/python_sccd_runtime/statecharts_core.py

@@ -566,13 +566,15 @@ class EventLoopControllerBase(ControllerBase):
         self.behind_schedule_callback = behind_schedule_callback
         self.behind_schedule_callback = behind_schedule_callback
         self.last_print_time = 0
         self.last_print_time = 0
         self.running = False
         self.running = False
+        self.input_condition = threading.Condition()
 
 
     def addInput(self, input_event, time_offset = 0, force_internal=False):
     def addInput(self, input_event, time_offset = 0, force_internal=False):
-        ControllerBase.addInput(self, input_event, time_offset, force_internal)
-        self.event_loop.clear()
-        self.simulated_time = self.getEarliestEventTime()
-        if not self.running:
-            self.run()
+        with self.input_condition:
+            ControllerBase.addInput(self, input_event, time_offset, force_internal)
+            self.event_loop.clear()
+            self.simulated_time = self.getEarliestEventTime()
+            if not self.running:
+                self.run()
 
 
     def start(self):
     def start(self):
         ControllerBase.start(self)
         ControllerBase.start(self)
@@ -587,10 +589,10 @@ class EventLoopControllerBase(ControllerBase):
         try:
         try:
             self.running = True
             self.running = True
             while 1:
             while 1:
-                # clear existing timeout
-                self.event_loop.clear()
-                # simulate
-                self.handleInput()
+                with self.input_condition:
+                    # clear existing timeout
+                    self.event_loop.clear()
+                    self.handleInput()
                 self.object_manager.stepAll()
                 self.object_manager.stepAll()
                 # schedule next timeout
                 # schedule next timeout
                 earliest_event_time = self.getEarliestEventTime()
                 earliest_event_time = self.getEarliestEventTime()