Class: RDF::Graph

Inherits:
Hash
  • Object
show all
Includes:
Parser
Defined in:
lib/lightrdf/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

parse, #serialize, #to_ntriples

Constructor Details

#initialize(triples = []) ⇒ Graph



12
13
14
15
16
17
# File 'lib/lightrdf/graph.rb', line 12

def initialize triples=[]
  super(nil)
  @ns   = {}
  @pool = {}
  self.triples = triples
end

Instance Attribute Details

#nsObject

Namespace set stored when parsing a file. Can be used for reference



7
8
9
# File 'lib/lightrdf/graph.rb', line 7

def ns
  @ns
end

#poolObject

Set of initialized RDF::NodeProxy objects



10
11
12
# File 'lib/lightrdf/graph.rb', line 10

def pool
  @pool
end

Instance Method Details

#<<(node) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/lightrdf/graph.rb', line 19

def << node
  node.graph.each do |subid, subnode|
    subnode.graph = self
    old_subnode = get(subid)
    self[subid] = old_subnode ? old_subnode.merge!(subnode) : subnode
  end
  old_node = get(node.id)
  self[node.id] = old_node ? old_node.merge!(node) : node
end

#==(graph2) ⇒ Object



79
80
81
# File 'lib/lightrdf/graph.rb', line 79

def == graph2
  triples.size == graph2.triples.size and contains?(graph2)
end

#[](id) ⇒ Object



29
30
31
# File 'lib/lightrdf/graph.rb', line 29

def [] id
  super(ID(id)) || Node.new(id, self)
end

#cloneObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/lightrdf/graph.rb', line 96

def clone
  graph = self.class.new
  each do |id, node|
    new_node = graph[id]
    node.each do |predicate, objects|
      new_node[predicate] = objects.map { |object| object.is_a?(Node) ? graph[object] : object }
    end
  end
  graph
end

#contains?(graph2) ⇒ Boolean

Returns true if the graph is contained into this one. It appropriately disambiguates blank nodes.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lightrdf/graph.rb', line 57

def contains? graph2
  triples  = self.triples
  triples2 = graph2.triples
  return false if triples2.size > triples.size
  
  bnodes  = self.keys.select   { |node| ID::bnode?(node) }
  bnodes2 = graph2.keys.select { |node| ID::bnode?(node) }

  # Tries all the mappings between blank nodes
  mappings = bnodes2.mappings(bnodes)

  mappings.each do |mapping|
    new_triples = triples2.map { |s,p,o| [ ID::bnode?(s) ? mapping[s] : s,
                                           ID::bnode?(p) ? mapping[p] : p,
                                           ID::bnode?(o) ? mapping[o] : o ]}
    diff = triples - new_triples
    return true if diff.size == triples.size - new_triples.size
  end
  
  false
end

#delete(value) ⇒ Object



107
108
109
# File 'lib/lightrdf/graph.rb', line 107

def delete value
  value.is_a?(Symbol) ? super : super(value.id)
end

#find(subject, predicate, object) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lightrdf/graph.rb', line 111

def find subject, predicate, object
  # Convert nodes into IDs
  subject   = subject.id   if subject.is_a?(Node)
  predicate = predicate.id if predicate.is_a?(Node)
  object    = object.id    if object.is_a?(Node)

  # Find nodes
  matches = triples.select { |s,p,o| (subject.nil?   or subject  ==[] or s==subject)   and
                                     (predicate.nil? or predicate==[] or p==predicate) and
                                     (object.nil?    or object   ==[] or o==object) }

  # Build results
  result = []
  result += matches.map {|t| t[0] } if subject.nil?
  result += matches.map {|t| t[1] } if predicate.nil?
  result += matches.map {|t| t[2] } if object.nil?
  
  # Return nodes, not IDs
  result.uniq.map { |id| id.is_a?(Symbol) ? Node(id, self) : id }
end

#getObject



4
# File 'lib/lightrdf/graph.rb', line 4

alias :get :[]

#inspectObject



33
34
35
# File 'lib/lightrdf/graph.rb', line 33

def inspect
  "{" + (values.map{|v| v.inspect} * ", ") + "}"
end

#merge(graph) ⇒ Object



40
41
42
# File 'lib/lightrdf/graph.rb', line 40

def merge graph
  RDF::Graph.new(triples + graph.triples)
end

#merge!(graph) ⇒ Object



36
37
38
39
# File 'lib/lightrdf/graph.rb', line 36

def merge! graph
  graph.values.each { |node| self << node }
  self
end

#node(id, type = nil) ⇒ Object

This is equivalent to [], but tries to return a NodeProxy It stores created objects in a pool



85
86
87
88
89
90
91
92
93
94
# File 'lib/lightrdf/graph.rb', line 85

def node id, type=nil
  id = ID(id)
  @pool[id] ||= begin
    node   = self[id]
    type ||= node.rdf::type.first
    klass  = Node.classes[type]
    raise Exception, "Unknown RDF-mapped type #{type}" unless klass
    klass.new(node)
  end
end

#nodesObject



32
# File 'lib/lightrdf/graph.rb', line 32

def nodes; values; end

#select(&block) ⇒ Object



51
52
53
# File 'lib/lightrdf/graph.rb', line 51

def select &block
  values.select &block
end

#triplesObject



43
44
45
46
# File 'lib/lightrdf/graph.rb', line 43

def triples
  triples = []; values.each { |n| triples += n.triples }
  triples
end

#triples=(triples) ⇒ Object



47
48
49
50
# File 'lib/lightrdf/graph.rb', line 47

def triples= triples
  self.clear
  triples.each { |s, p, o| self[s][p] += [o.is_a?(Symbol) ? self[o] : o] }
end