model_management.alc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. model = list_read(tagged_model, 1)
  21. selected_MM = model["metamodel"]
  22. new_model = instantiate_model(selected_MM)
  23. models = set_copy(models)
  24. while (read_nr_out(models)):
  25. tagged_model = set_pop(models)
  26. model_name = list_read(tagged_model, 0)
  27. model = list_read(tagged_model, 1)
  28. // Add all elements from 'model', but prepend it with the 'model_name'
  29. keys = set_to_list(dict_keys(model["model"]))
  30. while (read_nr_out(keys) > 0):
  31. key = list_pop(keys, 0)
  32. type = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][key]))
  33. if (is_edge(model["model"][key])):
  34. String src
  35. String dst
  36. src = (model_name + "/") + reverseKeyLookup(model["model"], read_edge_src(model["model"][key]))
  37. dst = (model_name + "/") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][key]))
  38. if (bool_and(dict_in(new_model["model"], src), dict_in(new_model["model"], dst))):
  39. instantiate_link(new_model, type, (model_name + "/") + key, (model_name + "/") + src, (model_name + "/") + dst)
  40. else:
  41. list_append(keys, key)
  42. elif (has_value(model["model"][key])):
  43. instantiate_value(new_model, type, (model_name + "/") + key, model["model"][key])
  44. else:
  45. instantiate_node(new_model, type, (model_name + "/") + key)
  46. return new_model!
  47. Element function model_copy(src_model : Element):
  48. Element dst_model
  49. Element queue
  50. Element name
  51. String type
  52. dst_model = instantiate_model(src_model["metamodel"])
  53. dict_add(dst_model, "inheritance", src_model["inheritance"])
  54. dict_add(dst_model, "model", create_node())
  55. dict_add(dst_model, "type_mapping", create_node())
  56. queue = set_to_list(dict_keys(src_model["model"]))
  57. while (read_nr_out(queue) > 0):
  58. name = list_pop(queue, 0)
  59. if (is_edge(src_model["model"][name])):
  60. // Is an edge, so potentially queue it
  61. String src
  62. String dst
  63. src = reverseKeyLookup(src_model["model"], read_edge_src(src_model["model"][name]))
  64. dst = reverseKeyLookup(src_model["model"], read_edge_dst(src_model["model"][name]))
  65. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  66. if (bool_and(dict_in(dst_model["model"], src), dict_in(dst_model["model"], dst))):
  67. // All present, so create the link between them
  68. instantiate_link(dst_model, type, name, src, dst)
  69. else:
  70. list_append(queue, name)
  71. elif (has_value(src_model["model"][name])):
  72. // Has a value, so copy that as well
  73. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  74. instantiate_value(dst_model, type, name, src_model["model"][name])
  75. else:
  76. // Is a node
  77. type = reverseKeyLookup(src_model["metamodel"]["model"], dict_read_node(src_model["type_mapping"], src_model["model"][name]))
  78. instantiate_node(dst_model, type, name)
  79. return dst_model!
  80. Element function model_retype_on_name(model : Element, new_MM : Element, operation : String, name : String):
  81. String key
  82. String type
  83. Element keys
  84. Integer length
  85. keys = dict_keys(model["model"])
  86. length = string_len(name)
  87. while (read_nr_out(keys) > 0):
  88. key = set_pop(keys)
  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], model["metamodel"]["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], model["metamodel"]["model"][string_substr(type, length, string_len(type))])
  99. else:
  100. model_delete_element(model, key)
  101. return model!