object_operations.alc 4.6 KB

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