lightrdf 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.3.0 2011-03-17
2
+
3
+ * Recursive addition of nodes to a graph
4
+ * merge and merge! for both nodes and graphs
5
+
1
6
  === 0.2.6 2011-03-16
2
7
 
3
8
  * Fix in graph consistency when building a graph from triples
@@ -1,6 +1,7 @@
1
1
  module RDF
2
2
  class Graph < Hash
3
3
  include Parser
4
+ alias :get :[]
4
5
 
5
6
  # Namespace set stored when parsing a file. Can be used for reference
6
7
  attr_accessor :ns
@@ -16,24 +17,28 @@ module RDF
16
17
  end
17
18
 
18
19
  def << node
19
- self[node.id] = node
20
+ node.graph.each do |subid, subnode|
21
+ subnode.graph = self
22
+ old_subnode = get(subid)
23
+ self[subid] = old_subnode ? old_subnode.merge!(subnode) : subnode
24
+ end
25
+ old_node = get(node.id)
26
+ self[node.id] = old_node ? old_node.merge!(node) : node
20
27
  end
21
28
 
22
29
  def [] id
23
30
  super(ID(id)) || Node.new(id, self)
24
31
  end
25
- def []= id, node
26
- node.graph = self
27
- super ID(id), node
28
- end
29
32
  def nodes; values; end
30
33
  def inspect
31
34
  "{" + (values.map{|v| v.inspect} * ", ") + "}"
32
35
  end
36
+ def merge! graph
37
+ graph.values.each { |node| self << node }
38
+ self
39
+ end
33
40
  def merge graph
34
- new_graph = Graph.new
35
- new_graph.triples = triples + graph.triples
36
- new_graph
41
+ RDF::Graph.new(triples + graph.triples)
37
42
  end
38
43
  def triples
39
44
  triples = []; values.each { |n| triples += n.triples }
data/lib/lightrdf/node.rb CHANGED
@@ -30,7 +30,7 @@ module RDF
30
30
  def initialize id=nil, graph=nil
31
31
  @id = ID(id)
32
32
  @graph = graph || Graph.new
33
- @graph << self
33
+ @graph[@id] = self
34
34
  end
35
35
 
36
36
  def method_missing method, *args
@@ -66,26 +66,25 @@ module RDF
66
66
  triples
67
67
  end
68
68
 
69
- def all_triples done=[]
70
- return [] if done.include?(self)
71
- done << self
72
- triples = self.triples
73
- triples.map { |s,p,o| [s,o] }.flatten.uniq.each { |node| triples += graph[node].all_triples(done) }
74
- triples
69
+ def all_triples
70
+ graph.triples
75
71
  end
76
72
 
77
73
  def merge node
78
- new_node = clone
74
+ new_node = Node(id)
79
75
  (self.keys + node.keys).uniq.each do |k|
80
76
  new_node[k] = (node[k] + self[k]).uniq
81
77
  end
82
78
  new_node
83
79
  end
84
-
85
- def clone
86
- Node.new self
80
+
81
+ def merge! node
82
+ (self.keys + node.keys).uniq.each do |k|
83
+ self[k] = (node[k] + self[k]).uniq
84
+ end
85
+ self
87
86
  end
88
-
87
+
89
88
  def bnode?
90
89
  ID.bnode?(id)
91
90
  end
data/lib/lightrdf.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module RDF
5
- VERSION = '0.2.6'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
 
8
8
  require 'rubygems'
data/lightrdf.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{lightrdf}
5
- s.version = "0.2.6"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jose Ignacio"]
9
- s.date = %q{2011-03-16}
9
+ s.date = %q{2011-03-17}
10
10
  s.default_executable = %q{yarfp}
11
11
  s.description = %q{RDF library}
12
12
  s.email = %q{joseignacio.fernandez@gmail.com}
@@ -97,7 +97,67 @@ class TestLightRDF < Test::Unit::TestCase
97
97
  assert g[Node('ex:bob')].foaf::weblog?(Node('http://www.awesomeweblogfordummies.com'))
98
98
  assert_equal 1, [g[Node('ex:bob')].graph.object_id, g[Node('ex:bob')].foaf::weblog.map(&:graph).map(&:object_id)].flatten.uniq.size
99
99
  end
100
+
101
+ def test_node_merge
102
+ a = Node('ex:alice')
103
+ a.foaf::name = "Alice"
104
+ b = Node('ex:alice')
105
+ b.foaf::age = "23"
106
+
107
+ c = a.merge(b)
108
+
109
+ assert_not_same ["Alice"], b.foaf::name
110
+ assert_not_same ["23"], a.foaf::age
111
+ assert_equal ["Alice"], c.foaf::name
112
+ assert_equal ["23"], c.foaf::age
113
+ end
114
+
115
+ def test_node_merge!
116
+ a = Node('ex:alice')
117
+ a.foaf::name = "Alice"
118
+ b = Node('ex:alice')
119
+ b.foaf::age = "23"
120
+
121
+ a.merge!(b)
122
+
123
+ assert_equal ["Alice"], a.foaf::name
124
+ assert_equal ["23"], a.foaf::age
125
+ end
126
+
127
+ def test_recursive_add
128
+ a = Node('ex:alice')
129
+ a.foaf::name = "Alice"
130
+ b = Node("ex:bob", a.graph)
131
+ b.foaf::age = "26"
132
+ a.foaf::knows = b
133
+
134
+ g = RDF::Graph.new
135
+ g << a
136
+
137
+ assert_equal ["Alice"], g[Node("ex:alice")].foaf::name
138
+ assert_equal [Node("ex:bob")], g[Node("ex:alice")].foaf::knows
139
+ assert_equal ["26"], g[Node("ex:bob")].foaf::age
140
+ end
100
141
 
142
+ def test_graph_merge!
143
+ a = Node('ex:bob')
144
+ a.foaf::name = "Bob"
145
+
146
+ b = Node('ex:alice')
147
+ b.foaf::name = "Alice"
148
+
149
+ g1 = RDF::Graph.new
150
+ g2 = RDF::Graph.new
151
+ g1 << a
152
+ g2 << b
153
+
154
+ g2.merge!(g1)
155
+
156
+ assert_equal ["Alice"], g2[Node('ex:alice')].foaf::name
157
+ assert_equal ["Bob"], g2[Node('ex:bob')].foaf::name
158
+ assert_equal 2, g2.triples.size
159
+ end
160
+
101
161
  def test_graph_merge
102
162
  a = Node('ex:bob')
103
163
  a.foaf::name = "Bob"
@@ -112,8 +172,10 @@ class TestLightRDF < Test::Unit::TestCase
112
172
 
113
173
  g3 = g1.merge(g2)
114
174
 
115
- assert ["Alice"], g3[Node('ex:Alice')].foaf::name
116
- assert ["Bob"], g3[Node('ex:bob')].foaf::name
175
+ assert_equal ["Alice"], g3[Node('ex:alice')].foaf::name
176
+ assert_equal ["Bob"], g3[Node('ex:bob')].foaf::name
177
+ assert_equal g1, a.graph
178
+ assert_equal g2, b.graph
117
179
  assert_equal 2, g3.triples.size
118
180
  end
119
181
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 6
9
- version: 0.2.6
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jose Ignacio
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-16 00:00:00 +01:00
17
+ date: 2011-03-17 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency