rdf 0.2.3 → 0.3.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/AUTHORS +1 -0
  2. data/{CONTRIBUTORS → CREDITS} +3 -1
  3. data/README +17 -8
  4. data/VERSION +1 -1
  5. data/etc/doap.nt +28 -10
  6. data/lib/rdf/format.rb +55 -47
  7. data/lib/rdf/mixin/countable.rb +3 -6
  8. data/lib/rdf/mixin/enumerable.rb +69 -69
  9. data/lib/rdf/mixin/indexable.rb +26 -0
  10. data/lib/rdf/mixin/mutable.rb +2 -2
  11. data/lib/rdf/mixin/queryable.rb +50 -12
  12. data/lib/rdf/mixin/writable.rb +8 -19
  13. data/lib/rdf/model/literal/boolean.rb +42 -6
  14. data/lib/rdf/model/literal/date.rb +17 -5
  15. data/lib/rdf/model/literal/datetime.rb +18 -6
  16. data/lib/rdf/model/literal/decimal.rb +32 -5
  17. data/lib/rdf/model/literal/double.rb +32 -5
  18. data/lib/rdf/model/literal/integer.rb +16 -5
  19. data/lib/rdf/model/literal/time.rb +6 -6
  20. data/lib/rdf/model/literal/token.rb +5 -5
  21. data/lib/rdf/model/literal/xml.rb +5 -5
  22. data/lib/rdf/model/literal.rb +24 -11
  23. data/lib/rdf/model/node.rb +14 -13
  24. data/lib/rdf/model/uri.rb +315 -42
  25. data/lib/rdf/model/value.rb +1 -1
  26. data/lib/rdf/ntriples/reader.rb +23 -15
  27. data/lib/rdf/ntriples/writer.rb +1 -1
  28. data/lib/rdf/query/pattern.rb +131 -15
  29. data/lib/rdf/query/solution.rb +94 -29
  30. data/lib/rdf/query/solutions.rb +202 -0
  31. data/lib/rdf/query/variable.rb +42 -18
  32. data/lib/rdf/query.rb +210 -160
  33. data/lib/rdf/reader.rb +300 -112
  34. data/lib/rdf/repository.rb +88 -6
  35. data/lib/rdf/transaction.rb +161 -0
  36. data/lib/rdf/util/cache.rb +5 -0
  37. data/lib/rdf/util/file.rb +31 -0
  38. data/lib/rdf/util/uuid.rb +36 -0
  39. data/lib/rdf/util.rb +2 -0
  40. data/lib/rdf/version.rb +3 -3
  41. data/lib/rdf/vocab.rb +43 -35
  42. data/lib/rdf/writer.rb +105 -50
  43. data/lib/rdf.rb +29 -27
  44. metadata +26 -17
@@ -27,15 +27,42 @@ module RDF; class Literal
27
27
  end
28
28
 
29
29
  ##
30
- # Converts the literal into its canonical lexical representation.
30
+ # Converts this literal into its canonical lexical representation.
31
31
  #
32
- # @return [Literal]
33
- # @see http://www.w3.org/TR/xmlschema-2/#boolean
34
- def canonicalize
32
+ # @return [RDF::Literal] `self`
33
+ # @see http://www.w3.org/TR/xmlschema-2/#boolean-canonical-representation
34
+ def canonicalize!
35
35
  @string = (@object ? :true : :false).to_s
36
36
  self
37
37
  end
38
38
 
39
+ ##
40
+ # Compares this literal to `other` for sorting purposes.
41
+ #
42
+ # @param [Object] other
43
+ # @return [Integer] `-1`, `0`, or `1`
44
+ # @since 0.3.0
45
+ def <=>(other)
46
+ case other
47
+ when TrueClass, FalseClass
48
+ to_i <=> (other ? 1 : 0)
49
+ when RDF::Literal::Boolean
50
+ to_i <=> other.to_i
51
+ else super
52
+ end
53
+ end
54
+
55
+ ##
56
+ # Returns `true` if this literal is equivalent to `other`.
57
+ #
58
+ # @param [Object] other
59
+ # @return [Boolean] `true` or `false`
60
+ # @since 0.3.0
61
+ def ==(other)
62
+ (self <=> other).zero?
63
+ end
64
+ alias_method :===, :==
65
+
39
66
  ##
