瀏覽代碼

Add SCCD models for communication in execute_MT and execute_AL (tested)

Yentl Van Tendeloo 8 年之前
父節點
當前提交
64bb31ae9f
共有 6 個文件被更改,包括 325 次插入14 次删除
  1. 105 0
      unit/log_output.py
  2. 40 0
      unit/log_output.xml
  3. 6 3
      wrappers/modelverse.py
  4. 115 0
      wrappers/poll_print.py
  5. 43 0
      wrappers/poll_print.xml
  6. 16 11
      wrappers/test_SCCD.py

+ 105 - 0
unit/log_output.py

@@ -0,0 +1,105 @@
+"""
+Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
+
+Date:   Wed Aug 23 14:57:48 2017
+
+Model author: Yentl Van Tendeloo
+Model name:   Logging
+Model description:
+For testing: append all input to a log until finished
+"""
+
+from sccd.runtime.statecharts_core import *
+
+# package "Logging"
+
+class Logging(RuntimeClassBase):
+    def __init__(self, controller, log):
+        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
+        Logging.user_defined_constructor(self, log)
+    
+    def user_defined_constructor(self, log):
+        self.log = log
+    
+    def user_defined_destructor(self):
+        pass
+    
+    
+    # builds Statechart structure
+    def build_statechart_structure(self):
+        
+        # state <root>
+        self.states[""] = State(0, "", self)
+        
+        # state /init
+        self.states["/init"] = State(1, "/init", self)
+        self.states["/init"].setEnter(self._init_enter)
+        self.states["/init"].setExit(self._init_exit)
+        
+        # state /finished
+        self.states["/finished"] = State(2, "/finished", self)
+        
+        # add children
+        self.states[""].addChild(self.states["/init"])
+        self.states[""].addChild(self.states["/finished"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/init"]
+        
+        # transition /init
+        _init_0 = Transition(self, self.states["/init"], [self.states["/init"]])
+        _init_0.setAction(self._init_0_exec)
+        _init_0.setTrigger(Event("input", "inp"))
+        self.states["/init"].addTransition(_init_0)
+        _init_1 = Transition(self, self.states["/init"], [self.states["/init"]])
+        _init_1.setTrigger(Event("_0after"))
+        self.states["/init"].addTransition(_init_1)
+        _init_2 = Transition(self, self.states["/init"], [self.states["/finished"]])
+        _init_2.setTrigger(Event("terminate", "inp"))
+        self.states["/init"].addTransition(_init_2)
+    
+    def _init_enter(self):
+        self.addTimer(0, 10)
+    
+    def _init_exit(self):
+        self.removeTimer(0)
+    
+    def _init_0_exec(self, parameters):
+        value = parameters[0]
+        self.log.append(value)
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/init"].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 == "Logging":
+            instance = Logging(self.controller, construct_params[0])
+            instance.associations = {}
+        else:
+            raise Exception("Cannot instantiate class " + class_name)
+        return instance
+
+class Controller(ThreadsControllerBase):
+    def __init__(self, log, 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("inp")
+        self.addOutputPort("outp")
+        self.object_manager.createInstance("Logging", [log])

+ 40 - 0
unit/log_output.xml

@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<diagram author="Yentl Van Tendeloo" name="Logging">
+    <description>
+        For testing: append all input to a log until finished
+    </description>
+
+    <inport name="inp"/>
+    <outport name="outp"/>
+
+    <class name="Logging" default="true">
+        <constructor>
+            <parameter name="log"/>
+            <body>
+                self.log = log
+                print("SC initialized")
+            </body>
+        </constructor>
+        <scxml initial="init">
+            <state id="init">
+                <transition event="input" port="inp" target=".">
+                    <parameter name="value"/>
+                    <script>
+                        self.log.append(value)
+                        print("Got value: " + str(value))
+                    </script>
+                </transition>
+
+                <transition after="10" target="."/>
+
+                <transition event="terminate" port="inp" target="../finished"/>
+            </state>
+
+            <state id="finished">
+                <script>
+                    print("FINISHED")
+                </script>
+            </state>
+        </scxml>
+    </class>
+</diagram>

+ 6 - 3
wrappers/modelverse.py

@@ -74,8 +74,11 @@ def _output_thread(outputs):
     while taskname is None:
         time.sleep(0.1)
 
-    while 1:
-        outputs.append(json.loads(urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname}))).read()))
+    try:
+        while 1:
+            outputs.append(json.loads(urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname}))).read()))
+    except:
+        pass
 
 thrd = threading.Thread(target=_output_thread, args=[outputs])
 thrd.daemon = True
@@ -104,7 +107,7 @@ def _exec_on_statechart(statechart):
             if input_event is not None:
                 # Expand the event and make it HTTP input
                 print("Got SC event: " + str(input_event))
-                _input(input_event)
+                _input(input_event.parameters)
                 
             time.sleep(0.01)
         

+ 115 - 0
wrappers/poll_print.py

