Module: ActiveFedora::SemanticNode

Includes:
MediaShelfClassLevelInheritableAttributes
Included in:
Base, RelsExtDatastream
Defined in:
lib/active_fedora/semantic_node.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PREDICATE_MAPPINGS =
Hash[:is_member_of => "isMemberOf",
:has_member => "hasMember",
:is_part_of => "isPartOf",
:has_part => "hasPart",
:is_member_of_collection => "isMemberOfCollection",
:has_collection_member => "hasCollectionMember",
:is_constituent_of => "isConstituentOf",
:has_constituent => "hasConstituent",
:is_subset_of => "isSubsetOf",
:has_subset => "hasSubset",
:is_derivation_of => "isDerivationOf",
:has_derivation => "hasDerivation",
:is_dependent_of => "isDependentOf",
:has_dependent => "hasDependent",
:is_description_of => "isDescriptionOf",
:has_description => "hasDescription",
:is_metadata_for => "isMetadataFor",
:has_metadata => "hasMetadata",
:is_annotation_of => "isAnnotationOf",
:has_annotation => "hasAnnotation",
:has_equivalent => "hasEquivalent",
:conforms_to => "conformsTo",
:has_model => "hasModel"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#internal_uriObject

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_solrObject

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_descObject

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_dirtyObject

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



33
34
35
# File 'lib/active_fedora/semantic_node.rb', line 33

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



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/active_fedora/semantic_node.rb', line 348

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



40
41
42
43
44
45
# File 'lib/active_fedora/semantic_node.rb', line 40

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



36
37
38
# File 'lib/active_fedora/semantic_node.rb', line 36

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


382
383
384
# File 'lib/active_fedora/semantic_node.rb', line 382

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



413
414
415
416
417
# File 'lib/active_fedora/semantic_node.rb', line 413

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_predicatesObject

** EXPERIMENTAL **

Return hash of inbound relationship names and predicate pairs



238
239
240
# File 'lib/active_fedora/semantic_node.rb', line 238

def inbound_named_relationship_predicates
  named_relationship_predicates.has_key?(:inbound) ? named_relationship_predicates[:inbound] : {}
end

#inbound_relationship_namesObject

** EXPERIMENTAL **

Return array of relationship names for all named inbound relationships (coming from other objects’ RELS-EXT and Solr)



277
278
279
# File 'lib/active_fedora/semantic_node.rb', line 277

def inbound_relationship_names
    named_relationships_desc.has_key?(:inbound) ? named_relationships_desc[:inbound].keys : []
end

#inbound_relationships(response_format = :uri) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_fedora/semantic_node.rb', line 113

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
        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)

Returns:

  • (Boolean)


302
303
304
305
306
307
308
# File 'lib/active_fedora/semantic_node.rb', line 302

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.

Returns:

  • (Boolean)


394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/active_fedora/semantic_node.rb', line 394

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_relationshipsObject

** 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.



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/active_fedora/semantic_node.rb', line 214

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_relationshipsObject

** EXPERIMENTAL **

Return hash of named_relationships defined within this object’s RELS-EXT It returns a hash of relationship name to arrays of objects



292
293
294
# File 'lib/active_fedora/semantic_node.rb', line 292

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



162
163
164
165
166
167
168
169
170
171
# File 'lib/active_fedora/semantic_node.rb', line 162

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_predicatesObject

** EXPERIMENTAL **

Return hash of relationship names and predicate pairs



245
246
247
# File 'lib/active_fedora/semantic_node.rb', line 245

def named_relationship_predicates
  @named_relationship_predicates ||= named_relationship_predicates_from_class
end

#named_relationship_predicates_from_classObject

** EXPERIMENTAL **

Return hash of relationship names and predicate pairs from class



252
253
254
255
256
257
258
259
260
261
# File 'lib/active_fedora/semantic_node.rb', line 252

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.



334
335
336
337
338
339
340
341
342
# File 'lib/active_fedora/semantic_node.rb', line 334

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.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_fedora/semantic_node.rb', line 180

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_descObject

