NameL3ss

comment

Jan 25th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. normalize list primitive response
  2. list query
  3. def build_list_name(list_uri:, list_name:, element_name:, customer_uri:, comment:)
  4. %(
  5. PREFIX rdfs: <#{PrefixesService::PREFIXES[:rdfs]}>
  6. PREFIX owl: <#{PrefixesService::PREFIXES[:owl]}>
  7. PREFIX rdf: <#{PrefixesService::PREFIXES[:rdf]}>
  8. PREFIX wco: <#{PrefixesService::PREFIXES[:wco]}>
  9. INSERT DATA {
  10. <#{list_uri}> a wco:List ;
  11. rdfs:subClassOf wco:CustomMetadatum ;
  12. rdfs:subClassOf wco:List ;
  13. rdfs:label "#{list_name}" ;
  14. #{comment_part(comment)} ;
  15. rdf:first <#{PrefixesService::PREFIXES[:wci]}#{element_name}> .
  16. <#{customer_uri}> wco:hasCustomMetadatum <#{list_uri}> .
  17. }
  18. )
  19. end
  20.  
  21. def find_by_list_name(customer_uri:, list_name:)
  22. %(
  23. PREFIX rdfs: <#{PrefixesService::PREFIXES[:rdfs]}>
  24. PREFIX wco: <#{PrefixesService::PREFIXES[:wco]}>
  25.  
  26. SELECT ?listLabel ?element ?comment
  27. WHERE {
  28. <#{customer_uri}> wco:hasCustomMetadatum wco:#{list_name} .
  29. ?list a wco:List ;
  30. rdfs:label ?listLabel ;
  31. rdf:rest*/rdf:first ?element .
  32. OPTIONAL { ?list rdfs:comment ?comment } .
  33. FILTER (STRSTARTS(STR(?list), "#{PrefixesService::PREFIXES[:wco]}#{list_name}"))
  34. }
  35. )
  36. end
  37.  
  38. def comment_part(comment)
  39. comment.nil? ? "" : "rdfs:comment \"#{comment}\" ;"
  40. end
  41. end
  42. private_class_method :comment_part
  43.  
  44. list service
  45. def create_list(list_name, elements, customer, comment)
  46. list_metadatum = "Metadatum-#{SecureRandom.uuid}"
  47. list_uri = "#{PrefixesService::PREFIXES[:wco]}#{list_metadatum}"
  48. element_name = build_name(element: elements.first)
  49. sparql_query = ListQuery.build_list_name(list_uri: list_uri,
  50. list_name: list_name,
  51. element_name: element_name,
  52. customer_uri: customer.uri, comment: comment)
  53.  
  54. result = GraphDBConnector.instance.sparql_api_statement(sparql_query)
  55. if result[:success]
  56. elements[1..-1].each do |element_name|
  57. element_name = build_name(element: element_name)
  58. sparql_query = ListQuery.build_elements(list_uri: list_uri, element_name: element_name)
  59. GraphDBConnector.instance.sparql_api_statement(sparql_query)
  60. end
  61.  
  62. { data: list_uri }
  63. else
  64. { error: result[:error] }
  65. end
  66. end
  67.  
  68. def find_by_list_name(customer, list_name)
  69. return nil unless customer
  70.  
  71. sparql_query = ListQuery.find_by_list_name(customer_uri: customer.uri, list_name: list_name)
  72.  
  73. result = GraphDBConnector.instance.query(sparql_query)
  74. return nil unless result[:success] && result[:data].present?
  75.  
  76. result[:data]
  77. end
  78.  
  79.  
  80. metadatum query
  81. def select_custom_metadata_for_customer(customer_uri:)
  82. %(
  83. PREFIX rdfs: <#{PrefixesService::PREFIXES[:rdfs]}>
  84. PREFIX wco: <#{PrefixesService::PREFIXES[:wco]}>
  85. PREFIX wci: <#{PrefixesService::PREFIXES[:wci]}>
  86.  
  87. SELECT ?metadatum ?metadatumLabel ?subClassOf ?comment WHERE {
  88. <#{customer_uri}> wco:hasCustomMetadatum ?metadatum .
  89. ?metadatum rdfs:subClassOf wco:CustomMetadatum ;
  90. rdfs:subClassOf wco:PrimitiveType ;
  91. rdfs:subClassOf ?subClassOf ;
  92. rdfs:label ?metadatumLabel ;
  93. OPTIONAL { ?metadatum rdfs:comment ?comment } .
  94. FILTER (?subClassOf NOT IN (
  95. wco:PrimitiveType, owl:Thing, wco:CustomMetadatum, wco:Metadatum)
  96. )
  97. FILTER(?subClassOf IN (wco:Date, wco:Number, wco:Text, wco:Time, wco:List, wco:Amount))
  98. }
  99. )
  100. end
  101.  
  102. ontology serializer
  103. comment = datum.comment.value rescue nil
  104. {
  105. uri: datum.metadatum.value,
  106. class_name: datum.metadatum.value.split("#").last,
  107. label: datum.metadatumLabel.value,
  108. comment: comment,
  109. sub_class_of: {
  110. uri: datum.subClassOf.value,
  111. type: datum.subClassOf.value.split("#").last
  112. }
  113. }
  114.  
  115. metadatum serializer
  116. def serializable_hash
  117. return nil unless @records
  118.  
  119. type = @records.find{|el| el if el[:subclassOf].value.split("#").last != @params[:name] }
  120. object = @records.find{|el| el if el[:subclassOf].value.split("#").last == @params[:name] }
  121.  
  122. uri, type = if type.present?
  123. [type[:subclassOf].value, type[:subclassOf].value.split("#").last]
  124. else
  125. ["#{PrefixesService::WEBDOX_URI}/ontology#List", "List"]
  126. end
  127. comment = object[:comment].value rescue nil
  128. {
  129. uri: object[:class].value,
  130. class_name: object[:class].value.split("#").last,
  131. label: object[:label].value,
  132. comment: comment,
  133. sub_class_of: {
  134. uri: uri,
  135. type: type
  136. }
  137. }
  138.  
  139. metadatum query
  140. def find_by_metadatum_name(name:, customer_uri:)
  141. sparql_query = %(
  142. PREFIX rdfs: <#{PrefixesService::PREFIXES[:rdfs]}>
  143. PREFIX owl: <#{PrefixesService::PREFIXES[:owl]}>
  144. PREFIX wco: <#{PrefixesService::PREFIXES[:wco]}>
  145.  
  146. SELECT ?class ?label ?subclassOf ?comment
  147. WHERE {
  148. <#{customer_uri}> wco:hasCustomMetadatum wco:#{name} .
  149. ?class a owl:Class ;
  150. rdfs:label ?label .
  151. OPTIONAL { ?class rdfs:subClassOf ?subclassOf }
  152. OPTIONAL { ?class rdfs:comment ?comment }
  153. FILTER (?subclassOf NOT IN (
  154. wco:PrimitiveType,owl:Thing, wco:CustomMetadatum, wco:Metadatum)
  155. )
  156. FILTER (?class = wco:#{name})
  157. }
  158. )
  159. result = GraphDBConnector.instance.query(sparql_query)
  160. return nil unless result[:success] && result[:data].any?
  161.  
  162. result[:data]
  163. end
  164.  
  165. contract request template serializer
  166. comment = el.comment.value rescue nil
  167. base_options = {
  168. contract_request_template_uri: contract_request_template.uri,
  169. metadatum_uri: el.ontology.value,
  170. metadatum_short_uri: el.ontology.value.split("#").last,
  171. metadatum_label: el.label.value,
  172. comment: comment
  173. }
  174.  
  175. contract request template query
  176. def metadata(contract_request_template_uri:)
  177. %(
  178. PREFIX wco: <#{PrefixesService::PREFIXES[:wco]}>
  179. PREFIX rdfs: <#{PrefixesService::PREFIXES[:rdfs]}>
  180.  
  181. SELECT DISTINCT ?ontology ?label ?type ?subClassOf ?comment
  182. WHERE {
  183. {
  184. <#{contract_request_template_uri}> wco:hasCustomMetadatumWRT ?ontology .
  185. ?ontology rdfs:label ?label .
  186. ?ontology rdf:type ?type .
  187. OPTIONAL { ?ontology rdfs:comment ?comment }
  188. OPTIONAL { ?ontology rdfs:subClassOf ?subClassOf . }
  189.  
  190. FILTER (
  191. (?type = wco:List && ?subClassOf = wco:CustomMetadatum) ||
  192. (?subClassOf IN (wco:Number, wco:Text, wco:Date, wco:Time, wco:Amount) && ?type = wco:CustomMetadatum)
  193. )
  194. }
  195.  
  196. UNION
  197. {
  198. SELECT ?ontology ?label ?type ?subClassOf ?comment
  199. WHERE {
  200. <#{contract_request_template_uri}> wco:hasAssignedMetadatum ?ontology .
  201. ?ontology rdfs:label ?label .
  202. ?ontology rdf:type ?type .
  203. ?ontology rdfs:subClassOf ?subClassOf .
  204. OPTIONAL { ?ontology rdfs:comment ?comment }
  205. FILTER (
  206. ?type IN (wco:Metadatum) && ?subClassOf IN (wco:Amount, wco:Metadatum-Party, wco:Metadatum-Counterparty
  207. ))
  208. }
  209. }
  210. }
  211. ORDER BY LCASE(?label)
  212. )
  213. end
  214.  
  215. add comments to ontology, parties, counterparties?
Advertisement
Add Comment
Please, Sign In to add comment