model_management.alc 4.8 KB

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