model_management.alc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. include "primitives.alh"
  2. include "io.alh"
  3. include "object_operations.alh"
  4. include "constructors.alh"
  5. include "metamodels.alh"
  6. include "library.alh"
  7. include "modelling.alh"
  8. Element function model_fuse(models : Element):
  9. Element new_model
  10. Element tagged_model
  11. String model_name
  12. Element model
  13. Element keys
  14. String key
  15. Element selected_MM
  16. String type
  17. // Read out some data first
  18. tagged_model = set_pop(models)
  19. set_add(models, tagged_model)
  20. new_model = instantiate_model(tagged_model[1]["metamodel"])
  21. // Do the iteration
  22. while (read_nr_out(models)):
  23. tagged_model = set_pop(models)
  24. model_name = string_join(list_read(tagged_model, 0), "/")
  25. model = list_read(tagged_model, 1)
  26. // Add all elements from 'model', but prepend it with the 'model_name'
  27. keys = set_to_list(dict_keys(model["model"]))
  28. while (read_nr_out(keys) > 0):
  29. key = list_pop(keys, 0)
  30. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  31. if (is_edge(model["model"][key])):
  32. String src
  33. String dst
  34. src = model_name + reverseKeyLookup(model["model"], read_edge_src(model["model"][key]))
  35. dst = model_name + reverseKeyLookup(model["model"], read_edge_dst(model["model"][key]))
  36. if (bool_and(dict_in(new_model["model"], src), dict_in(new_model["model"], dst))):
  37. instantiate_link(new_model, type, model_name + key, src, dst)
  38. else:
  39. list_append(keys, key)
  40. elif (has_value(model["model"][key])):
  41. instantiate_value(new_model, type, model_name + key, model["model"][key])
  42. else:
  43. instantiate_node(new_model, type, model_name + key)
  44. return new_model!
  45. Element function model_copy(src_model : Element):
  46. Element dst_model
  47. Element queue
  48. Element name
  49. String type
  50. dst_model = instantiate_model(src_model["metamodel"])
  51. dict_add(dst_model, "model", create_node())
  52. dict_add(dst_model, "type_mapping", create_node())
  53. queue = set_to_list(dict_keys(src_model["model"]))
  54. while (read_nr_out(queue) > 0):
  55. name = list_pop(queue, 0)
  56. if (is_edge(src_model["model"][name])):
  57. // Is an edge, so potentially queue it
  58. String src
  59. String dst
  60. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  61. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  62. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  63. if (bool_and(dict_in(dst_model["model"], src), dict_in(dst_model["model"], dst))):
  64. // All present, so create the link between them
  65. instantiate_link(dst_model, type, name, src, dst)
  66. else:
  67. list_append(queue, name)
  68. elif (has_value(src_model["model"][name])):
  69. // Has a value, so copy that as well
  70. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  71. instantiate_value(dst_model, type, name, src_model["model"][name])
  72. else:
  73. // Is a node
  74. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  75. instantiate_node(dst_model, type, name)
  76. return dst_model!
  77. Element function model_retype_on_name(model : Element, new_MM : Element, operation : String, name : String):
  78. String key
  79. String type
  80. Element keys
  81. Integer length
  82. keys = dict_keys(model["model"])
  83. length = string_len(name)
  84. while (read_nr_out(keys) > 0):
  85. key = set_pop(keys)
  86. if (dict_in(model["model"], key)):
  87. // Check if the element is still there, as a delete of a node might remove all attached links automatically
  88. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  89. if (operation == "+"):
  90. // Keep all, but augment typename
  91. dict_delete_node(model["type_mapping"], model["model"][key])
  92. dict_add(model["type_mapping"], model["model"][key], new_MM["model"][name + type])
  93. elif (operation == "-"):
  94. // Keep only if typename beginning matches and remove from typename
  95. if (string_startswith(type, name)):
  96. dict_delete_node(model["type_mapping"], model["model"][key])
  97. dict_add(model["type_mapping"], model["model"][key], new_MM["model"][string_substr(type, length, string_len(type))])
  98. else:
  99. model_delete_element(model, key)
  100. dict_delete(model, "metamodel")
  101. dict_add(model, "metamodel", new_MM)
  102. return model!
  103. Void function model_join(dst_model : Element, src_model : Element, retyping_key : String):
  104. Element queue
  105. Element mapping
  106. String name
  107. String type
  108. String src
  109. String dst
  110. queue = set_to_list(dict_keys(src_model["model"]))
  111. mapping = create_node()
  112. while (read_nr_out(queue) > 0):
  113. name = list_pop(queue, 0)
  114. if (is_edge(src_model["model"][name])):
  115. // Is an edge, so potentially queue it
  116. String src
  117. String dst
  118. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  119. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  120. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  121. if (bool_and(dict_in(mapping, src), dict_in(mapping, dst))):
  122. // All present, so create the link between them
  123. dict_add(mapping, name, instantiate_link(dst_model, retyping_key + type, "", mapping[src], mapping[dst]))
  124. else:
  125. list_append(queue, name)
  126. elif (has_value(src_model["model"][name])):
  127. // Has a value, so copy that as well
  128. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  129. dict_add(mapping, name, instantiate_value(dst_model, retyping_key + type, "", src_model["model"][name]))
  130. else:
  131. // Is a node
  132. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  133. dict_add(mapping, name, instantiate_node(dst_model, retyping_key + type, ""))
  134. return!
  135. Element function model_split(src_model : Element, target_metamodel : Element, retyping_key : String):
  136. Element dst_model
  137. dst_model = instantiate_model(target_metamodel)
  138. Element queue
  139. Element mapping
  140. String name
  141. String type
  142. String src
  143. String dst
  144. Integer length
  145. queue = set_to_list(dict_keys(src_model["model"]))
  146. mapping = create_node()
  147. length = string_len(retyping_key)
  148. while (read_nr_out(queue) > 0):
  149. name = list_pop(queue, 0)
  150. if (is_edge(src_model["model"][name])):
  151. // Is an edge, so potentially queue it
  152. String src
  153. String dst
  154. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  155. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  156. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  157. if (bool_and(dict_in(mapping, src), dict_in(mapping, dst))):
  158. // All present, so create the link between them
  159. dict_add(mapping, name, instantiate_link(dst_model, string_substr(type, length, string_len(type)), "", mapping[src], mapping[dst]))
  160. else:
  161. list_append(queue, name)
  162. elif (has_value(src_model["model"][name])):
  163. // Has a value, so copy that as well
  164. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  165. dict_add(mapping, name, instantiate_value(dst_model, string_substr(type, length, string_len(type)), "", src_model["model"][name]))
  166. else:
  167. // Is a node
  168. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  169. dict_add(mapping, name, instantiate_node(dst_model, string_substr(type, length, string_len(type)), ""))
  170. return dst_model!