bfs.alc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. Element visited
  14. Element visited_copy
  15. String option
  16. String dest
  17. model = params["reachability_graph"]
  18. worklist = create_node()
  19. initial = set_pop(allInstances(model, "InitialState"))
  20. work_unit = create_node()
  21. list_append(work_unit, initial)
  22. path = create_node()
  23. list_append(work_unit, path)
  24. visited = create_node()
  25. set_add(visited, initial)
  26. list_append(work_unit, visited)
  27. list_append(worklist, work_unit)
  28. while (read_nr_out(worklist) > 0):
  29. work_unit = list_pop(worklist, 0)
  30. state = work_unit[0]
  31. path = work_unit[1]
  32. visited = work_unit[2]
  33. log("Searching for length " + cast_v2s(read_nr_out(path)))
  34. if (value_eq(read_attribute(model, state, "error"), True)):
  35. // Found an error path!
  36. log("Found error path!")
  37. log(list_to_string(path))
  38. output("Found error path:")
  39. output(list_to_string(path))
  40. break!
  41. options = allOutgoingAssociationInstances(model, state, "Transition")
  42. while (read_nr_out(options) > 0):
  43. option = set_pop(options)
  44. dest = readAssociationDestination(model, option)
  45. if (set_in(visited, dest)):
  46. // Already visited this node, so skip
  47. continue!
  48. path_copy = dict_copy(path)
  49. visited_copy = set_copy(visited)
  50. set_add(visited_copy, dest)
  51. list_append(path_copy, read_attribute(model, option, "name"))
  52. work_unit = create_node()
  53. list_append(work_unit, dest)
  54. list_append(work_unit, path_copy)
  55. list_append(work_unit, visited_copy)
  56. list_append(worklist, work_unit)
  57. return create_node()!