object_operations.alc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 allOutgoingAssociationInstances(model : Element, source : Element, assoc : Element):
  22. // Read out all outgoing edges of the model and select those that are typed by the specified association
  23. // TODO for some reason this crashes if allInstances is used!
  24. Integer length
  25. length = read_nr_out(source)
  26. Integer counter
  27. counter = 0
  28. Element result
  29. result = create_node()
  30. Element edge
  31. while (counter < length):
  32. edge = read_out(source, counter)
  33. if (element_eq(dict_read_node(model["type_mapping"], edge), assoc)):
  34. set_add(result, edge)
  35. counter = counter + 1
  36. return result
  37. Element function allIncomingAssociationInstances(model : Element, source : Element, assoc : Element):
  38. // Read out all outgoing edges of the model and select those that are typed by the specified association
  39. Element result
  40. result = create_node()
  41. Element allinsts
  42. allinsts = allInstances(model, assoc)
  43. Element understudy
  44. while (0 < read_nr_out(allinsts)):
  45. understudy = set_pop(allinsts)
  46. if (element_eq(read_edge_dst(understudy), source)):
  47. set_add(result, understudy)
  48. return result
  49. Element function readElementByName(model : Element, name : String):
  50. return model["model"][name]
  51. Element function findAttribute(source : Element, attr_name : Element, types : Element, inheritance_link : Element):
  52. if (dict_in(source, attr_name)):
  53. return dict_read_edge(source, attr_name)
  54. else:
  55. Integer counter
  56. Integer i
  57. Element edge
  58. counter = read_nr_out(source)
  59. i = 0
  60. while (i < counter):
  61. edge = read_out(source, i)
  62. if (element_eq(dict_read_node(types, edge), inheritance_link)):
  63. return find_attribute(read_edge_dst(edge), attr_name, types, inheritance_link)
  64. i = i + 1
  65. // No return at the moment, as this crashes the MvK
  66. log("ERROR: could not find attribute")
  67. Element function readAttribute(model : Element, source : Element, attr_name : String):
  68. // Read out the edge we are talking about
  69. Element edge
  70. edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"])
  71. return read_edge_dst(set_pop(allOutgoingAssociationInstances(model, source, edge)))
  72. Element function deleteAttribute(model : Element, source : Element, attr_name : Element):
  73. Element edge
  74. edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"])
  75. Element found_elements
  76. found_elements = allOutgoingAssociationInstances(model, source, edge)
  77. Element rm_element
  78. if (list_len(found_elements) > 0):
  79. rm_element = read_edge_dst(set_pop(found_elements))
  80. dict_delete(model["type_mapping"], rm_element)
  81. delete_element(rm_element)
  82. return edge
  83. Element function getAttributeList(model : Element, mm : Element):
  84. Element result
  85. result = create_node()
  86. // Get all outgoing "dictionary" links
  87. Element set_own
  88. set_own = dict_keys(mm)
  89. // Filter them
  90. Element e
  91. while (0 < read_nr_out(set_own)):
  92. e = set_pop(set_own)
  93. if (is_physical_string(e)):
  94. if (has_value(mm[e])):
  95. // This is a primitive type, so add to the list
  96. dict_add(result, e, mm[e])
  97. // And go up to the possible supertypes
  98. Element found_elements
  99. found_elements = allOutgoingAssociationInstances(model, mm, model["inheritance"])
  100. Element dict_super
  101. Integer i
  102. Integer max
  103. Element set_super
  104. Element found_key
  105. while (0 < read_nr_out(found_elements)):
  106. // Now find the destination of these associations, which are the superclasses of this model
  107. dict_super = getAttributeList(model, read_edge_dst(set_pop(found_elements)))
  108. set_super = dict_keys(dict_super)
  109. // Merge the two
  110. while (0 < read_nr_out(set_super)):
  111. found_key = set_pop(set_super)
  112. dict_add(result, found_key, dict_super[found_key])
  113. return result
  114. Element function getInstantiatableAttributes(model : Element, element : Element):
  115. Element result
  116. result = create_node()
  117. // Get all outgoing "dictionary" links
  118. Element set_own
  119. set_own = dict_keys(element)
  120. // Filter them
  121. Element e
  122. while (0 < read_nr_out(set_own)):
  123. e = set_pop(set_own)
  124. if (bool_and(is_physical_string(e), has_value(element[e]))):
  125. // This is a primitive type, so add to the list
  126. dict_add(result, e, element[e])
  127. return result
  128. String function getName(m : Element, e : Element):
  129. Element element_keys
  130. Element s
  131. s = m["model"]
  132. element_keys = dict_keys(s)
  133. Element key
  134. while (0 < read_nr_out(element_keys)):
  135. key = set_pop(element_keys)
  136. if (element_eq(dict_read_node(s, key), e)):
  137. return key
  138. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  139. String function reverseNameLookup(s : Element, e : Element):
  140. Element element_keys
  141. element_keys = dict_keys(s)
  142. Element key
  143. while (0 < read_nr_out(element_keys)):
  144. key = set_pop(element_keys)
  145. if (element_eq(dict_read_node(s, key), e)):
  146. return key
  147. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")