bfs.alc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. Boolean function bfs(model : Element):
  5. String initial
  6. Element options
  7. Element worklist
  8. Element work_unit
  9. String state
  10. Element path
  11. Element path_copy
  12. String option
  13. Element visited
  14. String dest
  15. Integer total_states
  16. total_states = read_nr_out(allInstances(model, "ReachabilityGraph/State"))
  17. worklist = create_node()
  18. visited = create_node()
  19. initial = set_pop(allInstances(model, "ReachabilityGraph/InitialState"))
  20. list_append(worklist, create_tuple(initial, create_node()))
  21. set_add(visited, initial)
  22. while (read_nr_out(worklist) > 0):
  23. work_unit = list_pop(worklist, 0)
  24. state = work_unit[0]
  25. path = work_unit[1]
  26. if (read_nr_out(visited) == total_states):
  27. log("No error path found!")
  28. break!
  29. if (value_eq(read_attribute(model, state, "error"), True)):
  30. // Found an error path!
  31. log("Found error path!")
  32. log(list_to_string(path))
  33. output("Found error path:")
  34. output(list_to_string(path))
  35. break!
  36. options = allOutgoingAssociationInstances(model, state, "ReachabilityGraph/Transition")
  37. while (read_nr_out(options) > 0):
  38. option = set_pop(options)
  39. dest = readAssociationDestination(model, option)
  40. if (set_in(visited, dest)):
  41. continue!
  42. else:
  43. path_copy = dict_copy(path)
  44. list_append(path_copy, read_attribute(model, option, "name"))
  45. list_append(worklist, create_tuple(dest, path_copy))
  46. set_add(visited, dest)
  47. return True!