fsa_semantics.alc 8.7 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. log("Removed current state")
  52. if (auto):
  53. log("Auto-fixing the problem")
  54. cstate = set_pop(allInstances(new_runtime_model, "InitialState"))
  55. else:
  56. log("Please give new state")
  57. while (bool_not(dict_in(new_runtime_model["model"], cstate))):
  58. output("REQUEST_CURRENT_STATE")
  59. cstate = input()
  60. else:
  61. // Initialize for the first time
  62. cstate = set_pop(allInstances(new_runtime_model, "InitialState"))
  63. instantiate_link(new_runtime_model, "CurrentStateLink", "", cstate_obj, cstate)
  64. log("Sanitize OK")
  65. return new_runtime_model!
  66. Void function do_transition(model : Element, start_time : Float, event : String):
  67. // Read out current state
  68. String cstate_link
  69. String cstate_obj
  70. String cstate
  71. String transition
  72. String new_state
  73. String raise
  74. log("Transition executing")
  75. cstate_link = set_pop(allInstances(model, "CurrentStateLink"))
  76. cstate = readAssociationDestination(model, cstate_link)
  77. cstate_obj = readAssociationSource(model, cstate_link)
  78. // Read out all outgoing transitions
  79. Element all_transitions
  80. all_transitions = allOutgoingAssociationInstances(model, cstate, "Transition")
  81. output("SIM_TIME " + cast_v2s(time() - start_time))
  82. output("SIM_EVENT " + cast_v2s(event))
  83. while (read_nr_out(all_transitions) > 0):
  84. transition = set_pop(all_transitions)
  85. if (value_eq(read_attribute(model, transition, "event"), event)):
  86. // Found a match
  87. new_state = readAssociationDestination(model, transition)
  88. log("Transition to " + new_state)
  89. // Delete current state
  90. model_delete_element(model, cstate_link)
  91. // Set destination of state
  92. instantiate_link(model, "CurrentStateLink", "", cstate_obj, new_state)
  93. log("Do state change")
  94. output("SIM_STATE " + cast_v2s(read_attribute(model, new_state, "name")))
  95. // Raise "raise" attribute of transition
  96. raise = read_attribute(model, transition, "raise")
  97. if (element_neq(raise, read_root())):
  98. // Raise the event
  99. output("SIM_RAISE " + cast_v2s(raise))
  100. return !
  101. return!
  102. Void function execute_fsa(design_model : Element):
  103. String verify_result
  104. Element runtime_model
  105. Element old_runtime_model
  106. String cmd
  107. Boolean running
  108. String conforming
  109. Float simulation_time
  110. Float start_time
  111. Boolean automatic_sanitization
  112. automatic_sanitization = True
  113. start_time = time()
  114. simulation_time = 0.0
  115. old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  116. runtime_model = retype_to_runtime(design_model)
  117. conforming = conformance_scd(design_model)
  118. log("Conformance: " + conforming)
  119. if (conforming == "OK"):
  120. output("CONFORMANCE_OK")
  121. else:
  122. output("CONFORMANCE_FAIL")
  123. while (True):
  124. cmd = input()
  125. log("Do: " + cmd)
  126. if (cmd == "pause"):
  127. // Pausing merely stops a running simulation
  128. if (running):
  129. simulation_time = time() - start_time
  130. running = False
  131. output("PAUSED")
  132. elif (cmd == "simulate"):
  133. // Continue simulation, so reset the value
  134. if (bool_not(running)):
  135. start_time = time() - simulation_time
  136. running = True
  137. output("CONTINUE")
  138. elif (cmd == "auto_sanitize"):
  139. automatic_sanitization = input()
  140. elif (cmd == "event"):
  141. log("Got event")
  142. do_transition(runtime_model, start_time, input())
  143. elif (cmd == "read_available_attributes"):
  144. // Returns a list of all available attributes
  145. Element attr_list
  146. Element attrs
  147. Element attr
  148. attr_list = getAttributeList(design_model, input())
  149. attrs = dict_keys(attr_list)
  150. while (0 < read_nr_out(attrs)):
  151. attr = set_pop(attrs)
  152. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  153. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  154. output("AVAILABLE_ATTR_END")
  155. elif (cmd == "read_attribute"):
  156. // Returns the value of an attribute
  157. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  158. elif (bool_or(cmd == "switch_initial", bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association")))):
  159. // Modify the structure
  160. if (cmd == "set_attribute"):
  161. // Setting an attribute
  162. String element_name
  163. String attribute_name
  164. element_name = input()
  165. attribute_name = input()
  166. // Delete it if it exists already
  167. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  168. unset_attribute(design_model, element_name, attribute_name)
  169. // And finally set it
  170. instantiate_attribute(design_model, element_name, attribute_name, input())
  171. elif (cmd == "instantiate_node"):
  172. // Instantiate a node
  173. instantiate_node(design_model, input(), input())
  174. elif (cmd == "instantiate_association"):
  175. // Instantiate an association
  176. instantiate_link(design_model, input(), input(), input(), input())
  177. elif (cmd == "delete_element"):
  178. // Delete the provided element
  179. model_delete_element(design_model, input())
  180. elif (cmd == "switch_initial"):
  181. // Switch the initial state
  182. if (read_nr_out(allInstances(design_model, "InitialState")) > 0):
  183. retype(design_model, set_pop(allInstances(design_model, "InitialState")), "State")
  184. retype(design_model, input(), "InitialState")
  185. // After changes, we check whether or not the design model conforms
  186. if (conforming == "OK"):
  187. // Was correct, so store just to make sure
  188. simulation_time = time() - start_time
  189. conforming = conformance_scd(design_model)
  190. if (conforming == "OK"):
  191. // Conforming, so do the retyping and sanitization step
  192. runtime_model = retype_to_runtime(design_model)
  193. runtime_model = sanitize(runtime_model, old_runtime_model, automatic_sanitization)
  194. old_runtime_model = runtime_model
  195. start_time = time() - simulation_time
  196. output("CONFORMANCE_OK")
  197. log("Conformance became 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. if (bool_and(conforming == "OK", running)):
  204. output("SIM_TIME " + cast_v2s(time() - start_time))
  205. output("SIM_STATE " + cast_v2s(read_attribute(runtime_model, readAssociationDestination(runtime_model, set_pop(allInstances(runtime_model, "CurrentStateLink"))), "name")))
  206. Void function main():
  207. Element model
  208. String verify_result
  209. while (True):
  210. execute_fsa(instantiate_model(import_node("models/FiniteStateAutomata_Design")))
  211. return!