Class: Solis::Model
- Inherits:
-
Object
- Object
- Solis::Model
- Defined in:
- lib/solis/model.rb
Class Method Summary collapse
- .construct(level = 0) ⇒ Object
- .graph ⇒ Object
- .graph=(graph) ⇒ Object
- .graph_name ⇒ Object
- .graph_name=(graph_name) ⇒ Object
- .graph_prefix ⇒ Object
- .graph_prefix=(graph_prefix) ⇒ Object
- .language ⇒ Object
- .language=(language) ⇒ Object
- .make_id_for(model) ⇒ Object
- .metadata ⇒ Object
- .metadata=(m) ⇒ Object
- .model(level = 0) ⇒ Object
- .model_after_create(&blk) ⇒ Object
- .model_after_delete(&blk) ⇒ Object
- .model_after_read(&blk) ⇒ Object
- .model_after_update(&blk) ⇒ Object
- .model_before_create(&blk) ⇒ Object
- .model_before_delete(&blk) ⇒ Object
- .model_before_read(&blk) ⇒ Object
- .model_before_update(&blk) ⇒ Object
- .model_template(level = 0) ⇒ Object
- .shapes ⇒ Object
- .shapes=(s) ⇒ Object
- .sparql_endpoint ⇒ Object
- .sparql_endpoint=(sparql_endpoint) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
- #dump(format = :ttl, resolve_all = true) ⇒ Object
- #exists?(sparql) ⇒ Boolean
- #graph_id ⇒ Object
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
- #is_referenced?(sparql) ⇒ Boolean
- #name(plural = false) ⇒ Object
- #query ⇒ Object
- #save(validate_dependencies = true, top_level = true) ⇒ Object
- #to_graph(resolve_all = true) ⇒ Object
- #to_ttl(resolve_all = true) ⇒ Object
- #update(data, validate_dependencies = true, top_level = true) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/solis/model.rb', line 11 def initialize(attributes = {}) @model_name = self.class.name @model_plural_name = @model_name.pluralize @language = Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || 'en' raise "Please look at /#{@model_name.tableize}/model for structure to supply" if attributes.nil? attributes.each do |attribute, value| if self.class.[:attributes].keys.include?(attribute.to_s) if !self.class.[:attributes][attribute.to_s][:node_kind].nil? && !(value.is_a?(Hash) || value.is_a?(Array) || value.class.ancestors.include?(Solis::Model)) raise Solis::Error::InvalidAttributeError, "'#{@model_name}.#{attribute}' must be an object" end if self.class.[:attributes][attribute.to_s][:node_kind].is_a?(RDF::URI) && value.is_a?(Hash) inner_model = self.class.graph.shape_as_model(self.class.[:attributes][attribute.to_s][:datatype].to_s) value = inner_model.new(value) elsif self.class.[:attributes][attribute.to_s][:node_kind].is_a?(RDF::URI) && value.is_a?(Array) new_value = [] value.each do |v| if v.is_a?(Hash) inner_model = self.class.graph.shape_as_model(self.class.[:attributes][attribute.to_s][:datatype].to_s) new_value << inner_model.new(v) else new_value << v end end value = new_value end # switched off. currently language query parameters returns the value # value = { # "@language" => @language, # "@value" => value # } if self.class.metadata[:attributes][attribute.to_s][:datatype_rdf].eql?('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') value = value.first if value.is_a?(Array) && (attribute.eql?('id') || attribute.eql?(:id)) instance_variable_set("@#{attribute}", value) else raise Solis::Error::InvalidAttributeError, "'#{attribute}' is not part of the definition of #{@model_name}" end end self.class.make_id_for(self) # id = instance_variable_get("@id") # if id.nil? || (id.is_a?(String) && id&.empty?) # instance_variable_set("@id", SecureRandom.uuid) # end rescue StandardError => e Solis::LOGGER.error(e.) raise Solis::Error::GeneralError, "Unable to create entity #{@model_name}" end |
Class Method Details
.construct(level = 0) ⇒ Object
397 398 399 |
# File 'lib/solis/model.rb', line 397 def self.construct(level = 0) raise 'to bo implemented' end |
.graph ⇒ Object
349 350 351 |
# File 'lib/solis/model.rb', line 349 def self.graph @graph end |
.graph=(graph) ⇒ Object
353 354 355 |
# File 'lib/solis/model.rb', line 353 def self.graph=(graph) @graph = graph end |
.graph_name ⇒ Object
325 326 327 |
# File 'lib/solis/model.rb', line 325 def self.graph_name @graph_name end |
.graph_name=(graph_name) ⇒ Object
329 330 331 |
# File 'lib/solis/model.rb', line 329 def self.graph_name=(graph_name) @graph_name = graph_name end |
.graph_prefix ⇒ Object
337 338 339 |
# File 'lib/solis/model.rb', line 337 def self.graph_prefix @graph_prefix end |
.graph_prefix=(graph_prefix) ⇒ Object
333 334 335 |
# File 'lib/solis/model.rb', line 333 def self.graph_prefix=(graph_prefix) @graph_prefix = graph_prefix end |
.language ⇒ Object
357 358 359 |
# File 'lib/solis/model.rb', line 357 def self.language Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || @language || 'en' end |
.language=(language) ⇒ Object
361 362 363 |
# File 'lib/solis/model.rb', line 361 def self.language=(language) @language = language end |
.make_id_for(model) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/solis/model.rb', line 292 def self.make_id_for(model) raise "I need a SPARQL endpoint" if self.sparql_endpoint.nil? sparql = SPARQL::Client.new(self.sparql_endpoint) id = model.instance_variable_get("@id") if id.nil? || (id.is_a?(String) && id&.empty?) id_retries = 0 while id.nil? || sparql.query("ASK WHERE { ?s <#{self.graph_name}/id> \"#{id}\" }") id = SecureRandom.uuid id_retries+=1 end LOGGER.info("ID(#{id}) generated for #{self.name} in #{id_retries} retries") if ConfigFile[:debug] model.instance_variable_set("@id", id) end model end |
.metadata ⇒ Object
309 310 311 |
# File 'lib/solis/model.rb', line 309 def self. @metadata end |
.metadata=(m) ⇒ Object
313 314 315 |
# File 'lib/solis/model.rb', line 313 def self.(m) @metadata = m end |
.model(level = 0) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/solis/model.rb', line 365 def self.model(level = 0) m = { type: self.name.tableize, attributes: {} } self.[:attributes].each do |attribute, | if .key?(:class) && ![:class].nil? && [:class].value =~ /#{self.graph_name}/ && level == 0 cm = self.graph.shape_as_model(self.[:attributes][attribute][:datatype].to_s).model(level + 1) m[:attributes][attribute.to_sym] = cm[:attributes] else m[:attributes][attribute.to_sym] = { description: [:comment]&.value, mandatory: ([:mincount].to_i > 0), data_type: [:datatype] } end end m end |
.model_after_create(&blk) ⇒ Object
413 414 415 |
# File 'lib/solis/model.rb', line 413 def self.model_after_create(&blk) self.after_create_proc = blk end |
.model_after_delete(&blk) ⇒ Object
429 430 431 |
# File 'lib/solis/model.rb', line 429 def self.model_after_delete(&blk) self.after_delete_proc = blk end |
.model_after_read(&blk) ⇒ Object
405 406 407 |
# File 'lib/solis/model.rb', line 405 def self.model_after_read(&blk) self.after_read_proc = blk end |
.model_after_update(&blk) ⇒ Object
421 422 423 |
# File 'lib/solis/model.rb', line 421 def self.model_after_update(&blk) self.after_update_proc = blk end |
.model_before_create(&blk) ⇒ Object
409 410 411 |
# File 'lib/solis/model.rb', line 409 def self.model_before_create(&blk) self.before_create_proc = blk end |
.model_before_delete(&blk) ⇒ Object
425 426 427 |
# File 'lib/solis/model.rb', line 425 def self.model_before_delete(&blk) self.before_delete_proc = blk end |
.model_before_read(&blk) ⇒ Object
401 402 403 |
# File 'lib/solis/model.rb', line 401 def self.model_before_read(&blk) self.before_read_proc = blk end |
.model_before_update(&blk) ⇒ Object
417 418 419 |
# File 'lib/solis/model.rb', line 417 def self.model_before_update(&blk) self.before_update_proc = blk end |
.model_template(level = 0) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/solis/model.rb', line 382 def self.model_template(level = 0) m = { type: self.name.tableize, attributes: {} } self.[:attributes].each do |attribute, | if .key?(:class) && ![:class].nil? && [:class].value =~ /#{self.graph_name}/ && level == 0 cm = self.graph.shape_as_model(self.[:attributes][attribute][:datatype].to_s).model_template(level + 1) m[:attributes][attribute.to_sym] = cm[:attributes] else m[:attributes][attribute.to_sym] = '' end end m end |
.shapes ⇒ Object
321 322 323 |
# File 'lib/solis/model.rb', line 321 def self.shapes @shapes end |
.shapes=(s) ⇒ Object
317 318 319 |
# File 'lib/solis/model.rb', line 317 def self.shapes=(s) @shapes = s end |
.sparql_endpoint ⇒ Object
341 342 343 |
# File 'lib/solis/model.rb', line 341 def self.sparql_endpoint @sparql_endpoint end |
.sparql_endpoint=(sparql_endpoint) ⇒ Object
345 346 347 |
# File 'lib/solis/model.rb', line 345 def self.sparql_endpoint=(sparql_endpoint) @sparql_endpoint = sparql_endpoint end |
Instance Method Details
#destroy ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/solis/model.rb', line 110 def destroy raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? sparql = SPARQL::Client.new(self.class.sparql_endpoint) raise Solis::Error::QueryError, "#{self.id} is still referenced, refusing to delete" if is_referenced?(sparql) # sparql.query('delete{}') before_delete_proc&.call(self) # graph = as_graph(self, false) # Solis::LOGGER.info graph.dump(:ttl) if ConfigFile[:debug] query = %( with <#{self.class.graph_name}> delete {?s ?p ?o} where { values ?s {<#{self.graph_id}>} ?s ?p ?o } ) result = sparql.query(query) # result = sparql.delete_data(graph, graph: graph.name) after_delete_proc&.call(result) result end |
#dump(format = :ttl, resolve_all = true) ⇒ Object
86 87 88 89 |
# File 'lib/solis/model.rb', line 86 def dump(format = :ttl, resolve_all = true) graph = as_graph(self, resolve_all) graph.dump(format) end |
#exists?(sparql) ⇒ Boolean
288 289 290 |
# File 'lib/solis/model.rb', line 288 def exists?(sparql) sparql.query("ASK WHERE { <#{self.graph_id}> ?p ?o }") end |
#graph_id ⇒ Object
280 281 282 |
# File 'lib/solis/model.rb', line 280 def graph_id "#{self.class.graph_name}#{self.name.tableize}/#{self.id}" end |
#is_referenced?(sparql) ⇒ Boolean
284 285 286 |
# File 'lib/solis/model.rb', line 284 def is_referenced?(sparql) sparql.query("ASK WHERE { ?s ?p <#{self.graph_id}>. filter (!contains(str(?p), '_audit'))}") end |
#name(plural = false) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/solis/model.rb', line 64 def name(plural = false) if plural @model_plural_name else @model_name end end |
#query ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/solis/model.rb', line 72 def query raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? # before_read_proc&.call(self) result = Solis::Query.new(self) # after_read_proc&.call(result) result end |
#save(validate_dependencies = true, top_level = true) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/solis/model.rb', line 134 def save(validate_dependencies = true, top_level = true) raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? sparql = SPARQL::Client.new(self.class.sparql_endpoint) before_create_proc&.call(self) if self.exists?(sparql) data = properties_to_hash(self) result = update(data) else data = properties_to_hash(self) attributes = data.include?('attributes') ? data['attributes'] : data attributes.each_pair do |key, value| # check each key. if it is an entity process it unless self.class.[:attributes][key][:node].nil? #is it an entity value = [value] unless value.is_a?(Array) value.each do |sub_value| = self.class.graph.shape_as_model(self.class.[:attributes][key][:datatype].to_s).new(sub_value) = Solis::Options.instance.get[:embedded_readonly].map{|s| s.to_s} || [] if (.class.ancestors.map{|s| s.to_s} & ).empty? || top_level if .exists?(sparql) = properties_to_hash() .update(, validate_dependencies, false) else .save(validate_dependencies, false) end else Solis::LOGGER.info("#{.class.name} is embedded not allowed to change. Skipping") end end end end graph = as_graph(self, validate_dependencies) # File.open('/Users/mehmetc/Dropbox/AllSources/LP/graphiti-api/save.ttl', 'wb') do |file| # file.puts graph.dump(:ttl) # end Solis::LOGGER.info SPARQL::Client::Update::InsertData.new(graph, graph: graph.name).to_s if ConfigFile[:debug] result = sparql.insert_data(graph, graph: graph.name) end after_create_proc&.call(result) result rescue StandardError => e Solis::LOGGER.error e. Solis::LOGGER.error e. raise e end |
#to_graph(resolve_all = true) ⇒ Object
91 92 93 |
# File 'lib/solis/model.rb', line 91 def to_graph(resolve_all = true) as_graph(self, resolve_all) end |
#to_ttl(resolve_all = true) ⇒ Object
81 82 83 84 |
# File 'lib/solis/model.rb', line 81 def to_ttl(resolve_all = true) graph = as_graph(self, resolve_all) graph.dump(:ttl) end |
#update(data, validate_dependencies = true, top_level = true) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/solis/model.rb', line 186 def update(data, validate_dependencies = true, top_level = true) raise Solis::Error::GeneralError, "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? attributes = data.include?('attributes') ? data['attributes'] : data raise "id is mandatory when updating" unless attributes.keys.include?('id') id = attributes.delete('id') sparql = SPARQL::Client.new(self.class.sparql_endpoint) original_klass = self.query.filter({ language: nil, filters: { id: [id] } }).find_all.map { |m| m }&.first raise Solis::Error::NotFoundError if original_klass.nil? updated_klass = original_klass.deep_dup attributes.each_pair do |key, value| # check each key. if it is an entity process it unless original_klass.class.[:attributes][key][:node].nil? #it is an entity value = [value] unless value.is_a?(Array) value.each do |sub_value| = self.class.graph.shape_as_model(original_klass.class.[:attributes][key][:datatype].to_s).new(sub_value) = Solis::Options.instance.get[:embedded_readonly].map{|s| s.to_s} || [] if (.class.ancestors.map{|s| s.to_s} & ).empty? || top_level if .exists?(sparql) = properties_to_hash() .update(, validate_dependencies, false) else .save(validate_dependencies, false) end else Solis::LOGGER.info("#{.class.name} is embedded not allowed to change. Skipping") end end end updated_klass.instance_variable_set("@#{key}", value) end before_update_proc&.call(original_klass, updated_klass) properties_orignal_klass = properties_to_hash(original_klass) properties_updated_klass = properties_to_hash(updated_klass) if Hashdiff.best_diff(properties_orignal_klass, properties_updated_klass).empty? Solis::LOGGER.info("#{original_klass.class.name} unchanged, skipping") data = self.query.filter({ filters: { id: [id] } }).find_all.map { |m| m }&.first else delete_graph = as_graph(original_klass, false) where_graph = RDF::Graph.new(graph_name: RDF::URI("#{self.class.graph_name}#{self.name.tableize}/#{id}"), data: RDF::Repository.new) if id.is_a?(Array) id.each do |i| where_graph << [RDF::URI("#{self.class.graph_name}#{self.name.tableize}/#{i}"), :p, :o] end else where_graph << [RDF::URI("#{self.class.graph_name}#{self.name.tableize}/#{id}"), :p, :o] end insert_graph = as_graph(updated_klass, true) # puts delete_graph.dump(:ttl) #if ConfigFile[:debug] # puts insert_graph.dump(:ttl) #if ConfigFile[:debug] # puts where_graph.dump(:ttl) #if ConfigFile[:debug] # if ConfigFile[:debug] delete_insert_query = SPARQL::Client::Update::DeleteInsert.new(delete_graph, insert_graph, where_graph, graph: insert_graph.name).to_s delete_insert_query.gsub!('_:p', '?p') # puts delete_insert_query data = sparql.query(delete_insert_query) # pp data # end # sparql.delete_insert(delete_graph, insert_graph, where_graph, graph: insert_graph.name) data = self.query.filter({ filters: { id: [id] } }).find_all.map { |m| m }&.first if data.nil? sparql.insert_data(insert_graph, graph: insert_graph.name) data = self.query.filter({ filters: { id: [id] } }).find_all.map { |m| m }&.first end end after_update_proc&.call(updated_klass, data) data rescue StandardError => e original_graph = as_graph(original_klass, false) Solis::LOGGER.error(e.) Solis::LOGGER.error original_graph.dump(:ttl) sparql.insert_data(original_graph, graph: original_graph.name) raise e end |
#valid? ⇒ Boolean
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/solis/model.rb', line 95 def valid? begin graph = as_graph(self, false) rescue Solis::Error::InvalidAttributeError => e Solis::LOGGER.error(e.) end shacl = SHACL.get_shapes(self.class.graph.instance_variable_get(:"@graph")) report = shacl.execute(graph) report.conform? rescue StandardError => e false end |