include "primitives.alh" include "modelling.alh" include "random.alh" include "model_management.alh" include "object_operations.alh" include "modelling.alh" Boolean function simulate(model : Element): // Do a single simulation step log("Start PN step!") // Find enabled transitions Element all_transitions Element enabled_transitions String under_study Boolean enabled Element in_arcs String arc_under_study log("Got types: " + set_to_string(dict_keys(model["metamodel"]["model"]))) all_transitions = allInstances(model, "PetriNet_Runtime/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, "PetriNet_Runtime/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, "PetriNet_Runtime/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, "PetriNet_Runtime/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 log("Finished PN Step!") return True!