model_management.alc 4.6 KB

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