pn_simulate.alc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // Find enabled transitions
  10. Element all_transitions
  11. Element enabled_transitions
  12. String under_study
  13. Boolean enabled
  14. Element in_arcs
  15. String arc_under_study
  16. all_transitions = allInstances(model, "PetriNet_Runtime/Transition")
  17. enabled_transitions = create_node()
  18. while (0 < read_nr_out(all_transitions)):
  19. under_study = set_pop(all_transitions)
  20. enabled = True
  21. // Find all incoming transitions
  22. in_arcs = allIncomingAssociationInstances(model, under_study, "PetriNet_Runtime/P2T")
  23. while (0 < read_nr_out(in_arcs)):
  24. arc_under_study = set_pop(in_arcs)
  25. Integer present_tokens
  26. Integer required_tokens
  27. required_tokens = read_attribute(model, arc_under_study, "weight")
  28. present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens")
  29. if (present_tokens < required_tokens):
  30. // Less tokens than required, so disable the transition completely
  31. enabled = False
  32. if (enabled):
  33. set_add(enabled_transitions, under_study)
  34. // Pick random enabled transition
  35. String transition
  36. transition = random_choice(set_to_list(enabled_transitions))
  37. // Consume tokens
  38. Element workset
  39. String working_arc
  40. String working_place
  41. Integer new_value
  42. workset = allIncomingAssociationInstances(model, transition, "PetriNet_Runtime/P2T")
  43. while (0 < read_nr_out(workset)):
  44. working_arc = set_pop(workset)
  45. working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc]))
  46. new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  47. instantiate_attribute(model, working_place, "tokens", new_value)
  48. // Add tokens
  49. workset = allOutgoingAssociationInstances(model, transition, "PetriNet_Runtime/T2P")
  50. while (0 < read_nr_out(workset)):
  51. working_arc = set_pop(workset)
  52. working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc]))
  53. new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  54. instantiate_attribute(model, working_place, "tokens", new_value)
  55. // Finish up
  56. return True!