object_operations.alc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "constructors.alh"
  4. Element function allInstances(model : Element, type_name : String):
  5. Element type_mapping
  6. Element result
  7. Element type
  8. String key
  9. Element keys
  10. keys = dict_keys(model["model"])
  11. type = model["metamodel"]["model"][type_name]
  12. result = create_node()
  13. while (0 < list_len(keys)):
  14. key = set_pop(keys)
  15. if (is_nominal_instance(model, model["model"][key], type)):
  16. set_add(result, key)
  17. return result
  18. Element function selectPossibleIncoming(model : Element, target : String, limit_set : Element):
  19. // Find all possible incoming link types for the target model
  20. // Should also include those specified on the superclass(es)
  21. String type
  22. Element metamodel
  23. Element elem
  24. Element result
  25. result = create_node()
  26. metamodel = model["metamodel"]
  27. while (0 < list_len(limit_set)):
  28. type = set_pop(limit_set)
  29. elem = metamodel["model"][type]
  30. if (is_edge(elem)):
  31. if (is_nominal_instance(model, model["model"][target], read_edge_dst(elem))):
  32. set_add(result, type)
  33. return result
  34. Element function selectPossibleOutgoing(model : Element, source : String, limit_set : Element):
  35. // Find all possible outgoing link types for the source model
  36. // Should also include those specified on the superclass(es)
  37. String type
  38. Element metamodel
  39. Element elem
  40. Element result
  41. result = create_node()
  42. metamodel = model["metamodel"]
  43. while (0 < list_len(limit_set)):
  44. type = set_pop(limit_set)
  45. elem = metamodel["model"][type]
  46. if (is_edge(elem)):
  47. if (is_nominal_instance(model, model["model"][source], read_edge_src(elem))):
  48. set_add(result, type)
  49. return result
  50. Element function allOutgoingAssociationInstances(model : Element, source_name : String, assoc_name : String):
  51. // Read out all outgoing edges of the model and select those that are typed by the specified association
  52. // TODO for some reason this crashes if allInstances is used!
  53. Integer nr_out
  54. Integer i
  55. Element out
  56. String out_name
  57. Element result
  58. result = create_node()
  59. nr_out = read_nr_out(model["model"][source_name])
  60. i = 0
  61. while (i < nr_out):
  62. out = read_out(model["model"][source_name], i)
  63. if (set_in_node(model["model"], out)):
  64. out_name = reverseKeyLookup(model["model"], out)
  65. if (is_nominal_instance(model, out, model["metamodel"]["model"][assoc_name])):
  66. set_add(result, out_name)
  67. i = i + 1
  68. return result
  69. Element function allIncomingAssociationInstances(model : Element, target_name : String, assoc_name : String):
  70. // Read out all outgoing edges of the model and select those that are typed by the specified association
  71. Integer nr_in
  72. Integer i
  73. Element in
  74. String in_name
  75. Element result
  76. result = create_node()
  77. nr_in = read_nr_in(model["model"][target_name])
  78. i = 0
  79. while (i < nr_in):
  80. in = read_in(model["model"][target_name], i)
  81. if (set_in_node(model["model"], in)):
  82. in_name = reverseKeyLookup(model["model"], in)
  83. if (is_nominal_instance(model, in, model["metamodel"]["model"][assoc_name])):
  84. set_add(result, in_name)
  85. i = i + 1
  86. return result
  87. Element function getAttributeList(model : Element, element : String):
  88. Element result
  89. Element keys
  90. Element type
  91. Element attr_name
  92. String attr_type
  93. result = create_node()
  94. type = dict_read_node(model["type_mapping"], model["model"][element])
  95. keys = dict_keys(type)
  96. // Add our own attributes
  97. while (0 < list_len(keys)):
  98. attr_name = set_pop(keys)
  99. if (is_physical_string(attr_name)):
  100. attr_type = getName(model["metamodel"], type[attr_name])
  101. dict_add(result, attr_name, attr_type)
  102. // Go on to the metalevel
  103. // TODO
  104. return result
  105. Element function getInstantiatableAttributes(model : Element, element : String):
  106. Element result
  107. result = create_node()
  108. // Get all outgoing "dictionary" links
  109. Element set_own
  110. Element elem
  111. elem = model["model"][element]
  112. set_own = dict_keys(element)
  113. // Filter them
  114. Element e
  115. while (0 < read_nr_out(set_own)):
  116. e = set_pop(set_own)
  117. if (is_physical_string(e)):
  118. dict_add(result, e, getName(model, element[e]))
  119. return result
  120. String function getName(model : Element, element : Element):
  121. return reverseKeyLookup(model["model"], element)
  122. // TODO Utility functions!
  123. String function reverseKeyLookup(dict : Element, element : Element):
  124. Element elements
  125. String name
  126. elements = dict_keys(dict)
  127. while (0 < list_len(elements)):
  128. name = set_pop(elements)
  129. if (element_eq(dict[name], element)):
  130. return name
  131. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")
  132. String function print_dict(dict : Element):
  133. Element keys
  134. Element key
  135. String result
  136. keys = dict_keys(dict)
  137. result = ""
  138. while (0 < list_len(keys)):
  139. key = set_pop(keys)
  140. result = result + cast_v2s(key)
  141. result = result + ": "
  142. result = result + cast_v2s(dict[key])
  143. result = result + "\n"
  144. return result