Browse Source

Make both action and value function in RHS

Yentl Van Tendeloo 8 years ago
parent
commit
8cee0f64a5

BIN
bootstrap/bootstrap.m.gz


+ 6 - 0
bootstrap/ramify.alc

@@ -102,6 +102,9 @@ Element function ramify_func(model : Element, prepost : String):
 		//		value : funcdef {
 		//			target_upper_cardinality = 1
 		//		}
+		//		action : funcdef {
+		//			target_upper_cardinality = 1
+		//		}
 		//	}
 		instantiate_node(new_model, "Class", "Post_Element")
 		instantiate_link(new_model, "Association", "Post_Element_label", "Post_Element", "String")
@@ -111,6 +114,9 @@ Element function ramify_func(model : Element, prepost : String):
 		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)
+		instantiate_link(new_model, "Association", "Post_Element_action", "Post_Element", "funcdef")
+		instantiate_attribute(new_model, "Post_Element_action", "name", "action")
+		instantiate_attribute(new_model, "Post_Element_action", "target_upper_cardinality", 1)
 
 		// Association RHS_contains (RHS, Post_Element) {}
 		instantiate_link(new_model, "Association", "RHS_contains", "RHS", "Post_Element")

+ 29 - 8
bootstrap/transform.alc

@@ -193,6 +193,8 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 	String tmp
 	Element value
 	Element value_function
+	Element action
+	Element original_RHS_labels
 
 	LHS_labels = dict_keys(mapping)
 	RHS_labels = create_node()
@@ -206,6 +208,7 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 		dict_add(RHS_map, label, tmp)
 
 	remaining = set_overlap(LHS_labels, RHS_labels)
+	original_RHS_labels = set_copy(RHS_labels)
 	while (read_nr_out(remaining) > 0):
 		elem = set_pop(remaining)
 		set_remove(LHS_labels, elem)
@@ -215,19 +218,14 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 	labels_to_add = set_to_list(RHS_labels)
 
 	new_mapping = dict_copy(mapping)
-	while (read_nr_out(labels_to_remove) > 0):
-		// Remove the elements linked to these labels
-		label = set_pop(labels_to_remove)
-		model_delete_element(host_model, mapping[label])
-		dict_delete(new_mapping, label)
 
 	while (read_nr_out(labels_to_add) > 0):
 		// Add the elements linked to these labels
 		label = list_pop(labels_to_add, 0)
 		if (element_neq(read_attribute(RHS_model, RHS_map[label], "value"), read_root())):
-			// There is an action associated with this node
+			// There is a value associated with this node
 			value_function = read_attribute(RHS_model, RHS_map[label], "value")
-			value = value_function(host_model, RHS_model)
+			value = value_function(host_model, mapping)
 
 			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))
@@ -256,6 +254,30 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 			new_name = instantiate_node(host_model, original_typename, "")
 			dict_add(new_mapping, label, new_name)
 
+	while (read_nr_out(original_RHS_labels) > 0):
+		log("Checking whether actions must be executed!")
+		label = set_pop(original_RHS_labels)
+		log("Checking for actions!")
+		action = read_attribute(RHS_model, RHS_map[label], "action")
+		if (element_neq(action, read_root())):
+			action(host_model, new_mapping[label], mapping)
+		log("DONE")
+
+	while (read_nr_out(labels_to_remove) > 0):
+		// Remove the elements linked to these labels
+		label = set_pop(labels_to_remove)
+		model_delete_element(host_model, mapping[label])
+		dict_delete(new_mapping, label)
+
+	// Execute global action (whatever it may be)
+	Element RHS
+	RHS = allInstances(RHS_model, "RHS")
+	RHS = set_pop(RHS)
+	action = read_attribute(RHS_model, RHS, "action")
+	if (element_neq(action, read_root())):
+		log("Executing global action")
+		action(host_model, new_mapping)
+
 	return!
 
 Void function transform(host_model : Element, LHS_model : Element, RHS_model : Element):
