Browse Source

JSON service works with new modelverse client wrapper

Yentl Van Tendeloo 7 years ago
parent
commit
dd0c482bdd
4 changed files with 191 additions and 33 deletions
  1. 2 1
      scripts/JSON_service.py
  2. 52 0
      wrappers/classes/modelverse.xml
  3. 34 12
      wrappers/modelverse.py
  4. 103 20
      wrappers/modelverse_SCCD.py

+ 2 - 1
scripts/JSON_service.py

@@ -1,6 +1,6 @@
 import sys
 sys.path.append("wrappers")
-from modelverse_coded import *
+from modelverse import *
 import os
 import json
 
@@ -77,6 +77,7 @@ def json_service(port):
     print("JSON took %ss" % (time.time() - start))
 
 service_register("JSON", json_service)
+print("JSON service ready!")
 
 try:
     while raw_input() != "STOP":

File diff suppressed because it is too large
+ 52 - 0
wrappers/classes/modelverse.xml


+ 34 - 12
wrappers/modelverse.py

@@ -84,6 +84,7 @@ def init(address_param="127.0.0.1:8001", timeout=20.0):
     controller.addOutputListener("ready").fetch(-1)
 
     INPUT("init", None, [address_param, timeout])
+    controller.address = address_param
     return OUTPUT()
 
 def login(username, password):
@@ -232,7 +233,6 @@ def transformation_execute_AL(operation_name, input_models_dict, output_models_d
         return OUTPUT()
 
 def transformation_execute_MANUAL(operation_name, input_models_dict, output_models_dict, callback=None, tracability_model=""):
-    print("EXEC MANUAL")
     INPUT("transformation_execute_MANUAL", None, [operation_name, input_models_dict, output_models_dict, tracability_model])
     context = OUTPUT()
     if callback is not None:
@@ -390,30 +390,21 @@ def process_execute(process_name, prefix, callbacks=None):
             sc_ports[k] = v[0].addOutputListener(v[2])
 
     INPUT("process_execute", None, [process_name, prefix])
-    print("Process execute")
 
     operation = OUTPUT()
-    print("Operation: " + str(operation))
     while 1:
         if isinstance(operation, (list, tuple)):
             t, name, context = operation
             if t == "OP":
-                print("Got OP")
                 if name in callbacks:
-                    print("Callbacks")
                     callbacks[name](context)
-                    print("DONE")
                 INPUT("exit", context, [])
                 operation = OUTPUT()
-                print("OK, next operation: " + str(operation))
             elif t == "SC":
-                print("Got SC")
                 if name in callbacks:
                     statechart = callbacks[name]
-                    print("SC")
                 else:
                     statechart = None
-                    print("NONE")
 
                 while 1:
                     empty = True
@@ -426,13 +417,11 @@ def process_execute(process_name, prefix, callbacks=None):
                             if statechart:
                                 statechart[0].addInput(Event("input", statechart[1], response.parameters))
                         elif response.name == "result":
-                            print("Result")
                             # Finished execution, so continue and return result
                             if statechart:
                                 statechart[0].addInput(Event("terminate", statechart[1], []))
                             # Break from the most inner loop
                             operation = response.parameters[1]
-                            print("OK, next operation: " + str(operation))
                             break
                         empty = False
 
@@ -454,3 +443,36 @@ def process_execute(process_name, prefix, callbacks=None):
 def get_taskname():
     """Fetch the taskname of the current connection."""
     return controller.taskname
+
+""" Some hardcoded functions... Way easier to express them with code than with statecharts!"""
+import json
+import urllib
+import urllib2
+
+def service_register(name, function):
+    """Register a function as a service with a specific name."""
+    INPUT("service_register", None, [name, function])
+    port = OUTPUT()
+    return port
+
+def service_stop():
+    """Stop the currently executing process."""
+    INPUT("service_stop", None, [])
+    return OUTPUT()
+
+def service_get(port):
+    """Get the values on the specified port."""
+    val = json.loads(urllib2.urlopen(urllib2.Request("http://%s" % controller.address, urllib.urlencode({"op": "get_output", "taskname": port}))).read())
+    return val
+
+def service_set(port, value):
+    """Set a value on a specified port."""
+    value = json.dumps(value)
+    if isinstance(value, type([])):
+        urllib2.urlopen(urllib2.Request("http://%s" % controller.address, urllib.urlencode({"op": "set_input", "data": value, "taskname": port}))).read()
+    else:
+        urllib2.urlopen(urllib2.Request("http://%s" % controller.address, urllib.urlencode({"op": "set_input", "value": value, "taskname": port}))).read()
+
+def service_poll(port):
+    """Checks whether or not the Modelverse side has any input ready to be processed."""
+    raise NotImplementedError()

+ 103 - 20
wrappers/modelverse_SCCD.py

@@ -1,7 +1,7 @@
 """
 Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
 
-Date:   Tue Oct 31 10:44:35 2017
+Date:   Tue Oct 31 11:35:41 2017
 
 Model author: Yentl Van Tendeloo
 Model name:   MvK Server
@@ -517,64 +517,75 @@ class Modelverse(RuntimeClassBase):
         self.states["/initialized/behaviour/operations/save"] = State(110, "/initialized/behaviour/operations/save", self)
         self.states["/initialized/behaviour/operations/save"].setEnter(self._initialized_behaviour_operations_save_enter)
         
+        # state /initialized/behaviour/operations/service_register
+        self.states["/initialized/behaviour/operations/service_register"] = State(111, "/initialized/behaviour/operations/service_register", self)
+        self.states["/initialized/behaviour/operations/service_register"].setEnter(self._initialized_behaviour_operations_service_register_enter)
+        
+        # state /initialized/behaviour/operations/service_stop
+        self.states["/initialized/behaviour/operations/service_stop"] = State(112, "/initialized/behaviour/operations/service_stop", self)
+        self.states["/initialized/behaviour/operations/service_stop"].setEnter(self._initialized_behaviour_operations_service_stop_enter)
+        
         # state /initialized/behaviour/wait_for_action
-        self.states["/initialized/behaviour/wait_for_action"] = State(111, "/initialized/behaviour/wait_for_action", self)
+        self.states["/initialized/behaviour/wait_for_action"] = State(113, "/initialized/behaviour/wait_for_action", self)
         
         # state /initialized/behaviour/wait_for_action/disconnected
-        self.states["/initialized/behaviour/wait_for_action/disconnected"] = State(112, "/initialized/behaviour/wait_for_action/disconnected", self)
+        self.states["/initialized/behaviour/wait_for_action/disconnected"] = State(114, "/initialized/behaviour/wait_for_action/disconnected", self)
         
         # state /initialized/behaviour/wait_for_action/connected
-        self.states["/initialized/behaviour/wait_for_action/connected"] = State(113, "/initialized/behaviour/wait_for_action/connected", self)
+        self.states["/initialized/behaviour/wait_for_action/connected"] = State(115, "/initialized/behaviour/wait_for_action/connected", self)
         
         # state /initialized/behaviour/wait_for_action/megamodelling
-        self.states["/initialized/behaviour/wait_for_action/megamodelling"] = State(114, "/initialized/behaviour/wait_for_action/megamodelling", self)
+        self.states["/initialized/behaviour/wait_for_action/megamodelling"] = State(116, "/initialized/behaviour/wait_for_action/megamodelling", self)
+        
+        # state /initialized/behaviour/wait_for_action/service
+        self.states["/initialized/behaviour/wait_for_action/service"] = State(117, "/initialized/behaviour/wait_for_action/service", self)
         
         # state /initialized/behaviour/wait_for_action/modelling
-        self.states["/initialized/behaviour/wait_for_action/modelling"] = State(115, "/initialized/behaviour/wait_for_action/modelling", self)
+        self.states["/initialized/behaviour/wait_for_action/modelling"] = State(118, "/initialized/behaviour/wait_for_action/modelling", self)
         
         # state /initialized/behaviour/wait_for_action/modelling/recognized
-        self.states["/initialized/behaviour/wait_for_action/modelling/recognized"] = State(116, "/initialized/behaviour/wait_for_action/modelling/recognized", self)
+        self.states["/initialized/behaviour/wait_for_action/modelling/recognized"] = State(119, "/initialized/behaviour/wait_for_action/modelling/recognized", self)
         
         # state /initialized/behaviour/wait_for_action/modelling/recognized/manual
-        self.states["/initialized/behaviour/wait_for_action/modelling/recognized/manual"] = State(117, "/initialized/behaviour/wait_for_action/modelling/recognized/manual", self)
+        self.states["/initialized/behaviour/wait_for_action/modelling/recognized/manual"] = State(120, "/initialized/behaviour/wait_for_action/modelling/recognized/manual", self)
         
         # state /initialized/behaviour/wait_for_action/modelling/recognized/scripted
-        self.states["/initialized/behaviour/wait_for_action/modelling/recognized/scripted"] = State(118, "/initialized/behaviour/wait_for_action/modelling/recognized/scripted", self)
+        self.states["/initialized/behaviour/wait_for_action/modelling/recognized/scripted"] = State(121, "/initialized/behaviour/wait_for_action/modelling/recognized/scripted", self)
         
         # state /initialized/behaviour/wait_for_action/finished
-        self.states["/initialized/behaviour/wait_for_action/finished"] = State(119, "/initialized/behaviour/wait_for_action/finished", self)
+        self.states["/initialized/behaviour/wait_for_action/finished"] = State(122, "/initialized/behaviour/wait_for_action/finished", self)
         
         # state /initialized/behaviour/wait_for_action/history
-        self.states["/initialized/behaviour/wait_for_action/history"] = DeepHistoryState(120, "/initialized/behaviour/wait_for_action/history", self)
+        self.states["/initialized/behaviour/wait_for_action/history"] = DeepHistoryState(123, "/initialized/behaviour/wait_for_action/history", self)
         
         # state /initialized/behaviour/going_manual
-        self.states["/initialized/behaviour/going_manual"] = State(121, "/initialized/behaviour/going_manual", self)
+        self.states["/initialized/behaviour/going_manual"] = State(124, "/initialized/behaviour/going_manual", self)
         
         # state /initialized/behaviour/going_manual/init
-        self.states["/initialized/behaviour/going_manual/init"] = State(122, "/initialized/behaviour/going_manual/init", self)
+        self.states["/initialized/behaviour/going_manual/init"] = State(125, "/initialized/behaviour/going_manual/init", self)
         self.states["/initialized/behaviour/going_manual/init"].setEnter(self._initialized_behaviour_going_manual_init_enter)
         
         # state /initialized/behaviour/going_manual/search
-        self.states["/initialized/behaviour/going_manual/search"] = State(123, "/initialized/behaviour/going_manual/search", self)
+        self.states["/initialized/behaviour/going_manual/search"] = State(126, "/initialized/behaviour/going_manual/search", self)
         self.states["/initialized/behaviour/going_manual/search"].setEnter(self._initialized_behaviour_going_manual_search_enter)
         
         # state /initialized/behaviour/going_manual/OK
-        self.states["/initialized/behaviour/going_manual/OK"] = State(124, "/initialized/behaviour/going_manual/OK", self)
+        self.states["/initialized/behaviour/going_manual/OK"] = State(127, "/initialized/behaviour/going_manual/OK", self)
         self.states["/initialized/behaviour/going_manual/OK"].setEnter(self._initialized_behaviour_going_manual_OK_enter)
         
         # state /initialized/behaviour/going_scripted
-        self.states["/initialized/behaviour/going_scripted"] = State(125, "/initialized/behaviour/going_scripted", self)
+        self.states["/initialized/behaviour/going_scripted"] = State(128, "/initialized/behaviour/going_scripted", self)
         self.states["/initialized/behaviour/going_scripted"].setEnter(self._initialized_behaviour_going_scripted_enter)
         
         # state /initialized/behaviour/leaving_manual
-        self.states["/initialized/behaviour/leaving_manual"] = State(126, "/initialized/behaviour/leaving_manual", self)
+        self.states["/initialized/behaviour/leaving_manual"] = State(129, "/initialized/behaviour/leaving_manual", self)
         self.states["/initialized/behaviour/leaving_manual"].setEnter(self._initialized_behaviour_leaving_manual_enter)
         
         # state /initialized/queue
-        self.states["/initialized/queue"] = State(127, "/initialized/queue", self)
+        self.states["/initialized/queue"] = State(130, "/initialized/queue", self)
         
         # state /initialized/queue/queue
-        self.states["/initialized/queue/queue"] = State(128, "/initialized/queue/queue", self)
+        self.states["/initialized/queue/queue"] = State(131, "/initialized/queue/queue", self)
         
         # add children
         self.states[""].addChild(self.states["/init"])
@@ -652,6 +663,8 @@ class Modelverse(RuntimeClassBase):
         self.states["/initialized/behaviour/operations"].addChild(self.states["/initialized/behaviour/operations/undefine_attribute"])
         self.states["/initialized/behaviour/operations"].addChild(self.states["/initialized/behaviour/operations/all_instances"])
         self.states["/initialized/behaviour/operations"].addChild(self.states["/initialized/behaviour/operations/save"])
+        self.states["/initialized/behaviour/operations"].addChild(self.states["/initialized/behaviour/operations/service_register"])
+        self.states["/initialized/behaviour/operations"].addChild(self.states["/initialized/behaviour/operations/service_stop"])
         self.states["/initialized/behaviour/operations/login"].addChild(self.states["/initialized/behaviour/operations/login/wait_prompt_1"])
         self.states["/initialized/behaviour/operations/login"].addChild(self.states["/initialized/behaviour/operations/login/wait_prompt_2"])
         self.states["/initialized/behaviour/operations/login"].addChild(self.states["/initialized/behaviour/operations/login/wait_prompt_existing"])
@@ -695,6 +708,7 @@ class Modelverse(RuntimeClassBase):
         self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/disconnected"])
         self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/connected"])
         self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/megamodelling"])
+        self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/service"])
         self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/modelling"])
         self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/finished"])
         self.states["/initialized/behaviour/wait_for_action"].addChild(self.states["/initialized/behaviour/wait_for_action/history"])
@@ -1429,6 +1443,19 @@ class Modelverse(RuntimeClassBase):
         _initialized_behaviour_operations_save_1.setGuard(self._initialized_behaviour_operations_save_1_guard)
         self.states["/initialized/behaviour/operations/save"].addTransition(_initialized_behaviour_operations_save_1)
         
+        # transition /initialized/behaviour/operations/service_register
+        _initialized_behaviour_operations_service_register_0 = Transition(self, self.states["/initialized/behaviour/operations/service_register"], [self.states["/initialized/behaviour/wait_for_action/service"]])
+        _initialized_behaviour_operations_service_register_0.setAction(self._initialized_behaviour_operations_service_register_0_exec)
+        _initialized_behaviour_operations_service_register_0.setTrigger(None)
+        _initialized_behaviour_operations_service_register_0.setGuard(self._initialized_behaviour_operations_service_register_0_guard)
+        self.states["/initialized/behaviour/operations/service_register"].addTransition(_initialized_behaviour_operations_service_register_0)
+        
+        # transition /initialized/behaviour/operations/service_stop
+        _initialized_behaviour_operations_service_stop_0 = Transition(self, self.states["/initialized/behaviour/operations/service_stop"], [self.states["/initialized/behaviour/wait_for_action/megamodelling"]])
+        _initialized_behaviour_operations_service_stop_0.setTrigger(None)
+        _initialized_behaviour_operations_service_stop_0.setGuard(self._initialized_behaviour_operations_service_stop_0_guard)
+        self.states["/initialized/behaviour/operations/service_stop"].addTransition(_initialized_behaviour_operations_service_stop_0)
+        
         # transition /initialized/behaviour/wait_for_action/disconnected
         _initialized_behaviour_wait_for_action_disconnected_0 = Transition(self, self.states["/initialized/behaviour/wait_for_action/disconnected"], [self.states["/initialized/behaviour/init"]])
         _initialized_behaviour_wait_for_action_disconnected_0.setAction(self._initialized_behaviour_wait_for_action_disconnected_0_exec)
@@ -1629,10 +1656,27 @@ class Modelverse(RuntimeClassBase):
         _initialized_behaviour_wait_for_action_megamodelling_36.setTrigger(None)
         _initialized_behaviour_wait_for_action_megamodelling_36.setGuard(self._initialized_behaviour_wait_for_action_megamodelling_36_guard)
         self.states["/initialized/behaviour/wait_for_action/megamodelling"].addTransition(_initialized_behaviour_wait_for_action_megamodelling_36)
-        _initialized_behaviour_wait_for_action_megamodelling_37 = Transition(self, self.states["/initialized/behaviour/wait_for_action/megamodelling"], [self.states["/initialized/behaviour/going_manual"]])
+        _initialized_behaviour_wait_for_action_megamodelling_37 = Transition(self, self.states["/initialized/behaviour/wait_for_action/megamodelling"], [self.states["/initialized/behaviour/operations/service_register"]])
+        _initialized_behaviour_wait_for_action_megamodelling_37.setAction(self._initialized_behaviour_wait_for_action_megamodelling_37_exec)
         _initialized_behaviour_wait_for_action_megamodelling_37.setTrigger(None)
         _initialized_behaviour_wait_for_action_megamodelling_37.setGuard(self._initialized_behaviour_wait_for_action_megamodelling_37_guard)
         self.states["/initialized/behaviour/wait_for_action/megamodelling"].addTransition(_initialized_behaviour_wait_for_action_megamodelling_37)
+        _initialized_behaviour_wait_for_action_megamodelling_38 = Transition(self, self.states["/initialized/behaviour/wait_for_action/megamodelling"], [self.states["/initialized/behaviour/going_manual"]])
+        _initialized_behaviour_wait_for_action_megamodelling_38.setTrigger(None)
+        _initialized_behaviour_wait_for_action_megamodelling_38.setGuard(self._initialized_behaviour_wait_for_action_megamodelling_38_guard)
+        self.states["/initialized/behaviour/wait_for_action/megamodelling"].addTransition(_initialized_behaviour_wait_for_action_megamodelling_38)
+        
+        # transition /initialized/behaviour/wait_for_action/service
+        _initialized_behaviour_wait_for_action_service_0 = Transition(self, self.states["/initialized/behaviour/wait_for_action/service"], [self.states["/initialized/behaviour/wait_for_action/service"]])
+        _initialized_behaviour_wait_for_action_service_0.setAction(self._initialized_behaviour_wait_for_action_service_0_exec)
+        _initialized_behaviour_wait_for_action_service_0.setTrigger(None)
+        _initialized_behaviour_wait_for_action_service_0.setGuard(self._initialized_behaviour_wait_for_action_service_0_guard)
+        self.states["/initialized/behaviour/wait_for_action/service"].addTransition(_initialized_behaviour_wait_for_action_service_0)
+        _initialized_behaviour_wait_for_action_service_1 = Transition(self, self.states["/initialized/behaviour/wait_for_action/service"], [self.states["/initialized/behaviour/operations/service_stop"]])
+        _initialized_behaviour_wait_for_action_service_1.setAction(self._initialized_behaviour_wait_for_action_service_1_exec)
+        _initialized_behaviour_wait_for_action_service_1.setTrigger(None)
+        _initialized_behaviour_wait_for_action_service_1.setGuard(self._initialized_behaviour_wait_for_action_service_1_guard)
+        self.states["/initialized/behaviour/wait_for_action/service"].addTransition(_initialized_behaviour_wait_for_action_service_1)
         
         # transition /initialized/behaviour/wait_for_action/modelling/recognized/manual
         _initialized_behaviour_wait_for_action_modelling_recognized_manual_0 = Transition(self, self.states["/initialized/behaviour/wait_for_action/modelling/recognized/manual"], [self.states["/initialized/behaviour/leaving_manual"]])
@@ -2112,6 +2156,13 @@ class Modelverse(RuntimeClassBase):
     def _initialized_behaviour_operations_save_enter(self):
         self.raiseInternalEvent(Event("request", None, [['exit', 'model_modify', self.current_model]]))
     
+    def _initialized_behaviour_operations_service_register_enter(self):
+        self.raiseInternalEvent(Event("request", None, [['service_register', self.parameters[0]]]))
+        self.service_function = self.parameters[1]
+    
+    def _initialized_behaviour_operations_service_stop_enter(self):
+        self.raiseInternalEvent(Event("request", None, [['service_stop']]))
+    
     def _initialized_behaviour_going_manual_init_enter(self):
         self.context = None
         self.current_model = self.actions[None][0]["parameters"][0]
@@ -2338,6 +2389,7 @@ class Modelverse(RuntimeClassBase):
     
     def _initialized_http_mapper_init_0_exec(self, parameters):
         value = parameters[0]
+        print("HTTP output listening on " + str(self.taskname))
         self.big_step.outputEventOM(Event("narrow_cast", None, [self, self.http_clients[0], Event("HTTP_input", None, [urllib.urlencode({"op": "set_input", "data": json.dumps(value), "taskname": self.taskname}), None])]))
     
     def _initialized_http_mapper_init_0_guard(self, parameters):
@@ -2956,6 +3008,16 @@ class Modelverse(RuntimeClassBase):
     def _initialized_behaviour_operations_save_1_guard(self, parameters):
         return self.expect_response('Model loaded, ready for commands!')
     
+    def _initialized_behaviour_operations_service_register_0_exec(self, parameters):
+        port = self.split_response(self.responses.pop(0))[0]
+        self.raiseInternalEvent(Event("result", None, [port]))
+    
+    def _initialized_behaviour_operations_service_register_0_guard(self, parameters):
+        return self.expect_response_partial('Success: ', pop=False)
+    
+    def _initialized_behaviour_operations_service_stop_0_guard(self, parameters):
+        return self.expect_response('Success')
+    
     def _initialized_behaviour_wait_for_action_disconnected_0_exec(self, parameters):
         self.load_action(None)
     
@@ -3190,9 +3252,30 @@ class Modelverse(RuntimeClassBase):
     def _initialized_behaviour_wait_for_action_megamodelling_36_guard(self, parameters):
         return self.expect_action(None, 'model_types')
     
+    def _initialized_behaviour_wait_for_action_megamodelling_37_exec(self, parameters):
+        self.load_action(None)
+    
     def _initialized_behaviour_wait_for_action_megamodelling_37_guard(self, parameters):
+        return self.expect_action(None, 'service_register')
+    
+    def _initialized_behaviour_wait_for_action_megamodelling_38_guard(self, parameters):
         return self.expect_action(None, ['element_list', 'element_list_nice', 'types', 'types_full', 'read_info', 'read_attrs', 'read_defined_attrs', 'instantiate', 'delete_element', 'attr_assign', 'attr_assign_code', 'attr_delete', 'read_outgoing', 'read_incoming', 'read_association_source', 'read_association_destination', 'connections_between', 'define_attribute', 'undefine_attribute', 'all_instances', 'attr_optional', 'attr_type', 'attr_name'])
     
+    def _initialized_behaviour_wait_for_action_service_0_exec(self, parameters):
+        import modelverse
+        thrd = threading.Thread(target=self.service_function, args=[self.responses.pop(0)])
+        thrd.daemon = True
+        thrd.start()
+    
+    def _initialized_behaviour_wait_for_action_service_0_guard(self, parameters):
+        return self.expect_response_partial('', pop=False)
+    
+    def _initialized_behaviour_wait_for_action_service_1_exec(self, parameters):
+        self.load_action(None)
+    
+    def _initialized_behaviour_wait_for_action_service_1_guard(self, parameters):
+        return self.expect_action(None, 'service_stop')
+    
     def _initialized_behaviour_wait_for_action_modelling_recognized_manual_0_guard(self, parameters):
         return self.expect_action(self.context, 'exit')