Переглянути джерело

Fixed tests for constructor_models

Yentl Van Tendeloo 8 роки тому
батько
коміт
e90015647c

+ 12 - 5
bootstrap/conformance_scd.alc

@@ -152,6 +152,8 @@ String function conformance_scd(model : Element):
 					return "Source of model edge not typed by source of type: " + model_info(model, model_name)!
 
 				if (bool_not(is_nominal_instance(model, dst_model, dst_metamodel))):
+					log("got: " + dst_model)
+					log("expected: " + dst_metamodel)
 					return "Destination of model edge not typed by destination of type: " + model_info(model, model_name)!
 
 			// Check cardinality for all of our edges
@@ -209,12 +211,17 @@ String function conformance_scd(model : Element):
 
 			constraint_function = read_attribute(metamodel, typing[model_name], "constraint")
 			if (element_neq(constraint_function, read_root())):
+				log("Constraint function: " + cast_e2s(constraint_function))
 				String result
-				// constraint_function is a complex attribute, so read it out with import_node
-				log("Got constraint_function: " + cast_e2s(constraint_function))
-				constraint_function = import_node(constraint_function)
-				constraint_function = constraint_function["model"]["main"]
-				result = constraint_function(model, model_name)
+				Element params
+				params = create_node()
+				dict_add(params, "model", model)
+				dict_add(params, "name", model_name)
+
+				log("Invoke!")
+				result = execute_AL_model(constraint_function, params)
+				log("Result: " + cast_e2s(result))
+
 				if (result != "OK"):
 					return result!
 

+ 48 - 14
bootstrap/modelling.alc

@@ -351,20 +351,22 @@ Element function read_attribute(model : Element, element : String, attribute : S
 	Element typing
 	Element name
 
-	elem = model["model"][element]
-	typing = model["type_mapping"]
-	count = read_nr_out(elem)
-
-	i = 0
-	while (i < count):
-		edge = read_out(elem, i)
-		name = reverseKeyLookup(model["model"], edge)
-		if (dict_in(typing, name)):
-			edge_type_name = typing[name]
-			if (edge_type_name == reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(read_edge_src(model["metamodel"]["model"][edge_type_name]), attribute))):
-				return read_edge_dst(edge)!
-		i = i + 1
-
+	if (dict_in(model["model"], element)):
+		elem = model["model"][element]
+		typing = model["type_mapping"]
+		count = read_nr_out(elem)
+
+		i = 0
+		while (i < count):
+			edge = read_out(elem, i)
+			name = reverseKeyLookup(model["model"], edge)
+			if (dict_in(typing, name)):
+				edge_type_name = typing[name]
+				if (edge_type_name == reverseKeyLookup(model["metamodel"]["model"], dict_read_edge(read_edge_src(model["metamodel"]["model"][edge_type_name]), attribute))):
+					return read_edge_dst(edge)!
+			i = i + 1
+
+	// Not found: either element doesn't exist, or we couldn't find it
 	return read_root()!
 
 Void function unset_attribute(model : Element, element : String, attribute : String):
@@ -496,6 +498,12 @@ String function add_AL(model : Element, element : Element):
 				add_AL_links(model, todo, elem, type, "last_param", "param")
 				add_AL_links(model, todo, elem, type, "next", "")
 
+	// Mark the node as first
+	String initial
+	initial = instantiate_node(model, "Initial", "")
+	instantiate_link(model, "initial_funcdef", "", initial, reverseKeyLookup(model["model"], element))
+	log("Added initial link to!")
+
 	return reverseKeyLookup(model["model"], element)!
 
 Void function construct_model():
@@ -610,3 +618,29 @@ Element function construct_model_raw(metamodel : Element):
 			add_code_model(model, input(), construct_function())
 		else:
 			log("Modelling error: did not understand command " + command)
+
+Element function execute_AL_model(model_location : String, params : Element):
+	Element result
+	Element al_model
+	Element initial_function
+
+	// Fetch the model to execute
+	al_model = import_node(model_location)
+
+	// Find the initial function
+	initial_function = allInstances(al_model, "Initial")
+
+	if (read_nr_out(initial_function) == 0):
+		log("Could not find function to execute in this model!")
+		return create_node()!
+	elif (read_nr_out(initial_function) > 1):
+		log("Too many functions to execute in this model!")
+		return create_node()!
+	else:
+		initial_function = al_model["model"][set_pop(allAssociationDestinations(al_model, set_pop(initial_function), "initial_funcdef"))]
+		log("Found initial_function: " + cast_e2s(initial_function))
+		log("Keys: " + set_to_string(dict_keys(initial_function)))
+
+	// Execute that function
+	result = initial_function(params)
+	return result!

+ 46 - 25
integration/test_constructors_models.py

