simple_simulate.alc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // Check all incoming P2T links
  17. links = allIncomingAssociationInstances(model, transition, "PetriNet/P2T")
  18. enabled = True
  19. while (set_len(links) > 0):
  20. link = set_pop(links)
  21. if (cast_integer(read_attribute(model, link, "weight")) > cast_integer(read_attribute(model, readAssociationSource(model, link), "tokens"))):
  22. // Too few tokens, so skip
  23. enabled = False
  24. break!
  25. if (enabled):
  26. // All checks OK, so update the out locations
  27. links = allOutgoingAssociationInstances(model, transition, "PetriNet/T2P")
  28. while (set_len(links) > 0):
  29. link = set_pop(links)
  30. place = readAssociationDestination(model, link)
  31. instantiate_attribute(model, place, "tokens", cast_integer(read_attribute(model, place, "tokens")) + cast_integer(read_attribute(model, link, "weight")))
  32. // All checks OK, and update the in locations
  33. links = allOutgoingAssociationInstances(model, transition, "PetriNet/P2T")
  34. while (set_len(links) > 0):
  35. link = set_pop(links)
  36. place = readAssociationSource(model, link)
  37. instantiate_attribute(model, place, "tokens", cast_integer(read_attribute(model, place, "tokens")) - cast_integer(read_attribute(model, link, "weight")))
  38. return True!
  39. return False!