ramify.alc 12 KB

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