@@ -51,7 +51,7 @@ bottom = [
         "instantiate_attribute", "1", "attr_name", "optional", False,
 
         "instantiate_node", "1", "Class", "ComplexAttribute",
-        "instantiate_link", "1", "", "Inheritance", "ComplexAttribute", "Attribute",
+        "instantiate_link", "1", "Inheritance", "", "ComplexAttribute", "Attribute",
 
         "model_define_attribute", "1", "Class", "lower_cardinality", True, "Natural",
         "model_define_attribute", "1", "Class", "upper_cardinality", True, "Natural",
@@ -68,7 +68,7 @@ bottom = [
 bottom_link_al = [
         "model",
         "instantiate_node", "1", "ComplexAttribute", "ActionLanguage",
-        "instantiate_attribute", "1", "ActionLanguage", "type", "/models/ActionLanguage",
+        "instantiate_attribute", "1", "ActionLanguage", "type", "models/ActionLanguage",
 
         "model_define_attribute", "1", "Element", "constraint", True, "ActionLanguage",
         "exit",
@@ -97,9 +97,15 @@ action_language = [
         "instantiate_node", "2", "Class", "input",
         "instantiate_node", "2", "Class", "resolve",
         "instantiate_node", "2", "Class", "call",
-        "instantiate_node", "2", "Class", "Natural",
         "instantiate_node", "2", "Class", "String",
-        "instantiate_node", "2", "Class", "Boolean",
+        "instantiate_node", "2", "SimpleAttribute", "StringAttr",
+
+        "instantiate_node", "2", "Class", "Initial",
+        "instantiate_attribute", "2", "Initial", "lower_cardinality", 1,
+        "instantiate_attribute", "2", "Initial", "upper_cardinality", 1,
+        "instantiate_link", "2", "Association", "initial_funcdef", "Initial", "Action",
+        "instantiate_attribute", "2", "initial_funcdef", "target_lower_cardinality", 1,
+        "instantiate_attribute", "2", "initial_funcdef", "target_upper_cardinality", 1,
 
         "instantiate_link", "2", "Inheritance", "", "Action", "Element",
         "instantiate_link", "2", "Inheritance", "", "funcdef", "Action",
@@ -120,12 +126,10 @@ action_language = [
         "instantiate_link", "2", "Inheritance", "", "access", "Expression",
         "instantiate_link", "2", "Inheritance", "", "constant", "Expression",
         "instantiate_link", "2", "Inheritance", "", "input", "Expression",
-        "instantiate_link", "2", "Inheritance", "", "Natural", "Element",
         "instantiate_link", "2", "Inheritance", "", "String", "Element",
-        "instantiate_link", "2", "Inheritance", "", "Boolean", "Element",
 
         "instantiate_link", "2", "Association", "dict_link", "Action", "Element",
-        "model_define_attribute", "2", "dict_link", "name", False, "String",
+        "model_define_attribute", "2", "dict_link", "name", False, "StringAttr",
         "instantiate_attribute", "2", "dict_link", "target_upper_cardinality", 1,
 
         "instantiate_link", "2", "Association", "Statement_next", "Statement", "Statement",
@@ -192,7 +196,7 @@ def add_constraint(model, element, code):
     print(locals())
     return [
             "model",
-            "add_code_model", "AL", location,
+            "add_code_model", "2", location,
            ] + get_constructor(code) + [
             "instantiate_attribute", model, element, "constraint", location,
             "exit",
@@ -299,9 +303,9 @@ def conformance_check(node):
 code_natural = \
     """
         include "primitives.alh"
-        String function constraint(model : Element, name : String):
+        String function constraint(params : Element):
             Element self
-            self = model["model"][name]
+            self = params["model"]["model"][params["name"]]
             if (is_physical_int(self)):
                 if (integer_gte(self, 0)):
                     return "OK"!
@@ -314,8 +318,11 @@ code_natural = \
 code_string = \
     """
         include "primitives.alh"
-        Element function constraint(model : Element, name : String):
-            if (is_physical_string(model["model"][name])):
+        Element function constraint(params : Element):
+            Element self
+            self = params["model"]["model"][params["name"]]
+            log("In constraint")
+            if (is_physical_string(self)):
                 return "OK"!
             else:
                 return "String has non-string instance"!
@@ -324,8 +331,10 @@ code_string = \
 code_boolean = \
     """
         include "primitives.alh"
-        Element function constraint(model : Element, name : String):
-            if (is_physical_boolean(model["model"][name])):
+        Element function constraint(params : Element):
+            Element self
+            self = params["model"]["model"][params["name"]]
+            if (is_physical_boolean(self)):
                 return "OK"!
             else:
                 return "Boolean has non-boolean instance"!
@@ -335,9 +344,11 @@ code_location = \
     """
         include "primitives.alh"
         include "library.alh"
-        Element function constraint(model : Element, name : String):
-            if (is_physical_string(model["model"][name])):
-                if (element_neq(import_node(model["model"][name]), read_root())):
+        Element function constraint(params : Element):
+            Element self
+            self = params["model"]["model"][params["name"]]
+            if (is_physical_string(self)):
+                if (element_neq(import_node(self), read_root())):
                     return "OK"!
                 else:
                     return "Location references non-existing element!"!
@@ -349,14 +360,16 @@ code_complex_attribute = \
     """
         include "primitives.alh"
         include "library.alh"
-        Element function constraint(model : Element, name : String):
-            if (is_physical_string(model["model"][name])):
-                if (element_neq(import_node(model["model"][name]), read_root())):
+        Element function constraint(params : Element):
+            Element self
+            self = params["model"]["model"][params["name"]]
+            if (is_physical_string(self)):
+                if (element_neq(import_node(self), read_root())):
                     return "OK"!
                 else:
                     return "Complex Attribute references non-existing element!"!
             else:
-                return "Complex Attribute has non-string value"!
+                return ("Complex Attribute has non-string value: " + cast_e2s(self))!
     """
 
 class TestConstructorsModels(unittest.TestCase):
@@ -365,29 +378,37 @@ class TestConstructorsModels(unittest.TestCase):
         self.assertTrue(run_barebone(commands, ["OK"], 1))
 
     def test_constructors_action_language(self):
-        commands = bottom + action_language + bottom_link_al + conformance_check("models/SimpleClassDiagrams_new") + ["return", False]
+        commands = bottom + action_language + bottom_link_al + \
+                    add_constraint("2", "String", code_string) + \
+                    add_constraint("2", "StringAttr", code_string) + \
+                    conformance_check("models/ActionLanguage") + ["return", False]
         self.assertTrue(run_barebone(commands, ["OK"], 1))
 
     def test_constructors_constraints_scd(self):
         commands = bottom + action_language + bottom_link_al + \
+                    add_constraint("2", "String", code_string) + \
+                    add_constraint("2", "StringAttr", code_string) + \
                     add_constraint("1", "Natural", code_natural) + \
                     add_constraint("1", "String", code_string) + \
                     add_constraint("1", "Location", code_location) + \
                     add_constraint("1", "Boolean", code_boolean) + \
-                    add_constraint("1", "ComplexAttribute", code_complex_attribute) + \
+                    add_constraint("1", "ActionLanguage", code_complex_attribute) + \
                     conformance_check("models/SimpleClassDiagrams_new") + ["return", False]
         self.assertTrue(run_barebone(commands, ["OK"], 1))
 
     def test_constructors_constraints_pn(self):
         commands = bottom + action_language + bottom_link_al + \
+                    add_constraint("2", "String", code_string) + \
+                    add_constraint("2", "StringAttr", code_string) + \
                     add_constraint("1", "Natural", code_natural) + \
                     add_constraint("1", "String", code_string) + \
                     add_constraint("1", "Location", code_location) + \
                     add_constraint("1", "Boolean", code_boolean) + \
-                    add_constraint("1", "ComplexAttribute", code_complex_attribute) + \
+                    add_constraint("1", "ActionLanguage", code_complex_attribute) + \
                     instantiate_scd + \
-                    instantiate_pn + \
                     add_constraint("3", "Natural", code_natural) + \
+                    conformance_check("models/PetriNets_new") + \
+                    instantiate_pn + \
                     conformance_check("models/PN_instance") + ["return", False]
         self.assertTrue(run_barebone(commands, ["OK"], 1))
 

+ 1 - 0
interface/HUTN/includes/modelling.alh

@@ -21,3 +21,4 @@ Element function read_attribute(model : Element, elem : String, name : String)
 Void function model_delete_element(model : Element, name : String)
 String function model_define_attribute(model : Element, elem : String, name : String, optional : Boolean, type : String)
 Element function construct_model_raw(metamodel : Element)
+Element function execute_AL_model(model_location : String, params : Element)

+ 2 - 2
scripts/run_local_modelverse.py

@@ -7,5 +7,5 @@ if len(sys.argv) < 2:
     sys.stderr.write("    %s port\n" % sys.argv[0])
 else:
     subprocess.check_call([sys.executable, "-m", "sccd.compiler.sccdc", "-p", "threads", "server.xml"], cwd="hybrid_server")
-    #subprocess.call([sys.executable, "run_mvk_server.py"] + sys.argv[1:] + ["--kernel=baseline-jit"], cwd="hybrid_server")
-    subprocess.call([sys.executable, "run_mvk_server.py"] + sys.argv[1:] + ["--kernel=legacy-interpreter"], cwd="hybrid_server")
+    subprocess.call([sys.executable, "run_mvk_server.py"] + sys.argv[1:] + ["--kernel=baseline-jit"], cwd="hybrid_server")
+    #subprocess.call([sys.executable, "run_mvk_server.py"] + sys.argv[1:] + ["--kernel=legacy-interpreter"], cwd="hybrid_server")