浏览代码

Split the petri nets operational semantics from the pn_interface

Yentl Van Tendeloo 9 年之前
父节点
当前提交
bdbb6db0dd
共有 4 个文件被更改,包括 99 次插入137 次删除
  1. 5 2
      bootstrap/library.alc
  2. 0 102
      integration/code/pn_interface.alc
  3. 94 0
      integration/code/pn_semantics.alc
  4. 0 33
      integration/test_pn_interface.py

+ 5 - 2
bootstrap/library.alc

@@ -31,7 +31,10 @@ Element function import_node(model_name : String):
 	Element current
 	current = dict_read(read_root(), "__hierarchy")
 	while (counter_i < length):
-		current = current[splitted[counter_i]]
-		counter_i = counter_i + 1
+		if dict_in(current, splitted[counter_i]):
+			current = current[splitted[counter_i]]
+			counter_i = counter_i + 1
+		else:
+			return read_root()
 
 	return current

+ 0 - 102
integration/code/pn_interface.alc

@@ -8,95 +8,6 @@ include "metamodels.alh"
 include "modelling.alh"
 include "compilation_manager.alh"
 
-Element function pn_operations():
-	Element ops
-	ops = create_node()
-	dict_add(ops, "fire", petrinet_fire)
-	dict_add(ops, "enabled", petrinet_enabled)
-	return ops
-
-Element function petrinet_enabled(model : Element):
-	Element set_enabled
-	set_enabled = petrinet_enabled_set(model)
-	output("Enabled transitions:")
-	while (0 < read_nr_out(set_enabled)):
-		output(set_pop(set_enabled))
-	return model
-
-Element function petrinet_enabled_set(model : Element):
-	Element all_transitions
-	Element enabled_transitions
-	String under_study
-	Element in_arcs
-	String arc_under_study
-	Boolean enabled
-
-	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)
-
-	log("Got all enabled transitions!")
-	return enabled_transitions
-
-Element function petrinet_fire(model : Element):
-	output("Transition to fire?")
-	String transition
-	transition = input()
-	if (dict_in(model["model"], transition)):
-		if (set_in(petrinet_enabled_set(model), transition)):
-			Element workset
-			String working_place
-			String working_arc
-			Integer new_value
-
-			// Consume tokens
-			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"))
-				unset_attribute(model, working_place, "tokens")
-				instantiate_attribute(model, working_place, "tokens", new_value)
-				output((("  " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens")))
-
-			// 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"))
-				unset_attribute(model, working_place, "tokens")
-				instantiate_attribute(model, working_place, "tokens", new_value)
-				output((("  " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens")))
-			output("Transition fired!")
-		else:
-			output("Cannot fire if not enabled; aborting")
-	else:
-		output("Unknown transition; aborting")
-	return model
-	
 Element function model_loaded(model : Element):
 	String cmd
 
@@ -137,14 +48,6 @@ Element function model_loaded(model : Element):
 			output("  retype      -- Change the type of an element")
 			output("  switch      -- Switch between conformance bottom and the linguistic metamodel")
 			output("  exit        -- Unload the model and go back to the loading prompt")
-			if (bool_not(bottom)):
-				output("Model-specific operations:")
-				Element specific_ops
-				specific_ops = dict_keys(pn_operations())
-				String specific_op
-				while (0 < dict_len(specific_ops)):
-					specific_op = set_pop(specific_ops)
-					output("  " + specific_op)
 		elif (cmd == "exit"):
 			return model
 		elif (cmd == "instantiate"):
@@ -370,11 +273,6 @@ Element function model_loaded(model : Element):
 			else:
 				// We already switched the models and such, so we are already done!
 				output("Switching to linguistic metamodel!")
-		elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))):
-			// A model-specific operation, so execute that one
-			Element specific_op
-			specific_op = dict_read(pn_operations(), cmd)
-			specific_op(model)
 		else:
 			output("Unknown command: " + cast_v2s(cmd))
 			output("Use command 'help' to get a list of available commands")

+ 94 - 0
integration/code/pn_semantics.alc

@@ -0,0 +1,94 @@
+Element function petrinet_enabled(model : Element):
+    Element set_enabled
+    set_enabled = petrinet_enabled_set(model)
+    output("Enabled transitions:")
+    while (0 < read_nr_out(set_enabled)):
+        output(set_pop(set_enabled))
+    return model
+
+Element function petrinet_enabled_set(model : Element):
+    Element all_transitions
+    Element enabled_transitions
+    String under_study
+    Element in_arcs
+    String arc_under_study
+    Boolean enabled
+
+    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)
+
+    log("Got all enabled transitions!")
+    return enabled_transitions
+
+Element function petrinet_fire(model : Element):
+    output("Transition to fire?")
+    String transition
+    transition = input()
+    if (dict_in(model["model"], transition)):
+        if (set_in(petrinet_enabled_set(model), transition)):
+            Element workset
+            String working_place
+            String working_arc
+            Integer new_value
+
+            // Consume tokens
+            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"))
+                unset_attribute(model, working_place, "tokens")
+                instantiate_attribute(model, working_place, "tokens", new_value)
+                output((("  " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens")))
+
+            // 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"))
+                unset_attribute(model, working_place, "tokens")
+                instantiate_attribute(model, working_place, "tokens", new_value)
+                output((("  " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens")))
+            output("Transition fired!")
+        else:
+            output("Cannot fire if not enabled; aborting")
+    else:
+        output("Unknown transition; aborting")
+    return model
+
+Element function main():
+	Element model
+	String cmd
+
+	model = read_root()
+	while (element_eq(model, read_root())):
+		output("Which model do you want to execute with petri net semantics?")
+		model = import_node(input())
+
+	while (True):
+		output("Which operation do you want to execute?")
+		cmd = input()

+ 0 - 33
integration/test_pn_interface.py

@@ -81,15 +81,6 @@ def read_edge(name, t, src, dst, defs, attrs):
             ["Attributes:"] + \
             ([set(['  "%s" : "%s" = %s' % (m, mm, v) for m, mm, v in attrs])] if attrs else [])
 
-def enabled(enableds):
-    return ["Enabled transitions:"] + \
-            [set(enableds)]
-
-def fire(fired):
-    return ["Transition to fire?"] +  \
-            [set(["  %s: %s" % (p, v) for p, v in fired])] + \
-            ["Transition fired!"]
-
 delete =            ["Model to delete?", "Deleted!"]
 rename =            ["Old name?", "New name?", "Rename complete!"]
 attr_add =          ["Which model do you want to assign an attribute to?",
@@ -236,30 +227,6 @@ class TestPetrinetInterface(unittest.TestCase):
                 read_edge("t2p", "T2P", "t1", "p2", [], [("weight", "Natural", 1)]) + prompt,
             mode))
 
-    def test_po_pn_interface_enabled(self):
-        self.pn_interface_enabled("PO")
-
-    def test_co_pn_interface_enabled(self):
-        self.pn_interface_enabled("CO")
-
-    def pn_interface_enabled(self, mode):
-        self.assertTrue(run_file(all_files,
-            do_instantiate_simple + ['"enabled"'],
-            did_instantiate_simple + enabled(["t1"]) + prompt,
-            mode))
-
-    def test_po_pn_interface_fire(self):
-        self.pn_interface_fire("PO")
-
-    def test_co_pn_interface_fire(self):
-        self.pn_interface_fire("CO")
-
-    def pn_interface_fire(self, mode):
-        self.assertTrue(run_file(all_files,
-            do_instantiate_simple + ['"fire"', '"t1"'],
-            did_instantiate_simple + fire([("p1", 3), ("p2", 1)]) + prompt,
-            mode))
-
     def test_po_pn_interface_verify_OK(self):
         self.pn_interface_verify_OK("PO")