** 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}}}


319
320
321
# File 'lib/active_fedora/semantic_node.rb', line 319

def named_relationships_desc
  @named_relationships_desc ||= named_relationships_desc_from_class
end

#named_relationships_desc_from_classObject

** EXPERIMENTAL **

Get class instance variable named_relationships_desc that holds has_relationship metadata



326
327
328
# File 'lib/active_fedora/semantic_node.rb', line 326

def named_relationships_desc_from_class
  self.class.named_relationships_desc
end

#named_relationships_from_classObject

** EXPERIMENTAL **

Gets named relationships from the class using the current relationships hash and relationship name,predicate pairs.



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/active_fedora/semantic_node.rb', line 196

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_predicatesObject

** EXPERIMENTAL **

Return hash of outbound relationship names and predicate pairs



231
232
233
# File 'lib/active_fedora/semantic_node.rb', line 231

def outbound_named_relationship_predicates
  named_relationship_predicates.has_key?(:self) ? named_relationship_predicates[:self] : {}
end

#outbound_relationship_namesObject

** EXPERIMENTAL **

Return array of relationship names for all named outbound relationships (coming from this object’s RELS-EXT)



284
285
286
# File 'lib/active_fedora/semantic_node.rb', line 284

def outbound_relationship_names
    named_relationships_desc.has_key?(:self) ? named_relationships_desc[:self].keys : []
end

#outbound_relationshipsObject



134
135
136
137
138
139
140
# File 'lib/active_fedora/semantic_node.rb', line 134

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.



79
80
81
# File 'lib/active_fedora/semantic_node.rb', line 79

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.



71
72
73
# File 'lib/active_fedora/semantic_node.rb', line 71

def register_named_subject(subject)
  self.class.register_named_subject(subject)
end

#register_predicate(subject, predicate) ⇒ Object



59
60
61
62
63
64
# File 'lib/active_fedora/semantic_node.rb', line 59

def register_predicate(subject, predicate)
  register_subject(subject)
  if !relationships[subject].has_key?(predicate) 
    relationships[subject][predicate] = []
  end
end

#register_subject(subject) ⇒ Object



53
54
55
56
57
# File 'lib/active_fedora/semantic_node.rb', line 53

def register_subject(subject)
  if !relationships.has_key?(subject) 
      relationships[subject] = {} 
  end
end

#register_triple(subject, predicate, object) ⇒ Object



47
48
49
50
51
# File 'lib/active_fedora/semantic_node.rb', line 47

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

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/active_fedora/semantic_node.rb', line 106

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_namesObject

** EXPERIMENTAL **

Return array all relationship names



266
267
268
269
270
271
272
# File 'lib/active_fedora/semantic_node.rb', line 266

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.



143
144
145
146
# File 'lib/active_fedora/semantic_node.rb', line 143

def relationships(outbound_only=true)
  @relationships ||= relationships_from_class
  outbound_only ? @relationships : @relationships.merge(:inbound=>inbound_relationships)
end

#relationships_from_classObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/active_fedora/semantic_node.rb', line 148

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



367
368
369
370
371
372
373
# File 'lib/active_fedora/semantic_node.rb', line 367

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



86
87
88
89
# File 'lib/active_fedora/semantic_node.rb', line 86

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



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 422

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#"
      else
        xmlns="info:fedora/fedora-system:def/relations-external#"
      end
      # puts ". #{predicate} #{target} #{xmlns}"
      xml.root.elements["rdf:Description"].add_element(self.class.predicate_lookup(predicate), {"xmlns" => "#{xmlns}", "rdf:resource"=>target})
    end
  end
  xml.to_s
end

#unregister_triple(subject, predicate, object) ⇒ Object

** EXPERIMENTAL **

Remove the subject, predicate, and object triple from the relationships hash



94
95
96
97
98
99
100
101
# File 'lib/active_fedora/semantic_node.rb', line 94

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