Browse Source

Asynchronously read output

Yentl Van Tendeloo 8 years ago
parent
commit
9a3d13bcd1
2 changed files with 23 additions and 11 deletions
  1. 16 4
      wrappers/modelverse.py
  2. 7 7
      wrappers/test.py

+ 16 - 4
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 = Queue.Queue()
+
+def _output_thread(outputs):
+    while taskname is None:
+        time.sleep(0.1)
+
+    while 1:
+        outputs.put(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,14 @@ 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())
+        last_output = outputs.get()
         #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

+ 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))