bfs.alc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = set_len(allInstances(model, "ReachabilityGraph/State"))
  17. worklist = list_create()
  18. visited = set_create()
  19. initial = set_pop(allInstances(model, "ReachabilityGraph/InitialState"))
  20. list_append(worklist, create_tuple(initial, dict_create()))
  21. set_add(visited, initial)
  22. while (list_len(worklist) > 0):
  23. work_unit = list_pop(worklist, 0)
  24. state = work_unit[0]
  25. path = work_unit[1]
  26. if (set_len(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. Integer i
  34. i = 0
  35. while (i < list_len(path)):
  36. output(list_read(path, i))
  37. i = i + 1
  38. break!
  39. options = allOutgoingAssociationInstances(model, state, "ReachabilityGraph/Transition")
  40. while (set_len(options) > 0):
  41. option = set_pop(options)
  42. dest = readAssociationDestination(model, option)
  43. if (set_in(visited, dest)):
  44. continue!
  45. else:
  46. path_copy = dict_copy(path)
  47. list_append(path_copy, read_attribute(model, option, "name"))
  48. list_append(worklist, create_tuple(dest, path_copy))
  49. set_add(visited, dest)
  50. return True!