ramify.alc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. include "primitives.alh"
  2. include "object_operations.alh"
  3. include "modelling.alh"
  4. include "metamodels.alh"
  5. Element function ramify_func(model : Element, prepost : String):
  6. // Create new model structure
  7. Element new_model
  8. new_model = create_node()
  9. dict_add(new_model, "model", create_node())
  10. dict_add(new_model, "type_mapping", create_node())
  11. dict_add(new_model, "metamodel", model["metamodel"])
  12. dict_add(new_model, "inheritance", model["inheritance"])
  13. // Get local variables for parts
  14. Element old_m
  15. Element new_m
  16. Element mm
  17. String inheritor
  18. new_m = new_model["model"]
  19. old_m = model["model"]
  20. mm = new_model["metamodel"]["model"]
  21. Boolean is_pre
  22. is_pre = (prepost == "pre")
  23. // Add in some primitives
  24. instantiate_node(new_model, "Class", "Natural")
  25. instantiate_node(new_model, "Class", "String")
  26. instantiate_node(new_model, "Class", "Any")
  27. // Add in the complete AL metamodel
  28. add_AL_to_MM(new_model)
  29. // Add some default elements
  30. if (is_pre):
  31. // Class LHS {
  32. // constraint : funcdef {
  33. // target_upper_cardinality = 1
  34. // }
  35. // upper_cardinality = 1
  36. // lower_cardinality = 1
  37. // }
  38. instantiate_node(new_model, "Class", "LHS")
  39. instantiate_attribute(new_model, "LHS", "lower_cardinality", 1)
  40. instantiate_attribute(new_model, "LHS", "upper_cardinality", 1)
  41. instantiate_link(new_model, "Association", "LHS_constraint", "LHS", "funcdef")
  42. instantiate_attribute(new_model, "LHS_constraint", "name", "constraint")
  43. instantiate_attribute(new_model, "LHS_constraint", "target_upper_cardinality", 1)
  44. // Class NAC {}
  45. //instantiate_node(new_model, "Class", "NAC")
  46. // Class Pre_Element {
  47. // label : String {
  48. // target_lower_cardinality = 1
  49. // target_upper_cardinality = 1
  50. // }
  51. // constraint : funcdef {
  52. // target_upper_cardinality = 1
  53. // }
  54. // }
  55. instantiate_node(new_model, "Class", "Pre_Element")
  56. instantiate_link(new_model, "Association", "Pre_Element_label", "Pre_Element", "String")
  57. instantiate_attribute(new_model, "Pre_Element_label", "name", "label")
  58. instantiate_attribute(new_model, "Pre_Element_label", "target_lower_cardinality", 1)
  59. instantiate_attribute(new_model, "Pre_Element_label", "target_upper_cardinality", 1)
  60. instantiate_link(new_model, "Association", "Pre_Element_constraint", "Pre_Element", "funcdef")
  61. instantiate_attribute(new_model, "Pre_Element_constraint", "name", "constraint")
  62. instantiate_attribute(new_model, "Pre_Element_constraint", "target_upper_cardinality", 1)
  63. // Association LHS_contains (LHS, Pre_Element) {}
  64. instantiate_link(new_model, "Association", "LHS_contains", "LHS", "Pre_Element")
  65. instantiate_attribute(new_model, "LHS_contains", "name", "contains")
  66. // Association NAC_contains (NAC, Pre_Element) {}
  67. //instantiate_link(new_model, "Association", "NAC_contains", "NAC", "Pre_Element")
  68. //instantiate_attribute(new_model, "NAC_contains", "name", "contains")
  69. inheritor = "Pre_Element"
  70. else:
  71. // Class RHS {
  72. // action : FuncDef {
  73. // target_upper_cardinality = 1
  74. // }
  75. // upper_cardinality = 1
  76. // lower_cardinality = 1
  77. // }
  78. instantiate_node(new_model, "Class", "RHS")
  79. instantiate_attribute(new_model, "RHS", "lower_cardinality", 1)
  80. instantiate_attribute(new_model, "RHS", "upper_cardinality", 1)
  81. instantiate_link(new_model, "Association", "RHS_action", "RHS", "funcdef")
  82. instantiate_attribute(new_model, "RHS_action", "name", "action")
  83. instantiate_attribute(new_model, "RHS_action", "target_upper_cardinality", 1)
  84. // Class Post_Element {
  85. // label : String {
  86. // target_lower_cardinality = 1
  87. // target_upper_cardinality = 1
  88. // }
  89. // value : funcdef {
  90. // target_upper_cardinality = 1
  91. // }
  92. // action : funcdef {
  93. // target_upper_cardinality = 1
  94. // }
  95. // }
  96. instantiate_node(new_model, "Class", "Post_Element")
  97. instantiate_link(new_model, "Association", "Post_Element_label", "Post_Element", "String")
  98. instantiate_attribute(new_model, "Post_Element_label", "name", "label")
  99. instantiate_attribute(new_model, "Post_Element_label", "target_lower_cardinality", 1)
  100. instantiate_attribute(new_model, "Post_Element_label", "target_upper_cardinality", 1)
  101. instantiate_link(new_model, "Association", "Post_Element_value", "Post_Element", "funcdef")
  102. instantiate_attribute(new_model, "Post_Element_value", "name", "value")
  103. instantiate_attribute(new_model, "Post_Element_value", "target_upper_cardinality", 1)
  104. instantiate_link(new_model, "Association", "Post_Element_action", "Post_Element", "funcdef")
  105. instantiate_attribute(new_model, "Post_Element_action", "name", "action")
  106. instantiate_attribute(new_model, "Post_Element_action", "target_upper_cardinality", 1)
  107. // Association RHS_contains (RHS, Post_Element) {}
  108. instantiate_link(new_model, "Association", "RHS_contains", "RHS", "Post_Element")
  109. instantiate_attribute(new_model, "RHS_contains", "name", "contains")
  110. inheritor = "Post_Element"
  111. // Basics are added, now we need to add each node and transition, but slightly modified
  112. Element keys
  113. String key
  114. Element new_entry
  115. Element entry
  116. String append
  117. if (is_pre):
  118. append = "Pre_"
  119. else:
  120. append = "Post_"
  121. keys = dict_keys(old_m)
  122. String type_name
  123. String old_source
  124. String old_target
  125. Element to_link
  126. to_link = create_node()
  127. while (read_nr_out(keys) > 0):
  128. key = set_pop(keys)
  129. entry = old_m[key]
  130. type_name = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], entry))
  131. if (type_name == "Class"):
  132. instantiate_node(new_model, type_name, append + key)
  133. // Also make it inherit from the "root element"
  134. instantiate_link(new_model, "Inheritance", "", append + key, inheritor)
  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. instantiate_link(new_model, type_name, append + key, append + old_source, append + old_target)
  139. // 1) Make it inherit from the Root Element (Pre_Element or Post_Element)
  140. instantiate_link(new_model, "Inheritance", "", append + key, inheritor)
  141. elif (type_name == "Inheritance"):
  142. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  143. old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
  144. instantiate_link(new_model, type_name, append + key, append + old_source, append + old_target)
  145. elif (bool_not(has_value(entry))):
  146. create_edge(to_link, entry)
  147. while (read_nr_out(to_link) > 0):
  148. entry = set_pop(to_link)
  149. type_name = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], entry))
  150. // Primitive values themselves are not copied, so skip that here
  151. // An instance link, so just copy over (don't make element of Root Element), and don't even copy if it is lower cardinality
  152. // It is probably an attribute at the meta-language level, so check which one it is
  153. String name
  154. name = read_attribute(model["metamodel"], type_name, "name")
  155. if (bool_not(bool_or(bool_or(name == "lower_cardinality", name == "source_lower_cardinality"), bool_or(name == "constraint", name == "target_lower_cardinality")))):
  156. // Not a lower cardinality or constraint, so copy
  157. old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
  158. if (bool_or(bool_or(name == "name", name == "upper_cardinality"), bool_or(name == "source_upper_cardinality", name == "target_upper_cardinality"))):
  159. // Keep the old name for the physical attributes!
  160. instantiate_attribute(new_model, append + old_source, name, read_edge_dst(entry))
  161. else:
  162. // Rename the attributes as well
  163. instantiate_attribute(new_model, append + old_source, append + name, read_edge_dst(entry))
  164. return new_model!
  165. Element function ramify(model : Element):
  166. Element rv
  167. rv = create_node()
  168. // Create Pre part
  169. dict_add(rv, "pre", ramify_func(model, "pre"))
  170. // Create Post part
  171. dict_add(rv, "post", ramify_func(model, "post"))
  172. return rv!