Module: ActiveFedora::SemanticNode

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) load_from_solr

Returns the value of attribute load_from_solr



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

def load_from_solr
  @load_from_solr
end

- (Object) relationships_loaded

Returns the value of attribute relationships_loaded



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

def relationships_loaded
  @relationships_loaded
end

- (Object) subject

Returns the value of attribute subject



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

def subject
  @subject
end

Instance Method Details

- (Object) add_relationship(predicate, target, literal = false)

Add a relationship to the Object.

Parameters:

  • predicate
  • object

    Either a string URI or an object that is a kind of ActiveFedora::Base



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

def add_relationship(predicate, target, literal=false)
  object_relations.add(predicate, target, literal)
  rels_ext.dirty = true
end

- (Object) assert_kind_of(n, o, t)



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

- (Object) build_statement(uri, predicate, target, literal = false)

Create an RDF statement

Parameters:

  • uri

    a string represending the subject

  • predicate

    a predicate symbol

  • target

    an object to store



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

def build_statement(uri, predicate, target, literal=false)
  ActiveSupport::Deprecation.warn("ActiveFedora::Base#build_statement has been deprecated.")
  raise "Not allowed anymore" if uri == :self
  target = target.internal_uri if target.respond_to? :internal_uri
  subject =  RDF::URI.new(uri)  #TODO cache
  unless literal or target.is_a? RDF::Resource
    begin
      target_uri = (target.is_a? URI) ? target : URI.parse(target)
      if target_uri.scheme.nil?
        raise ArgumentError, "Invalid target \"#{target}\". Must have namespace."
      end
      if target_uri.to_s =~ /\A[\w\-]+:[\w\-]+\Z/
        raise ArgumentError, "Invalid target \"#{target}\". Target should be a complete URI, and not a pid."
      end
    rescue URI::InvalidURIError
      raise ArgumentError, "Invalid target \"#{target}\". Target must be specified as a literal, or be a valid URI."
    end
  end
  if literal
    object = RDF::Literal.new(target)
  elsif target.is_a? RDF::Resource
    object = target
  else
    object = RDF::URI.new(target)
  end
  RDF::Statement.new(subject, find_graph_predicate(predicate), object)

end

- (Boolean) conforms_to?(model_class)

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.

Parameters:

  • the (Class)

    model class name to check if an object conforms_to that model

Returns:

  • (Boolean)

    true if this object conforms to the given model name



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_fedora/semantic_node.rb', line 44

def conforms_to?(model_class)
  if self.kind_of?(model_class)
    #check has model and class match
    mod = relationships.first(:predicate=>Predicates.find_graph_predicate(:has_model))
    if mod
      expected = self.class.to_class_uri
      if mod.object.to_s == expected
        return true
      else
        raise "has_model relationship check failed for model #{model_class} raising exception, expected: '#{expected}' actual: '#{mod.object.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

- (Object) ids_for_outbound(predicate)



153
154
155
156
157
158
# File 'lib/active_fedora/semantic_node.rb', line 153

def ids_for_outbound(predicate)
  (object_relations[predicate] || []).map do |o|
    o = o.to_s if o.kind_of? RDF::Literal
    o.kind_of?(String) ? o.gsub("info:fedora/", "") : o.pid
  end
end

- (Hash) inbound_relationship_predicates

Return hash of inbound relationship names and predicate pairs

Returns:

  • (Hash)

    A hash of inbound relationship names mapped to predicates used



195
196
197
# File 'lib/active_fedora/semantic_node.rb', line 195

def inbound_relationship_predicates
  relationship_predicates.has_key?(:inbound) ? relationship_predicates[:inbound] : {}
end

- (Object) inbound_relationships(response_format = :uri)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_fedora/semantic_node.rb', line 109

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)    
        #inbound relationships are always object properties
        items.push(object.internal_uri)
      else
        items.push(object)
      end
    end
    unless items.empty?
      rel_values.merge!({predicate=>items})
    end
  end
  return rel_values  
end

- (Object) load_relationships



146
147
148
149
150
151
# File 'lib/active_fedora/semantic_node.rb', line 146

def load_relationships
  self.relationships_loaded = true
  content = rels_ext.content
  return unless content.present?
  RelsExtDatastream.from_xml content, rels_ext
end

- (Object) object_relations



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

def object_relations
  load_relationships if !relationships_loaded
  @object_relations ||= RelationshipGraph.new
end

- (Hash) outbound_relationship_predicates

Return hash of outbound relationship names and predicate pairs

Returns:

  • (Hash)

    A hash of outbound relationship names mapped to predicates used



189
190
191
# File 'lib/active_fedora/semantic_node.rb', line 189

def outbound_relationship_predicates
  relationship_predicates.has_key?(:self) ? relationship_predicates[:self] : {}
end

- (Object) outbound_relationships



129
130
131
# File 'lib/active_fedora/semantic_node.rb', line 129

def outbound_relationships()
  relationships.statements
end

- (Hash) relationship_predicates

Return hash of relationship names and predicate pairs (inbound and outbound). It retrieves this information via the relationships_desc hash in the class.

Returns:

  • (Hash)

    A hash of relationship names (inbound and outbound) mapped to predicates used



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/active_fedora/semantic_node.rb', line 175

def relationship_predicates
  return @relationship_predicates if @relationship_predicates
  @relationship_predicates = {}
  relationships_desc.each_pair do |subj, names|
    @relationship_predicates[subj] = {}
    names.each_pair do |name, args|
      @relationship_predicates[subj][name] = args[:predicate]
    end
  end
  @relationship_predicates
end

- (Object) relationships(*args)

If no arguments are supplied, return the whole RDF::Graph. if a predicate is supplied as a parameter, then it returns the result of quering the graph with that predicate



135
136
137
138
139
140
141
142
143
144
# File 'lib/active_fedora/semantic_node.rb', line 135

def relationships(*args)
  load_relationships if !relationships_loaded

  if args.empty?
    raise "Must have internal_uri" unless internal_uri
    return object_relations.to_graph(internal_uri)
  end
  rels = object_relations[args.first] || []
  rels.map {|o| o.respond_to?(:internal_uri) ? o.internal_uri : o }   #TODO, could just return the object
end

- (Object) relationships_are_dirty



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

def relationships_are_dirty
  object_relations.dirty
end

- (Object) relationships_are_dirty=(val)



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

def relationships_are_dirty=(val)
  object_relations.dirty = val
end

- (Hash) relationships_desc

Return hash that persists relationship metadata defined by has_relationship calls

Examples:

For the following relationship


has_relationship "audio_records", :has_part, :type=>AudioRecord

Results in the following returned by relationships_desc
{:self=>{"audio_records"=>{:type=>AudioRecord, :singular=>nil, :predicate=>:has_part, :inbound=>false}}}

Returns:

  • (Hash)

    Hash of relationship subject (:self or :inbound) mapped to nested hashs of each relationship name mapped to another hash relationship options



168
169
170
# File 'lib/active_fedora/semantic_node.rb', line 168

def relationships_desc
  @relationships_desc ||= self.class.relationships_desc
end

- (Object) remove_relationship(predicate, obj, literal = false)

Remove a Rels-Ext relationship from the Object.

Parameters:

  • predicate
  • object

    Either a string URI or an object that responds to .pid



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

def remove_relationship(predicate, obj, literal=false)
  object_relations.delete(predicate, obj)
  self.relationships_are_dirty = true
  rels_ext.dirty = true
end