pn_simulate.alc 2.6 KB

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