|
@@ -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")
|