pn_semantics.alc 3.8 KB

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