fsa_semantics.alc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. Element all_attributes
  22. runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  23. all_blocks = 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. return runtime_model!
  41. Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
  42. Element all_blocks
  43. Element all_links
  44. String element_name
  45. String attr_name
  46. String attr_value
  47. String attribute
  48. String time
  49. Element all_attributes
  50. Float current_time
  51. all_blocks = allInstances(new_runtime_model, "Block")
  52. while (list_len(all_blocks) > 0):
  53. element_name = set_pop(all_blocks)
  54. if (dict_in(old_runtime_model["model"], element_name)):
  55. if (is_nominal_instance(new_runtime_model, element_name, "ICBlock")):
  56. instantiate_attribute(new_runtime_model, element_name, "last_in", read_attribute(old_runtime_model, element_name, "last_in"))
  57. if (is_nominal_instance(new_runtime_model, element_name, "IntegratorBlock")):
  58. instantiate_attribute(new_runtime_model, element_name, "last_out", read_attribute(old_runtime_model, element_name, "last_out"))
  59. instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
  60. else:
  61. instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
  62. if (dict_in(old_runtime_model["model"], "time")):
  63. current_time = read_attribute(old_runtime_model, "time", "current_time")
  64. else:
  65. current_time = 0
  66. time = instantiate_node(new_runtime_model, "Time", "time")
  67. instantiate_attribute(new_runtime_model, time, "start_time", current_time)
  68. instantiate_attribute(new_runtime_model, time, "current_time", current_time)
  69. return new_runtime_model!
  70. Void function dict_overwrite(d : Element, key : Element, value : Element):
  71. if (dict_in(d, key)):
  72. dict_delete(d, key)
  73. if (dict_in_node(d, key)):
  74. dict_delete_node(d, key)
  75. dict_add(d, key, value)
  76. return !
  77. Integer function min(a : Integer, b : Integer):
  78. if (a < b):
  79. return a!
  80. else:
  81. return b!
  82. Element function list_pop(list : Element):
  83. Integer top
  84. Element t
  85. top = list_len(list) - 1
  86. t = list_read(list, top)
  87. list_delete(list, top)
  88. return t!
  89. String function readType(model : Element, name : String):
  90. return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
  91. Integer function list_index_of(lst : Element, elem : Element):
  92. Integer i
  93. i = 0
  94. while (i < read_nr_out(lst)):
  95. if (value_eq(list_read(lst, i), elem)):
  96. return i!
  97. else:
  98. i = i + 1
  99. return -1!
  100. Void function execute_fsa(design_model : Element):
  101. String verify_result
  102. Element runtime_model
  103. Element old_runtime_model
  104. String cmd
  105. Boolean running
  106. String conforming
  107. Float simulation_time
  108. Float start_time
  109. start_time = time()
  110. simulation_time = 0.0
  111. old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
  112. runtime_model = retype_to_runtime(design_model)
  113. runtime_model = sanitize(runtime_model, old_runtime_model)
  114. conforming = conformance_scd(design_model)
  115. if (conforming == "OK"):
  116. output("CONFORMANCE_OK")
  117. else:
  118. output("CONFORMANCE_FAIL")
  119. while (True):
  120. cmd = input()
  121. // Process input
  122. if (cmd == "pause"):
  123. // Pausing merely stops a running simulation
  124. simulation_time = time() - start_time
  125. output("PAUSED")
  126. while (cmd != "simulate"):
  127. cmd = input()
  128. start_time = time() - simulation_time
  129. output("CONTINUE")
  130. elif (cmd == "event"):
  131. String evt
  132. evt = input()
  133. transition(runtime_model, start_time, evt)
  134. elif (cmd == "read_available_attributes"):
  135. // Returns a list of all available attributes
  136. Element attr_list
  137. Element attrs
  138. Element attr
  139. attr_list = getAttributeList(design_model, input())
  140. attrs = dict_keys(attr_list)
  141. while (0 < read_nr_out(attrs)):
  142. attr = set_pop(attrs)
  143. output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
  144. output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
  145. output("AVAILABLE_ATTR_END")
  146. elif (cmd == "read_attribute"):
  147. // Returns the value of an attribute
  148. output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
  149. elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
  150. // Modify the structure
  151. if (cmd == "set_attribute"):
  152. // Setting an attribute
  153. String element_name
  154. String attribute_name
  155. element_name = input()
  156. attribute_name = input()
  157. // Delete it if it exists already
  158. if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
  159. unset_attribute(design_model, element_name, attribute_name)
  160. // And finally set it
  161. instantiate_attribute(design_model, element_name, attribute_name, input())
  162. elif (cmd == "instantiate_node"):
  163. // Instantiate a node
  164. instantiate_node(design_model, input(), input())
  165. elif (cmd == "instantiate_association"):
  166. // Instantiate an association
  167. instantiate_link(design_model, input(), input(), input(), input())
  168. elif (cmd == "delete_element"):
  169. // Delete the provided element
  170. model_delete_element(design_model, input())
  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)
  180. schedule_init = create_schedule(runtime_model)
  181. schedule_run = read_root()
  182. old_runtime_model = runtime_model
  183. start_time = time() - simulation_time
  184. output("CONFORMANCE_OK")
  185. else:
  186. // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
  187. output("CONFORMANCE_FAIL " + conforming)
  188. else:
  189. log("Did not understand command: " + cmd)
  190. Float function v2f(i : Element):
  191. return cast_s2f(cast_v2s(i))!