fsa_semantics.alc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. new_state = readAssociationDestination(model, transition)
  85. log("Transition to " + new_state)
  86. // Delete current state
  87. model_delete_element(model, cstate_link)
  88. // Set destination of state
  89. instantiate_link(model, "CurrentStateLink", "", cstate_obj, new_state)
  90. log("Do state change")
  91. output("SIM_STATE " + cast_v2s(read_attribute(model, new_state, "name")))
  92. // Raise "raise" attribute of transition
  93. raise = read_attribute(model, transition, "raise")
  94. if (element_neq(raise, read_root())):
  95. // Raise the event
  96. output("SIM_RAISE " + cast_v2s(raise))
  97. return !
  98. return!
  99. Void function execute_fsa(design_model : Element):
  100. String verify_result
  101. Element runtime_model
  102. Element old_runtime_model
  103. String cmd
  104. Boolean running
  105. String conforming
  106. Float simulation_time
  107. Float start_time
  108. Boolean automatic_sanitization
  109. automatic_sanitization = True
  110. start_time = time()
  111. simulation_time = 0.0
  112. old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  113. runtime_model = retype_to_runtime(design_model)
  114. conforming = conformance_scd(design_model)
  115. log("Conformance: " + conforming)
  116. if (conforming == "OK"):
  117. output("CONFORMANCE_OK")
  118. else:
  119. output("CONFORMANCE_FAIL")
  120. while (True):
  121. cmd = input()
  122. log("Do: " + cmd)
  123. if (cmd == "pause"):
  124. // Pausing merely stops a running simulation
  125. if (running):
  126. simulation_time = time() - start_time
  127. running = False
  128. output("PAUSED")
  129. elif (cmd == "simulate"):
  130. // Continue simulation, so reset the value
  131. if (bool_not(running)):
  132. start_time = time() - simulation_time
  133. running = True
  134. output("CONTINUE")
  135. elif (cmd == "auto_sanitize"):
  136. automatic_sanitization = input()
  137. elif (cmd == "event"):
  138. log("Got event")
  139. do_transition(runtime_model, start_time, input())
  140. elif (cmd == "read_available_attributes"):
  141. // Returns a list of all available attributes
  142. Element attr_list
  143. Element attrs
  144. Element attr
  145. attr_list = getAttributeList(design_model, input())
  146. attrs = dict_keys(attr_list)
  147. while (0 < read_nr_out(attrs)):
  148. attr = set_pop(attrs)
  149. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  150. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  151. output("AVAILABLE_ATTR_END")
  152. elif (cmd == "read_attribute"):
  153. // Returns the value of an attribute
  154. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  155. elif (bool_or(cmd == "switch_initial", bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association")))):
  156. // Modify the structure
  157. if (cmd == "set_attribute"):
  158. // Setting an attribute
  159. String element_name
  160. String attribute_name
  161. element_name = input()
  162. attribute_name = input()
  163. // Delete it if it exists already
  164. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  165. unset_attribute(design_model, element_name, attribute_name)
  166. // And finally set it
  167. instantiate_attribute(design_model, element_name, attribute_name, input())
  168. elif (cmd == "instantiate_node"):
  169. // Instantiate a node
  170. instantiate_node(design_model, input(), input())
  171. elif (cmd == "instantiate_association"):
  172. // Instantiate an association
  173. instantiate_link(design_model, input(), input(), input(), input())
  174. elif (cmd == "delete_element"):
  175. // Delete the provided element
  176. model_delete_element(design_model, input())
  177. elif (cmd == "switch_initial"):
  178. // Switch the initial state
  179. if (read_nr_out(allInstances(design_model, "InitialState")) > 0):
  180. retype(design_model, set_pop(allInstances(design_model, "InitialState")), "State")
  181. retype(design_model, input(), "InitialState")
  182. // After changes, we check whether or not the design model conforms
  183. if (conforming == "OK"):
  184. // Was correct, so store just to make sure
  185. simulation_time = time() - start_time
  186. conforming = conformance_scd(design_model)
  187. if (conforming == "OK"):
  188. // Conforming, so do the retyping and sanitization step
  189. runtime_model = retype_to_runtime(design_model)
  190. runtime_model = sanitize(runtime_model, old_runtime_model, automatic_sanitization)
  191. old_runtime_model = runtime_model
  192. start_time = time() - simulation_time
  193. output("CONFORMANCE_OK")
  194. log("Conformance became OK")
  195. else:
  196. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  197. output("CONFORMANCE_FAIL " + conforming)
  198. else:
  199. log("Did not understand command: " + cmd)
  200. if (conforming == "OK"):
  201. output("SIM_TIME " + cast_v2s(time() - start_time))
  202. output("SIM_STATE " + cast_v2s(read_attribute(runtime_model, readAssociationDestination(runtime_model, set_pop(allInstances(runtime_model, "CurrentStateLink"))), "name")))
  203. Void function main():
  204. Element model
  205. String verify_result
  206. while (True):
  207. execute_fsa(instantiate_model(import_node("models/FiniteStateAutomata_Design")))