ramify.alc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. include "primitives.alh"
  2. include "object_operations.alh"
  3. include "modelling.alh"
  4. include "metamodels.alh"
  5. include "model_management.alh"
  6. include "conformance_scd.alh"
  7. Element function ramify(model : Element):
  8. // Create new model structure
  9. Element new_model
  10. new_model = create_node()
  11. dict_add(new_model, "model", create_node())
  12. dict_add(new_model, "type_mapping", create_node())
  13. dict_add(new_model, "metamodel", model["metamodel"])
  14. dict_add(new_model, "source", model)
  15. dict_add(new_model, "target", model)
  16. // Get local variables for parts
  17. Element old_m
  18. Element new_m
  19. Element mm
  20. new_m = new_model["model"]
  21. old_m = model["model"]
  22. mm = new_model["metamodel"]["model"]
  23. // Add in some primitives
  24. instantiate_node(new_model, "SimpleAttribute", "Natural")
  25. instantiate_attribute(new_model, "Natural", "constraint", constraint_Natural)
  26. instantiate_node(new_model, "SimpleAttribute", "String")
  27. instantiate_attribute(new_model, "String", "constraint", constraint_String)
  28. instantiate_node(new_model, "SimpleAttribute", "Boolean")
  29. instantiate_attribute(new_model, "Boolean", "constraint", constraint_Boolean)
  30. instantiate_node(new_model, "SimpleAttribute", "Location")
  31. instantiate_attribute(new_model, "Location", "constraint", constraint_Location)
  32. instantiate_node(new_model, "ComplexAttribute", "ActionLanguage")
  33. instantiate_attribute(new_model, "ActionLanguage", "constraint", constraint_ActionLanguage)
  34. model_define_attribute(new_model, "ActionLanguage", "type", False, "Location")
  35. // Add some default elements
  36. // Class LHS_Root {
  37. // constraint? : ActionLanguage
  38. // upper_cardinality = 1
  39. // lower_cardinality = 1
  40. // }
  41. instantiate_node(new_model, "Class", "LHS_Root")
  42. instantiate_attribute(new_model, "LHS_Root", "lower_cardinality", 1)
  43. instantiate_attribute(new_model, "LHS_Root", "upper_cardinality", 1)
  44. model_define_attribute(new_model, "LHS_Root", "LHS_constraint", True, "ActionLanguage")
  45. // Class LHS : LHS_Root {}
  46. instantiate_node(new_model, "Class", "LHS")
  47. instantiate_link(new_model, "Inheritance", "", "LHS", "LHS_Root")
  48. // Class NAC : LHS_Root {}
  49. instantiate_node(new_model, "Class", "NAC")
  50. instantiate_link(new_model, "Inheritance", "", "NAC", "LHS_Root")
  51. // Class PreElement {
  52. // label : String
  53. // constraint? : ActionLanguage
  54. // }
  55. instantiate_node(new_model, "Class", "PreElement")
  56. model_define_attribute(new_model, "PreElement", "label", False, "String")
  57. model_define_attribute(new_model, "PreElement", "matching_constraint", True, "ActionLanguage")
  58. // Association LHS_contains (LHS_Root, PreElement) {}
  59. instantiate_link(new_model, "Association", "LHS_contains", "LHS_Root", "PreElement")
  60. // Class RHS {
  61. // action? : ActionLanguage
  62. // upper_cardinality = 1
  63. // lower_cardinality = 1
  64. // }
  65. instantiate_node(new_model, "Class", "RHS")
  66. instantiate_attribute(new_model, "RHS", "lower_cardinality", 1)
  67. instantiate_attribute(new_model, "RHS", "upper_cardinality", 1)
  68. model_define_attribute(new_model, "RHS", "action", True, "ActionLanguage")
  69. // Class PostElement {
  70. // label : String
  71. // action? : ActionLanguage
  72. // }
  73. instantiate_node(new_model, "Class", "PostElement")
  74. model_define_attribute(new_model, "PostElement", "label", False, "String")
  75. model_define_attribute(new_model, "PostElement", "action", True, "ActionLanguage")
  76. // Association RHS_contains (RHS, PostElement) {}
  77. instantiate_link(new_model, "Association", "RHS_contains", "RHS", "PostElement")
  78. // Basics are added, now we need to add each node and transition, but slightly modified
  79. Element keys
  80. String key
  81. Element new_entry
  82. Element entry
  83. keys = set_to_list(dict_keys(old_m))
  84. String type_name
  85. String old_source
  86. String old_target
  87. String attr_name
  88. Element masked_attributes
  89. Element copied_attributes
  90. masked_attributes = create_node()
  91. set_add(masked_attributes, "constraint")
  92. set_add(masked_attributes, "lower_cardinality")
  93. set_add(masked_attributes, "source_lower_cardinality")
  94. set_add(masked_attributes, "target_lower_cardinality")
  95. copied_attributes = create_node()
  96. set_add(copied_attributes, "upper_cardinality")
  97. set_add(copied_attributes, "source_upper_cardinality")
  98. set_add(copied_attributes, "target_upper_cardinality")
  99. log("Start RAM")
  100. log("Set: " + list_to_string(keys))
  101. while (read_nr_out(keys) > 0):
  102. key = list_pop(keys, 0)
  103. entry = old_m[key]
  104. log("CHECK " + key)
  105. type_name = read_type(model, key)
  106. if (type_name == "Class"):
  107. instantiate_node(new_model, type_name, "Pre_" + key)
  108. instantiate_node(new_model, type_name, "Post_" + key)
  109. // Also make it inherit from the "root element"
  110. instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "PreElement")
  111. instantiate_link(new_model, "Inheritance", "", "Post_" + key, "PostElement")
  112. log("Added " + key)
  113. elif (type_name == "AttributeLink"):
  114. // Got an attribute, so find out the source and name
  115. log("Attribute link!")
  116. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  117. attr_name = read_attribute(model, key, "name")
  118. log("Search " + old_source)
  119. log("Got " + set_to_string(dict_keys(new_model["model"])))
  120. // TODO distinguish between Simple and Complex Attributes
  121. if (dict_in(new_model["model"], "Pre_" + old_source)):
  122. // Only if it is a class that it originates from is a class, should we add this
  123. if (is_nominal_instance(model, old_source, "Element")):
  124. // If the attribute is one used in the conformance check, we should mask it
  125. // Add the attribute in the new model, altering its name and making it optional
  126. log("Adding attribute " + attr_name)
  127. log(" to " + old_source)
  128. model_define_attribute(new_model, "Pre_" + old_source, attr_name + "_constraint", True, "ActionLanguage")
  129. model_define_attribute(new_model, "Post_" + old_source, attr_name + "_value", True, "ActionLanguage")
  130. else:
  131. log("Non-element origin for attribute link " + key)
  132. else:
  133. // Queue for later
  134. list_append(keys, key)
  135. elif (type_name == "Association"):
  136. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  137. old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
  138. if (bool_and(dict_in(new_model["model"], "Pre_" + old_source), dict_in(new_model["model"], "Pre_" + old_target))):
  139. instantiate_link(new_model, type_name, "Pre_" + key, "Pre_" + old_source, "Pre_" + old_target)
  140. instantiate_link(new_model, type_name, "Post_" + key, "Post_" + old_source, "Post_" + old_target)
  141. // Make it inherit from the Root Element (PreElement or PostElement)
  142. instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "PreElement")
  143. instantiate_link(new_model, "Inheritance", "", "Post_" + key, "PostElement")
  144. else:
  145. // Queue for later
  146. list_append(keys, key)
  147. log("Wait for S " + old_source)
  148. log("Wait for T " + old_target)
  149. log("Got: " + set_to_string(dict_keys(new_model["model"])))
  150. elif (type_name == "Inheritance"):
  151. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  152. old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
  153. if (bool_and(dict_in(new_model["model"], "Pre_" + old_source), dict_in(new_model["model"], "Pre_" + old_target))):
  154. instantiate_link(new_model, type_name, "Pre_" + key, "Pre_" + old_source, "Pre_" + old_target)
  155. instantiate_link(new_model, type_name, "Post_" + key, "Post_" + old_source, "Post_" + old_target)
  156. else:
  157. // Queue for later
  158. list_append(keys, key)
  159. elif (bool_not(has_value(entry))):
  160. type_name = read_type(model, key)
  161. // Primitive values themselves are not copied, so skip that here
  162. attr_name = read_attribute(model, type_name, "name")
  163. if (read_type(model["metamodel"], type_name) == "Attribute"):
  164. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  165. if (set_in(copied_attributes, attr_name)):
  166. log("Copy attribute " + attr_name)
  167. log(" to " + old_source)
  168. instantiate_attribute(new_model, "Pre_" + old_source, attr_name, read_attribute(model, old_source, attr_name))
  169. instantiate_attribute(new_model, "Post_" + old_source, attr_name, read_attribute(model, old_source, attr_name))
  170. else:
  171. log("Masked attribute: " + attr_name)
  172. log(" for " + old_source)
  173. else:
  174. log("DROP " + key)
  175. // Define schedule over these elements
  176. // Class Entry {}
  177. instantiate_node(new_model, "Class", "Entry")
  178. // Class Success : Entry {}
  179. instantiate_node(new_model, "Class", "Success")
  180. instantiate_link(new_model, "Inheritance", "", "Success", "Entry")
  181. // Class Failure : Entry {}
  182. instantiate_node(new_model, "Class", "Failure")
  183. instantiate_link(new_model, "Inheritance", "", "Failure", "Entry")
  184. // Class Rule : Entry {}
  185. instantiate_node(new_model, "Class", "Rule")
  186. instantiate_link(new_model, "Inheritance", "", "Rule", "Entry")
  187. // Association OnSuccess(Rule, Entry){
  188. // target_lower_cardinality = 1
  189. // target_upper_cardinality = 1
  190. // }
  191. instantiate_link(new_model, "Association", "OnSuccess", "Rule", "Entry")
  192. instantiate_attribute(new_model, "OnSuccess", "target_lower_cardinality", 1)
  193. instantiate_attribute(new_model, "OnSuccess", "target_upper_cardinality", 1)
  194. // Association OnFailure(Rule, Entry){
  195. // target_lower_cardinality = 1
  196. // target_upper_cardinality = 1
  197. // }
  198. instantiate_link(new_model, "Association", "OnFailure", "Rule", "Entry")
  199. instantiate_attribute(new_model, "OnFailure", "target_lower_cardinality", 1)
  200. instantiate_attribute(new_model, "OnFailure", "target_upper_cardinality", 1)
  201. // Class LHSRule : Rule {}
  202. instantiate_node(new_model, "Class", "LHSRule")
  203. instantiate_link(new_model, "Inheritance", "", "LHSRule", "Rule")
  204. // Association LHSLink (LHSRule, LHS) {
  205. // target_lower_cardinality = 1
  206. // target_upper_cardinality = 1
  207. // }
  208. instantiate_link(new_model, "Association", "LHSLink", "LHSRule", "LHS")
  209. instantiate_attribute(new_model, "LHSLink", "target_lower_cardinality", 1)
  210. instantiate_attribute(new_model, "LHSLink", "target_upper_cardinality", 1)
  211. // Association NACLink (LHSRule, NAC) {}
  212. instantiate_link(new_model, "Association", "NACLink", "LHSRule", "NAC")
  213. // Class RHSRule : Rule {}
  214. instantiate_node(new_model, "Class", "RHSRule")
  215. instantiate_link(new_model, "Inheritance", "", "RHSRule", "Rule")
  216. // Association RHSLink (RHSRule, RHS) {
  217. // target_lower_cardinality = 1
  218. // target_upper_cardinality = 1
  219. // }
  220. instantiate_link(new_model, "Association", "RHSLink", "RHSRule", "RHS")
  221. instantiate_attribute(new_model, "RHSLink", "target_lower_cardinality", 1)
  222. instantiate_attribute(new_model, "RHSLink", "target_upper_cardinality", 1)
  223. // Class Query : LHSRule {}
  224. instantiate_node(new_model, "Class", "Query")
  225. instantiate_link(new_model, "Inheritance", "", "Query", "LHSRule")
  226. // Class Atomic : LHSRule, RHSRule {}
  227. instantiate_node(new_model, "Class", "Atomic")
  228. instantiate_link(new_model, "Inheritance", "", "Atomic", "LHSRule")
  229. instantiate_link(new_model, "Inheritance", "", "Atomic", "RHSRule")
  230. // Class ForAll : LHSRule, RHSRule {}
  231. instantiate_node(new_model, "Class", "ForAll")
  232. instantiate_link(new_model, "Inheritance", "", "ForAll", "LHSRule")
  233. instantiate_link(new_model, "Inheritance", "", "ForAll", "RHSRule")
  234. // Class Composite : Rule {}
  235. instantiate_node(new_model, "Class", "Composite")
  236. instantiate_link(new_model, "Inheritance", "", "Composite", "Rule")
  237. // Association Initial(Composite, Entry){
  238. // target_lower_cardinality = 1
  239. // target_upper_cardinality = 1
  240. // }
  241. instantiate_link(new_model, "Association", "Initial", "Composite", "Entry")
  242. instantiate_attribute(new_model, "Initial", "target_lower_cardinality", 1)
  243. instantiate_attribute(new_model, "Initial", "target_upper_cardinality", 1)
  244. // Association Contains(Composite, Entry){
  245. // source_upper_cardinality = 1
  246. // target_lower_cardinality = 1
  247. // }
  248. instantiate_link(new_model, "Association", "Contains", "Composite", "Entry")
  249. instantiate_attribute(new_model, "Contains", "source_upper_cardinality", 1)
  250. instantiate_attribute(new_model, "Contains", "target_lower_cardinality", 1)
  251. return new_model!