Class: ActiveFedora::RelationshipGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/relationship_graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRelationshipGraph

Returns a new instance of RelationshipGraph.



7
8
9
10
# File 'lib/active_fedora/relationship_graph.rb', line 7

def initialize 
  self.dirty = false
  self.relationships = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#dirtyObject

Returns the value of attribute dirty.



4
5
6
# File 'lib/active_fedora/relationship_graph.rb', line 4

def dirty
  @dirty
end

#relationshipsObject

Returns the value of attribute relationships.



4
5
6
# File 'lib/active_fedora/relationship_graph.rb', line 4

def relationships
  @relationships
end

Instance Method Details

#[](predicate) ⇒ Object



51
52
53
54
# File 'lib/active_fedora/relationship_graph.rb', line 51

def [](predicate)
  uri = uri_predicate(predicate)
  relationships[uri]
end

#add(predicate, object, literal = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_fedora/relationship_graph.rb', line 16

def add(predicate, object, literal=false)
  uri = uri_predicate(predicate)
  unless has_predicate? uri
    relationships[uri] = []
  end
  object = RDF::Literal.new(object) if literal
  unless relationships[uri].include?(object)
    @dirty = true
    relationships[uri] << object
  end
end

#build_statement(uri, predicate, target) ⇒ Object

Create an RDF statement

Parameters:

  • uri

    a string represending the subject

  • predicate (Symbol, URI)

    a predicate

  • target

    an object to store



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/relationship_graph.rb', line 73

def build_statement(uri, predicate, target)
  raise "Not allowed anymore" if uri == :self
  target = target.internal_uri if target.respond_to? :internal_uri
  subject =  RDF::URI.new(uri)  #TODO cache
  if target.is_a? RDF::Literal or target.is_a? RDF::Resource
    object = target
  else
    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
    object = RDF::URI.new(target)
  end
  RDF::Statement.new(subject, uri_predicate(predicate), object)

end

#delete(predicate, object = nil) ⇒ Object

Remove the statement matching the predicate and object

Parameters:

  • predicate (Symbol, URI)

    the predicate to delete

  • object (defaults to: nil)

    the object to delete, if nil, all statements with this predicate are deleted.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_fedora/relationship_graph.rb', line 31

def delete(predicate, object = nil)
  uri = uri_predicate(predicate)
  return unless has_predicate? uri 
  if object.nil?
    @dirty = true
    relationships.delete(uri)
    return
  end
  if relationships[uri].include?(object)
    @dirty = true
    relationships[uri].delete(object)
  end
  if object.respond_to?(:internal_uri) && relationships[uri].include?(object.internal_uri)
    @dirty = true
    relationships[uri].delete(object.internal_uri)
  elsif object.is_a? String 
    relationships[uri].delete_if{|obj| obj.respond_to?(:internal_uri) && obj.internal_uri == object}
  end
end

#has_predicate?(predicate) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_predicate?(predicate)
  relationships.has_key? uri_predicate(predicate)
end

#to_graph(subject_uri) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_fedora/relationship_graph.rb', line 56

def to_graph(subject_uri)
  # need to destroy relationships and rewrite it.
  subject =  RDF::URI.new(subject_uri)
  graph = RDF::Graph.new
  relationships.each do |predicate, values|
    values.each do |object|
      graph.insert build_statement(subject,  predicate, object)
    end
  end

  graph
end

#uri_predicate(predicate) ⇒ Object



97
98
99
100
# File 'lib/active_fedora/relationship_graph.rb', line 97

def uri_predicate(predicate)
  return predicate if predicate.kind_of? RDF::URI
  ActiveFedora::Predicates.find_graph_predicate(predicate)
end