fsa_semantics.alc 8.4 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. if (read_nr_out(allInstances(old_runtime_model, "CurrentState")) > 0):
  47. cstate = readAssociationDestination(old_runtime_model, set_pop(allInstances(old_runtime_model, "CurrentStateLink")))
  48. cstate_obj = instantiate_node(new_runtime_model, "CurrentState", "")
  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. start_time = time()
  106. simulation_time = 0.0
  107. old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  108. runtime_model = retype_to_runtime(design_model)
  109. conforming = conformance_scd(design_model)
  110. log("Conformance: " + conforming)
  111. if (conforming == "OK"):
  112. output("CONFORMANCE_OK")
  113. else:
  114. output("CONFORMANCE_FAIL")
  115. while (True):
  116. log("Check input")
  117. if (has_input()):
  118. cmd = input()
  119. else:
  120. if (conforming == "OK"):
  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. simulation_time = time() - start_time
  133. output("PAUSED")
  134. while (cmd != "simulate"):
  135. cmd = input()
  136. start_time = time() - simulation_time
  137. output("CONTINUE")
  138. elif (cmd == "auto_sanitize"):
  139. automatic_sanitization = input()
  140. elif (cmd == "event"):
  141. String evt
  142. evt = input()
  143. do_transition(runtime_model, start_time, evt)
  144. elif (cmd == "read_available_attributes"):
  145. // Returns a list of all available attributes
  146. Element attr_list
  147. Element attrs
  148. Element attr
  149. attr_list = getAttributeList(design_model, input())
  150. attrs = dict_keys(attr_list)
  151. while (0 < read_nr_out(attrs)):
  152. attr = set_pop(attrs)
  153. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  154. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  155. output("AVAILABLE_ATTR_END")
  156. elif (cmd == "read_attribute"):
  157. // Returns the value of an attribute
  158. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  159. elif (bool_or(cmd == "switch_initial", bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association")))):
  160. // Modify the structure
  161. if (cmd == "set_attribute"):
  162. // Setting an attribute
  163. String element_name
  164. String attribute_name
  165. element_name = input()
  166. attribute_name = input()
  167. // Delete it if it exists already
  168. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  169. unset_attribute(design_model, element_name, attribute_name)
  170. // And finally set it
  171. instantiate_attribute(design_model, element_name, attribute_name, input())
  172. elif (cmd == "instantiate_node"):
  173. // Instantiate a node
  174. instantiate_node(design_model, input(), input())
  175. elif (cmd == "instantiate_association"):
  176. // Instantiate an association
  177. instantiate_link(design_model, input(), input(), input(), input())
  178. elif (cmd == "delete_element"):
  179. // Delete the provided element
  180. model_delete_element(design_model, input())
  181. elif (cmd == "switch_initial"):
  182. // Switch the initial state
  183. if (read_nr_out(allInstances(design_model, "InitialState")) > 0):
  184. retype(design_model, set_pop(allInstances(design_model, "InitialState")), "State")
  185. retype(design_model, input(), "InitialState")
  186. // After changes, we check whether or not the design model conforms
  187. if (conforming == "OK"):
  188. // Was correct, so store just to make sure
  189. simulation_time = time() - start_time
  190. conforming = conformance_scd(design_model)
  191. if (conforming == "OK"):
  192. // Conforming, so do the retyping and sanitization step
  193. runtime_model = retype_to_runtime(design_model)
  194. runtime_model = sanitize(runtime_model, old_runtime_model, automatic_sanitization)
  195. old_runtime_model = runtime_model
  196. start_time = time() - simulation_time
  197. output("CONFORMANCE_OK")
  198. else:
  199. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  200. output("CONFORMANCE_FAIL " + conforming)
  201. else:
  202. log("Did not understand command: " + cmd)
  203. Void function main():
  204. Element model
  205. String verify_result
  206. while (True):
  207. execute_fsa(instantiate_model(import_node("models/FiniteStateAutomata_Design")))