@@ -0,0 +1,115 @@
+"""
+Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
+
+Date:   Wed Aug 23 16:04:59 2017
+
+Model author: Yentl Van Tendeloo
+Model name:   Logging
+Model description:
+For testing: I/O Statechart for the Modelverse
+"""
+
+from sccd.runtime.statecharts_core import *
+
+# package "Logging"
+
+class Logging(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
+        Logging.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 /init
+        self.states["/init"] = State(1, "/init", self)
+        self.states["/init"].setEnter(self._init_enter)
+        self.states["/init"].setExit(self._init_exit)
+        
+        # state /finished
+        self.states["/finished"] = State(2, "/finished", self)
+        
+        # add children
+        self.states[""].addChild(self.states["/init"])
+        self.states[""].addChild(self.states["/finished"])
+        self.states[""].fixTree()
+        self.states[""].default_state = self.states["/init"]
+        
+        # transition /init
+        _init_0 = Transition(self, self.states["/init"], [self.states["/init"]])
+        _init_0.setAction(self._init_0_exec)
+        _init_0.setTrigger(Event("input", "inp"))
+        self.states["/init"].addTransition(_init_0)
+        _init_1 = Transition(self, self.states["/init"], [self.states["/init"]])
+        _init_1.setTrigger(Event("_0after"))
+        self.states["/init"].addTransition(_init_1)
+        _init_2 = Transition(self, self.states["/init"], [self.states["/finished"]])
+        _init_2.setTrigger(Event("terminate", "inp"))
+        self.states["/init"].addTransition(_init_2)
+        _init_3 = Transition(self, self.states["/init"], [self.states["/init"]])
+        _init_3.setAction(self._init_3_exec)
+        _init_3.setTrigger(Event("raw_inp", "user_inp"))
+        self.states["/init"].addTransition(_init_3)
+    
+    def _init_enter(self):
+        self.addTimer(0, 10)
+    
+    def _init_exit(self):
+        self.removeTimer(0)
+    
+    def _init_0_exec(self, parameters):
+        value = parameters[0]
+        print(value)
+    
+    def _init_3_exec(self, parameters):
+        inp = parameters[0]
+        print("Got raw input")
+        self.big_step.outputEvent(Event("output", "outp", [inp]))
+    
+    def initializeStatechart(self):
+        # enter default state
+        self.default_targets = self.states["/init"].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 == "Logging":
+            instance = Logging(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("user_inp")
+        self.addInputPort("inp")
+        self.addOutputPort("outp")
+        self.object_manager.createInstance("Logging", [])

+ 43 - 0
wrappers/poll_print.xml

@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<diagram author="Yentl Van Tendeloo" name="Logging">
+    <description>
+        For testing: I/O Statechart for the Modelverse
+    </description>
+
+    <inport name="user_inp"/>
+    <inport name="inp"/>
+    <outport name="outp"/>
+
+    <class name="Logging" default="true">
+        <scxml initial="init">
+            <state id="init">
+                <transition event="input" port="inp" target=".">
+                    <parameter name="value"/>
+                    <script>
+                        print(value)
+                    </script>
+                </transition>
+
+                <transition after="10" target="."/>
+
+                <transition event="terminate" port="inp" target="../finished"/>
+
+                <transition event="raw_inp" port="user_inp" target=".">
+                    <parameter name="inp"/>
+                    <script>
+                        print("Got raw input")
+                    </script>
+                    <raise event="output" port="outp" scope="output">
+                        <parameter expr="inp"/>
+                    </raise>
+                </transition>
+            </state>
+
+            <state id="finished">
+                <script>
+                    print("FINISHED")
+                </script>
+            </state>
+        </scxml>
+    </class>
+</diagram>

+ 16 - 11
wrappers/test_SCCD.py

@@ -1,6 +1,7 @@
 import sys
 sys.path.append("wrappers")
 from modelverse import *
+from sccd.runtime.statecharts_core import Event
 
 init()
 login("admin", "admin")
@@ -19,19 +20,23 @@ except ModelExists:
 # Update the model of the traffic light
 if "my_SCCD" in [i[0] for i in model_list("models")]:
     model_delete("models/my_SCCD")
-#model_add("models/my_SCCD", "formalisms/SCCD", open("models/dynamic_trafficlight.mvc", 'r').read())
-model_add("models/my_SCCD", "formalisms/SCCD", open("integration/code/SCCD_all.mvc", 'r').read())
+model_add("models/my_SCCD", "formalisms/SCCD", open("models/dynamic_trafficlight.mvc", 'r').read())
 
 # Add SCCD execution semantics
 transformation_add_AL({"SCCD": "formalisms/SCCD"}, {"trace": "formalisms/SCCD_Trace"}, "models/SCCD_execute", open("models/SCCD_execute.alc", 'r').read())
 
-# Execute it, while interacting through the console
-def callback(inp):
-    print(inp)
-    inp = raw_input()
-    if inp == "":
-        return None
-    else:
-        return inp
+import poll_print
+ctrl = poll_print.Controller(keep_running=False)
 
-transformation_execute_AL("models/SCCD_execute", {"SCCD": "models/my_SCCD"}, {"trace": "models/my_trace"}, callback=callback)
+thrd = threading.Thread(target=ctrl.start)
+thrd.daemon = True
+thrd.start()
+
+transformation_execute_AL("models/SCCD_execute", {"SCCD": "models/my_SCCD"}, {"trace": "models/my_trace"}, statechart=(ctrl, "inp", "outp"))
+
+import select
+while thrd.is_alive():
+    if select.select([sys.stdin], [], [], 0.1)[0]:
+        ctrl.addInput(Event("raw_inp", "user_inp", [sys.stdin.readline().strip()]))
+
+thrd.join()