ソースを参照

Fixes for ActionLanguage

Yentl Van Tendeloo 8 年 前
コミット
0af7ce5eec

BIN
bootstrap/bootstrap.m.gz


+ 1 - 1
bootstrap/modelling.alc

@@ -372,7 +372,7 @@ Element function read_attribute(model : Element, element : String, attribute : S
 			i = i + 1
 
 	else:
-		log("Element does not exist!")
+		log("Element does not exist: " + element)
 
 	// Not found: either element doesn't exist, or we couldn't find it
 	return read_root()!

+ 22 - 3
core/core_algorithm.alc

@@ -644,19 +644,25 @@ Void function user_function_skip_init(user_id : String):
 							Element func
 
 							output("Action Language execution starts!")
+							log("Getting full model for transformation")
 							action_model = get_full_model(transformation_id)
+							log("Got model: " + cast_e2s(action_model))
 
 							// 1) Group source models in dictionary
 							//  --> This is just the "inputs" variable
+							log("Create inputs")
 
 							// 2) Execute action language model
 							func = get_func_AL_model(action_model)
+							log("Ready to execute: " + cast_e2s(func))
 							result = func(inputs)
+							log("Result: " + cast_e2s(result))
 
 							// 3) Split output dictionary back to seperate models
 							output_keys = dict_keys(outputs)
 							while (read_nr_out(output_keys) > 0):
 								key = set_pop(output_keys)
+								log("Splitting " + key)
 
 								// Check if the destination model already exists
 								if (get_model_id(outputs[key]) == ""):
@@ -666,6 +672,8 @@ Void function user_function_skip_init(user_id : String):
 									// Model exists, so we overwrite
 									model_overwrite(result[key], get_model_id(outputs[key]))
 
+							log("Finished")
+
 						else:
 							output("Did not know how to interpret model of type " + exact_type)
 					else:
@@ -884,6 +892,9 @@ Void function user_function_skip_init(user_id : String):
 			String name
 
 			name = input()
+			source = create_node()
+			target = create_node()
+
 			while (name != ""):
 				model_id = get_model_id(name)
 				if (model_id != ""):
@@ -915,25 +926,33 @@ Void function user_function_skip_init(user_id : String):
 			output("Name of Action Language model?")
 			name = input()
 
+			log("Add AL is fine!")
 			if (get_model_id(name) == ""):
-				String new_model
 				// Finished with all information, now create the model itself!
 				output("Waiting for model constructors...")
-				new_model = construct_model_raw(import_node(read_attribute(core, get_model_id("ActionLanguage"), "location")))
-				model_create(new_model, name, user_id, get_model_id("ActionLanguage"), "ActionLanguage")
+				log("AL model: " + cast_e2s(get_model_id("ActionLanguage")))
+				log("Location: " + cast_e2s(read_attribute(core, get_model_id("ActionLanguage"), "location")))
+				log("Imported: " + cast_e2s(import_node(read_attribute(core, get_model_id("ActionLanguage"), "location"))))
+				add_code_model(import_node(read_attribute(core, get_model_id("ActionLanguage"), "location")), "AL/" + name, construct_function())
+				log("Exported to " + cast_e2s(import_node("AL/" + name)))
+				model_create(import_node("AL/" + name), name, user_id, get_model_id("ActionLanguage"), "ActionLanguage")
 				model_id = get_model_id(name)
 
 				// Extend metadata with info on source and target
 				String link
 				String dst
 
+				log("Adding inputs")
 				while (read_nr_out(source) > 0):
 					dst = set_pop(source)
+					log(" for " + dst)
 					link = instantiate_link(core, "transformInput", "", model_id, dst)
 					instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
 
+				log("Adding outputs")
 				while (read_nr_out(target) > 0):
 					dst = set_pop(target)
+					log(" for " + dst)
 					link = instantiate_link(core, "transformOutput", "", model_id, dst)
 					instantiate_attribute(core, link, "name", read_attribute(core, dst, "name"))
 			else:

+ 75 - 71
integration/code/pn_simulate.alc

@@ -1,76 +1,80 @@
 include "primitives.alh"
 include "modelling.alh"
 include "random.alh"
+include "model_management.alh"
+include "object_operations.alh"
+include "modelling.alh"
 
 Element function simulate(inputs : Element):
-    Element outputs
-    outputs = create_node()
-
-    // Copy over the model to the output dictionary
-    dict_add(outputs, "models/PetriNets_Runtime", model_copy(inputs["models/PetriNets_Runtime"))
-
-    // Do a single simulation step
-    Element model
-    model = outputs["models/PetriNets_Runtime"]
-
-    // Find enabled transitions
-    Element all_transitions
-    Element enabled_transitions
-    String under_study
-    Boolean enabled
-    Element in_arcs
-
-    all_transitions = allInstances(model, "Transition")
-    enabled_transitions = create_node()
-
-    while (0 < read_nr_out(all_transitions)):
-        under_study = set_pop(all_transitions)
-        enabled = True
-
-        // Find all incoming transitions
-        in_arcs = allIncomingAssociationInstances(model, under_study, "P2T")
-
-        while (0 < read_nr_out(in_arcs)):
-            arc_under_study = set_pop(in_arcs)
-
-            Integer present_tokens
-            Integer required_tokens
-            required_tokens = read_attribute(model, arc_under_study, "weight")
-            log("Weight: " + cast_i2s(required_tokens))
-            present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens")
-            log("Tokens: " + cast_i2s(present_tokens))
-            if (present_tokens < required_tokens):
-                // Less tokens than required, so disable the transition completely
-                enabled = False
-
-        if (enabled):
-            set_add(enabled_transitions, under_study)
-
-    // Pick random enabled transition
-    String transition
-
-    transition = random_choice(enabled_transitions)
-
-    // Consume tokens
-    Element workset
-    String working_arc
-    String working_place
-    Integer new_value
-
-    workset = allIncomingAssociationInstances(model, transition, "P2T")
-    while (0 < read_nr_out(workset)):
-        working_arc = set_pop(workset)
-        working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc]))
-        new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
-        instantiate_attribute(model, working_place, "tokens", new_value)
-
-    // Add tokens
-    workset = allOutgoingAssociationInstances(model, transition, "T2P")
-    while (0 < read_nr_out(workset)):
-        working_arc = set_pop(workset)
-        working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc]))
-        new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
-        instantiate_attribute(model, working_place, "tokens", new_value)
-
-    // Finish up
-    return outputs!
+	Element outputs
+	outputs = create_node()
+
+	// Copy over the model to the output dictionary
+	dict_add(outputs, "models/PetriNets_Runtime", model_copy(inputs["models/PetriNets_Runtime"]))
+
+	// Do a single simulation step
+	Element model
+	model = outputs["models/PetriNets_Runtime"]
+
+	// Find enabled transitions
+	Element all_transitions
+	Element enabled_transitions
+	String under_study
+	Boolean enabled
+	Element in_arcs
+	String arc_under_study
+
+	all_transitions = allInstances(model, "Transition")
+	enabled_transitions = create_node()
+
+	while (0 < read_nr_out(all_transitions)):
+		under_study = set_pop(all_transitions)
+		enabled = True
+
+		// Find all incoming transitions
+		in_arcs = allIncomingAssociationInstances(model, under_study, "P2T")
+
+		while (0 < read_nr_out(in_arcs)):
+			arc_under_study = set_pop(in_arcs)
+
+			Integer present_tokens
+			Integer required_tokens
+			required_tokens = read_attribute(model, arc_under_study, "weight")
+			log("Weight: " + cast_i2s(required_tokens))
+			present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens")
+			log("Tokens: " + cast_i2s(present_tokens))
+			if (present_tokens < required_tokens):
+				// Less tokens than required, so disable the transition completely
+				enabled = False
+
+		if (enabled):
+			set_add(enabled_transitions, under_study)
+
+	// Pick random enabled transition
+	String transition
+
+	transition = random_choice(enabled_transitions)
+
+	// Consume tokens
+	Element workset
+	String working_arc
+	String working_place
+	Integer new_value
+
+	workset = allIncomingAssociationInstances(model, transition, "P2T")
+	while (0 < read_nr_out(workset)):
+		working_arc = set_pop(workset)
+		working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc]))
+		new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
+		instantiate_attribute(model, working_place, "tokens", new_value)
+
+	// Add tokens
+	workset = allOutgoingAssociationInstances(model, transition, "T2P")
+	while (0 < read_nr_out(workset)):
+		working_arc = set_pop(workset)
+		working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc]))
+		new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
+		instantiate_attribute(model, working_place, "tokens", new_value)
+
+	// Finish up
+	return outputs!

