Module: ActiveFedora::SemanticNode

Extended by:
ActiveSupport::Concern
Included in:
Base, RelsExtDatastream
Defined in:
lib/active_fedora/semantic_node.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute 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

#named_relationship_descObject

Returns the value of attribute named_relationship_desc.



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

def named_relationship_desc
  @named_relationship_desc
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

Instance Method Details

#add_relationship(relationship) ⇒ Object



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

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

TODO I think we can remove named_relationship_desc from attr_accessor - jcoyne



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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_fedora/semantic_node.rb', line 72

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

#load_inbound_relationship(name, predicate, opts = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/active_fedora/semantic_node.rb', line 174

def load_inbound_relationship(name, predicate, opts={})
  opts = {:rows=>25}.merge(opts)
  query = self.class.inbound_relationship_query(self.pid,"#{name}")
  return [] if query.empty?
  solr_result = SolrService.instance.conn.query(query, :rows=>opts[:rows])
  if opts[:response_format] == :solr
    return solr_result
  else
    if opts[:response_format] == :id_array
      id_array = []
      solr_result.hits.each do |hit|
        id_array << hit[SOLR_DOCUMENT_ID]
      end
      return id_array
    elsif opts[:response_format] == :load_from_solr || self.load_from_solr
      return ActiveFedora::SolrService.reify_solr_results(solr_result,{:load_from_solr=>true})
    else
      return ActiveFedora::SolrService.reify_solr_results(solr_result)
    end
  end
end

#load_outbound_relationship(name, predicate, opts = {}) ⇒ Object



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
# File 'lib/active_fedora/semantic_node.rb', line 196

def load_outbound_relationship(name, predicate, opts={})
  id_array = []
  if !outbound_relationships[predicate].nil? 
    outbound_relationships[predicate].each do |rel|
      id_array << rel.gsub("info:fedora/", "")
    end
  end
  if opts[:response_format] == :id_array  && !self.class.relationship_has_solr_filter_query?(:self,"#{name}")
    return id_array
  else
    query = self.class.outbound_relationship_query("#{name}",id_array)
    solr_result = SolrService.instance.conn.query(query)
    if opts[:response_format] == :solr
      return solr_result
    elsif opts[:response_format] == :id_array
      id_array = []
      solr_result.hits.each do |hit|
        id_array << hit[SOLR_DOCUMENT_ID]
      end
      return id_array
    elsif opts[:response_format] == :load_from_solr || self.load_from_solr
      return ActiveFedora::SolrService.reify_solr_results(solr_result,{:load_from_solr=>true})
    else
      return ActiveFedora::SolrService.reify_solr_results(solr_result)
    end
  end
end

#outbound_relationshipsObject



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

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



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

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

#register_subject(subject) ⇒ Object



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

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

#register_triple(subject, predicate, object) ⇒ Object



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

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)


65
66
67
68
69
70
# File 'lib/active_fedora/semantic_node.rb', line 65

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.



103
104
105
106
# File 'lib/active_fedora/semantic_node.rb', line 103

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

#relationships_from_classObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/active_fedora/semantic_node.rb', line 108

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



45
46
47
48
# File 'lib/active_fedora/semantic_node.rb', line 45

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



123
124
125
126
127
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
# File 'lib/active_fedora/semantic_node.rb', line 123

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



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

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