object_operations.alc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "constructors.alh"
  4. Element function allInstances(model_ai : Element, type_ai : Element):
  5. Element type_mapping_ai
  6. Element result_ai
  7. type_mapping_ai = dict_read(model_ai, "type_mapping")
  8. result_ai = create_node()
  9. Integer counter_ai
  10. counter_ai = 0
  11. Integer length_ai
  12. length_ai = read_nr_out(type_mapping_ai)
  13. Element edge_ai
  14. while (integer_lt(counter_ai, length_ai)):
  15. edge_ai = read_out(type_mapping_ai, counter_ai)
  16. if (element_eq(read_edge_dst(edge_ai), type_ai)):
  17. // Found an element of the specified type
  18. set_add(result_ai, read_edge_dst(read_out(edge_ai, 0)))
  19. counter_ai = integer_addition(counter_ai, 1)
  20. return result_ai
  21. Element function allOutgoingAssociationInstances(model_aoai : Element, source_aoai : Element, assoc_aoai : 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_aoai
  25. length_aoai = read_nr_out(source_aoai)
  26. Integer counter_aoai
  27. counter_aoai = 0
  28. Element result_aoai
  29. result_aoai = create_node()
  30. Element edge_aoai
  31. while (integer_lt(counter_aoai, length_aoai)):
  32. edge_aoai = read_out(source_aoai, counter_aoai)
  33. if (element_eq(dict_read_node(dict_read(model_aoai, "type_mapping"), edge_aoai), assoc_aoai)):
  34. set_add(result_aoai, edge_aoai)
  35. counter_aoai = integer_addition(counter_aoai, 1)
  36. return result_aoai
  37. Element function allIncomingAssociationInstances(model_aiai : Element, source_aiai : Element, assoc_aiai : Element):
  38. // Read out all outgoing edges of the model and select those that are typed by the specified association
  39. Element result_aiai
  40. result_aiai = create_node()
  41. Element allinsts_aiai
  42. allinsts_aiai = allInstances(model_aiai, assoc_aiai)
  43. Element understudy_aiai
  44. while (integer_lt(0, read_nr_out(allinsts_aiai))):
  45. understudy_aiai = set_pop(allinsts_aiai)
  46. if (element_eq(read_edge_dst(understudy_aiai), source_aiai)):
  47. set_add(result_aiai, understudy_aiai)
  48. return result_aiai
  49. Element function readElementByName(model_rebn : Element, name_rebn : String):
  50. return dict_read(dict_read(model_rebn, "model"), name_rebn)
  51. Element function findAttribute(source_fa : Element, attr_name_fa : Element, types_fa : Element, inheritance_link_fa : Element):
  52. if (dict_in(source_fa, attr_name_fa)):
  53. return dict_read_edge(source_fa, attr_name_fa)
  54. else:
  55. Integer counter_fa
  56. Integer i_fa
  57. Element edge_fa
  58. counter_fa = read_nr_out(source_fa)
  59. i_fa = 0
  60. while (integer_lt(i_fa, counter_fa)):
  61. edge_fa = read_out(source_fa, i_fa)
  62. if (element_eq(dict_read_node(types_fa, edge_fa), inheritance_link_fa)):
  63. return find_attribute(read_edge_dst(edge_fa), attr_name_fa, types_fa, inheritance_link_fa)
  64. i_fa = integer_addition(i_fa, 1)
  65. // No return at the moment, as this crashes the MvK
  66. log("ERROR: could not find attribute")
  67. Element function readAttribute(model_ra : Element, source_ra : Element, attr_name_ra : String):
  68. // Read out the edge we are talking about
  69. Element edge_ra
  70. edge_ra = findAttribute(dict_read_node(dict_read(model_ra, "type_mapping"), source_ra), attr_name_ra, dict_read(model_ra, "type_mapping"), dict_read(model_ra, "inheritance"))
  71. return read_edge_dst(set_pop(allOutgoingAssociationInstances(model_ra, source_ra, edge_ra)))
  72. Element function setAttribute(model_sa : Element, source_sa : Element, attr_name_sa : String, value_sa : Element):
  73. // Read out which edge we are talking about
  74. Element edge_sa
  75. edge_sa = deleteAttribute(model_sa, source_sa, attr_name_sa)
  76. // Now make the element
  77. Element created_edge_sa
  78. created_edge_sa = create_edge(source_sa, value_sa)
  79. dict_add(dict_read(model_sa, "type_mapping"), created_edge_sa, edge_sa)
  80. dict_add(dict_read(model_sa, "type_mapping"), value_sa, read_edge_dst(edge_sa))
  81. add_to_model(model_sa, "", value_sa)
  82. add_to_model(model_sa, "", created_edge_sa)
  83. return True
  84. Element function deleteAttribute(model_da : Element, source_da : Element, attr_name_da : Element):
  85. Element edge_da
  86. edge_da = findAttribute(dict_read_node(dict_read(model_da, "type_mapping"), source_da), attr_name_da, dict_read(model_da, "type_mapping"), dict_read(model_da, "inheritance"))
  87. Element found_elements_da
  88. found_elements_da = allOutgoingAssociationInstances(model_da, source_da, edge_da)
  89. Element rm_element_da
  90. if (integer_gt(list_len(found_elements_da), 0)):
  91. rm_element_da = read_edge_dst(set_pop(found_elements_da))
  92. dict_delete(dict_read(model_da, "type_mapping"), rm_element_da)
  93. delete_element(rm_element_da)
  94. return edge_da
  95. Element function getAttributeList(model_gali : Element, mm_gali : Element):
  96. Element result_gali
  97. result_gali = create_node()
  98. // Get all outgoing "dictionary" links
  99. Element set_gali_own
  100. set_gali_own = dict_keys(mm_gali)
  101. // Filter them
  102. Element e_gali
  103. while (integer_lt(0, read_nr_out(set_gali_own))):
  104. e_gali = set_pop(set_gali_own)
  105. if (type_eq(typeof(e_gali), String)):
  106. if (type_eq(typeof(dict_read(mm_gali, e_gali)), Type)):
  107. // This is a primitive type, so add to the list
  108. dict_add(result_gali, e_gali, dict_read(mm_gali, e_gali))
  109. // And go up to the possible supertypes
  110. Element found_elements_gali
  111. found_elements_gali = allOutgoingAssociationInstances(model_gali, mm_gali, dict_read(model_gali, "inheritance"))
  112. Element dict_gali_super
  113. Integer i_gali
  114. Integer max_gali
  115. Element set_gali_super
  116. Element found_key_gali
  117. while (integer_lt(0, read_nr_out(found_elements_gali))):
  118. // Now find the destination of these associations, which are the superclasses of this model
  119. dict_gali_super = getAttributeList(model_gali, read_edge_dst(set_pop(found_elements_gali)))
  120. set_gali_super = dict_keys(dict_gali_super)
  121. // Merge the two
  122. while (integer_lt(0, read_nr_out(set_gali_super))):
  123. found_key_gali = set_pop(set_gali_super)
  124. dict_add(result_gali, found_key_gali, dict_read(dict_gali_super, found_key_gali))
  125. return result_gali
  126. Element function getInstantiatableAttributes(model_gili : Element, element_gili : Element):
  127. Element result_gili
  128. result_gili = create_node()
  129. // Get all outgoing "dictionary" links
  130. Element set_gili_own
  131. set_gili_own = dict_keys(element_gili)
  132. // Filter them
  133. Element e_gili
  134. while (integer_lt(0, read_nr_out(set_gili_own))):
  135. e_gili = set_pop(set_gili_own)
  136. if (bool_and(type_eq(typeof(e_gili), String), type_eq(typeof(dict_read(element_gili, e_gili)), Type))):
  137. // This is a primitive type, so add to the list
  138. dict_add(result_gili, e_gili, dict_read(element_gili, e_gili))
  139. return result_gili
  140. String function getName(m : Element, e : Element):
  141. Element element_keys
  142. Element s
  143. s = dict_read(m, "model")
  144. element_keys = dict_keys(s)
  145. while (integer_lt(0, read_nr_out(element_keys))):
  146. Element key
  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_rnl : Element, e_rnl : Element):
  152. Element element_keys_rnl
  153. element_keys_rnl = dict_keys(s_rnl)
  154. while (integer_lt(0, read_nr_out(element_keys_rnl))):
  155. Element key_rnl
  156. key_rnl = set_pop(element_keys_rnl)
  157. if (element_eq(dict_read_node(s_rnl, key_rnl), e_rnl)):
  158. return key_rnl
  159. return string_join(string_join("(unknown: ", cast_e2s(e_rnl)), " )")