123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- include "primitives.alh"
- include "modelling.alh"
- include "object_operations.alh"
- Element function reachability_graph(params : Element, output_mms : Element):
- Element result
- Element model
- Element workset
- Element out_model
- Element in_model
- Element transition_vectors_produce
- Element transition_vectors_consume
- Element all_transitions
- Element all_transitions_original
- Element tv
- Element links
- Element mappings
- Element reachable_states
- Element keys
- String transition
- String new_transition
- String state
- String name
- String link
- String place
- String key
- Integer link_weight
- Integer initial
- Integer state_id
- Integer next_id
- Boolean possible
- Element all_places
- Element dict_repr
- Element new_dict_repr
- Element work_unit
- result = create_node()
- out_model = instantiate_model(output_mms["ReachabilityGraph"])
- in_model = params["PetriNet"]
- // Create a dictionary representation for each transition
- transition_vectors_produce = create_node()
- transition_vectors_consume = create_node()
- all_transitions = allInstances(in_model, "Transition")
- while (read_nr_out(all_transitions) > 0):
- transition = set_pop(all_transitions)
- log("Consider transition " + transition)
- tv = create_node()
- links = allIncomingAssociationInstances(in_model, transition, "P2T")
- while (read_nr_out(links) > 0):
- link = set_pop(links)
- name = reverseKeyLookup(in_model["model"], read_edge_src(in_model["model"][link]))
- link_weight = read_attribute(in_model, link, "weight")
- dict_add(tv, name, link_weight)
- dict_add(transition_vectors_consume, transition, tv)
- tv = create_node()
- links = allOutgoingAssociationInstances(in_model, transition, "T2P")
- while (read_nr_out(links) > 0):
- link = set_pop(links)
- name = reverseKeyLookup(in_model["model"], read_edge_dst(in_model["model"][link]))
- link_weight = read_attribute(in_model, link, "weight")
- dict_add(tv, name, link_weight)
- dict_add(transition_vectors_produce, transition, tv)
- workset = create_node()
- all_places = allInstances(in_model, "Place")
- dict_repr = create_node()
- while (read_nr_out(all_places) > 0):
- place = set_pop(all_places)
- dict_add(dict_repr, place, read_attribute(in_model, place, "tokens"))
- all_transitions_original = allInstances(in_model, "Transition")
- mappings = create_node()
- state_id = 0
- next_id = 1
- reachable_states = create_node()
- dict_add(reachable_states, state_id, dict_repr)
- dict_add(mappings, state_id, create_node())
- set_add(workset, state_id)
- // And add in the model itself
- state = instantiate_node(out_model, "State", cast_i2s(state_id))
- instantiate_attribute(out_model, state, "name", cast_i2s(state_id))
- keys = dict_keys(dict_repr)
- while (read_nr_out(keys) > 0):
- key = set_pop(keys)
- place = instantiate_node(out_model, "Place", "")
- instantiate_attribute(out_model, place, "name", read_attribute(in_model, key, "name"))
- instantiate_attribute(out_model, place, "tokens", dict_repr[key])
- instantiate_link(out_model, "Contains", "", state, place)
- while (read_nr_out(workset) > 0):
- state_id = set_pop(workset)
- // TODO check if dict_repr is already in the set of reachable states
- dict_repr = reachable_states[state_id]
- // Compute how the PN behaves with this specific state
- // For this, we fetch all transitions and check if they are enabled
- all_transitions = set_copy(all_transitions_original)
- while (read_nr_out(all_transitions) > 0):
- transition = set_pop(all_transitions)
- log("Test transition: " + cast_v2s(read_attribute(in_model, transition, "name")))
- keys = dict_keys(transition_vectors_consume[transition])
- possible = True
- while (read_nr_out(keys) > 0):
- key = set_pop(keys)
- // Compare the values in the state with those consumed by the transition
- if (integer_lt(dict_repr[key], transition_vectors_consume[transition][key])):
- // Impossible transition, so discard this one
- possible = False
- log("Not applicable!")
- break!
- if (possible):
- log("Applicable!")
- new_dict_repr = dict_copy(dict_repr)
- // Transition can execute, so compute and add the new state based on the consume/produce vectors
- log("Before transition: " + dict_to_string(new_dict_repr))
- keys = dict_keys(transition_vectors_consume[transition])
- while (read_nr_out(keys) > 0):
- key = set_pop(keys)
- dict_overwrite(new_dict_repr, key, integer_subtraction(new_dict_repr[key], transition_vectors_consume[transition][key]))
- keys = dict_keys(transition_vectors_produce[transition])
- while (read_nr_out(keys) > 0):
- key = set_pop(keys)
- dict_overwrite(new_dict_repr, key, integer_addition(new_dict_repr[key], transition_vectors_produce[transition][key]))
- log("After transition: " + dict_to_string(new_dict_repr))
- // Check if this state already has an associated ID
- Integer other_state_id
- Integer target_id
- keys = dict_keys(reachable_states)
- target_id = -1
- while (read_nr_out(keys) > 0):
- other_state_id = set_pop(keys)
- if (dict_eq(reachable_states[other_state_id], new_dict_repr)):
- target_id = other_state_id
- break!
- if (target_id == -1):
- // New state
- target_id = next_id
- next_id = next_id + 1
- // Add to all data structures
- dict_add(reachable_states, target_id, new_dict_repr)
- dict_add(mappings, target_id, create_node())
- set_add(workset, target_id)
- // And add in the model itself
- state = instantiate_node(out_model, "State", cast_i2s(target_id))
- instantiate_attribute(out_model, state, "name", cast_i2s(target_id))
- keys = dict_keys(new_dict_repr)
- while (read_nr_out(keys) > 0):
- key = set_pop(keys)
- place = instantiate_node(out_model, "Place", "")
- instantiate_attribute(out_model, place, "name", read_attribute(in_model, key, "name"))
- instantiate_attribute(out_model, place, "tokens", new_dict_repr[key])
- instantiate_link(out_model, "Contains", "", state, place)
- // Anyway, we have found a transition, which we should store
- dict_add(mappings[state_id], transition, target_id)
- // And also store it in the model itself
- new_transition = instantiate_link(out_model, "Transition", "", cast_i2s(state_id), cast_i2s(target_id))
- instantiate_attribute(out_model, new_transition, "name", read_attribute(in_model, transition, "name"))
- log("Add transition name: " + cast_v2s(read_attribute(in_model, transition, "name")))
- dict_add(result, "ReachabilityGraph", out_model)
- return result!
|