Преглед изворни кода

Many changes to allow for actions to be executed in the RHS

Yentl Van Tendeloo пре 8 година
родитељ
комит
22c683e3b9

BIN
bootstrap/bootstrap.m.gz


+ 35 - 0
bootstrap/modelling.alc

@@ -106,6 +106,16 @@ String function instantiate_node(model : Element, type_name : String, instance_n
 
 	return actual_name!
 
+String function instantiate_value(model : Element, type_name : String, instance_name : String, value : Element):
+	// Create a node typed by a node from the metamodel
+	// Basically create a node and type it immediately
+	String actual_name
+
+	actual_name = model_add_value(model, instance_name, value)
+	retype(model, actual_name, type_name)
+
+	return actual_name!
+
 String function find_attribute_type(model : Element, elem : String, name : String):
 	String mm_elem
 	String direct_type
@@ -181,6 +191,31 @@ Void function instantiate_attribute(model : Element, element : String, attribute
 
 	return!
 
+Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element):
+	String ref
+	ref = add_AL(model, code)
+	instantiate_existing_attribute(model, element, attribute_name, ref)
+
+	return!
+
+Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attr_ref : String):
+	// Instantiate an attribute of something that needs to be instantiated
+	// Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
+	String attr_type
+	String attr_name
+
+	attr_type = find_attribute_type(model, element, attribute_name)
+
+	if (attr_type == ""):
+		log("Could not find attribute " + cast_v2s(attribute_name))
+		return!
+		
+	// Make a copy of the value, as it is likely that this value is reused later on
+	retype(model, attr_ref, reverseKeyLookup(model["metamodel"]["model"], read_edge_dst(model["metamodel"]["model"][attr_type])))
+	instantiate_link(model, attr_type, "", element, attr_ref)
+
+	return!
+
 String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
 	// Create a typed link between two nodes
 	String actual_name

+ 6 - 6
bootstrap/ramify.alc

@@ -35,7 +35,7 @@ Element function ramify_func(model : Element, prepost : String):
 	// Add some default elements
 	if (is_pre):
 		//	Class LHS {
-		//		constraint : Expression {
+		//		constraint : funcdef {
 		//			target_upper_cardinality = 1
 		//		}
 		//		upper_cardinality = 1
@@ -44,7 +44,7 @@ Element function ramify_func(model : Element, prepost : String):
 		instantiate_node(new_model, "Class", "LHS")
 		instantiate_attribute(new_model, "LHS", "lower_cardinality", 1)
 		instantiate_attribute(new_model, "LHS", "upper_cardinality", 1)
-		instantiate_link(new_model, "Association", "LHS_constraint", "LHS", "Expression")
+		instantiate_link(new_model, "Association", "LHS_constraint", "LHS", "funcdef")
 		instantiate_attribute(new_model, "LHS_constraint", "name", "constraint")
 		instantiate_attribute(new_model, "LHS_constraint", "target_upper_cardinality", 1)
 
@@ -56,7 +56,7 @@ Element function ramify_func(model : Element, prepost : String):
 		//			target_lower_cardinality = 1
 		//			target_upper_cardinality = 1
 		//		}
-		//		constraint : Expression {
+		//		constraint : funcdef {
 		//			target_upper_cardinality = 1
 		//		}
 		//	}
@@ -65,7 +65,7 @@ Element function ramify_func(model : Element, prepost : String):
 		instantiate_attribute(new_model, "Pre_Element_label", "name", "label")
 		instantiate_attribute(new_model, "Pre_Element_label", "target_lower_cardinality", 1)
 		instantiate_attribute(new_model, "Pre_Element_label", "target_upper_cardinality", 1)
-		instantiate_link(new_model, "Association", "Pre_Element_constraint", "Pre_Element", "Expression")
+		instantiate_link(new_model, "Association", "Pre_Element_constraint", "Pre_Element", "funcdef")
 		instantiate_attribute(new_model, "Pre_Element_constraint", "name", "constraint")
 		instantiate_attribute(new_model, "Pre_Element_constraint", "target_upper_cardinality", 1)
 
@@ -99,7 +99,7 @@ Element function ramify_func(model : Element, prepost : String):
 		//			target_lower_cardinality = 1
 		//			target_upper_cardinality = 1
 		//		}
-		//		value : Expression {
+		//		value : funcdef {
 		//			target_upper_cardinality = 1
 		//		}
 		//	}
