fsa_semantics.alc 8.0 KB

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