reachability.alc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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["pn"]
  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. tv = create_node()
  45. links = allIncomingAssociationInstances(in_model, transition, "P2T")
  46. while (read_nr_out(links) > 0):
  47. link = set_pop(links)
  48. name = reverseKeyLookup(in_model["model"], read_edge_src(in_model["model"][link]))
  49. link_weight = read_attribute(in_model, link, "weight")
  50. dict_add(tv, name, link_weight)
  51. dict_add(transition_vectors_consume, transition, tv)
  52. tv = create_node()
  53. links = allOutgoingAssociationInstances(in_model, transition, "T2P")
  54. while (read_nr_out(links) > 0):
  55. link = set_pop(links)
  56. name = reverseKeyLookup(in_model["model"], read_edge_dst(in_model["model"][link]))
  57. link_weight = read_attribute(in_model, link, "weight")
  58. dict_add(tv, name, link_weight)
  59. dict_add(transition_vectors_produce, transition, tv)
  60. workset = create_node()
  61. all_places = allInstances(in_model, "Place")
  62. dict_repr = create_node()
  63. while (read_nr_out(all_places) > 0):
  64. place = set_pop(all_places)
  65. dict_add(dict_repr, place, read_attribute(in_model, place, "tokens"))
  66. all_transitions_original = allInstances(in_model, "Transition")
  67. mappings = create_node()
  68. state_id = 0
  69. next_id = 1
  70. reachable_states = create_node()
  71. dict_add(reachable_states, state_id, dict_repr)
  72. dict_add(mappings, state_id, create_node())
  73. set_add(workset, state_id)
  74. // And add in the model itself
  75. state = instantiate_node(out_model, "State", cast_i2s(state_id))
  76. instantiate_attribute(out_model, state, "name", cast_i2s(state_id))
  77. keys = dict_keys(dict_repr)
  78. while (read_nr_out(keys) > 0):
  79. key = set_pop(keys)
  80. place = instantiate_node(out_model, "Place", "")
  81. instantiate_attribute(out_model, place, "name", read_attribute(in_model, key, "name"))
  82. instantiate_attribute(out_model, place, "tokens", dict_repr[key])
  83. instantiate_link(out_model, "Contains", "", state, place)
  84. while (read_nr_out(workset) > 0):
  85. state_id = set_pop(workset)
  86. log("Work on state ID " + cast_v2s(state_id))
  87. log("Reachable states: " + cast_i2s(read_nr_out(reachable_states)))
  88. log("Workset: " + cast_i2s(read_nr_out(workset)))
  89. // TODO check if dict_repr is already in the set of reachable states
  90. dict_repr = reachable_states[state_id]
  91. // Compute how the PN behaves with this specific state
  92. // For this, we fetch all transitions and check if they are enabled
  93. all_transitions = set_copy(all_transitions_original)
  94. while (read_nr_out(all_transitions) > 0):
  95. transition = set_pop(all_transitions)
  96. log("Test " + cast_v2s(read_attribute(in_model, transition, "name")))
  97. keys = dict_keys(transition_vectors_consume[transition])
  98. possible = True
  99. while (read_nr_out(keys) > 0):
  100. key = set_pop(keys)
  101. // Compare the values in the state with those consumed by the transition
  102. if (integer_lt(dict_repr[key], transition_vectors_consume[transition][key])):
  103. // Impossible transition, so discard this one
  104. possible = False
  105. break!
  106. if (possible):
  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. keys = dict_keys(transition_vectors_consume[transition])
  110. while (read_nr_out(keys) > 0):
  111. key = set_pop(keys)
  112. dict_overwrite(new_dict_repr, key, integer_subtraction(new_dict_repr[key], transition_vectors_consume[transition][key]))
  113. keys = dict_keys(transition_vectors_produce[transition])
  114. while (read_nr_out(keys) > 0):
  115. key = set_pop(keys)
  116. dict_overwrite(new_dict_repr, key, integer_addition(new_dict_repr[key], transition_vectors_produce[transition][key]))
  117. // Check if this state already has an associated ID
  118. Integer other_state_id
  119. Integer target_id
  120. keys = dict_keys(reachable_states)
  121. target_id = -1
  122. while (read_nr_out(keys) > 0):
  123. other_state_id = set_pop(keys)
  124. if (dict_eq(reachable_states[other_state_id], new_dict_repr)):
  125. target_id = other_state_id
  126. break!
  127. if (target_id == -1):
  128. // New state
  129. target_id = next_id
  130. next_id = next_id + 1
  131. // Add to all data structures
  132. dict_add(reachable_states, target_id, new_dict_repr)
  133. dict_add(mappings, target_id, create_node())
  134. set_add(workset, target_id)
  135. // And add in the model itself
  136. state = instantiate_node(out_model, "State", cast_i2s(target_id))
  137. instantiate_attribute(out_model, state, "name", cast_i2s(target_id))
  138. keys = dict_keys(new_dict_repr)
  139. // TODO optimize this part?
  140. while (read_nr_out(keys) > 0):
  141. key = set_pop(keys)
  142. place = instantiate_node(out_model, "Place", "")
  143. instantiate_attribute(out_model, place, "name", read_attribute(in_model, key, "name"))
  144. instantiate_attribute(out_model, place, "tokens", new_dict_repr[key])
  145. instantiate_link(out_model, "Contains", "", state, place)
  146. // Anyway, we have found a transition, which we should store
  147. dict_add(mappings[state_id], transition, target_id)
  148. // And also store it in the model itself
  149. new_transition = instantiate_link(out_model, "Transition", "", cast_i2s(state_id), cast_i2s(target_id))
  150. instantiate_attribute(out_model, new_transition, "name", read_attribute(in_model, transition, "name"))
  151. dict_add(result, "ReachabilityGraph", out_model)
  152. return result!