model_management.alc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, "inheritance", src_model["inheritance"])
  62. dict_add(dst_model, "model", create_node())
  63. dict_add(dst_model, "type_mapping", create_node())
  64. queue = set_to_list(dict_keys(src_model["model"]))
  65. while (read_nr_out(queue) > 0):
  66. name = list_pop(queue, 0)
  67. if (is_edge(src_model["model"][name])):
  68. // Is an edge, so potentially queue it
  69. String src
  70. String dst
  71. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  72. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  73. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  74. if (bool_and(dict_in(dst_model["model"], src), dict_in(dst_model["model"], dst))):
  75. // All present, so create the link between them
  76. instantiate_link(dst_model, type, name, src, dst)
  77. else:
  78. list_append(queue, name)
  79. elif (has_value(src_model["model"][name])):
  80. // Has a value, so copy that as well
  81. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  82. instantiate_value(dst_model, type, name, src_model["model"][name])
  83. else:
  84. // Is a node
  85. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  86. instantiate_node(dst_model, type, name)
  87. return dst_model!
  88. Element function model_retype_on_name(model : Element, new_MM : Element, operation : String, name : String):
  89. String key
  90. String type
  91. Element keys
  92. Integer length
  93. keys = dict_keys(model["model"])
  94. length = string_len(name)
  95. while (read_nr_out(keys) > 0):
  96. key = set_pop(keys)
  97. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  98. if (operation == "+"):
  99. // Keep all, but augment typename
  100. dict_delete_node(model["type_mapping"], model["model"][key])
  101. dict_add(model["type_mapping"], model["model"][key], model["metamodel"]["model"][name + type])
  102. elif (operation == "-"):
  103. // Keep only if typename beginning matches and remove from typename
  104. if (string_startswith(type, name)):
  105. dict_delete_node(model["type_mapping"], model["model"][key])
  106. dict_add(model["type_mapping"], model["model"][key], model["metamodel"]["model"][string_substr(type, length, string_len(type))])
  107. else:
  108. model_delete_element(model, key)
  109. return model!