reachability.alc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. Element function reachability_graph(params : Element, output_mms : Element):
  5. Element result
  6. Element model
  7. Element workset
  8. Element out_model
  9. Element in_model
  10. Element transition_vectors_produce
  11. Element transition_vectors_consume
  12. Element all_transitions
  13. Element all_transitions_original
  14. Element tv
  15. Element links
  16. Element mappings
  17. Element reachable_states
  18. Element keys
  19. String transition
  20. String new_transition
  21. String state
  22. String name
  23. String link
  24. String place
  25. String key
  26. Integer link_weight
  27. Integer initial
  28. Integer state_id
  29. Integer next_id
  30. Boolean possible
  31. Element all_places
  32. Element dict_repr
  33. Element new_dict_repr
  34. Element work_unit
  35. result = create_node()
  36. out_model = instantiate_model(output_mms["ReachabilityGraph"])
  37. in_model = params["PetriNet"]
  38. // Create a dictionary representation for each transition
  39. transition_vectors_produce = create_node()
  40. transition_vectors_consume = create_node()
  41. all_transitions = allInstances(in_model, "Transition")
  42. while (read_nr_out(all_transitions) > 0):
  43. transition = set_pop(all_transitions)
  44. log("Consider transition " + transition)
  45. tv = create_node()
  46. links = allIncomingAssociationInstances(in_model, transition, "P2T")
  47. while (read_nr_out(links) > 0):
  48. link = set_pop(links)
  49. name = reverseKeyLookup(in_model["model"], read_edge_src(in_model["model"][link]))
  50. link_weight = read_attribute(in_model, link, "weight")
  51. dict_add(tv, name, link_weight)
  52. dict_add(transition_vectors_consume, transition, tv)
  53. tv = create_node()
  54. links = allOutgoingAssociationInstances(in_model, transition, "T2P")
  55. while (read_nr_out(links) > 0):
  56. link = set_pop(links)
  57. name = reverseKeyLookup(in_model["model"], read_edge_dst(in_model["model"][link]))
  58. link_weight = read_attribute(in_model, link, "weight")
  59. dict_add(tv, name, link_weight)
  60. dict_add(transition_vectors_produce, transition, tv)
  61. workset = create_node()
  62. all_places = allInstances(in_model, "Place")
  63. dict_repr = create_node()
  64. while (read_nr_out(all_places) > 0):
  65. place = set_pop(all_places)
  66. dict_add(dict_repr, place, read_attribute(in_model, place, "tokens"))
  67. all_transitions_original = allInstances(in_model, "Transition")
  68. mappings = create_node()
  69. state_id = 0
  70. next_id = 1
  71. reachable_states = create_node()
  72. dict_add(reachable_states, state_id, dict_repr)
  73. dict_add(mappings, state_id, create_node())
  74. set_add(workset, state_id)
  75. // And add in the model itself
  76. state = instantiate_node(out_model, "State", cast_i2s(state_id))
  77. instantiate_attribute(out_model, state, "name", cast_i2s(state_id))
  78. keys = dict_keys(dict_repr)
  79. while (read_nr_out(keys) > 0):
  80. key = set_pop(keys)
  81. place = instantiate_node(out_model, "Place", "")
  82. instantiate_attribute(out_model, place, "name", read_attribute(in_model, key, "name"))
  83. instantiate_attribute(out_model, place, "tokens", dict_repr[key])
  84. instantiate_link(out_model, "Contains", "", state, place)
  85. while (read_nr_out(workset) > 0):
  86. state_id = set_pop(workset)
  87. // TODO check if dict_repr is already in the set of reachable states
  88. dict_repr = reachable_states[state_id]
  89. // Compute how the PN behaves with this specific state
  90. // For this, we fetch all transitions and check if they are enabled
  91. all_transitions = set_copy(all_transitions_original)
  92. while (read_nr_out(all_transitions) > 0):
  93. transition = set_pop(all_transitions)
  94. log("Test transition: " + cast_v2s(read_attribute(in_model, transition, "name")))
  95. keys = dict_keys(transition_vectors_consume[transition])
  96. possible = True
  97. while (read_nr_out(keys) > 0):
  98. key = set_pop(keys)
  99. // Compare the values in the state with those consumed by the transition
  100. if (integer_lt(dict_repr[key], transition_vectors_consume[transition][key])):
  101. // Impossible transition, so discard this one
  102. possible = False
  103. log("Not applicable!")
  104. break!
  105. if (possible):
  106. log("Applicable!")
  107. new_dict_repr = dict_copy(dict_repr)
  108. // Transition can execute, so compute and add the new state based on the consume/produce vectors
  109. log("Before transition: " + dict_to_string(new_dict_repr))
  110. keys = dict_keys(transition_vectors_consume[transition])
  111. while (read_nr_out(keys) > 0):
  112. key = set_pop(keys)
  113. dict_overwrite(new_dict_repr, key, integer_subtraction(new_dict_repr[key], transition_vectors_consume[transition][key]))
  114. keys = dict_keys(transition_vectors_produce[transition])
  115. while (read_nr_out(keys) > 0):
  116. key = set_pop(keys)
  117. dict_overwrite(new_dict_repr, key, integer_addition(new_dict_repr[key], transition_vectors_produce[transition][key]))
  118. log("After transition: " + dict_to_string(new_dict_repr))
  119. // Check if this state already has an associated ID
  120. Integer other_state_id
  121. Integer target_id
  122. keys = dict_keys(reachable_states)
  123. target_id = -1
  124. while (read_nr_out(keys) > 0):
  125. other_state_id = set_pop(keys)
  126. if (dict_eq(reachable_states[other_state_id], new_dict_repr)):
  127. target_id = other_state_id
  128. break!
  129. if (target_id == -1):
  130. // New state
  131. target_id = next_id
  132. next_id = next_id + 1
  133. // Add to all data structures
  134. dict_add(reachable_states, target_id, new_dict_repr)
  135. dict_add(mappings, target_id, create_node())
  136. set_add(workset, target_id)
  137. // And add in the model itself
  138. state = instantiate_node(out_model, "State", cast_i2s(target_id))
  139. instantiate_attribute(out_model, state, "name", cast_i2s(target_id))
  140. keys = dict_keys(new_dict_repr)
  141. while (read_nr_out(keys) > 0):
  142. key = set_pop(keys)
  143. place = instantiate_node(out_model, "Place", "")
  144. instantiate_attribute(out_model, place, "name", read_attribute(in_model, key, "name"))
  145. instantiate_attribute(out_model, place, "tokens", new_dict_repr[key])
  146. instantiate_link(out_model, "Contains", "", state, place)
  147. // Anyway, we have found a transition, which we should store
  148. dict_add(mappings[state_id], transition, target_id)
  149. // And also store it in the model itself
  150. new_transition = instantiate_link(out_model, "Transition", "", cast_i2s(state_id), cast_i2s(target_id))
  151. instantiate_attribute(out_model, new_transition, "name", read_attribute(in_model, transition, "name"))
  152. log("Add transition name: " + cast_v2s(read_attribute(in_model, transition, "name")))
  153. dict_add(result, "ReachabilityGraph", out_model)
  154. return result!