bfs.alc 1.5 KB

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