model_management.alc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. Element models
  18. // Read out some data first
  19. tagged_model = set_pop(models)
  20. set_add(models, tagged_model)
  21. new_model = instantiate_model(tagged_model[1])
  22. // Do the iteration
  23. while (read_nr_out(models)):
  24. tagged_model = set_pop(models)
  25. model_name = list_read(tagged_model, 0)
  26. model = list_read(tagged_model, 1)
  27. // Add all elements from 'model', but prepend it with the 'model_name'
  28. keys = set_to_list(dict_keys(model["model"]))
  29. while (read_nr_out(keys) > 0):
  30. key = list_pop(keys, 0)
  31. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  32. if (is_edge(model["model"][key])):
  33. String src
  34. String dst
  35. src = model_name + reverseKeyLookup(model["model"], read_edge_src(model["model"][key]))
  36. dst = model_name + reverseKeyLookup(model["model"], read_edge_dst(model["model"][key]))
  37. if (bool_and(dict_in(new_model["model"], src), dict_in(new_model["model"], dst))):
  38. instantiate_link(new_model, type, model_name + key, src, dst)
  39. else:
  40. list_append(keys, key)
  41. elif (has_value(model["model"][key])):
  42. instantiate_value(new_model, type, model_name + key, model["model"][key])
  43. else:
  44. instantiate_node(new_model, type, model_name + key)
  45. return new_model!
  46. Element function model_copy(src_model : Element):
  47. Element dst_model
  48. Element queue
  49. Element name
  50. String type
  51. dst_model = instantiate_model(src_model["metamodel"])
  52. dict_add(dst_model, "model", create_node())
  53. dict_add(dst_model, "type_mapping", create_node())
  54. queue = set_to_list(dict_keys(src_model["model"]))
  55. while (read_nr_out(queue) > 0):
  56. name = list_pop(queue, 0)
  57. if (is_edge(src_model["model"][name])):
  58. // Is an edge, so potentially queue it
  59. String src
  60. String dst
  61. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  62. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  63. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  64. if (bool_and(dict_in(dst_model["model"], src), dict_in(dst_model["model"], dst))):
  65. // All present, so create the link between them
  66. instantiate_link(dst_model, type, name, src, dst)
  67. else:
  68. list_append(queue, name)
  69. elif (has_value(src_model["model"][name])):
  70. // Has a value, so copy that as well
  71. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  72. instantiate_value(dst_model, type, name, src_model["model"][name])
  73. else:
  74. // Is a node
  75. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  76. instantiate_node(dst_model, type, name)
  77. return dst_model!
  78. Element function model_retype_on_name(model : Element, new_MM : Element, operation : String, name : String):
  79. String key
  80. String type
  81. Element keys
  82. Integer length
  83. keys = dict_keys(model["model"])
  84. length = string_len(name)
  85. while (read_nr_out(keys) > 0):
  86. key = set_pop(keys)
  87. if (dict_in(model["model"], key)):
  88. // Check if the element is still there, as a delete of a node might remove all attached links automatically
  89. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  90. if (operation == "+"):
  91. // Keep all, but augment typename
  92. dict_delete_node(model["type_mapping"], model["model"][key])
  93. dict_add(model["type_mapping"], model["model"][key], new_MM["model"][name + type])
  94. elif (operation == "-"):
  95. // Keep only if typename beginning matches and remove from typename
  96. if (string_startswith(type, name)):
  97. dict_delete_node(model["type_mapping"], model["model"][key])
  98. dict_add(model["type_mapping"], model["model"][key], new_MM["model"][string_substr(type, length, string_len(type))])
  99. else:
  100. model_delete_element(model, key)
  101. dict_delete(model, "metamodel")
  102. dict_add(model, "metamodel", new_MM)
  103. return model!
  104. Void function model_join(dst_model : Element, src_model : Element, retyping_key : String):
  105. Element queue
  106. Element mapping
  107. String name
  108. String type
  109. String src
  110. String dst
  111. queue = set_to_list(dict_keys(src_model["model"]))
  112. mapping = create_node()
  113. while (read_nr_out(queue) > 0):
  114. name = list_pop(queue, 0)
  115. if (is_edge(src_model["model"][name])):
  116. // Is an edge, so potentially queue it
  117. String src
  118. String dst
  119. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  120. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  121. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  122. if (bool_and(dict_in(mapping, src), dict_in(mapping, dst))):
  123. // All present, so create the link between them
  124. dict_add(mapping, name, instantiate_link(dst_model, retyping_key + type, "", mapping[src], mapping[dst]))
  125. else:
  126. list_append(queue, name)
  127. elif (has_value(src_model["model"][name])):
  128. // Has a value, so copy that as well
  129. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  130. dict_add(mapping, name, instantiate_value(dst_model, retyping_key + type, "", src_model["model"][name]))
  131. else:
  132. // Is a node
  133. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  134. dict_add(mapping, name, instantiate_node(dst_model, retyping_key + type, ""))
  135. return!
  136. Element function model_split(source_model : Element, target_metamodel : Element, retyping_key : String):
  137. Element model
  138. return model!