@@ -108,7 +108,7 @@ Element function ramify_func(model : Element, prepost : String):
 		instantiate_attribute(new_model, "Post_Element_label", "name", "label")
 		instantiate_attribute(new_model, "Post_Element_label", "target_lower_cardinality", 1)
 		instantiate_attribute(new_model, "Post_Element_label", "target_upper_cardinality", 1)
-		instantiate_link(new_model, "Association", "Post_Element_value", "Post_Element", "Expression")
+		instantiate_link(new_model, "Association", "Post_Element_value", "Post_Element", "funcdef")
 		instantiate_attribute(new_model, "Post_Element_value", "name", "value")
 		instantiate_attribute(new_model, "Post_Element_value", "target_upper_cardinality", 1)
 

+ 23 - 2
bootstrap/transform.alc

@@ -173,6 +173,8 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 	String new_name
 	Element RHS_map
 	String tmp
+	Element value
+	Element value_function
 
 	LHS_labels = dict_keys(mapping)
 	RHS_labels = create_node()
@@ -182,6 +184,7 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 	while (read_nr_out(RHS_elements) > 0):
 		tmp = set_pop(RHS_elements)
 		label = read_attribute(RHS_model, tmp, "label")
+		log("Read label of " + cast_v2s(tmp))
 		set_add(RHS_labels, label)
 		dict_add(RHS_map, label, tmp)
 
@@ -200,12 +203,30 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 		label = set_pop(labels_to_remove)
 		model_delete_element(host_model, mapping[label])
 		dict_delete(new_mapping, label)
+		log("Removed " + cast_v2s(label))
 
 	while (read_nr_out(labels_to_add) > 0):
-		// Add the elementsl inked to these labels
+		// Add the elements linked to these labels
 		label = list_pop(labels_to_add, 0)
 		log("Add element linked to label " + label)
-		if (is_edge(RHS_model["model"][mapping[label]])):
+		log("Label: " + cast_v2s(label))
+		log("Element with label: " + cast_v2s(RHS_map[label]))
+		log("Element: " + cast_e2s(RHS_model["model"][RHS_map[label]]))
+		log("Value: " + cast_v2s(read_attribute(RHS_model, RHS_map[label], "label")))
+		if (element_eq(read_attribute(RHS_model, RHS_map[label], "value"), read_root())):
+			log("Executing action!")
+			// There is an action associated with this node
+			value_function = read_attribute(RHS_model, RHS_model["model"][RHS_map[label]], "value")
+			value = value_function(host_model, RHS_model)
+			log("Got value to assign: " + cast_v2s(value))
+
+			typename = reverseKeyLookup(RHS_model["metamodel"]["model"], dict_read_node(RHS_model["type_mapping"], RHS_model["model"][RHS_map[label]]))
+			original_typename = string_substr(typename, 5, string_len(typename))
+			log("Instantiate type " + original_typename)
+			new_name = instantiate_value(host_model, original_typename, "", value)
+			dict_add(new_mapping, label, new_name)
+
+		elif (is_edge(RHS_model["model"][mapping[label]])):
 			// Edge
 			src = read_attribute(RHS_model, reverseKeyLookup(RHS_model["model"], read_edge_src(RHS_model["model"][RHS_map[label]])), "label")
 			dst = read_attribute(RHS_model, reverseKeyLookup(RHS_model["model"], read_edge_dst(RHS_model["model"][RHS_map[label]])), "label")

+ 20 - 16
integration/code/pn_interface.alc

@@ -96,22 +96,8 @@ Element function model_loaded(model : Element):
 				output("Element not found in metamodel; aborting")
 
 		elif (cmd == "constrain"):
-			output("Element to constrain (empty for global)?")
-			String model_name
-			model_name = input()
-
-			if (model_name == ""):
-				// Global constraint
-				output("Give input to function constructors for GLOBAL constraint!")
-				set_model_constraints(model, construct_function())
-			elif (dict_in(model["model"], model_name)):
-				// Local constraint for this model
-				output("Give input to function constructors for LOCAL constraint!")
-				add_constraint(model, model_name, construct_function())
-				output("Added constraint to model!")
-			else:
-				// Local constraint, but model not found
-				output("Unknown model; aborting")
+			output("Give input to function constructors for GLOBAL constraint!")
+			set_model_constraints(model, construct_function())
 		elif (cmd == "modify"):
 			String model_name
 			output("Element to modify?")
@@ -149,6 +135,24 @@ Element function model_loaded(model : Element):
 					output("No such attribute!")
 			else:
 				output("No such model!")