40
67
  # Returns the value as a string.
41
68
  #
@@ -44,6 +71,15 @@ module RDF; class Literal
44
71
  @string || @object.to_s
45
72
  end
46
73
 
74
+ ##
75
+ # Returns the value as an integer.
76
+ #
77
+ # @return [Integer] `0` or `1`
78
+ # @since 0.3.0
79
+ def to_i
80
+ @object ? 1 : 0
81
+ end
82
+
47
83
  ##
48
84
  # Returns `true` if this value is `true`.
49
85
  #
@@ -59,5 +95,5 @@ module RDF; class Literal
59
95
  def false?
60
96
  @object.equal?(false)
61
97
  end
62
- end # class Boolean
63
- end; end # class RDF::Literal
98
+ end # Boolean
99
+ end; end # RDF::Literal
@@ -23,15 +23,27 @@ module RDF; class Literal
23
23
  end
24
24
 
25
25
  ##
26
- # Converts the literal into its canonical lexical representation.
26
+ # Converts this literal into its canonical lexical representation.
27
27
  #
28
- # @return [Literal]
28
+ # @return [RDF::Literal] `self`
29
29
  # @see http://www.w3.org/TR/xmlschema-2/#date
30
- def canonicalize
30
+ def canonicalize!
31
31
  @string = @object.strftime('%Y-%m-%d%Z').sub(/\+00:00|UTC/, 'Z')
32
32
  self
33
33
  end
34
34
 
35
+ ##
36
+ # Returns `true` if the value adheres to the defined grammar of the
37
+ # datatype.
38
+ #
39
+ # Special case for date and dateTime, for which '0000' is not a valid year
40
+ #
41
+ # @return [Boolean]
42
+ # @since 0.2.1
43
+ def valid?
44
+ !!(value =~ GRAMMAR) && value !~ %r(\A0000)
45
+ end
46
+
35
47
  ##
36
48
  # Returns the value as a string.
37
49
  #
@@ -39,5 +51,5 @@ module RDF; class Literal
39
51
  def to_s
40
52
  @string || @object.strftime('%Y-%m-%d%Z').sub(/\+00:00|UTC/, 'Z')
41
53
  end
42
- end # class Date
43
- end; end # class RDF::Literal
54
+ end # Date
55
+ end; end # RDF::Literal
@@ -23,15 +23,27 @@ module RDF; class Literal
23
23
  end
24
24
 
25
25
  ##
26
- # Converts the literal into its canonical lexical representation.
26
+ # Converts this literal into its canonical lexical representation.
27
27
  #
28
- # @return [Literal]
28
+ # @return [RDF::Literal] `self`
29
29
  # @see http://www.w3.org/TR/xmlschema-2/#dateTime
30
- def canonicalize
31
- @string = @object.strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
30
+ def canonicalize!
31
+ @string = @object.new_offset(0).strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
32
32
  self
33
33
  end
34
34
 
35
+ ##
36
+ # Returns `true` if the value adheres to the defined grammar of the
37
+ # datatype.
38
+ #
39
+ # Special case for date and dateTime, for which '0000' is not a valid year
40
+ #
41
+ # @return [Boolean]
42
+ # @since 0.2.1
43
+ def valid?
44
+ !!(value =~ GRAMMAR) && value !~ %r(\A0000)
45
+ end
46
+
35
47
  ##
36
48
  # Returns the value as a string.
37
49
  #
@@ -39,5 +51,5 @@ module RDF; class Literal
39
51
  def to_s
40
52
  @string || @object.strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
41
53
  end
42
- end # class DateTime
43
- end; end # class RDF::Literal
54
+ end # DateTime
55
+ end; end # RDF::Literal
@@ -28,11 +28,11 @@ module RDF; class Literal
28
28
  end
