Module: ActiveFedora::SemanticNode

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#internal_uriObject

Returns the value of attribute internal_uri.



9
10
11
# File 'lib/active_fedora/semantic_node.rb', line 9

def internal_uri
  @internal_uri
end

#load_from_solrObject

Returns the value of attribute load_from_solr.



9
10
11
# File 'lib/active_fedora/semantic_node.rb', line 9

def load_from_solr
  @load_from_solr
end

#relationships_are_dirtyObject

Returns the value of attribute relationships_are_dirty.



9
10
11
# File 'lib/active_fedora/semantic_node.rb', line 9

def relationships_are_dirty
  @relationships_are_dirty
end

Class Method Details

.included(klass) ⇒ Object



12
13
14
15
# File 'lib/active_fedora/semantic_node.rb', line 12

def self.included(klass)
  klass.extend(ClassMethods)
  klass.send(:include, ActiveFedora::RelationshipsHelper)
end

Instance Method Details

#add_relationship(relationship) ⇒ Object



21
22
23
24
25
26
# File 'lib/active_fedora/semantic_node.rb', line 21

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



17
18
19
# File 'lib/active_fedora/semantic_node.rb', line 17

def assert_kind_of(n, o,t)
  raise "Assertion failure: #{n}: #{o} is not of type #{t}" unless o.kind_of?(t)
end

#inbound_relationships(response_format = :uri) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_fedora/semantic_node.rb', line 77

def inbound_relationships(response_format=:uri)
  rel_values = {}
  inbound_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

#outbound_relationshipsObject



99
100
101
102
103
104
105
# File 'lib/active_fedora/semantic_node.rb', line 99

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_predicate(subject, predicate) ⇒ Object



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

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

#register_subject(subject) ⇒ Object



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

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

#register_triple(subject, predicate, object) ⇒ Object



28
29
30
31
32
# File 'lib/active_fedora/semantic_node.rb', line 28

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)


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

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

#relationships(outbound_only = true) ⇒ Object

If outbound_only is false, inbound relationships will be included.



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

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

#relationships_from_classObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/active_fedora/semantic_node.rb', line 113

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_relationship(relationship) ⇒ Object

** EXPERIMENTAL **

Remove the given ActiveFedora::Relationship from this object



50
51
52
53
# File 'lib/active_fedora/semantic_node.rb', line 50

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

Parameters:

  • pid (String)
  • relationships (Hash) (defaults to: self.relationships)

    (optional) @default self.relationships



128
129
130
131
132
133
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
# File 'lib/active_fedora/semantic_node.rb', line 128

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



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

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