FSA_simulate.alc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "conformance_scd.alh"
  5. include "io.alh"
  6. include "metamodels.alh"
  7. include "mini_modify.alh"
  8. include "library.alh"
  9. Boolean function main(model : Element):
  10. String input_value
  11. Float start_time
  12. String current_state
  13. String old_state
  14. Element transitions
  15. String transition
  16. start_time = time()
  17. Element all_states
  18. String element_name
  19. all_states = allInstances(model, "FSA/State")
  20. while (set_len(all_states) > 0):
  21. element_name = set_pop(all_states)
  22. if (value_eq(read_attribute(model, element_name, "initial"), True)):
  23. current_state = element_name
  24. old_state = element_name
  25. break!
  26. while (True):
  27. if (has_input()):
  28. input_value = list_read(string_split(input(), "\n"), 0)
  29. if (input_value == "__EXIT__"):
  30. break!
  31. transitions = allOutgoingAssociationInstances(model, current_state, "FSA/Transition")
  32. while (set_len(transitions) > 0):
  33. transition = set_pop(transitions)
  34. if (cast_string(read_attribute(model, transition, "trigger")) == input_value):
  35. if (element_neq(read_attribute(model, transition, "raise"), read_root())):
  36. log(cast_value(time() - start_time) + " output " + cast_string(read_attribute(model, transition, "raise")))
  37. output(cast_value(time() - start_time) + " output " + cast_string(read_attribute(model, transition, "raise")))
  38. if (element_neq(read_attribute(model, transition, "script"), read_root())):
  39. Element func
  40. func = get_func_AL_model(import_node(read_attribute(model, transition, "script")))
  41. func()
  42. current_state = readAssociationDestination(model, transition)
  43. break!
  44. output(cast_value(time() - start_time) + " " + cast_string(read_attribute(model, current_state, "name")))
  45. sleep(0.2)
  46. return True!