fsa_semantics.alc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "library.alh"
  5. include "conformance_scd.alh"
  6. include "io.alh"
  7. include "metamodels.alh"
  8. include "compilation_manager.alh"
  9. Element function retype_to_runtime(design_model : Element):
  10. Element runtime_model
  11. Element all_states
  12. Element all_links
  13. String mm_type_name
  14. String element_name
  15. String attr_name
  16. String attr_value
  17. String attribute
  18. String src
  19. String dst
  20. String time
  21. runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  22. all_blocks = allInstances(design_model, "State")
  23. while (list_len(all_states) > 0):
  24. element_name = set_pop(all_states)
  25. mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
  26. element_name = instantiate_node(runtime_model, mm_type_name, element_name)
  27. instantiate_attribute(runtime_model, element_name, "name", read_attribute(design_model, element_name, "name"))
  28. // Don't merge this together with the block conversion, as the destination block might not exist yet!
  29. all_links = allInstances(design_model, "Transition")
  30. while (read_nr_out(all_links) > 0):
  31. element_name = set_pop(all_links)
  32. src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
  33. dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
  34. instantiate_link(runtime_model, "Transition", element_name, src, dst)
  35. instantiate_attribute(runtime_model, element_name, "event", read_attribute(design_model, element_name, "event"))
  36. if (element_neq(read_attribute(design_model, element_name, "raise"), read_root())):
  37. // There is a raise attribute
  38. instantiate_attribute(runtime_model, element_name, "raise", read_attribute(design_model, element_name, "raise"))
  39. return runtime_model!
  40. Element function sanitize(new_runtime_model : Element, old_runtime_model : Element, auto : Boolean):
  41. String cstate
  42. String cstate_obj
  43. cstate_obj = instantiate_node(new_runtime_model, "CurrentState", "")
  44. cstate = readAssociationDestination(old_runtime_model, set_pop(allInstances(old_runtime_model, "CurrentStateLink")))
  45. if (bool_not(dict_in(new_runtime_model["model"], cstate))):
  46. // Current state removed, so fix
  47. if (auto):
  48. cstate = set_pop(allInstances(new_runtime_model, "InitialState"))
  49. else:
  50. while (bool_not(dict_in(new_runtime_model["model"], cstate))):
  51. output("REQUEST_CURRENT_STATE")
  52. cstate = input()
  53. instantiate_link(new_runtime_model, "CurrentStateLink", "", cstate_obj, cstate)
  54. return new_runtime_model!
  55. Void function do_transition(model : Element, start_time : Float, event : String):
  56. // Read out current state
  57. String cstate_link
  58. String cstate_obj
  59. String cstate
  60. String transition
  61. String new_state
  62. cstate_link = set_pop(allInstances(model, "CurrentStateLink"))
  63. cstate = readAssociationDestination(model, cstate_link)
  64. cstate_obj = readAssociationSource(model, cstate_link)
  65. // Read out all outgoing transitions
  66. Element all_transitions
  67. all_transitions = allOutgoingAssociationInstances(model, cstate, "Transition")
  68. output("SIMTIME " + cast_v2s(time() - start_time))
  69. output("EVENT " + cast_v2s(event))
  70. while (read_nr_out(all_transitions) > 0):
  71. transition = set_pop(all_transitions)
  72. if (value_eq(read_attribute(model, transition, "event"), event)):
  73. // Found a match
  74. model_delete_element(model, cstate_link)
  75. // Set destination of state
  76. instantiate_link(model, "CurrentStateLink", "", cstate_obj, new_state)
  77. output("STATE " + cast_v2s(new_state))
  78. // Raise "raise" attribute of transition
  79. raise = read_attribute(model, transition, "raise")
  80. if (element_neq(raise, read_root())):
  81. // Raise the event
  82. output("RAISE " + cast_v2s(raise))
  83. return !
  84. return!
  85. Void function execute_fsa(design_model : Element):
  86. String verify_result
  87. Element runtime_model
  88. Element old_runtime_model
  89. String cmd
  90. Boolean running
  91. String conforming
  92. Float simulation_time
  93. Float start_time
  94. start_time = time()
  95. simulation_time = 0.0
  96. old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  97. runtime_model = retype_to_runtime(design_model)
  98. runtime_model = sanitize(runtime_model, old_runtime_model, True)
  99. conforming = conformance_scd(design_model)
  100. if (conforming == "OK"):
  101. output("CONFORMANCE_OK")
  102. else:
  103. output("CONFORMANCE_FAIL")
  104. while (True):
  105. if (has_input()):
  106. cmd = input()
  107. else:
  108. cmd = "skip"
  109. // Process input
  110. if (cmd == "skip"):
  111. output("SIMTIME " + cast_v2s(time() - start_time))
  112. output("STATE " + cast_v2s(readAssociationDestination(model, set_pop(allInstances(model, "CurrentStateLink")))))
  113. elif (cmd == "pause"):
  114. // Pausing merely stops a running simulation
  115. simulation_time = time() - start_time
  116. output("PAUSED")
  117. while (cmd != "simulate"):
  118. cmd = input()
  119. start_time = time() - simulation_time
  120. output("CONTINUE")
  121. elif (cmd == "event"):
  122. String evt
  123. evt = input()
  124. do_transition(runtime_model, start_time, evt)
  125. elif (cmd == "read_available_attributes"):
  126. // Returns a list of all available attributes
  127. Element attr_list
  128. Element attrs
  129. Element attr
  130. attr_list = getAttributeList(design_model, input())
  131. attrs = dict_keys(attr_list)
  132. while (0 < read_nr_out(attrs)):
  133. attr = set_pop(attrs)
  134. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  135. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  136. output("AVAILABLE_ATTR_END")
  137. elif (cmd == "read_attribute"):
  138. // Returns the value of an attribute
  139. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  140. elif (bool_or(cmd == "switch_initial", bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association")))):
  141. // Modify the structure
  142. if (cmd == "set_attribute"):
  143. // Setting an attribute
  144. String element_name
  145. String attribute_name
  146. element_name = input()
  147. attribute_name = input()
  148. // Delete it if it exists already
  149. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  150. unset_attribute(design_model, element_name, attribute_name)
  151. // And finally set it
  152. instantiate_attribute(design_model, element_name, attribute_name, input())
  153. elif (cmd == "instantiate_node"):
  154. // Instantiate a node
  155. instantiate_node(design_model, input(), input())
  156. elif (cmd == "instantiate_association"):
  157. // Instantiate an association
  158. instantiate_link(design_model, input(), input(), input(), input())
  159. elif (cmd == "delete_element"):
  160. // Delete the provided element
  161. model_delete_element(design_model, input())
  162. elif (cmd == "switch_initial"):
  163. // Switch the initial state
  164. if (read_nr_out(allInstances(design_model, "InitialState")) > 0):
  165. retype(design_model, set_pop(allInstances(design_model, "InitialState")), "State")
  166. retype(design_model, input(), "InitialState")
  167. // After changes, we check whether or not the design model conforms
  168. if (conforming == "OK"):
  169. // Was correct, so store just to make sure
  170. simulation_time = time() - start_time
  171. conforming = conformance_scd(design_model)
  172. if (conforming == "OK"):
  173. // Conforming, so do the retyping and sanitization step
  174. runtime_model = retype_to_runtime(design_model)
  175. runtime_model = sanitize(runtime_model, old_runtime_model)
  176. schedule_init = create_schedule(runtime_model)
  177. schedule_run = read_root()
  178. old_runtime_model = runtime_model
  179. start_time = time() - simulation_time
  180. output("CONFORMANCE_OK")
  181. else:
  182. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  183. output("CONFORMANCE_FAIL " + conforming)
  184. else:
  185. log("Did not understand command: " + cmd)