29
29
 
30
30
  ##
31
- # Converts the literal into its canonical lexical representation.
31
+ # Converts this literal into its canonical lexical representation.
32
32
  #
33
- # @return [Literal]
33
+ # @return [RDF::Literal] `self`
34
34
  # @see http://www.w3.org/TR/xmlschema-2/#decimal
35
- def canonicalize
35
+ def canonicalize!
36
36
  # Can't use simple %f transformation due to special requirements from
37
37
  # N3 tests in representation
38
38
  @string = begin
@@ -46,6 +46,33 @@ module RDF; class Literal
46
46
  self
47
47
  end
48
48
 
49
+ ##
50
+ # Compares this literal to `other` for sorting purposes.
51
+ #
52
+ # @param [Object] other
53
+ # @return [Integer] `-1`, `0`, or `1`
54
+ # @since 0.3.0
55
+ def <=>(other)
56
+ case other
57
+ when Numeric
58
+ to_d <=> other
59
+ when RDF::Literal::Decimal, RDF::Literal::Double
60
+ to_d <=> other.to_d
61
+ else super
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Returns `true` if this literal is equivalent to `other`.
67
+ #
68
+ # @param [Object] other
69
+ # @return [Boolean] `true` or `false`
70
+ # @since 0.3.0
71
+ def ==(other)
72
+ (self <=> other).zero?
73
+ end
74
+ alias_method :===, :==
75
+
49
76
  ##
50
77
  # Returns the absolute value of `self`.
51
78
  #
@@ -177,5 +204,5 @@ module RDF; class Literal
177
204
  def to_r
178
205
  @object.to_r # only available on Ruby 1.9+
179
206
  end
180
- end # class Decimal
181
- end; end # class RDF::Literal
207
+ end # Decimal
208
+ end; end # RDF::Literal
@@ -30,11 +30,11 @@ module RDF; class Literal
30
30
  end
31
31
 
32
32
  ##
33
- # Converts the literal into its canonical lexical representation.
33
+ # Converts this literal into its canonical lexical representation.
34
34
  #
35
- # @return [Literal]
35
+ # @return [RDF::Literal] `self`
36
36
  # @see http://www.w3.org/TR/xmlschema-2/#double
37
- def canonicalize
37
+ def canonicalize!
38
38
  # Can't use simple %f transformation due to special requirements from
39
39
  # N3 tests in representation
40
40
  @string = case
@@ -51,6 +51,33 @@ module RDF; class Literal
51
51
  self
52
52
  end
53
53
 
54
+ ##
55
+ # Compares this literal to `other` for sorting purposes.
56
+ #
57
+ # @param [Object] other
58
+ # @return [Integer] `-1`, `0`, or `1`
59
+ # @since 0.3.0
60
+ def <=>(other)
61
+ case other
62
+ when Numeric
63
+ to_d <=> other
64
+ when RDF::Literal::Decimal, RDF::Literal::Double
65
+ to_d <=> other.to_d
66
+ else super
67
+ end
68
+ end
69
+
70
+ ##
71
+ # Returns `true` if this literal is equivalent to `other`.
72
+ #
73
+ # @param [Object] other
74
+ # @return [Boolean] `true` or `false`
75
+ # @since 0.3.0
76
+ def ==(other)
77
+ (self <=> other).zero?
78
+ end
79
+ alias_method :===, :==
80
+
54
81
  ##
55
82
  # Returns `true` if the value is an invalid IEEE floating point number.
56
83
  #
@@ -253,5 +280,5 @@ module RDF; class Literal
253
280
  def to_r
254
281
  @object.to_r # only available on Ruby 1.9+
255
282
  end
256
- end # class Double
257
- end; end # class RDF::Literal
283
+ end # Double
284
+ end; end # RDF::Literal
@@ -30,11 +30,11 @@ module RDF; class Literal
30
30
  end
31
31
 
32
32
  ##
33
- # Converts the literal into its canonical lexical representation.
33
+ # Converts this literal into its canonical lexical representation.
34
34
  #
