ramify.alc 11 KB

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