+ 10 - 16
integration/test_mvc.py

@@ -1190,10 +1190,10 @@ class TestModelverseCore(unittest.TestCase):
                 [],
             mode))
 
-    def test_po_transform_add_MT_pn_AL_simulate(self):
-        self.transform_add_MT_pn_AL_simulate("PO")
+    def test_po_transform_add_AL_pn_simulate(self):
+        self.transform_add_AL_pn_simulate("PO")
 
-    def transform_add_MT_pn_AL_simulate(self, mode):
+    def transform_add_AL_pn_simulate(self, mode):
         self.assertTrue(run_file(all_files,
             [ "root", "root", "root", 
                 "model_add",
@@ -1263,7 +1263,7 @@ class TestModelverseCore(unittest.TestCase):
                     "PetriNets_Runtime",
                     "",
                     "pn_step",
-                    ] + get_model_constructor(open("integration/code/pn_simulate.alc", "r").read()) + [
+                    ] + get_constructor(open("integration/code/pn_simulate.alc", "r").read()) + [
                 "transformation_add_MT",
                     "PetriNets_RAM",
                     "PetriNets",
@@ -1411,18 +1411,12 @@ class TestModelverseCore(unittest.TestCase):
                 "Name of new transformation?",
                 "Waiting for model constructors...",
                 "Ready for command...",
-                # transformation_add_MT
-                "RAMified metamodel to use?",
-                "Supported metamodels:",
-                set(["  PetriNets",
-                     "  PetriNets_Runtime",
-                    ]),
-                "",
-                "Which ones do you want to use as source (empty string to finish)?",
+                # transformation_add_AL
+                "Which metamodels do you want to use as source for the action code (empty string to finish)?",
                 "Model added as source",
-                "Which ones do you want to use as target (empty string to finish)?",
+                "Which metamodels do you want to use as target for the action code (empty string to finish)?",
                 "Model added as target",
-                "Name of new transformation?",
+                "Name of Action Language model?",
                 "Waiting for model constructors...",
                 "Ready for command...",
                 # transformation_add_MT
@@ -1446,7 +1440,7 @@ class TestModelverseCore(unittest.TestCase):
                      "  pn_print : PetriNets_RAM",
                      "  pn_design_to_runtime : PetriNets_RAM",
                      "  pn_runtime_to_design : PetriNets_RAM",
-                     "  pn_step : PetriNets_RAM",
+                     "  pn_step : ActionLanguage",
                      "  __merged_PetriNets_RAM : SimpleClassDiagrams",
                      "  PetriNets_RAM : SimpleClassDiagrams",
                      "  my_pn : PetriNets",
@@ -1457,7 +1451,7 @@ class TestModelverseCore(unittest.TestCase):
                 set(["[ModelTransformation] pn_print : PetriNets_RAM",
                      "[ModelTransformation] pn_design_to_runtime : PetriNets_RAM",
                      "[ModelTransformation] pn_runtime_to_design : PetriNets_RAM",
-                     "[ModelTransformation] pn_step : PetriNets_RAM"]),
+                     "[ActionLanguage] pn_step : ActionLanguage"]),
                 "Ready for command...",
                 # transformation_execute (pn_print)
                 "Which transformation do you want to execute?",

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

@@ -22,3 +22,4 @@ 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 get_func_AL_model(model_location : String)
+Void function add_code_model(model : Element, export_name : String, code : Element)