35
- # @return [Literal]
35
+ # @return [RDF::Literal] `self`
36
36
  # @see http://www.w3.org/TR/xmlschema-2/#integer
37
- def canonicalize
37
+ def canonicalize!
38
38
  @string = @object.to_s if @object
39
39
  self
40
40
  end
@@ -203,5 +203,16 @@ module RDF; class Literal
203
203
  def to_r
204
204
  @object.to_r
205
205
  end
206
- end # class Integer
207
- end; end # class RDF::Literal
206
+
207
+ ##
208
+ # Returns the value as an `OpenSSL::BN` instance.
209
+ #
210
+ # @return [OpenSSL::BN]
211
+ # @see http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/classes/OpenSSL/BN.html
212
+ # @since 0.2.4
213
+ def to_bn
214
+ require 'openssl' unless defined?(OpenSSL::BN)
215
+ OpenSSL::BN.new(to_s)
216
+ end
217
+ end # Integer
218
+ end; end # RDF::Literal
@@ -27,7 +27,7 @@ module RDF; class Literal
27
27
  end
28
28
 
29
29
  ##
30
- # Converts the literal into its canonical lexical representation.
30
+ # Converts this literal into its canonical lexical representation.
31
31
  #
32
32
  # §3.2.8.2 Canonical representation
33
33
  #
@@ -37,10 +37,10 @@ module RDF; class Literal
37
37
  # time zone must be Coordinated Universal Time (UTC) indicated by a "Z".
38
38
  # Additionally, the canonical representation for midnight is 00:00:00.
39
39
  #
40
- # @return [Literal]
40
+ # @return [RDF::Literal] `self`
41
41
  # @see http://www.w3.org/TR/xmlschema-2/#time
42
- def canonicalize
43
- @string = @object.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
42
+ def canonicalize!
43
+ @string = @object.utc.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
44
44
  self
45
45
  end
46
46
 
@@ -51,5 +51,5 @@ module RDF; class Literal
51
51
  def to_s
