rdf-isomorphic 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -14,11 +14,19 @@ module RDF
14
14
  module Isomorphic
15
15
 
16
16
  # Returns `true` if this RDF::Enumerable is isomorphic with another.
17
+ #
18
+ # Takes a :canonicalize => true argument. If true, RDF::Literals will be
19
+ # canonicalized while producing a bijection. This results in broader
20
+ # matches for isomorphism in the case of equivalent literals with different
21
+ # representations.
22
+ #
23
+ # @param opts [Hash<Symbol => Any>] options
24
+ # @param other [RDF::Enumerable]
17
25
  # @return [Boolean]
18
26
  # @example
19
27
  # repository_a.isomorphic_with repository_b #=> true
20
- def isomorphic_with?(other)
21
- !(bijection_to(other).nil?)
28
+ def isomorphic_with?(other, opts = {})
29
+ !(bijection_to(other, opts).nil?)
22
30
  end
23
31
 
24
32
  alias_method :isomorphic?, :isomorphic_with?
@@ -27,12 +35,19 @@ module RDF
27
35
  # Returns a hash of RDF::Nodes => RDF::Nodes representing an isomorphic
28
36
  # bijection of this RDF::Enumerable's to another RDF::Enumerable's blank
29
37
  # nodes, or nil if a bijection cannot be found.
38
+ #
39
+ # Takes a :canonicalize => true argument. If true, RDF::Literals will be
40
+ # canonicalized while producing a bijection. This results in broader
41
+ # matches for isomorphism in the case of equivalent literals with different
42
+ # representations.
43
+ #
30
44
  # @example
31
45
  # repository_a.bijection_to repository_b
32
46
  # @param other [RDF::Enumerable]
47
+ # @param opts [Hash<Symbol => Any>] options
33
48
  # @return [Hash, nil]
34
- def bijection_to(other)
35
-
49
+ def bijection_to(other, opts = {})
50
+
36
51
  grounded_stmts_match = (count == other.count)
37
52
 
38
53
  grounded_stmts_match &&= each_statement.all? do | stmt |
@@ -49,7 +64,7 @@ module RDF
49
64
 
50
65
  nodes = RDF::Isomorphic.blank_nodes_in(blank_stmts)
51
66
  other_nodes = RDF::Isomorphic.blank_nodes_in(other_blank_stmts)
52
- build_bijection_to blank_stmts, nodes, other_blank_stmts, other_nodes
67
+ build_bijection_to blank_stmts, nodes, other_blank_stmts, other_nodes, {}, {}, opts
53
68
  else
54
69
  nil
55
70
  end
@@ -72,9 +87,10 @@ module RDF
72
87
  # @param [Array] other_nodes
73
88
  # @param [Hash] these_grounded_hashes
74
89
  # @param [Hash] other_grounded_hashes
90
+ # @param [Hash] options
75
91
  # @return [nil,Hash]
76
92
  # @private
77
- def build_bijection_to(anon_stmts, nodes, other_anon_stmts, other_nodes, these_grounded_hashes = {}, other_grounded_hashes = {})
93
+ def build_bijection_to(anon_stmts, nodes, other_anon_stmts, other_nodes, these_grounded_hashes = {}, other_grounded_hashes = {}, opts = {})
78
94
 
79
95
  # Create a hash signature of every node, based on the signature of
80
96
  # statements it exists in.
@@ -82,8 +98,8 @@ module RDF
82
98
  # that information to eliminate possible recursion combinations.
83
99
  #
84
100
  # Any mappings given in the method parameters are considered grounded.
85
- these_hashes, these_ungrounded_hashes = RDF::Isomorphic.hash_nodes(anon_stmts, nodes, these_grounded_hashes)
86
- other_hashes, other_ungrounded_hashes = RDF::Isomorphic.hash_nodes(other_anon_stmts, other_nodes, other_grounded_hashes)
101
+ these_hashes, these_ungrounded_hashes = RDF::Isomorphic.hash_nodes(anon_stmts, nodes, these_grounded_hashes, opts[:canonicalize])
102
+ other_hashes, other_ungrounded_hashes = RDF::Isomorphic.hash_nodes(other_anon_stmts, other_nodes, other_grounded_hashes, opts[:canonicalize])
87
103
 
