pn_simulate.alc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Element function simulate(inputs : Element):
  8. log("Start PN step!")
  9. Element outputs
  10. outputs = create_node()
  11. log("Inputs: " + dict_to_string(inputs))
  12. log("PN input: " + cast_e2s(inputs["PetriNets_Runtime"]))
  13. log("Outgoing: " + set_to_string(dict_keys(inputs["PetriNets_Runtime"])))
  14. // Copy over the model to the output dictionary
  15. dict_add(outputs, "PetriNets_Runtime", model_copy(inputs["PetriNets_Runtime"]))
  16. log("Copy OK!")
  17. // Do a single simulation step
  18. Element model
  19. model = outputs["PetriNets_Runtime"]
  20. // Find enabled transitions
  21. Element all_transitions
  22. Element enabled_transitions
  23. String under_study
  24. Boolean enabled
  25. Element in_arcs
  26. String arc_under_study
  27. all_transitions = allInstances(model, "Transition")
  28. enabled_transitions = create_node()
  29. while (0 < read_nr_out(all_transitions)):
  30. under_study = set_pop(all_transitions)
  31. enabled = True
  32. // Find all incoming transitions
  33. in_arcs = allIncomingAssociationInstances(model, under_study, "P2T")
  34. while (0 < read_nr_out(in_arcs)):
  35. arc_under_study = set_pop(in_arcs)
  36. Integer present_tokens
  37. Integer required_tokens
  38. required_tokens = read_attribute(model, arc_under_study, "weight")
  39. log("Weight: " + cast_i2s(required_tokens))
  40. present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens")
  41. log("Tokens: " + cast_i2s(present_tokens))
  42. if (present_tokens < required_tokens):
  43. // Less tokens than required, so disable the transition completely
  44. enabled = False
  45. if (enabled):
  46. set_add(enabled_transitions, under_study)
  47. // Pick random enabled transition
  48. String transition
  49. transition = random_choice(enabled_transitions)
  50. // Consume tokens
  51. Element workset
  52. String working_arc
  53. String working_place
  54. Integer new_value
  55. workset = allIncomingAssociationInstances(model, transition, "P2T")
  56. while (0 < read_nr_out(workset)):
  57. working_arc = set_pop(workset)
  58. working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc]))
  59. new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  60. instantiate_attribute(model, working_place, "tokens", new_value)
  61. // Add tokens
  62. workset = allOutgoingAssociationInstances(model, transition, "T2P")
  63. while (0 < read_nr_out(workset)):
  64. working_arc = set_pop(workset)
  65. working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc]))
  66. new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight"))
  67. instantiate_attribute(model, working_place, "tokens", new_value)
  68. // Finish up
  69. log("Finished PN Step!")
  70. return outputs!