@@ -274,5 +296,4 @@ Void function transform(host_model : Element, LHS_model : Element, RHS_model : E
 		rewrite(host_model, RHS_model, mapping)
 	else:
 		output("No mapping found!")
-
 	return!

+ 1 - 0
integration/code/pn_interface.alc

@@ -39,6 +39,7 @@ Element function model_loaded(model : Element):
 			output("  instantiate -- Create a new model element")
 			output("  delete      -- Delete an existing element")
 			output("  attr_add    -- Add an attribute to an element")
+			output("  attr_add_code -- Add a code attribute to an element")
 			output("  attr_del    -- Delete an attribute of an element")
 			output("  constrain   -- Add a constraint function to the model")
 			output("  rename      -- Rename an existing element")

+ 75 - 3
integration/test_pn_interface.py

@@ -541,7 +541,7 @@ Element function constraint(model : Element, name : String):
         action_code = \
             """
 include "primitives.alh"
-Integer function value(host_model : Element, RHS_model : Element):
+Integer function value(host_model : Element, mapping : Element):
 \treturn 5!
             """
 
@@ -549,14 +549,14 @@ Integer function value(host_model : Element, RHS_model : Element):
             """
 include "primitives.alh"
 Boolean function constraint(host_model : Element, name : String):
-\treturn value_eq(host_model["model"][name], 1)!
+\treturn value_eq(host_model["model"][name], 2)!
             """
 
         global_constraint_code = \
             """
 include "primitives.alh"
 Boolean function constraint(host_model : Element, mapping : Element):
-\treturn value_eq(host_model["model"][mapping["0_tokens"]], 1)!
+\treturn value_eq(host_model["model"][mapping["0_tokens"]], 2)!
             """
 
         constructor_action = get_constructor(action_code)
@@ -627,3 +627,75 @@ Boolean function constraint(host_model : Element, mapping : Element):
                 ],
             None, "PO"))
 
+    def test_po_pn_interface_transform_UI(self):
+        constraint_code = \
+            """
+include "primitives.alh"
+include "modelling.alh"
+Boolean function constraint(host_model : Element, name : String):
+\treturn value_eq(read_attribute(host_model, name, "tokens"), 1)!
+            """
+
+        action_code = \
+            """
+include "primitives.alh"
+include "modelling.alh"
+Void function action(host_model : Element, name : String, mapping : Element):
+\tunset_attribute(host_model, name, "tokens")
+\tinstantiate_attribute(host_model, name, "tokens", 5)
+\treturn!
+            """
+
+        constructor_action = get_constructor(action_code)
+        constructor_constraint = get_constructor(constraint_code)
+
+        self.assertTrue(run_file(all_files,
+            ["new", "PetriNets", "pn",
+                "instantiate", "Place", "p1",
+                "instantiate", "Place", "p2",
+                "instantiate", "Transition", "t1",
+                "instantiate", "P2T", "p2t", "p1", "t1",
+                "instantiate", "P2T", "p2t2", "p2", "t1",
+                "attr_add", "p1", "tokens", 1,
+                "attr_add", "p2", "tokens", 2,
+                "attr_add", "p2t", "weight", 1,
+                "attr_add", "p2t2", "weight", 2,
+                "exit",
+             "ramify", "PetriNets",
+             "new", "PetriNets_PRE", "pn_LHS",
+                "instantiate", "LHS", "lhs",
+                "instantiate", "Pre_Place", "p",
+                "instantiate", "Pre_Transition", "t",
+                "instantiate", "Pre_P2T", "pt", "p", "t",
+                "instantiate", "LHS_contains", "", "lhs", "p",
+                "instantiate", "LHS_contains", "", "lhs", "t",
+                "instantiate", "LHS_contains", "", "lhs", "pt",
+                "attr_add", "p", "label", "0_p",
+                "attr_add", "t", "label", "1_t",
+                "attr_add", "pt", "label", "2_pt",
+                "attr_add_code", "p", "constraint",
+            ] + constructor_constraint + [
+                "exit",
+             "new", "PetriNets_POST", "pn_RHS",
+                "instantiate", "RHS", "rhs",
+                "instantiate", "Post_Place", "p",
+                "instantiate", "Post_Transition", "t",
+                "instantiate", "RHS_contains", "", "rhs", "p",
+                "instantiate", "RHS_contains", "", "rhs", "t",
+                "attr_add", "p", "label", "0_p",
+                "attr_add", "t", "label", "1_t",
+                "attr_add_code", "p", "action",
+            ] + constructor_action + [
+                "exit",
+             "transform", "pn", "pn_LHS", "pn_RHS",
+             "load", "pn",
+                "list",
+                "verify",
+                "read",
+                "p1",
+                "read",
+                "p2",
+                "exit",
+                ],
+            None, "PO"))
+