pn_semantics.alc 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. Element function petrinet_enabled(model : Element):
  2. Element set_enabled
  3. set_enabled = petrinet_enabled_set(model)
  4. output("Enabled transitions:")
  5. while (0 < read_nr_out(set_enabled)):
  6. output(set_pop(set_enabled))
  7. return model
  8. Element function petrinet_enabled_set(model : Element):
  9. Element all_transitions
  10. Element enabled_transitions
  11. String under_study
  12. Element in_arcs
  13. String arc_under_study
  14. Boolean enabled
  15. all_transitions = allInstances(model, "Transition")
  16. enabled_transitions = create_node()
  17. while (0 < read_nr_out(all_transitions)):
  18. under_study = set_pop(all_transitions)
  19. enabled = True
  20. // Find all incoming transitions
  21. in_arcs = allIncomingAssociationInstances(model, under_study, "P2T")
  22. while (0 < read_nr_out(in_arcs)):
  23. arc_under_study = set_pop(in_arcs)
  24. Integer present_tokens
  25. Integer required_tokens
  26. required_tokens = read_attribute(model, arc_under_study, "weight")
  27. log("Weight: " + cast_i2s(required_tokens))
  28. present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens")
  29. log("Tokens: " + cast_i2s(present_tokens))
  30. if (present_tokens < required_tokens):
  31. // Less tokens than required, so disable the transition completely
  32. enabled = False
  33. if (enabled):
  34. set_add(enabled_transitions, under_study)
  35. log("Got all enabled transitions!")
  36. return enabled_transitions
  37. Element function petrinet_fire(model : Element):
  38. output("Transition to fire?")
  39. String transition
  40. transition = input()
  41. if (dict_in(model["model"], transition)):
  42. if (set_in(petrinet_enabled_set(model), transition)):
  43. Element workset
  44. String working_place
  45. String working_arc
  46. Integer new_value
  47. // Consume tokens
  48. workset = allIncomingAssociationInstances(model, transition, "P2T")
  49. while (0 < read_nr_out(workset)):
  50. working_arc = set_pop(workset)
  51. working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc]))
  52. new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  53. unset_attribute(model, working_place, "tokens")
  54. instantiate_attribute(model, working_place, "tokens", new_value)
  55. output(((" " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens")))
  56. // Add tokens
  57. workset = allOutgoingAssociationInstances(model, transition, "T2P")
  58. while (0 < read_nr_out(workset)):
  59. working_arc = set_pop(workset)
  60. working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc]))
  61. new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  62. unset_attribute(model, working_place, "tokens")
  63. instantiate_attribute(model, working_place, "tokens", new_value)
  64. output(((" " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens")))
  65. output("Transition fired!")
  66. else:
  67. output("Cannot fire if not enabled; aborting")
  68. else:
  69. output("Unknown transition; aborting")
  70. return model
  71. Element function main():
  72. Element model
  73. String cmd
  74. model = read_root()
  75. while (element_eq(model, read_root())):
  76. output("Which model do you want to execute with petri net semantics?")
  77. model = import_node(input())
  78. while (True):
  79. output("Which operation do you want to execute?")
  80. cmd = input()