ramify.alc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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, "SimplAttribute", "Natural")
  24. instantiate_node(new_model, "SimplAttribute", "String")
  25. instantiate_node(new_model, "SimplAttribute", "Boolean")
  26. instantiate_node(new_model, "SimplAttribute", "Element")
  27. // Add in the complete AL metamodel
  28. add_AL_to_MM(new_model)
  29. // Add some default elements
  30. // Class LHS_Root {
  31. // constraint? : funcdef
  32. // upper_cardinality = 1
  33. // lower_cardinality = 1
  34. // }
  35. instantiate_node(new_model, "Class", "LHS_Root")
  36. instantiate_attribute(new_model, "LHS_Root", "lower_cardinality", 1)
  37. instantiate_attribute(new_model, "LHS_Root", "upper_cardinality", 1)
  38. model_define_attribute(new_model, "LHS_Root", "LHS_constraint", True, "funcdef")
  39. // Class LHS : LHS_Root {}
  40. instantiate_node(new_model, "Class", "LHS")
  41. instantiate_link(new_model, "Inheritance", "", "LHS", "LHS_Root")
  42. // Class NAC : LHS_Root {}
  43. instantiate_node(new_model, "Class", "NAC")
  44. instantiate_link(new_model, "Inheritance", "", "NAC", "LHS_Root")
  45. // Class PreElement {
  46. // label : String
  47. // constraint? : funcdef
  48. // }
  49. instantiate_node(new_model, "Class", "PreElement")
  50. model_define_attribute(new_model, "PreElement", "label", False, "String")
  51. model_define_attribute(new_model, "PreElement", "matching_constraint", True, "funcdef")
  52. // Association LHS_contains (LHS_Root, PreElement) {}
  53. instantiate_link(new_model, "Association", "LHS_contains", "LHS_Root", "PreElement")
  54. // Class RHS {
  55. // action? : FuncDef
  56. // upper_cardinality = 1
  57. // lower_cardinality = 1
  58. // }
  59. instantiate_node(new_model, "Class", "RHS")
  60. instantiate_attribute(new_model, "RHS", "lower_cardinality", 1)
  61. instantiate_attribute(new_model, "RHS", "upper_cardinality", 1)
  62. model_define_attribute(new_model, "RHS", "action", True, "funcdef")
  63. // Class PostElement {
  64. // label : String
  65. // action? : funcdef
  66. // }
  67. instantiate_node(new_model, "Class", "PostElement")
  68. model_define_attribute(new_model, "PostElement", "label", False, "String")
  69. model_define_attribute(new_model, "PostElement", "action", True, "funcdef")
  70. // Association RHS_contains (RHS, PostElement) {}
  71. instantiate_link(new_model, "Association", "RHS_contains", "RHS", "PostElement")
  72. // Basics are added, now we need to add each node and transition, but slightly modified
  73. Element keys
  74. String key
  75. Element new_entry
  76. Element entry
  77. keys = set_to_list(dict_keys(old_m))
  78. String type_name
  79. String old_source
  80. String old_target
  81. String attr_name
  82. Element masked_attributes
  83. Element copied_attributes
  84. masked_attributes = create_node()
  85. set_add(masked_attributes, "constraint")
  86. set_add(masked_attributes, "lower_cardinality")
  87. set_add(masked_attributes, "source_lower_cardinality")
  88. set_add(masked_attributes, "target_lower_cardinality")
  89. copied_attributes = create_node()
  90. set_add(copied_attributes, "upper_cardinality")
  91. set_add(copied_attributes, "source_upper_cardinality")
  92. set_add(copied_attributes, "target_upper_cardinality")
  93. while (read_nr_out(keys) > 0):
  94. key = list_pop(keys, read_nr_out(keys) - 1)
  95. entry = old_m[key]
  96. type_name = read_type(model, key)
  97. if (type_name == "Class"):
  98. instantiate_node(new_model, type_name, "Pre_" + key)
  99. instantiate_node(new_model, type_name, "Post_" + key)
  100. // Also make it inherit from the "root element"
  101. instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "PreElement")
  102. instantiate_link(new_model, "Inheritance", "", "Post_" + key, "PostElement")
  103. elif (type_name == "Attribute"):
  104. // Got an attribute, so find out the source and name
  105. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  106. attr_name = read_attribute(model, key, "name")
  107. if (set_in(new_model["model"], "Pre_" + old_source)):
  108. // Only if it is a class that it originates from is a class, should we add this
  109. if (read_type(model, old_source) == "Class"):
  110. // If the attribute is one used in the conformance check, we should mask it
  111. // Add the attribute in the new model, altering its name and making it optional
  112. log("Adding attribute " + attr_name)
  113. log(" to " + old_source)
  114. model_define_attribute(new_model, "Pre_" + old_source, attr_name + "_constraint", True, "funcdef")
  115. model_define_attribute(new_model, "Post_" + old_source, attr_name + "_value", True, "funcdef")
  116. else:
  117. log("Non-class origin for attribute link " + key)
  118. else:
  119. // Queue for later
  120. list_append(keys, key)
  121. elif (type_name == "Association"):
  122. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  123. old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
  124. if (bool_and(set_in(new_model["model"], old_source), set_in(new_model["model"], old_target))):
  125. instantiate_link(new_model, type_name, "Pre_" + key, "Pre_" + old_source, "Pre_" + old_target)
  126. instantiate_link(new_model, type_name, "Post_" + key, "Post_" + old_source, "Post_" + old_target)
  127. // Make it inherit from the Root Element (PreElement or PostElement)
  128. instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "PreElement")
  129. instantiate_link(new_model, "Inheritance", "", "Post_" + key, "PostElement")
  130. else:
  131. // Queue for later
  132. list_append(keys, key)
  133. elif (type_name == "Inheritance"):
  134. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  135. old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
  136. if (bool_and(set_in(new_model["model"], old_source), set_in(new_model["model"], old_target))):
  137. instantiate_link(new_model, type_name, "Pre_" + key, "Pre_" + old_source, "Pre_" + old_target)
  138. instantiate_link(new_model, type_name, "Post_" + key, "Post_" + old_source, "Post_" + old_target)
  139. else:
  140. // Queue for later
  141. list_append(keys, key)
  142. elif (bool_not(has_value(entry))):
  143. type_name = read_type(model, key)
  144. // Primitive values themselves are not copied, so skip that here
  145. attr_name = read_attribute(model, type_name, "name")
  146. if (read_type(model["metamodel"], type_name) == "Attribute"):
  147. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  148. if (set_in(copied_attributes, attr_name)):
  149. log("Copy attribute " + attr_name)
  150. log(" to " + old_source)
  151. instantiate_attribute(new_model, "Pre_" + old_source, attr_name, read_attribute(model, old_source, attr_name))
  152. instantiate_attribute(new_model, "Post_" + old_source, attr_name, read_attribute(model, old_source, attr_name))
  153. else:
  154. log("Masked attribute: " + attr_name)
  155. log(" for " + old_source)
  156. // Define schedule over these elements
  157. // Class Entry {}
  158. instantiate_node(new_model, "Class", "Entry")
  159. // Class Success : Entry {}
  160. instantiate_node(new_model, "Class", "Success")
  161. instantiate_link(new_model, "Inheritance", "", "Success", "Entry")
  162. // Class Failure : Entry {}
  163. instantiate_node(new_model, "Class", "Failure")
  164. instantiate_link(new_model, "Inheritance", "", "Failure", "Entry")
  165. // Class Rule : Entry {}
  166. instantiate_node(new_model, "Class", "Rule")
  167. instantiate_link(new_model, "Inheritance", "", "Rule", "Entry")
  168. // Association OnSuccess(Rule, Entry){
  169. // target_lower_cardinality = 1
  170. // target_upper_cardinality = 1
  171. // }
  172. instantiate_link(new_model, "Association", "OnSuccess", "Rule", "Entry")
  173. instantiate_attribute(new_model, "OnSuccess", "target_lower_cardinality", 1)
  174. instantiate_attribute(new_model, "OnSuccess", "target_upper_cardinality", 1)
  175. // Association OnFailure(Rule, Entry){
  176. // target_lower_cardinality = 1
  177. // target_upper_cardinality = 1
  178. // }
  179. instantiate_link(new_model, "Association", "OnFailure", "Rule", "Entry")
  180. instantiate_attribute(new_model, "OnFailure", "target_lower_cardinality", 1)
  181. instantiate_attribute(new_model, "OnFailure", "target_upper_cardinality", 1)
  182. // Class LHSRule : Rule {}
  183. instantiate_node(new_model, "Class", "LHSRule")
  184. instantiate_link(new_model, "Inheritance", "", "LHSRule", "Rule")
  185. // Association LHSLink (LHSRule, LHS) {
  186. // target_lower_cardinality = 1
  187. // target_upper_cardinality = 1
  188. // }
  189. instantiate_link(new_model, "Association", "LHSLink", "LHSRule", "LHS")
  190. instantiate_attribute(new_model, "LHSLink", "target_lower_cardinality", 1)
  191. instantiate_attribute(new_model, "LHSLink", "target_upper_cardinality", 1)
  192. // Association NACLink (LHSRule, NAC) {}
  193. instantiate_link(new_model, "Association", "NACLink", "LHSRule", "NAC")
  194. // Class RHSRule : Rule {}
  195. instantiate_node(new_model, "Class", "RHSRule")
  196. instantiate_link(new_model, "Inheritance", "", "RHSRule", "Rule")
  197. // Association RHSLink (RHSRule, RHS) {
  198. // target_lower_cardinality = 1
  199. // target_upper_cardinality = 1
  200. // }
  201. instantiate_link(new_model, "Association", "RHSLink", "RHSRule", "RHS")
  202. instantiate_attribute(new_model, "RHSLink", "target_lower_cardinality", 1)
  203. instantiate_attribute(new_model, "RHSLink", "target_upper_cardinality", 1)
  204. // Class Query : LHSRule {}
  205. instantiate_node(new_model, "Class", "Query")
  206. instantiate_link(new_model, "Inheritance", "", "Query", "LHSRule")
  207. // Class Atomic : LHSRule, RHSRule {}
  208. instantiate_node(new_model, "Class", "Atomic")
  209. instantiate_link(new_model, "Inheritance", "", "Atomic", "LHSRule")
  210. instantiate_link(new_model, "Inheritance", "", "Atomic", "RHSRule")
  211. // Class ForAll : LHSRule, RHSRule {}
  212. instantiate_node(new_model, "Class", "ForAll")
  213. instantiate_link(new_model, "Inheritance", "", "ForAll", "LHSRule")
  214. instantiate_link(new_model, "Inheritance", "", "ForAll", "RHSRule")
  215. // Class Composite : Rule {}
  216. instantiate_node(new_model, "Class", "Composite")
  217. instantiate_link(new_model, "Inheritance", "", "Composite", "Rule")
  218. // Association Initial(Composite, Entry){
  219. // target_lower_cardinality = 1
  220. // target_upper_cardinality = 1
  221. // }
  222. instantiate_link(new_model, "Association", "Initial", "Composite", "Entry")
  223. instantiate_attribute(new_model, "Initial", "target_lower_cardinality", 1)
  224. instantiate_attribute(new_model, "Initial", "target_upper_cardinality", 1)
  225. // Association Contains(Composite, Entry){
  226. // source_upper_cardinality = 1
  227. // target_lower_cardinality = 1
  228. // }
  229. instantiate_link(new_model, "Association", "Contains", "Composite", "Entry")
  230. instantiate_attribute(new_model, "Contains", "source_upper_cardinality", 1)
  231. instantiate_attribute(new_model, "Contains", "target_lower_cardinality", 1)
  232. return new_model!