fsa_semantics.alc 8.5 KB

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