object_operations.alc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 setAttribute(model : Element, source : Element, attr_name : String, value : Element):
  73. // Read out which edge we are talking about
  74. Element edge
  75. edge = deleteAttribute(model, source, attr_name)
  76. // Now make the element
  77. Element created_edge
  78. created_edge = create_edge(source, value)
  79. dict_add(model["type_mapping"], created_edge, edge)
  80. dict_add(model["type_mapping"], value, read_edge_dst(edge))
  81. add_to_model(model, "", value)
  82. add_to_model(model, "", created_edge)
  83. return True
  84. Element function deleteAttribute(model : Element, source : Element, attr_name : Element):
  85. Element edge
  86. edge = findAttribute(dict_read_node(model["type_mapping"], source), attr_name, model["type_mapping"], model["inheritance"])
  87. Element found_elements
  88. found_elements = allOutgoingAssociationInstances(model, source, edge)
  89. Element rm_element
  90. if (list_len(found_elements) > 0):
  91. rm_element = read_edge_dst(set_pop(found_elements))
  92. dict_delete(model["type_mapping"], rm_element)
  93. delete_element(rm_element)
  94. return edge
  95. Element function getAttributeList(model : Element, mm : Element):
  96. Element result
  97. result = create_node()
  98. // Get all outgoing "dictionary" links
  99. Element set_own
  100. set_own = dict_keys(mm)
  101. // Filter them
  102. Element e
  103. while (0 < read_nr_out(set_own)):
  104. e = set_pop(set_own)
  105. if (is_physical_string(e)):
  106. if (has_value(mm[e])):
  107. // This is a primitive type, so add to the list
  108. dict_add(result, e, mm[e])
  109. // And go up to the possible supertypes
  110. Element found_elements
  111. found_elements = allOutgoingAssociationInstances(model, mm, model["inheritance"])
  112. Element dict_super
  113. Integer i
  114. Integer max
  115. Element set_super
  116. Element found_key
  117. while (0 < read_nr_out(found_elements)):
  118. // Now find the destination of these associations, which are the superclasses of this model
  119. dict_super = getAttributeList(model, read_edge_dst(set_pop(found_elements)))
  120. set_super = dict_keys(dict_super)
  121. // Merge the two
  122. while (0 < read_nr_out(set_super)):
  123. found_key = set_pop(set_super)
  124. dict_add(result, found_key, dict_super[found_key])
  125. return result
  126. Element function getInstantiatableAttributes(model : Element, element : Element):
  127. Element result
  128. result = create_node()
  129. // Get all outgoing "dictionary" links
  130. Element set_own
  131. set_own = dict_keys(element)
  132. // Filter them
  133. Element e
  134. while (0 < read_nr_out(set_own)):
  135. e = set_pop(set_own)
  136. if (bool_and(is_physical_string(e), has_value(element[e]))):
  137. // This is a primitive type, so add to the list
  138. dict_add(result, e, element[e])
  139. return result
  140. String function getName(m : Element, e : Element):
  141. Element element_keys
  142. Element s
  143. s = m["model"]
  144. element_keys = dict_keys(s)
  145. Element key
  146. while (0 < read_nr_out(element_keys)):
  147. key = set_pop(element_keys)
  148. if (element_eq(dict_read_node(s, key), e)):
  149. return key
  150. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
  151. String function reverseNameLookup(s : Element, e : Element):
  152. Element element_keys
  153. element_keys = dict_keys(s)
  154. Element key
  155. while (0 < read_nr_out(element_keys)):
  156. key = set_pop(element_keys)
  157. if (element_eq(dict_read_node(s, key), e)):
  158. return key
  159. return string_join(string_join("(unknown: ", cast_e2s(e)), " )")