88
104
  # Grounded hashes are built at the same rate between the two graphs (if
89
105
  # they are isomorphic). If there exists a grounded node in one that is
@@ -168,7 +184,7 @@ module RDF
168
184
  # @param [Hash] grounded_hashes
169
185
  # @private
170
186
  # @return [Hash, Hash]
171
- def self.hash_nodes(statements, nodes, grounded_hashes)
187
+ def self.hash_nodes(statements, nodes, grounded_hashes, canonicalize = false)
172
188
  hashes = grounded_hashes.dup
173
189
  ungrounded_hashes = {}
174
190
  hash_needed = true
@@ -180,7 +196,7 @@ module RDF
180
196
  starting_grounded_nodes = hashes.size
181
197
  nodes.each do | node |
182
198
  unless hashes.member? node
183
- grounded, hash = node_hash_for(node, statements, hashes)
199
+ grounded, hash = node_hash_for(node, statements, hashes, canonicalize)
184
200
  if grounded
185
201
  hashes[node] = hash
186
202
  end
@@ -215,12 +231,12 @@ module RDF
215
231
  # for the hash
216
232
  # @private
217
233
  # @return [Boolean, String]
218
- def self.node_hash_for(node,statements,hashes)
234
+ def self.node_hash_for(node, statements, hashes, canonicalize)
219
235
  statement_signatures = []
220
236
  grounded = true
221
237
  statements.each do | statement |
222
238
  if (statement.object == node) || (statement.subject == node)
223
- statement_signatures << hash_string_for(statement,hashes,node)
239
+ statement_signatures << hash_string_for(statement, hashes, node, canonicalize)
224
240
  [statement.subject, statement.object].each do | resource |
225
241
  grounded = false unless grounded(resource, hashes) || resource == node
226
242
  end
@@ -235,11 +251,11 @@ module RDF
235
251
  # string signatures for grounded node elements.
236
252
  # return [String]
237
253
  # @private
238
- def self.hash_string_for(statement,hashes,node)
254
+ def self.hash_string_for(statement, hashes, node, canonicalize)
239
255
  string = ""
240
- string << string_for_node(statement.subject,hashes,node)
256
+ string << string_for_node(statement.subject, hashes, node, canonicalize)
241
257
  string << statement.predicate.to_s
242
- string << string_for_node(statement.object,hashes,node)
258
+ string << string_for_node(statement.object, hashes, node, canonicalize)
243
259
  string
244
260
  end
245
261
 
@@ -257,7 +273,7 @@ module RDF
257
273
  # nodes will return their hashed form.
258
274
  # @return [String]
259
275
  # @private
260
- def self.string_for_node(node, hashes,target)
276
+ def self.string_for_node(node, hashes,target, canonicalize)
261
277
  case
262
278
  when node == target
263
279
  "itself"
@@ -268,7 +284,7 @@ module RDF
268
284
  # RDF.rb auto-boxing magic makes some literals the same when they
269
285
  # should not be; the ntriples serializer will take care of us
270
286
  when node.literal?
271
- node.class.name + RDF::NTriples.serialize(node)
287
+ node.class.name + RDF::NTriples.serialize(canonicalize ? node.canonicalize : node)
272
288
  else
273
289
  node.to_s
274
290
  end
@@ -2,7 +2,7 @@ module RDF::Isomorphic
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- TINY = 0
5
+ TINY = 1
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Lavender
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-26 00:00:00 -05:00
18
+ date: 2010-11-15 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -23,13 +23,13 @@ dependencies:
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - ~>
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 0
30
30
  - 2
31
- - 0
32
- version: 0.2.0
31
+ - 4
32
+ version: 0.2.4
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -37,7 +37,7 @@ dependencies:
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ~>
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  segments:
43
43
  - 0
@@ -51,7 +51,7 @@ dependencies:
51
51
  prerelease: false
52
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - ">="
54
+ - - ~>
55
55
  - !ruby/object:Gem::Version
56
56
  segments:
57
57
  - 1