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
-
#model_class_name(plural = false) ⇒ Object
Removed the ‘name’ instance method to avoid conflicts with ‘name’ attributes Use model_class_name instead, or access @model_name directly.
- #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 |
# 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) 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
442 443 444 |
# File 'lib/solis/model.rb', line 442 def self.construct(level = 0) raise 'to be implemented' end |
.graph ⇒ Object
391 392 393 |
# File 'lib/solis/model.rb', line 391 def self.graph @graph end |
.graph=(graph) ⇒ Object
395 396 397 |
# File 'lib/solis/model.rb', line 395 def self.graph=(graph) @graph = graph end |
.graph_name ⇒ Object
367 368 369 |
# File 'lib/solis/model.rb', line 367 def self.graph_name @graph_name end |
.graph_name=(graph_name) ⇒ Object
371 372 373 |
# File 'lib/solis/model.rb', line 371 def self.graph_name=(graph_name) @graph_name = graph_name end |
.graph_prefix ⇒ Object
379 380 381 |
# File 'lib/solis/model.rb', line 379 def self.graph_prefix @graph_prefix end |
.graph_prefix=(graph_prefix) ⇒ Object
375 376 377 |
# File 'lib/solis/model.rb', line 375 def self.graph_prefix=(graph_prefix) @graph_prefix = graph_prefix end |
.language ⇒ Object
399 400 401 |
# File 'lib/solis/model.rb', line 399 def self.language Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || @language || 'en' end |
.language=(language) ⇒ Object
403 404 405 |
# File 'lib/solis/model.rb', line 403 def self.language=(language) @language = language end |
.make_id_for(model) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/solis/model.rb', line 327 def self.make_id_for(model) raise "I need a SPARQL endpoint" if self.sparql_endpoint.nil? sparql = Solis::Store::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) elsif id.to_s =~ /^https?:\/\// id = id.to_s.split('/').last LOGGER.info("ID(#{id}) normalised for #{self.name}") 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 #{self.name}" end |
.metadata ⇒ Object
351 352 353 |
# File 'lib/solis/model.rb', line 351 def self. end |
.metadata=(m) ⇒ Object
355 356 357 |
# File 'lib/solis/model.rb', line 355 def self.=(m) = m end |
.model(level = 0) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/solis/model.rb', line 407 def self.model(level = 0) m = { type: self.name.tableize, attributes: {} } self.[:attributes].each do |attribute, | is_array = (([:maxcount].nil? || ([:maxcount].to_i > 1)) && ![:datatype].eql?(:lang_string)) attribute_name = is_array ? "#{attribute}[]" : attribute attribute_name = [:mincount].to_i > 0 ? "#{attribute_name}*" : attribute_name 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_name.to_sym] = cm[:attributes] else m[:attributes][attribute_name.to_sym] = { description: [:comment]&.value, mandatory: ([:mincount].to_i > 0), data_type: [:datatype] } m[:attributes][attribute_name.to_sym][:order] = [:order]&.value.to_i if .key?(:order) && ![:order].nil? end end m end |
.model_after_create(&blk) ⇒ Object
458 459 460 |
# File 'lib/solis/model.rb', line 458 def self.model_after_create(&blk) self.after_create_proc = blk end |
.model_after_delete(&blk) ⇒ Object
474 475 476 |
# File 'lib/solis/model.rb', line 474 def self.model_after_delete(&blk) self.after_delete_proc = blk end |
.model_after_read(&blk) ⇒ Object
450 451 452 |
# File 'lib/solis/model.rb', line 450 def self.model_after_read(&blk) self.after_read_proc = blk end |
.model_after_update(&blk) ⇒ Object
466 467 468 |
# File 'lib/solis/model.rb', line 466 def self.model_after_update(&blk) self.after_update_proc = blk end |
.model_before_create(&blk) ⇒ Object
454 455 456 |
# File 'lib/solis/model.rb', line 454 def self.model_before_create(&blk) self.before_create_proc = blk end |
.model_before_delete(&blk) ⇒ Object
470 471 472 |
# File 'lib/solis/model.rb', line 470 def self.model_before_delete(&blk) self.before_delete_proc = blk end |
.model_before_read(&blk) ⇒ Object
446 447 448 |
# File 'lib/solis/model.rb', line 446 def self.model_before_read(&blk) self.before_read_proc = blk end |
.model_before_update(&blk) ⇒ Object
462 463 464 |
# File 'lib/solis/model.rb', line 462 def self.model_before_update(&blk) self.before_update_proc = blk end |
.model_template(level = 0) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/solis/model.rb', line 427 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
363 364 365 |
# File 'lib/solis/model.rb', line 363 def self.shapes @shapes end |
.shapes=(s) ⇒ Object
359 360 361 |
# File 'lib/solis/model.rb', line 359 def self.shapes=(s) @shapes = s end |
.sparql_endpoint ⇒ Object
383 384 385 |
# File 'lib/solis/model.rb', line 383 def self.sparql_endpoint @sparql_endpoint end |
.sparql_endpoint=(sparql_endpoint) ⇒ Object
387 388 389 |
# File 'lib/solis/model.rb', line 387 def self.sparql_endpoint=(sparql_endpoint) @sparql_endpoint = sparql_endpoint end |
Instance Method Details
#destroy ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/solis/model.rb', line 108 def destroy raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? sparql = Solis::Store::Sparql::Client.new(self.class.sparql_endpoint) raise Solis::Error::QueryError, "#{self.id} is still referenced, refusing to delete" if is_referenced?(sparql) before_delete_proc&.call(self) query = %( with <#{self.class.graph_name}> delete {?s ?p ?o} where { values ?s {<#{self.graph_id}>} ?s ?p ?o } ) result = sparql.query(query) if result.count > 0 if result.first.bound?(result.variable_names.first) && result.first[result.variable_names.first].value =~ /done$/ after_delete_proc&.call(self) else after_delete_proc&.call(result) end end result end |
#dump(format = :ttl, resolve_all = true) ⇒ Object
84 85 86 87 |
# File 'lib/solis/model.rb', line 84 def dump(format = :ttl, resolve_all = true) graph = as_graph(self, resolve_all) graph.dump(format) end |
#exists?(sparql) ⇒ Boolean
323 324 325 |
# File 'lib/solis/model.rb', line 323 def exists?(sparql) sparql.query("ASK WHERE { <#{self.graph_id}> ?p ?o }") end |
#graph_id ⇒ Object
315 316 317 |
# File 'lib/solis/model.rb', line 315 def graph_id "#{self.class.graph_name}#{tableized_class_name(self)}/#{self.id}" end |
#is_referenced?(sparql) ⇒ Boolean
319 320 321 |
# File 'lib/solis/model.rb', line 319 def is_referenced?(sparql) sparql.query("ASK WHERE { ?s ?p <#{self.graph_id}>. filter (!contains(str(?s), 'audit') && !contains(str(?p), 'audit'))}") end |
#model_class_name(plural = false) ⇒ Object
Removed the ‘name’ instance method to avoid conflicts with ‘name’ attributes Use model_class_name instead, or access @model_name directly
62 63 64 65 66 67 68 |
# File 'lib/solis/model.rb', line 62 def model_class_name(plural = false) if plural @model_plural_name else @model_name end end |
#query ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/solis/model.rb', line 70 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
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 185 186 187 |
# File 'lib/solis/model.rb', line 136 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 readonly_list = (Solis::Options.instance.get[:embedded_readonly] || []).map(&:to_s) attributes.each_pair do |key, value| unless self.class.[:attributes][key][:node].nil? 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) if readonly_entity?(, readonly_list) # Readonly entities (code tables) should never be modified # Only verify they exist, do not create or update them unless .exists?(sparql) Solis::LOGGER.warn("#{embedded.class.name} (id: #{embedded.id}) is readonly but does not exist in database. Skipping.") end else # Non-readonly entities can be created or updated if .exists?(sparql) = properties_to_hash() .update(, validate_dependencies, false) else .save(validate_dependencies, false) end end end end end graph = as_graph(self, validate_dependencies) 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. raise e end |
#to_graph(resolve_all = true) ⇒ Object
89 90 91 |
# File 'lib/solis/model.rb', line 89 def to_graph(resolve_all = true) as_graph(self, resolve_all) end |
#to_ttl(resolve_all = true) ⇒ Object
79 80 81 82 |
# File 'lib/solis/model.rb', line 79 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
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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/solis/model.rb', line 189 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: self.class.language, filters: { id: [id] } }).find_all.map { |m| m }&.first raise Solis::Error::NotFoundError if original_klass.nil? updated_klass = original_klass.deep_dup # Cache readonly entities list once readonly_list = (Solis::Options.instance.get[:embedded_readonly] || []).map(&:to_s) # Track entities to potentially delete entities_to_check_for_deletion = {} attributes.each_pair do |key, value| unless original_klass.class.[:attributes][key][:node].nil? value = [value] unless value.is_a?(Array) # Get original embedded entities for this attribute = original_klass.instance_variable_get("@#{key}") = [] unless .nil? || .is_a?(Array) ||= [] # Track original IDs original_ids = .map { |e| solis_model?(e) ? e.id : nil }.compact # Build new array of embedded entities = [] new_ids = [] value.each do |sub_value| = self.class.graph.shape_as_model(original_klass.class.[:attributes][key][:datatype].to_s).new(sub_value) new_ids << .id if .id if readonly_entity?(, readonly_list) # Readonly entities (code tables) should never be modified # Only verify they exist, do not create or update them if .exists?(sparql) << else Solis::LOGGER.warn("#{embedded.class.name} (id: #{embedded.id}) is readonly but does not exist in database. Skipping.") end else # Non-readonly entities can be created or updated if .exists?(sparql) = properties_to_hash() .update(, validate_dependencies, false) << else = .save(validate_dependencies, false) << end end end # Identify orphaned entities (in original but not in new) # Note: Readonly entities will be filtered out in delete_orphaned_entities orphaned_ids = original_ids - new_ids unless orphaned_ids.empty? orphaned_entities = .select { |e| solis_model?(e) && orphaned_ids.include?(e.id) } entities_to_check_for_deletion[key] = orphaned_entities end # Replace entire array with new values maxcount = original_klass.class.[:attributes][key][:maxcount] value = maxcount && maxcount == 1 ? .first : end updated_klass.instance_variable_set("@#{key}", value) end before_update_proc&.call(original_klass, updated_klass) properties_original_klass = properties_to_hash(original_klass) properties_updated_klass = properties_to_hash(updated_klass) if Hashdiff.best_diff(properties_original_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}#{tableized_class_name(self)}/#{id}"), data: RDF::Repository.new) if id.is_a?(Array) id.each do |i| where_graph << [RDF::URI("#{self.class.graph_name}#{tableized_class_name(self)}/#{i}"), :p, :o] end else where_graph << [RDF::URI("#{self.class.graph_name}#{tableized_class_name(self)}/#{id}"), :p, :o] end insert_graph = as_graph(updated_klass, true) 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') data = sparql.query(delete_insert_query) 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 # Delete orphaned entities after successful update delete_orphaned_entities(entities_to_check_for_deletion, sparql) end after_update_proc&.call(updated_klass, data) data rescue StandardError => e original_graph = as_graph(original_klass, false) if defined?(original_klass) && original_klass Solis::LOGGER.error(e.) Solis::LOGGER.error original_graph.dump(:ttl) if defined?(original_graph) && original_graph Solis::LOGGER.error delete_insert_query if defined?(delete_insert_query) sparql.insert_data(original_graph, graph: original_graph.name) if defined?(original_graph) && original_graph && defined?(sparql) && sparql raise e end |
#valid? ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/solis/model.rb', line 93 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 |