Module: ActiveFedora::SemanticNode
- Included in:
- Base, RelsExtDatastream
- Defined in:
- lib/active_fedora/semantic_node.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#internal_uri ⇒ Object
Returns the value of attribute internal_uri.
-
#load_from_solr ⇒ Object
Returns the value of attribute load_from_solr.
-
#named_relationship_desc ⇒ Object
Returns the value of attribute named_relationship_desc.
-
#relationships_are_dirty ⇒ Object
Returns the value of attribute relationships_are_dirty.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_named_relationship(name, object) ⇒ Object
** EXPERIMENTAL **.
- #add_relationship(relationship) ⇒ Object
- #assert_kind_of(n, o, t) ⇒ Object
-
#assert_kind_of_model(name, object, model_class) ⇒ Object
** EXPERIMENTAL **.
- #class_from_name(name) ⇒ Object
-
#inbound_named_relationship_predicates ⇒ Object
** EXPERIMENTAL **.
-
#inbound_relationship_names ⇒ Object
** EXPERIMENTAL **.
- #inbound_relationships(response_format = :uri) ⇒ Object
-
#is_named_relationship?(name, outbound_only = true) ⇒ Boolean
** EXPERIMENTAL **.
-
#kind_of_model?(model_class) ⇒ Boolean
** EXPERIMENTAL **.
-
#named_inbound_relationships ⇒ Object
** EXPERIMENTAL **.
-
#named_outbound_relationships ⇒ Object
** EXPERIMENTAL **.
-
#named_relationship(name) ⇒ Object
** EXPERIMENTAL **.
-
#named_relationship_predicates ⇒ Object
** EXPERIMENTAL **.
-
#named_relationship_predicates_from_class ⇒ Object
** EXPERIMENTAL **.
-
#named_relationship_type(name) ⇒ Object
** EXPERIMENTAL **.
-
#named_relationships(outbound_only = true) ⇒ Object
** EXPERIMENTAL **.
-
#named_relationships_desc ⇒ Object
** EXPERIMENTAL **.
-
#named_relationships_desc_from_class ⇒ Object
** EXPERIMENTAL **.
-
#named_relationships_from_class ⇒ Object
** EXPERIMENTAL **.
-
#outbound_named_relationship_predicates ⇒ Object
** EXPERIMENTAL **.
-
#outbound_relationship_names ⇒ Object
** EXPERIMENTAL **.
- #outbound_relationships ⇒ Object
-
#register_named_relationship(subject, name, predicate, opts) ⇒ Object
** EXPERIMENTAL **.
-
#register_named_subject(subject) ⇒ Object
** EXPERIMENTAL **.
- #register_predicate(subject, predicate) ⇒ Object
- #register_subject(subject) ⇒ Object
- #register_triple(subject, predicate, object) ⇒ Object
-
#relationship_exists?(subject, predicate, object) ⇒ Boolean
** EXPERIMENTAL **.
-
#relationship_names ⇒ Object
** EXPERIMENTAL **.
-
#relationships(outbound_only = true) ⇒ Object
If outbound_only is false, inbound relationships will be included.
- #relationships_from_class ⇒ Object
-
#remove_named_relationship(name, object) ⇒ Object
** EXPERIMENTAL **.
-
#remove_relationship(relationship) ⇒ Object
** EXPERIMENTAL **.
-
#to_rels_ext(pid, relationships = self.relationships) ⇒ Object
Creates a RELS-EXT datastream for insertion into a Fedora Object Note: This method is implemented on SemanticNode instead of RelsExtDatastream because SemanticNode contains the relationships array.
-
#unregister_triple(subject, predicate, object) ⇒ Object
** EXPERIMENTAL **.
Instance Attribute Details
#internal_uri ⇒ Object
Returns the value of attribute internal_uri.
6 7 8 |
# File 'lib/active_fedora/semantic_node.rb', line 6 def internal_uri @internal_uri end |
#load_from_solr ⇒ Object
Returns the value of attribute load_from_solr.
6 7 8 |
# File 'lib/active_fedora/semantic_node.rb', line 6 def load_from_solr @load_from_solr end |
#named_relationship_desc ⇒ Object
Returns the value of attribute named_relationship_desc.
6 7 8 |
# File 'lib/active_fedora/semantic_node.rb', line 6 def named_relationship_desc @named_relationship_desc end |
#relationships_are_dirty ⇒ Object
Returns the value of attribute relationships_are_dirty.
6 7 8 |
# File 'lib/active_fedora/semantic_node.rb', line 6 def relationships_are_dirty @relationships_are_dirty end |
Class Method Details
.included(klass) ⇒ Object
9 10 11 |
# File 'lib/active_fedora/semantic_node.rb', line 9 def self.included(klass) klass.extend(ClassMethods) end |
Instance Method Details
#add_named_relationship(name, object) ⇒ Object
** EXPERIMENTAL **
Add an outbound relationship for given named relationship See ActiveFedora::SemanticNode::ClassMethods.has_relationship
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/active_fedora/semantic_node.rb', line 326 def add_named_relationship(name, object) if is_named_relationship?(name,true) if named_relationships_desc[:self][name].has_key?(:type) klass = class_from_name(named_relationships_desc[:self][name][:type]) unless klass.nil? (assert_kind_of_model 'object', object, klass) end end #r = ActiveFedora::Relationship.new({:subject=>:self,:predicate=>outbound_named_relationship_predicates[name],:object=>object}) #add_relationship(r) add_relationship(outbound_named_relationship_predicates[name],object) else false end end |
#add_relationship(relationship) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/active_fedora/semantic_node.rb', line 17 def add_relationship(relationship) # Only accept ActiveFedora::Relationships as input arguments assert_kind_of 'relationship', relationship, ActiveFedora::Relationship self.relationships_are_dirty = true register_triple(relationship.subject, relationship.predicate, relationship.object) end |
#assert_kind_of(n, o, t) ⇒ Object
13 14 15 |
# File 'lib/active_fedora/semantic_node.rb', line 13 def assert_kind_of(n, o,t) raise "Assertion failure: #{n}: #{o} is not of type #{t}" unless o.kind_of?(t) end |
#assert_kind_of_model(name, object, model_class) ⇒ Object
** EXPERIMENTAL **
Throws an assertion error if kind_of_model? returns false for object and model_class
Parameters
name: Name of object (just label for output)
object: The object to test
model_class: The model class used to in kind_of_model? check on object
360 361 362 |
# File 'lib/active_fedora/semantic_node.rb', line 360 def assert_kind_of_model(name, object, model_class) raise "Assertion failure: #{name}: #{object.pid} does not have model #{model_class}, it has model #{relationships[:self][:has_model]}" unless object.kind_of_model?(model_class) end |
#class_from_name(name) ⇒ Object
391 392 393 394 395 |
# File 'lib/active_fedora/semantic_node.rb', line 391 def class_from_name(name) klass = name.to_s.split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)} (!klass.nil? && klass.is_a?(::Class)) ? klass : nil end |
#inbound_named_relationship_predicates ⇒ Object
** EXPERIMENTAL **
Return hash of inbound relationship names and predicate pairs
216 217 218 |
# File 'lib/active_fedora/semantic_node.rb', line 216 def inbound_named_relationship_predicates named_relationship_predicates.has_key?(:inbound) ? named_relationship_predicates[:inbound] : {} end |
#inbound_relationship_names ⇒ Object
** EXPERIMENTAL **
Return array of relationship names for all named inbound relationships (coming from other objects’ RELS-EXT and Solr)
255 256 257 |
# File 'lib/active_fedora/semantic_node.rb', line 255 def inbound_relationship_names named_relationships_desc.has_key?(:inbound) ? named_relationships_desc[:inbound].keys : [] end |
#inbound_relationships(response_format = :uri) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_fedora/semantic_node.rb', line 90 def inbound_relationships(response_format=:uri) rel_values = {} inbound_named_relationship_predicates.each_pair do |name,predicate| objects = self.send("#{name}",{:response_format=>response_format}) items = [] objects.each do |object| if (response_format == :uri) #create a Relationship object so that it generates the appropriate uri #inbound relationships are always object properties r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>predicate, :object=>object) items.push(r.object) else items.push(object) end end unless items.empty? rel_values.merge!({predicate=>items}) end end return rel_values end |
#is_named_relationship?(name, outbound_only = true) ⇒ Boolean
** EXPERIMENTAL **
Returns true if the given relationship name is a named relationship
Parameters
name: Name of relationship
outbound_only: If false checks inbound relationships as well (defaults to true)
280 281 282 283 284 285 286 |
# File 'lib/active_fedora/semantic_node.rb', line 280 def is_named_relationship?(name, outbound_only=true) if outbound_only outbound_relationship_names.include?(name) else (outbound_relationship_names.include?(name)||inbound_relationship_names.include?(name)) end end |
#kind_of_model?(model_class) ⇒ Boolean
** EXPERIMENTAL **
Checks that this object is matches the model class passed in. It requires two steps to pass to return true
1. It has a hasModel relationship of the same model
2. kind_of? returns true for the model passed in
This method can most often be used to detect if an object from Fedora that was created with a different model was then used to populate this object.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/active_fedora/semantic_node.rb', line 372 def kind_of_model?(model_class) if self.kind_of?(model_class) #check has model and class match if relationships[:self].has_key?(:has_model) r = ActiveFedora::Relationship.new(:subject=>:self, :predicate=>:has_model, :object=>ActiveFedora::ContentModel.pid_from_ruby_class(self.class)) if relationships[:self][:has_model].first.to_s.eql?(r.object.to_s) return true else raise "has_model relationship check failed for model #{model_class} raising exception, expected: '#{r.object.to_s}' actual: '#{relationships[:self][:has_model].to_s}'" end else raise "has_model relationship does not exist for model #{model_class} check raising exception" end else raise "kind_of? check failed for model #{model_class}, actual #{self.class} raising exception" end return false end |
#named_inbound_relationships ⇒ Object
** EXPERIMENTAL **
Return hash of named_relationships defined within other objects’ RELS-EXT It returns a hash of relationship name to arrays of objects. It requeries solr each time this method is called.
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/active_fedora/semantic_node.rb', line 192 def named_inbound_relationships rels = {} if named_relationships_desc.has_key?(:inbound)&&!named_relationships_desc[:inbound].empty?() inbound_rels = inbound_relationships if named_relationship_predicates.has_key?(:inbound) named_relationship_predicates[:inbound].each do |name, predicate| rels[name] = inbound_rels.has_key?(predicate) ? inbound_rels[predicate] : [] end end end return rels end |
#named_outbound_relationships ⇒ Object
** EXPERIMENTAL **
Return hash of named_relationships defined within this object’s RELS-EXT It returns a hash of relationship name to arrays of objects
270 271 272 |
# File 'lib/active_fedora/semantic_node.rb', line 270 def named_outbound_relationships named_relationships_desc.has_key?(:self) ? named_relationships[:self] : {} end |
#named_relationship(name) ⇒ Object
** EXPERIMENTAL **
Return array of objects for a given relationship name
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/active_fedora/semantic_node.rb', line 140 def named_relationship(name) rels = nil if inbound_relationship_names.include?(name) rels = named_relationships(false)[:inbound][name] elsif outbound_relationship_names.include?(name) rels = named_relationships[:self][name] end rels = [] if rels.nil? return rels end |
#named_relationship_predicates ⇒ Object
** EXPERIMENTAL **
Return hash of relationship names and predicate pairs
223 224 225 |
# File 'lib/active_fedora/semantic_node.rb', line 223 def named_relationship_predicates @named_relationship_predicates ||= named_relationship_predicates_from_class end |
#named_relationship_predicates_from_class ⇒ Object
** EXPERIMENTAL **
Return hash of relationship names and predicate pairs from class
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/active_fedora/semantic_node.rb', line 230 def named_relationship_predicates_from_class rels = {} named_relationships_desc.each_pair do |subj, names| rels[subj] = {} names.each_pair do |name, args| rels[subj][name] = args[:predicate] end end return rels end |
#named_relationship_type(name) ⇒ Object
** EXPERIMENTAL **
Return the value of :type for the relationship for name passed in. It defaults to ActiveFedora::Base.
312 313 314 315 316 317 318 319 320 |
# File 'lib/active_fedora/semantic_node.rb', line 312 def named_relationship_type(name) if is_named_relationship?(name,true) subject = outbound_relationship_names.include?(name)? :self : :inbound if named_relationships_desc[subject][name].has_key?(:type) return class_from_name(named_relationships_desc[subject][name][:type]) end end return nil end |
#named_relationships(outbound_only = true) ⇒ Object
** EXPERIMENTAL **
Gets the named relationships hash of subject=>name=>object_array It has an optional parameter of outbound_only that defaults true. If false it will include inbound relationships in the results. Also, it will only reload outbound relationships if the relationships hash has changed since the last time this method was called.
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_fedora/semantic_node.rb', line 158 def named_relationships(outbound_only=true) #make sure to update if relationships have been updated if @relationships_are_dirty == true @named_relationships = named_relationships_from_class() @relationships_are_dirty = false end #this will get called normally on first fetch if relationships are not dirty @named_relationships ||= named_relationships_from_class() outbound_only ? @named_relationships : @named_relationships.merge(:inbound=>named_inbound_relationships) end |
#named_relationships_desc ⇒ Object
** EXPERIMENTAL **
Return hash that stores named relationship metadata defined by has_relationship calls
Example
For the following relationship
has_relationship "audio_records", :has_part, :type=>AudioRecord
Results in the following returned by named_relationships_desc
{:self=>{"audio_records"=>{:type=>AudioRecord, :singular=>nil, :predicate=>:has_part, :inbound=>false}}}
297 298 299 |
# File 'lib/active_fedora/semantic_node.rb', line 297 def named_relationships_desc @named_relationships_desc ||= named_relationships_desc_from_class end |
#named_relationships_desc_from_class ⇒ Object
** EXPERIMENTAL **
Get class instance variable named_relationships_desc that holds has_relationship metadata
304 305 306 |
# File 'lib/active_fedora/semantic_node.rb', line 304 def named_relationships_desc_from_class self.class.named_relationships_desc end |
#named_relationships_from_class ⇒ Object
** EXPERIMENTAL **
Gets named relationships from the class using the current relationships hash and relationship name,predicate pairs.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/active_fedora/semantic_node.rb', line 174 def named_relationships_from_class() rels = {} named_relationship_predicates.each_pair do |subj, names| if relationships.has_key?(subj) rels[subj] = {} names.each_pair do |name, predicate| rels[subj][name] = (relationships[subj].has_key?(predicate) ? relationships[subj][predicate] : []) end end end return rels end |
#outbound_named_relationship_predicates ⇒ Object
** EXPERIMENTAL **
Return hash of outbound relationship names and predicate pairs
209 210 211 |
# File 'lib/active_fedora/semantic_node.rb', line 209 def outbound_named_relationship_predicates named_relationship_predicates.has_key?(:self) ? named_relationship_predicates[:self] : {} end |
#outbound_relationship_names ⇒ Object
** EXPERIMENTAL **
Return array of relationship names for all named outbound relationships (coming from this object’s RELS-EXT)
262 263 264 |
# File 'lib/active_fedora/semantic_node.rb', line 262 def outbound_relationship_names named_relationships_desc.has_key?(:self) ? named_relationships_desc[:self].keys : [] end |
#outbound_relationships ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/active_fedora/semantic_node.rb', line 112 def outbound_relationships() if !internal_uri.nil? && !relationships[internal_uri].nil? return relationships[:self].merge(relationships[internal_uri]) else return relationships[:self] end end |
#register_named_relationship(subject, name, predicate, opts) ⇒ Object
** EXPERIMENTAL **
Internal method that adds relationship name and predicate pair to either an outbound (:self) or inbound (:inbound) relationship types. This method just calls the class method counterpart of this method.
56 57 58 |
# File 'lib/active_fedora/semantic_node.rb', line 56 def register_named_relationship(subject, name, predicate, opts) self.class.register_named_relationship(subject, name, predicate, opts) end |
#register_named_subject(subject) ⇒ Object
** EXPERIMENTAL **
Internal method that ensures a relationship subject such as :self and :inbound exist within the named_relationships_desc hash tracking named relationships metadata. This method just calls the class method counterpart of this method.
48 49 50 |
# File 'lib/active_fedora/semantic_node.rb', line 48 def register_named_subject(subject) self.class.register_named_subject(subject) end |
#register_predicate(subject, predicate) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/active_fedora/semantic_node.rb', line 36 def register_predicate(subject, predicate) register_subject(subject) if !relationships[subject].has_key?(predicate) relationships[subject][predicate] = [] end end |
#register_subject(subject) ⇒ Object
30 31 32 33 34 |
# File 'lib/active_fedora/semantic_node.rb', line 30 def register_subject(subject) if !relationships.has_key?(subject) relationships[subject] = {} end end |
#register_triple(subject, predicate, object) ⇒ Object
24 25 26 27 28 |
# File 'lib/active_fedora/semantic_node.rb', line 24 def register_triple(subject, predicate, object) register_subject(subject) register_predicate(subject, predicate) relationships[subject][predicate] << object end |
#relationship_exists?(subject, predicate, object) ⇒ Boolean
** EXPERIMENTAL **
Returns true if a relationship exists for the given subject, predicate, and object triple
83 84 85 86 87 88 |
# File 'lib/active_fedora/semantic_node.rb', line 83 def relationship_exists?(subject, predicate, object) outbound_only = (subject != :inbound) #cache the call in case it is retrieving inbound as well, don't want to hit solr too many times cached_relationships = relationships(outbound_only) cached_relationships.has_key?(subject)&&cached_relationships[subject].has_key?(predicate)&&cached_relationships[subject][predicate].include?(object) end |
#relationship_names ⇒ Object
** EXPERIMENTAL **
Return array all relationship names
244 245 246 247 248 249 250 |
# File 'lib/active_fedora/semantic_node.rb', line 244 def relationship_names names = [] named_relationships_desc.each_key do |subject| names = names.concat(named_relationships_desc[subject].keys) end names end |
#relationships(outbound_only = true) ⇒ Object
If outbound_only is false, inbound relationships will be included.
121 122 123 124 |
# File 'lib/active_fedora/semantic_node.rb', line 121 def relationships(outbound_only=true) @relationships ||= relationships_from_class outbound_only ? @relationships : @relationships.merge(:inbound=>inbound_relationships) end |
#relationships_from_class ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_fedora/semantic_node.rb', line 126 def relationships_from_class rels = {} self.class.relationships.each_pair do |subj, pred| rels[subj] = {} pred.each_key do |pred_key| rels[subj][pred_key] = [] end end return rels end |
#remove_named_relationship(name, object) ⇒ Object
** EXPERIMENTAL **
Remove an object from the named relationship
345 346 347 348 349 350 351 |
# File 'lib/active_fedora/semantic_node.rb', line 345 def remove_named_relationship(name, object) if is_named_relationship?(name,true) remove_relationship(outbound_named_relationship_predicates[name],object) else return false end end |
#remove_relationship(relationship) ⇒ Object
** EXPERIMENTAL **
Remove the given ActiveFedora::Relationship from this object
63 64 65 66 |
# File 'lib/active_fedora/semantic_node.rb', line 63 def remove_relationship(relationship) @relationships_are_dirty = true unregister_triple(relationship.subject, relationship.predicate, relationship.object) end |
#to_rels_ext(pid, relationships = self.relationships) ⇒ Object
Creates a RELS-EXT datastream for insertion into a Fedora Object Note: This method is implemented on SemanticNode instead of RelsExtDatastream because SemanticNode contains the relationships array
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/active_fedora/semantic_node.rb', line 401 def to_rels_ext(pid, relationships=self.relationships) starter_xml = <<-EOL <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="info:fedora/#{pid}"> </rdf:Description> </rdf:RDF> EOL xml = REXML::Document.new(starter_xml) # Iterate through the hash of predicates, adding an element to the RELS-EXT for each "object" in the predicate's corresponding array. # puts "" # puts "Iterating through a(n) #{self.class}" # puts "=> whose relationships are #{self.relationships.inspect}" # puts "=> and whose outbound relationships are #{self.outbound_relationships.inspect}" self.outbound_relationships.each do |predicate, targets_array| targets_array.each do |target| xmlns=String.new case predicate when :has_model, "hasModel", :hasModel xmlns="info:fedora/fedora-system:def/model#" begin rel_predicate = self.class.predicate_lookup(predicate,xmlns) rescue UnregisteredPredicateError xmlns = nil rel_predicate = nil end else xmlns="info:fedora/fedora-system:def/relations-external#" begin rel_predicate = self.class.predicate_lookup(predicate,xmlns) rescue UnregisteredPredicateError xmlns = nil rel_predicate = nil end end unless xmlns && rel_predicate rel_predicate, xmlns = self.class.find_predicate(predicate) end # puts ". #{predicate} #{target} #{xmlns}" literal = URI.parse(target).scheme.nil? if literal xml.root.elements["rdf:Description"].add_element(rel_predicate, {"xmlns" => "#{xmlns}"}).add_text(target) else xml.root.elements["rdf:Description"].add_element(rel_predicate, {"xmlns" => "#{xmlns}", "rdf:resource"=>target}) end end end xml.to_s end |
#unregister_triple(subject, predicate, object) ⇒ Object
** EXPERIMENTAL **
Remove the subject, predicate, and object triple from the relationships hash
71 72 73 74 75 76 77 78 |
# File 'lib/active_fedora/semantic_node.rb', line 71 def unregister_triple(subject, predicate, object) if relationship_exists?(subject, predicate, object) relationships[subject][predicate].delete_if {|curObj| curObj == object} relationships[subject].delete(predicate) if relationships[subject][predicate].nil? || relationships[subject][predicate].empty? else return false end end |