+		elif (cmd == "attr_add_code"):
+			String model_name
+			output("Which model do you want to assign a coded attribute to?")
+			model_name = input()
+			if (dict_in(model["model"], model_name)):
+				Element attrs
+				attrs = getAttributeList(model, model_name)
+				String attr_name
+				output("Which attribute do you wish to assign?")
+				attr_name = input()
+				if (set_in(dict_keys(attrs), attr_name)):
+					output("Constructors for code?")
+					instantiate_attribute_code(model, model_name, attr_name, construct_function())
+					output("Added code!")
+				else:
+					output("No such attribute!")
+			else:
+				output("No such model!")
 		elif (cmd == "attr_del"):
 			String model_name
 			output("Which model do you want to remove an attribute of?")

+ 14 - 7
integration/test_pn_interface.py

@@ -456,7 +456,6 @@ Element function constraint(model : Element, name : String):
             """
 
         constructors = get_constructor(constraint_code)
-        print(constructors)
 
         self.assertTrue(run_file(all_files,
             ["new", "SimpleClassDiagrams", "RPGame",
@@ -496,7 +495,7 @@ Element function constraint(model : Element, name : String):
                 "attr_add", "tile_top", "target_upper_cardinality", 1,
                 "attr_add", "tile_bottom", "source_upper_cardinality", 1,
                 "attr_add", "tile_bottom", "target_upper_cardinality", 1,
-                "constrain", "Tile",
+                "attr_add_code", "Tile", "constraint",
             ] + constructors + ["verify"] + ["exit"] + [
                 "new", "RPGame", "my_game",
                 "instantiate", "Scene", "scene",
@@ -539,6 +538,15 @@ Element function constraint(model : Element, name : String):
             mode))
 
     def test_po_pn_interface_transform(self):
+        constraint_code = \
+            """
+include "primitives.alh"
+Integer function value(host_model : Element, RHS_model : Element):
+\treturn 5!
+            """
+
+        constructors = get_constructor(constraint_code)
+
         self.assertTrue(run_file(all_files,
             ["new", "PetriNets", "pn",
                 "instantiate", "Place", "p1",
@@ -573,20 +581,19 @@ Element function constraint(model : Element, name : String):
              "new", "PetriNets_POST", "pn_RHS",
                 "instantiate", "RHS", "rhs",
                 "instantiate", "Post_Place", "p",
-                "instantiate", "Post_Place", "p2",
                 "instantiate", "Post_Transition", "t",
                 "instantiate", "Post_Natural", "tokens",
                 "instantiate", "Post_Place_tokens", "p_tokens", "p", "tokens",
                 "instantiate", "RHS_contains", "", "rhs", "p",
-                "instantiate", "RHS_contains", "", "rhs", "p2",
                 "instantiate", "RHS_contains", "", "rhs", "t",
                 "instantiate", "RHS_contains", "", "rhs", "p_tokens",
                 "instantiate", "RHS_contains", "", "rhs", "tokens",
-                "attr_add", "tokens", "label", "0_tokens",
-                "attr_add", "p_tokens", "label", "1_p_tokens",
+                "attr_add", "tokens", "label", "5_tokens",
+                "attr_add", "p_tokens", "label", "6_p_tokens",
                 "attr_add", "p", "label", "2_p",
                 "attr_add", "t", "label", "3_t",
-                "attr_add", "p2", "label", "5_p",
+                "attr_add_code", "tokens", "value",
+            ] + constructors + [
                 "exit",
              "transform", "pn", "pn_LHS", "pn_RHS",
              "load", "pn",

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

@@ -7,10 +7,13 @@ Void function retype_model(model : Element, metamodel : Element)
 Void function retype(model : Element, element : String, type : String)
 Element function instantiate_model(metamodel : Element)
 String function instantiate_node(model : Element, type_name : String, instance_name : String)
+String function instantiate_value(model : Element, type_name : String, instance_name : String, value : Element)
 Element function find_attribute_type(model : Element, elem : String, name : String)
 Element function get_superclasses(model : Element, elem : Element)
 Element function find_attribute_definer(model : Element, elem : Element, name : String)
 Void function instantiate_attribute(model : Element, element : String, attribute_name : String, value : Element)
+Void function instantiate_attribute_code(model : Element, element : String, attribute_name : String, code : Element)
+Void function instantiate_existing_attribute(model : Element, element : String, attribute_name : String, attribute_ref : String)
 String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String)
 Void function define_inheritance(model : Element, inheritance_name : String)
 Void function unset_attribute(model : Element, elem : String, name : String)