lightrdf 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.3.2 2011-03-22
2
+
3
+ * Added RDF::Graph#== and RDF::Graph#contains methods that disambiguate bnodes
4
+
1
5
  === 0.3.1 2011-03-18
2
6
 
3
7
  * Fix in Node#all_triples method
@@ -52,6 +52,34 @@ module RDF
52
52
  values.select &block
53
53
  end
54
54
 
55
+ # Returns true if the graph is contained into this one.
56
+ # It appropriately disambiguates blank nodes.
57
+ def contains? graph2
58
+ triples = self.triples
59
+ triples2 = graph2.triples
60
+ return false if triples2.size > triples.size
61
+
62
+ bnodes = self.keys.select { |node| ID::bnode?(node) }
63
+ bnodes2 = graph2.keys.select { |node| ID::bnode?(node) }
64
+
65
+ # Tries all the mappings between blank nodes
66
+ mappings = bnodes2.mappings(bnodes)
67
+
68
+ mappings.each do |mapping|
69
+ new_triples = triples2.map { |s,p,o| [ ID::bnode?(s) ? mapping[s] : s,
70
+ ID::bnode?(p) ? mapping[p] : p,
71
+ ID::bnode?(o) ? mapping[o] : o ]}
72
+ diff = triples - new_triples
73
+ return true if diff.size == triples.size - new_triples.size
74
+ end
75
+
76
+ false
77
+ end
78
+
79
+ def == graph2
80
+ triples.size == graph2.triples.size and contains?(graph2)
81
+ end
82
+
55
83
  # This is equivalent to [], but tries to return a NodeProxy
56
84
  # It stores created objects in a pool
57
85
  def node id, type=nil
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.3.1'
5
+ VERSION = '0.3.2'
6
6
  end
7
7
 
8
8
  require 'rubygems'
@@ -21,3 +21,22 @@ require "lightrdf/graph"
21
21
  require "lightrdf/node"
22
22
  require "lightrdf/node_proxy"
23
23
  require "lightrdf/repository"
24
+
25
+ class Array
26
+ # Returns an array of hashes with the possible mappings
27
+ # between the elements in both arrays
28
+ def mappings array2
29
+ return [{}] if self.empty? or array2.empty?
30
+ mappings = []
31
+ each do |item|
32
+ mapping = {}
33
+ array2.each do |item2|
34
+ mapping[item] = item2
35
+ (self - [item]).mappings(array2 - [item2]).each do |submapping|
36
+ mappings << mapping.merge(submapping)
37
+ end
38
+ end
39
+ end
40
+ mappings.uniq
41
+ end
42
+ end
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.3.1"
5
+ s.version = "0.3.2"
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-18}
9
+ s.date = %q{2011-03-22}
10
10
  s.default_executable = %q{yarfp}
11
11
  s.description = %q{RDF library}
12
12
  s.email = %q{joseignacio.fernandez@gmail.com}
@@ -309,4 +309,23 @@ foaf: http://xmlns.com/foaf/0.1/
309
309
  person2 = Foaf::Person.new(node)
310
310
  assert_equal person1.id, person2.id
311
311
  end
312
+
313
+ def test_graph_contains
314
+ graph1 = RDF::Graph.new [ [Node('ex:alice'), Node('foaf:age'), "24"]]
315
+ graph2 = RDF::Graph.new [ [Node('ex:alice'), Node('foaf:knows'), Node('_:bnode392')],
316
+ [Node('_:bnode392'), Node('foaf:name'), "Bob"] ]
317
+ graph3 = RDF::Graph.new [ [Node('ex:alice'), Node('foaf:knows'), Node('_:bnode965')],
318
+ [Node('_:bnode965'), Node('foaf:name'), "Bob"] ]
319
+ graph4 = RDF::Graph.new [ [Node('ex:alice'), Node('foaf:knows'), Node('_:bnode392')],
320
+ [Node('_:bnode392'), Node('foaf:name'), "Robert"] ]
321
+ graph5 = RDF::Graph.new [ [Node('ex:alice'), Node('foaf:knows'), Node('_:bnode390')],
322
+ [Node('_:bnode390'), Node('foaf:name'), "Robert"],
323
+ [Node('ex:alice'), Node('foaf:knows'), Node('_:bnode967')],
324
+ [Node('_:bnode967'), Node('foaf:name'), "Bob"]]
325
+ assert !graph1.contains?(graph2)
326
+ assert graph2.contains?(graph3)
327
+ assert !graph2.contains?(graph4)
328
+ assert graph2 != graph4
329
+ assert graph5.contains?(graph2)
330
+ end
312
331
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
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-18 00:00:00 +01:00
17
+ date: 2011-03-22 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency