浏览代码

Merge branch 'testing' into semi_SCCD_client

Yentl Van Tendeloo 8 年之前
父节点
当前提交
462d2cfdd0
共有 2 个文件被更改,包括 31 次插入14 次删除
  1. 24 7
      wrappers/modelverse.py
  2. 7 7
      wrappers/test.py

+ 24 - 7
wrappers/modelverse.py

@@ -6,6 +6,7 @@ from urllib2 import URLError
 import sys
 import time
 import threading
+import Queue
 
 COMPILER_PATH = "interface/HUTN"
 
@@ -66,6 +67,18 @@ mode = MODE_UNCONNECTED
 prev_mode = None
 current_model = None
 registered_metamodels = {}
+outputs = []
+
+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()))
+
+thrd = threading.Thread(target=_output_thread, args=[outputs])
+thrd.daemon = True
+thrd.start()
 
 def _get_metamodel(model):
     global registered_metamodels
@@ -167,15 +180,18 @@ def _compile_model(code):
 
     return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
 
-def _output(expected=None,port=None):
-    if port is None:
-        port = taskname
+def _output(expected=None):
     try:
         global last_output
-        last_output = json.loads(urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": port}))).read())
+
+        while not outputs:
+            time.sleep(0.01)
+
+        last_output = outputs.pop(0)
         #print("[OUT] %s" % last_output)
     except:
         raise UnknownError()
+
     if expected is not None and last_output != expected:
         raise InterfaceMismatch(_last_output(), expected)
     return last_output
@@ -256,13 +272,12 @@ def init(address_param="http://127.0.0.1:8001", timeout=20.0):
     """Starts up the connection to the Modelverse."""
     global mode
     global address
-    global taskname
     address = address_param
     start_time = time.time()
-    taskname = random.random()
+    task = random.random()
     while 1:
         try:
-            _input_raw('"%s"' % taskname, "task_manager")
+            _input_raw('"%s"' % task, "task_manager")
             mode = MODE_UNAUTHORIZED
             break
         except URLError as e:
@@ -270,6 +285,8 @@ def init(address_param="http://127.0.0.1:8001", timeout=20.0):
                 raise ConnectionError(e.reason)
             else:
                 time.sleep(0.1)
+    global taskname
+    taskname = task
 
 def login(username, password):
     """Log in a user, if user doesn't exist, it is created."""

+ 7 - 7
wrappers/test.py

@@ -34,40 +34,40 @@ login("admin", "admin")
 # Add the metamodels for PetriNet and ReachabilityGraph
 print("Add metamodels")
 try:
-    model_add("PetriNet", "SimpleClassDiagrams", open("models/petrinets.mvc").read())
+    model_add("formalisms/PetriNet", "formalisms/SimpleClassDiagrams", open("models/petrinets.mvc").read())
 except ModelExists:
     pass
 
 try:
-    model_add("ReachabilityGraph", "SimpleClassDiagrams", open("models/reachability_graph.mvc").read())
+    model_add("formalisms/ReachabilityGraph", "formalisms/SimpleClassDiagrams", open("models/reachability_graph.mvc").read())
 except ModelExists:
     pass
 
 print("Add model")
 try:
-    model_add("my_pn", "PetriNet", open("models/my_pn.mvc").read())
+    model_add("models/my_pn", "formalisms/PetriNet", open("models/my_pn.mvc").read())
 except ModelExists:
     pass
 
 # Add the action language code to transform between them
 print("Add AL model")
 try:
-    transformation_add_AL({"PetriNet": "PetriNet"}, {"ReachabilityGraph": "ReachabilityGraph"}, "analyseReachability", open("models/reachability.alc", "r").read())
+    transformation_add_AL({"PetriNet": "formalisms/PetriNet"}, {"ReachabilityGraph": "formalisms/ReachabilityGraph"}, "models/analyseReachability", open("models/reachability.alc", "r").read())
 except ModelExists:
     pass
 
 # Add an example model transformation to print the reachability graph
 print("Add MT model")
 try:
-    transformation_add_MT({"ReachabilityGraph": "ReachabilityGraph"}, {}, "printReachability", open("models/reachabilitygraph_print.mvc").read())
+    transformation_add_MT({"ReachabilityGraph": "formalisms/ReachabilityGraph"}, {}, "models/printReachability", open("models/reachabilitygraph_print.mvc").read())
 except ModelExists:
     pass
 
 # Do the reachability graph generation
 print("Execute AL")
-status = transformation_execute_AL("analyseReachability", {"PetriNet": "my_pn"}, {"ReachabilityGraph": "my_reachability"}, callback=print_mv)
+status = transformation_execute_AL("models/analyseReachability", {"PetriNet": "models/my_pn"}, {"ReachabilityGraph": "models/my_reachability"}, callback=print_mv)
 print("Reachability generation success: " + str(status))
 
 print("Execute MT")
-status = transformation_execute_MT("printReachability", {"ReachabilityGraph": "my_reachability"}, {}, callback=print_mv)
+status = transformation_execute_MT("models/printReachability", {"ReachabilityGraph": "models/my_reachability"}, {}, callback=print_mv)
 print("Reachability printing success: " + str(status))