pn_simulate.alc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "random.alh"
  4. include "model_management.alh"
  5. include "object_operations.alh"
  6. include "modelling.alh"
  7. Boolean function simulate(model : Element):
  8. // Do a single simulation step
  9. log("Start PN step!")
  10. // Find enabled transitions
  11. Element all_transitions
  12. Element enabled_transitions
  13. String under_study
  14. Boolean enabled
  15. Element in_arcs
  16. String arc_under_study
  17. all_transitions = allInstances(model, "PetriNets/Transition")
  18. enabled_transitions = create_node()
  19. while (0 < read_nr_out(all_transitions)):
  20. under_study = set_pop(all_transitions)
  21. enabled = True
  22. // Find all incoming transitions
  23. in_arcs = allIncomingAssociationInstances(model, under_study, "PetriNets/P2T")
  24. while (0 < read_nr_out(in_arcs)):
  25. arc_under_study = set_pop(in_arcs)
  26. Integer present_tokens
  27. Integer required_tokens
  28. required_tokens = read_attribute(model, arc_under_study, "weight")
  29. log("Weight: " + cast_i2s(required_tokens))
  30. present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens")
  31. log("Tokens: " + cast_i2s(present_tokens))
  32. if (present_tokens < required_tokens):
  33. // Less tokens than required, so disable the transition completely
  34. enabled = False
  35. if (enabled):
  36. set_add(enabled_transitions, under_study)
  37. // Pick random enabled transition
  38. String transition
  39. transition = random_choice(enabled_transitions)
  40. // Consume tokens
  41. Element workset
  42. String working_arc
  43. String working_place
  44. Integer new_value
  45. workset = allIncomingAssociationInstances(model, transition, "PetriNets/P2T")
  46. while (0 < read_nr_out(workset)):
  47. working_arc = set_pop(workset)
  48. working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc]))
  49. new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  50. instantiate_attribute(model, working_place, "tokens", new_value)
  51. // Add tokens
  52. workset = allOutgoingAssociationInstances(model, transition, "PetriNets/T2P")
  53. while (0 < read_nr_out(workset)):
  54. working_arc = set_pop(workset)
  55. working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc]))
  56. new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  57. instantiate_attribute(model, working_place, "tokens", new_value)
  58. // Finish up
  59. log("Finished PN Step!")
  60. return True!