object_operations.alc 5.0 KB

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