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
400 401 402 |
# File 'lib/solis/model.rb', line 400 def self.construct(level = 0) raise 'to bo implemented' end |
.graph ⇒ Object
352 353 354 |
# File 'lib/solis/model.rb', line 352 def self.graph @graph end |
.graph=(graph) ⇒ Object
356 357 358 |
# File 'lib/solis/model.rb', line 356 def self.graph=(graph) @graph = graph end |
.graph_name ⇒ Object
328 329 330 |
# File 'lib/solis/model.rb', line 328 def self.graph_name @graph_name end |
.graph_name=(graph_name) ⇒ Object
332 333 334 |
# File 'lib/solis/model.rb', line 332 def self.graph_name=(graph_name) @graph_name = graph_name end |
.graph_prefix ⇒ Object
340 341 342 |
# File 'lib/solis/model.rb', line 340 def self.graph_prefix @graph_prefix end |
.graph_prefix=(graph_prefix) ⇒ Object
336 337 338 |
# File 'lib/solis/model.rb', line 336 def self.graph_prefix=(graph_prefix) @graph_prefix = graph_prefix end |
.language ⇒ Object
360 361 362 |
# File 'lib/solis/model.rb', line 360 def self.language Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || @language || 'en' end |
.language=(language) ⇒ Object
364 365 366 |
# File 'lib/solis/model.rb', line 364 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 308 309 310 |
# 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 rescue StandardError => e Solis::LOGGER.error(e.) raise Solis::Error::GeneralError, "Error generating id for #{@model_name}" end |
.metadata ⇒ Object
312 313 314 |
# File 'lib/solis/model.rb', line 312 def self. @metadata end |
.metadata=(m) ⇒ Object
316 317 318 |
# File 'lib/solis/model.rb', line 316 def self.(m) @metadata = m end |
.model(level = 0) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/solis/model.rb', line 368 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
416 417 418 |
# File 'lib/solis/model.rb', line 416 def self.model_after_create(&blk) self.after_create_proc = blk end |
.model_after_delete(&blk) ⇒ Object
432 433 434 |
# File 'lib/solis/model.rb', line 432 def self.model_after_delete(&blk) self.after_delete_proc = blk end |
.model_after_read(&blk) ⇒ Object
408 409 410 |
# File 'lib/solis/model.rb', line 408 def self.model_after_read(&blk) self.after_read_proc = blk end |
.model_after_update(&blk) ⇒ Object
424 425 426 |
# File 'lib/solis/model.rb', line 424 def self.model_after_update(&blk) self.after_update_proc = blk end |
.model_before_create(&blk) ⇒ Object
412 413 414 |
# File 'lib/solis/model.rb', line 412 def self.model_before_create(&blk) self.before_create_proc = blk end |
.model_before_delete(&blk) ⇒ Object
428 429 430 |
# File 'lib/solis/model.rb', line 428 def self.model_before_delete(&blk) self.before_delete_proc = blk end |
.model_before_read(&blk) ⇒ Object
404 405 406 |
# File 'lib/solis/model.rb', line 404 def self.model_before_read(&blk) self.before_read_proc = blk end |
.model_before_update(&blk) ⇒ Object
420 421 422 |
# File 'lib/solis/model.rb', line 420 def self.model_before_update(&blk) self.before_update_proc = blk end |
.model_template(level = 0) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/solis/model.rb', line 385 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
324 325 326 |
# File 'lib/solis/model.rb', line 324 def self.shapes @shapes end |
.shapes=(s) ⇒ Object
320 321 322 |
# File 'lib/solis/model.rb', line 320 def self.shapes=(s) @shapes = s end |
.sparql_endpoint ⇒ Object
344 345 346 |
# File 'lib/solis/model.rb', line 344 def self.sparql_endpoint @sparql_endpoint end |
.sparql_endpoint=(sparql_endpoint) ⇒ Object
348 349 350 |
# File 'lib/solis/model.rb', line 348 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(self) self 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 value = .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 |