ソースを参照

First working version of PetriNets transformation that marks transition as "executing"

Yentl Van Tendeloo 8 年 前
コミット
49d7ad8c3e

+ 1 - 0
.gitignore

@@ -3,5 +3,6 @@
 *.dot
 .cache
 *.swp
+*.swo
 __pycache__
 hybrid_server/server.py

BIN
bootstrap/bootstrap.m.gz


+ 9 - 0
bootstrap/modelling.alc

@@ -241,6 +241,15 @@ Void function model_delete_element(model : Element, name : String):
 
 	return!
 
+String function model_define_attribute(model : Element, elem : String, name : String, type : String):
+	// Create the necessary links to make it an attribute
+	String edge_name
+
+	edge_name = instantiate_link(model, "Association", "", elem, type)
+	instantiate_attribute(model, edge_name, "name", name)
+
+	return edge_name!
+
 Element function read_attribute(model : Element, element : String, attribute : String):
 	Integer i
 	Integer count

+ 12 - 0
integration/code/pn_interface.alc

@@ -41,6 +41,7 @@ Element function model_loaded(model : 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("  attr_def    -- Define a new attribute")
 			output("  constrain   -- Add a constraint function to the model")
 			output("  rename      -- Rename an existing element")
 			output("  modify      -- Modify the attributes of an element")
@@ -53,6 +54,17 @@ Element function model_loaded(model : Element):
 			output("  exit        -- Unload the model and go back to the loading prompt")
 		elif (cmd == "exit"):
 			return model!
+		elif (cmd == "attr_def"):
+			String name
+			String attr
+			String type
+			output("Which element do you want to define an attribute for?")
+			name = input()
+			output("What is the name of the attribute?")
+			attr = input()
+			output("Type of attribute?")
+			type = input()
+			model_define_attribute(model, name, attr, type)
 		elif (cmd == "instantiate"):
 			String mm_type_name
 			output("Type to instantiate?")

+ 48 - 122
integration/test_pn_interface.py

@@ -537,103 +537,25 @@ Element function constraint(model : Element, name : String):
                 ["OK"],
             mode))
 
-    def test_po_pn_interface_transform(self):
-        action_code = \
-            """
-include "primitives.alh"
-Integer function value(host_model : Element, mapping : Element):
-\treturn 5!
-            """
-
-        constraint_code = \
-            """
-include "primitives.alh"
-Boolean function constraint(host_model : Element, name : String):
-\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"]], 2)!
-            """
-
-        constructor_action = get_constructor(action_code)
-        constructor_constraint = get_constructor(constraint_code)
-        constructor_global_constraint = get_constructor(global_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_Natural", "tokens",
-                "instantiate", "Pre_Place_tokens", "p_tokens", "p", "tokens",
-                "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",
-                "instantiate", "LHS_contains", "", "lhs", "tokens",
-                "instantiate", "LHS_contains", "", "lhs", "p_tokens",
-                "attr_add", "tokens", "label", "0_tokens",
-                "attr_add", "p_tokens", "label", "1_p_tokens",
-                "attr_add_code", "tokens", "constraint",
-            ] + constructor_constraint + [
-                "attr_add", "p", "label", "2_p",
-                "attr_add", "t", "label", "3_t",
-                "attr_add", "pt", "label", "4_pt",
-                "attr_add_code", "lhs", "constraint",
-            ] + constructor_global_constraint + [
-                "exit",
-             "new", "PetriNets_POST", "pn_RHS",
-                "instantiate", "RHS", "rhs",
-                "instantiate", "Post_Place", "p",
-                "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", "t",
-                "instantiate", "RHS_contains", "", "rhs", "p_tokens",
-                "instantiate", "RHS_contains", "", "rhs", "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_code", "tokens", "value",
-            ] + constructor_action + [
-                "exit",
-             "transform", "pn", "pn_LHS", "pn_RHS",
-             "load", "pn",
-                "list",
-                "verify",
-                "read",
-                "p1",
-                "read",
-                "p2",
-                "exit",
-                ],
-            None, "PO"))
-
-    def test_po_pn_interface_transform_UI(self):
+    def test_po_pn_interface_transform_pn(self):
         constraint_code = \
             """
 include "primitives.alh"
 include "modelling.alh"
+include "object_operations.alh"
 Boolean function constraint(host_model : Element, name : String):
-\treturn value_eq(read_attribute(host_model, name, "tokens"), 1)!
+\t// Make sure that all places have enough tokens
+\tElement links
+\tElement link
+\tElement place
+\tlinks = allIncomingAssociationInstances(host_model, name, "P2T")
+\twhile (read_nr_out(links) > 0):
+\t\tlink = set_pop(links)
+\t\tplace = readAssociationSource(host_model, link)
+\t\tif (integer_lt(read_attribute(host_model, place, "tokens"), read_attribute(host_model, link, "weight"))):
+\t\t\t// Not enough tokens for this weight
+\t\t\treturn False!
+\treturn True!
             """
 
         action_code = \