52
52
  @string || @object.strftime('%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
53
53
  end
54
- end # class Time
55
- end; end # class RDF::Literal
54
+ end # Time
55
+ end; end # RDF::Literal
@@ -19,11 +19,11 @@ module RDF; class Literal
19
19
  end
20
20
 
21
21
  ##
22
- # Converts the literal into its canonical lexical representation.
22
+ # Converts this literal into its canonical lexical representation.
23
23
  #
24
- # @return [Literal]
24
+ # @return [RDF::Literal] `self`
25
25
  # @see http://www.w3.org/TR/xmlschema-2/#boolean
26
- def canonicalize
26
+ def canonicalize!
27
27
  @string = @object.to_s if @object
28
28
  self
29
29
  end
@@ -43,5 +43,5 @@ module RDF; class Literal
43
43
  def to_s
44
44
  @string || @object.to_s
45
45
  end
46
- end # class Token
47
- end; end # class RDF::Literal
46
+ end # Token
47
+ end; end # RDF::Literal
@@ -19,11 +19,11 @@ module RDF; class Literal
19
19
  end
20
20
 
21
21
  ##
22
- # Converts the literal into its canonical lexical representation.
22
+ # Converts this literal into its canonical lexical representation.
23
23
  #
24
- # @return [Literal]
24
+ # @return [RDF::Literal] `self`
25
25
  # @see http://www.w3.org/TR/xml-exc-c14n/
26
- def canonicalize
26
+ def canonicalize!
27
27
  # TODO: implement XML canonicalization
28
28
  self
29
29
  end
@@ -35,5 +35,5 @@ module RDF; class Literal
35
35
  def to_s
36
36
  @string || @object.to_s # TODO
37
37
  end
38
- end # class XML
39
- end; end # class RDF::Literal
38
+ end # XML
39
+ end; end # RDF::Literal
@@ -100,8 +100,8 @@ module RDF
100
100
  end
101
101
  literal = klass.allocate
102
102
  literal.send(:initialize, value, options)
103
- literal.validate if options[:validate]
104
- literal.canonicalize if options[:canonicalize]
103
+ literal.validate! if options[:validate]
104
+ literal.canonicalize! if options[:canonicalize]
105
105
  literal
106
106
  end
107
107
 
@@ -263,23 +263,36 @@ module RDF
263
263
  # Validates the value using {#valid?}, raising an error if the value is
264
264
  # invalid.
265
265
  #
266
+ # @return [RDF::Literal] `self`
266
267
  # @raise [ArgumentError] if the value is invalid
267
- # @return [Literal]
268
268
  # @since 0.2.1
269
- def validate
270
- raise ArgumentError.new("#{to_s.inspect} is not a valid <#{datatype.to_s}> literal") if invalid?
269
+ def validate!
270
+ raise ArgumentError, "#{to_s.inspect} is not a valid <#{datatype.to_s}> literal" if invalid?
271
271
  self
272
272
  end
273
- alias_method :validate!, :validate
273
+ alias_method :validate, :validate!
274
274
 
275
275
  ##
276
- # Converts the literal into its canonical lexical representation.
276
+ # Returns a copy of this literal converted into its canonical lexical
277
+ # representation.
277
278
  #
278
- # Subclasses should override this as needed and appropriate.
279
+ # Subclasses should override `#canonicalize!` as needed and appropriate,
280
+ # not this method.
279
281
  #
280
- # @return [Literal]
282
+ # @return [RDF::Literal]
281
283
  # @since 0.2.1
282
284
  def canonicalize
285
+ self.dup.canonicalize!
286
+ end
287
+
288
+ ##
289
+ # Converts this literal into its canonical lexical representation.
290
+ #
291
+ # Subclasses should override this as needed and appropriate.
292
+ #
293
+ # @return [RDF::Literal] `self`
294
+ # @since 0.3.0
295
+ def canonicalize!
283
296
  @language = @language.to_s.downcase.to_sym if @language
284
297
  self
285
298
  end
@@ -299,5 +312,5 @@ module RDF
299
312
  def inspect
300
313
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self))
301
314
  end
302
- end
303
- end
315
+ end # Literal
316
+ end # RDF
@@ -9,27 +9,28 @@ module RDF
9
9
  # bnode = RDF::Node.uuid
10
10
  # bnode.to_s #=> "_:504c0a30-0d11-012d-3f50-001b63cac539"
11
11
  #
12
- # @see http://rubygems.org/gems/uuid
13
- # @see http://rubygems.org/gems/uuidtools
14
12
  class Node
15
13
  include RDF::Resource
16
14
 
17
15
  ##
18
16
  # Returns a blank node with a random UUID-based identifier.
19
17
  #
18
+ # @param [Hash{Symbol => Object}] options
19
+ # @option options [Regexp] :grammar (nil)
20
+ # a grammar specification that the generated UUID must match
20
21
  # @return [RDF::Node]
21
- def self.uuid
22
- begin
23
- require 'uuid'
24
- self.new(UUID.generate)
25
- rescue LoadError => e
26
- begin
27
- require 'uuidtools'
28
- self.new(UUIDTools::UUID.random_create)
29
- rescue LoadError => e
30
- raise LoadError.new("no such file to load -- uuid or uuidtools")
31
- end
22
+ def self.uuid(options = {})
23
+ case
24
+ when options[:grammar]
25
+ # The UUID is generated such that its initial part is guaranteed
26
+ # to match the given `grammar`, e.g. `/^[A-Za-z][A-Za-z0-9]*/`.
27
+ # Some RDF storage systems (e.g. AllegroGraph) require this.
28
+ # @see http://github.com/bendiken/rdf/pull/43
29
+ uuid = RDF::Util::UUID.generate(options) until uuid =~ options[:grammar]
30
+ else
31
+ uuid = RDF::Util::UUID.generate(options)
32
32
  end
33
+ self.new(uuid)
33
34
  end
34
35
 
35
36
  ##