Browse Source

Some migrations to allow for compiled constructors

Yentl Van Tendeloo 9 years ago
parent
commit
afbd9ca2ee
2 changed files with 64 additions and 28 deletions
  1. 24 25
      integration/test_pn_interface.py
  2. 40 3
      integration/utils.py

+ 24 - 25
integration/test_pn_interface.py

@@ -1,10 +1,6 @@
 import unittest
-import sys
-sys.path.append("interface/HUTN")
 
-from hutn_compiler.compiler import main as do_compile
-
-from utils import run_file
+from utils import run_file, get_constructor
 
 do_instantiate_simple = [
             '"new"', '"PetriNets"', '"abc"',
@@ -408,28 +404,30 @@ class TestPetrinetInterface(unittest.TestCase):
 
         constraint_code = \
             """
-            include "primitives.alh"
-            include "object_operations.alh"
-
-            Element function constraint(model : Element, name : String):
-                Element associations
-                Element back_associations
-                Element association
-
-                associations = allOutgoingAssociationInstances(model, name, "tile_left")
-                while (0 < list_len(associations)):
-                    association = set_pop(associations)
-                    destination = reverseKeyLookup(model["model"], read_edge_dst(association))
-                    back_associations = allOutgoingAssociationInstances(model, destination, "tile_right")
-                    if (list_len(back_associations) < 1):
-                        return "Left link does not have a right link back"
-                    else:
-                        association = list_get(back_associations, 0)
-                        destination = reverseKeyLookup(model["model"], read_edge_dst(association))
-                        if (destination != name):
-                            return "Left link does not have a right link back to the same node"
+include "primitives.alh"
+include "object_operations.alh"
+
+Element function constraint(model : Element, name : String):
+\tElement associations
+\tElement back_associations
+\tElement association
+\tString destination
+\tassociations = allOutgoingAssociationInstances(model, name, "tile_left")
+\twhile (0 < list_len(associations)):
+\t\tassociation = set_pop(associations)
+\t\tdestination = reverseKeyLookup(model["model"], read_edge_dst(association))
+\t\tback_associations = allOutgoingAssociationInstances(model, destination, "tile_right")
+\t\tif (list_len(back_associations) < 1):
+\t\t\treturn "Left link does not have a right link back"
+\t\telse:
+\t\t\tassociation = list_read(back_associations, 0)
+\t\t\tdestination = reverseKeyLookup(model["model"], read_edge_dst(association))
+\t\t\tif (destination != name):
+\t\t\t\treturn "Left link does not have a right link back to the same node"
             """
 
+        constructors = get_constructor(constraint_code)
+
         self.assertTrue(run_file(all_files,
             ['"new"', '"SimpleClassDiagrams"', '"RPGame"',
                 '"instantiate"', '"Class"', '"Scene"',
@@ -471,6 +469,7 @@ class TestPetrinetInterface(unittest.TestCase):
                 '"attr_add"', '"tile_top"', '"target_upper_cardinality"', '1',
                 '"attr_add"', '"tile_bottom"', '"source_upper_cardinality"', '1',
                 '"attr_add"', '"tile_bottom"', '"target_upper_cardinality"', '1',
+            ] + constructors + [
                 '"verify"',
             ],
             init + new + loaded + (instantiate_node + prompt) * 7 + (instantiate_edge + prompt) * 10 + (attr_add + prompt) * 22 + ["OK"] + prompt,

+ 40 - 3
integration/utils.py

@@ -11,6 +11,9 @@ import subprocess
 import signal
 import random
 
+sys.path.append("interface/HUTN")
+from hutn_compiler.compiler import main as do_compile
+
 username = "test_user"
 parallel_push = True
 
@@ -139,6 +142,7 @@ def run_file(files, parameters, expected, mode):
         # Send in the actual request and wait for replies
         var_list = {}
         data = []
+        got_output = []
         for p in parameters:
             if isinstance(p, int):
                 if p not in var_list:
@@ -150,8 +154,28 @@ def run_file(files, parameters, expected, mode):
                         return False
 
                     val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username})), timeout=10).read()
-                    val = val.split("=", 2)[1].split("&", 1)[0]
-                    var_list[p] = val
+                    #TODO if there is a value here, we have to store it for later use!
+                    l, r = val.split("&", 1)
+                    if l.startswith("id"):
+                        id_str = l
+                        val_str = r
+                    else:
+                        id_str = r
+                        val_str = l
+                    id_val = id_str.split("=", 1)[1]
+                    val_val = val_str.split("=", 1)[1]
+                    #TODO this doesn't work!!! we expect it to be filled in, which it isn't!
+
+                    if val_val == "None":
+                        print("Value is for us: keeping")
+                        print(id_val)
+                        print(val_val)
+                        var_list[p] = id_val
+                    else:
+                        print("Value not for us: skip")
+                        print(id_val)
+                        print(val_val)
+                        got_output.append(val)
                     continue
                 else:
                     val = var_list[p]
@@ -166,7 +190,10 @@ def run_file(files, parameters, expected, mode):
         for e in expected:
             c = len(e) if isinstance(e, set) else 1
             for _ in range(c):
-                val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username})), timeout=240).read()
+                if got_output:
+                    val = got_output.pop(0)
+                else:
+                    val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username})), timeout=240).read()
 
                 if proc.returncode is not None:
                     # Modelverse has already terminated, which isn't a good sign!
@@ -305,3 +332,13 @@ def run_barebone(parameters, expected, interface="0", timeout=False, wait=False,
         return not timeout
     finally:
         kill(proc)
+
+def get_constructor(code):
+    with open("__constraint.al", "w") as f:
+        f.write(code)
+        f.flush()
+
+    constructors = do_compile("__constraint.al", "interface/HUTN/grammars/actionlanguage.g", "CS")
+
+    print("Got constructors: " + str(constructors))
+    return constructors