simple_simulate.alc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. Boolean function main(model : Element):
  5. Element transitions
  6. Element links
  7. String transition
  8. String link
  9. String place
  10. Boolean enabled
  11. transitions = allInstances(model, "PetriNet/Transition")
  12. // Iterate over all transitions
  13. while (set_len(transitions) > 0):
  14. // Check if it is enabled
  15. transition = set_pop(transitions)
  16. log("Check transition " + cast_string(transition))
  17. // Check all incoming P2T links
  18. links = allIncomingAssociationInstances(model, transition, "PetriNet/P2T")
  19. enabled = True
  20. while (set_len(links) > 0):
  21. link = set_pop(links)
  22. if (cast_integer(read_attribute(model, link, "weight")) > cast_integer(read_attribute(model, readAssociationSource(model, link), "tokens"))):
  23. // Too few tokens, so skip
  24. enabled = False
  25. break!
  26. if (enabled):
  27. log("Enabled!")
  28. // All checks OK, so update the out locations
  29. links = allOutgoingAssociationInstances(model, transition, "PetriNet/T2P")
  30. while (set_len(links) > 0):
  31. link = set_pop(links)
  32. place = readAssociationDestination(model, link)
  33. instantiate_attribute(model, place, "tokens", cast_integer(read_attribute(model, place, "tokens")) + cast_integer(read_attribute(model, link, "weight")))
  34. log("New tokens TGT: " + cast_string(read_attribute(model, place, "tokens")))
  35. // All checks OK, and update the in locations
  36. links = allIncomingAssociationInstances(model, transition, "PetriNet/P2T")
  37. while (set_len(links) > 0):
  38. link = set_pop(links)
  39. place = readAssociationSource(model, link)
  40. instantiate_attribute(model, place, "tokens", cast_integer(read_attribute(model, place, "tokens")) - cast_integer(read_attribute(model, link, "weight")))
  41. log("New tokens SRC: " + cast_string(read_attribute(model, place, "tokens")))
  42. return True!
  43. return False!