@@ -641,7 +563,8 @@ Boolean function constraint(host_model : Element, name : String):
 include "primitives.alh"
 include "modelling.alh"
 Void function action(host_model : Element, name : String, mapping : Element):
-\tinstantiate_attribute(host_model, name, "tokens", read_attribute(host_model, mapping["0_p"], "tokens"))
+\tunset_attribute(host_model, name, "executing")
+\tinstantiate_attribute(host_model, name, "executing", True)
 \treturn!
             """
 
@@ -649,54 +572,57 @@ Void function action(host_model : Element, name : String, mapping : Element):
         constructor_constraint = get_constructor(constraint_code)
 
         self.assertTrue(run_file(all_files,
-            ["new", "PetriNets", "pn",
+            ["new", "SimpleClassDiagrams", "PetriNets_runtime",
+                "set_inheritance", "Inheritance",
+                "instantiate", "Class", "Natural",
+                "instantiate", "Class", "Boolean",
+                "instantiate", "Class", "Place",
+                "attr_def", "Place", "tokens", "Natural",
+                "attr_def", "Place", "executed", "Boolean",
+                "instantiate", "Class", "Transition",
+                "attr_def", "Transition", "executing", "Boolean",
+                "instantiate", "Association", "P2T", "Place", "Transition",
+                "attr_def", "P2T", "weight", "Natural",
+                "instantiate", "Association", "T2P", "Transition", "Place",
+                "attr_def", "T2P", "weight", "Natural",
+                "exit",
+             "new", "PetriNets_runtime", "pn",
                 "instantiate", "Place", "p1",
+                "attr_add", "p1", "tokens", 1,
+                "attr_add", "p1", "executed", False,
                 "instantiate", "Place", "p2",
+                "attr_add", "p2", "tokens", 2,
+                "attr_add", "p2", "executed", False,
                 "instantiate", "Transition", "t1",
+                "attr_add", "t1", "executing", False,
                 "instantiate", "P2T", "p2t", "p1", "t1",
+                "attr_add", "p2t", "weight", 2,
                 "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,
+                "attr_add", "p2t2", "weight", 1,
                 "exit",
-             "ramify", "PetriNets",
-             "new", "PetriNets_PRE", "pn_LHS",
+             "ramify", "PetriNets_runtime",
+             "new", "PetriNets_runtime_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",
+                "attr_add", "t", "label", "0",
+                #"attr_add_code", "t", "constraint", 
+            #] + constructor_constraint + [
                 "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",
+             "new", "PetriNets_runtime_POST", "pn_RHS",
                 "instantiate", "RHS", "rhs",
-                "instantiate", "Post_Place", "p",
-                "instantiate", "Post_Place", "p2",
                 "instantiate", "Post_Transition", "t",
-                "instantiate", "RHS_contains", "", "rhs", "p",
-                "instantiate", "RHS_contains", "", "rhs", "p2",
-                "instantiate", "RHS_contains", "", "rhs", "t",
-                "attr_add", "p", "label", "0_p",
-                "attr_add", "t", "label", "1_t",
-                "attr_add", "p2", "label", "3_p",
-                "attr_add_code", "p2", "action",
+                "attr_add", "t", "label", "0",
+                "attr_add_code", "t", "action",
             ] + constructor_action + [
+                "instantiate", "RHS_contains", "", "rhs", "t",
                 "exit",
              "transform", "pn", "pn_LHS", "pn_RHS",
              "load", "pn",
                 "list",
                 "verify",
                 "read",
-                "p1",
-                "read",
-                "p2",
+                "t1",
                 "exit",
                 ],
             None, "PO"))

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

@@ -21,3 +21,4 @@ Void function construct_model()
 Element function read_attribute(model : Element, elem : String, name : String)
 Void function model_delete_element(model : Element, name : String)
 Void function add_constraint(model : Element, name : String, constraint : Action)
+String function model_define_attribute(model : Element, elem : String, name : String, type : String)