rdf 0.3.0.pre → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,6 +40,7 @@ module RDF
40
40
  # @see http://www.w3.org/TR/rdf-concepts/#section-Datatypes-intro
41
41
  class Literal
42
42
  autoload :Boolean, 'rdf/model/literal/boolean'
43
+ autoload :Numeric, 'rdf/model/literal/numeric'
43
44
  autoload :Integer, 'rdf/model/literal/integer'
44
45
  autoload :Double, 'rdf/model/literal/double'
45
46
  autoload :Decimal, 'rdf/model/literal/decimal'
@@ -49,7 +50,7 @@ module RDF
49
50
  autoload :Token, 'rdf/model/literal/token'
50
51
  autoload :XML, 'rdf/model/literal/xml'
51
52
 
52
- include RDF::Value
53
+ include RDF::Term
53
54
 
54
55
  ##
55
56
  # @private
@@ -105,6 +106,10 @@ module RDF
105
106
  literal
106
107
  end
107
108
 
109
+ TRUE = RDF::Literal.new(true).freeze
110
+ FALSE = RDF::Literal.new(false).freeze
111
+ ZERO = RDF::Literal.new(0).freeze
112
+
108
113
  # @return [Symbol] The language tag (optional).
109
114
  attr_accessor :language
110
115
 
@@ -162,7 +167,7 @@ module RDF
162
167
  ##
163
168
  # Returns `true`.
164
169
  #
165
- # @return [Boolean]
170
+ # @return [Boolean] `true` or `false`
166
171
  def literal?
167
172
  true
168
173
  end
@@ -170,7 +175,7 @@ module RDF
170
175
  ##
171
176
  # Returns `false`.
172
177
  #
173
- # @return [Boolean]
178
+ # @return [Boolean] `true` or `false`
174
179
  def anonymous?
175
180
  false
176
181
  end
@@ -184,56 +189,69 @@ module RDF
184
189
  end
185
190
 
186
191
  ##
187
- # @return [Boolean]
192
+ # Returns `true` if this literal is equal to `other`.
193
+ #
194
+ # @example
195
+ # RDF::Literal(1).eql?(RDF::Literal(1.0)) #=> false
196
+ #
197
+ # @param [Object] other
198
+ # @return [Boolean] `true` or `false`
188
199
  def eql?(other)
189
- other.is_a?(Literal) && self == other
200
+ self.equal?(other) ||
201
+ (self.class.eql?(other.class) &&
202
+ self.datatype.eql?(other.datatype) &&
203
+ self == other)
190
204
  end
191
205
 
192
206
  ##
193
- # @return [Boolean]
207
+ # Returns `true` if this literal is equivalent to `other`.
208
+ #
209
+ # @example
210
+ # RDF::Literal(1) == RDF::Literal(1.0) #=> true
211
+ #
212
+ # @param [Object] other
213
+ # @return [Boolean] `true` or `false`
194
214
  def ==(other)
195
215
  case other
196
216
  when Literal
197
- value.eql?(other.value) &&
198
- language.eql?(other.language) &&
199
- datatype.eql?(other.datatype)
217
+ self.value.eql?(other.value) &&
218
+ self.language.eql?(other.language) &&
219
+ self.datatype.eql?(other.datatype)
200
220
  when String
201
- value.eql?(other) &&
202
- language.nil? &&
203
- datatype.nil?
221
+ self.plain? && self.value.eql?(other)
204
222
  else false
205
223
  end
206
224
  end
225
+ alias_method :===, :==
207
226
 
208
227
  ##
209
228
  # Returns `true` if this is a plain literal.
210
229
  #
211
- # @return [Boolean]
230
+ # @return [Boolean] `true` or `false`
212
231
  # @see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal
213
232
  def plain?
214
233
  language.nil? && datatype.nil?
215
234
  end
235
+ alias_method :simple?, :plain?
216
236
 
217
237
  ##
218
238
  # Returns `true` if this is a language-tagged literal.
219
239
  #
220
- # @return [Boolean]
240
+ # @return [Boolean] `true` or `false`
221
241
  # @see http://www.w3.org/TR/rdf-concepts/#dfn-plain-literal
222
242
  def has_language?
223
243
  !language.nil?
224
244
  end
225
-
226
245
  alias_method :language?, :has_language?
227
246
 
228
247
  ##
229
248
  # Returns `true` if this is a datatyped literal.
230
249
  #
231
- # @return [Boolean]
250
+ # @return [Boolean] `true` or `false`
232
251
  # @see http://www.w3.org/TR/rdf-concepts/#dfn-typed-literal
233
252
  def has_datatype?
234
253
  !datatype.nil?
235
254
  end
236
-
237
255
  alias_method :datatype?, :has_datatype?
238
256
  alias_method :typed?, :has_datatype?
239
257
  alias_method :datatyped?, :has_datatype?
@@ -242,7 +260,7 @@ module RDF
242
260
  # Returns `true` if the value adheres to the defined grammar of the
243
261
  # datatype.
244
262
  #
245
- # @return [Boolean]
263
+ # @return [Boolean] `true` or `false`
246
264
  # @since 0.2.1
247
265
  def valid?
248
266
  grammar = self.class.const_get(:GRAMMAR) rescue nil
@@ -253,7 +271,7 @@ module RDF
253
271
  # Returns `true` if the value does not adhere to the defined grammar of
254
272
  # the datatype.
255
273
  #
256
- # @return [Boolean]
274
+ # @return [Boolean] `true` or `false`
257
275
  # @since 0.2.1
258
276
  def invalid?
259
277
  !valid?
@@ -2,7 +2,7 @@ module RDF
2
2
  ##
3
3
  # An RDF resource.
4
4
  module Resource
5
- include RDF::Value
5
+ include RDF::Term
6
6
 
7
7
  ##
8
8
  # Instantiates an {RDF::Node} or an {RDF::URI}, depending on the given
@@ -24,5 +24,5 @@ module RDF
24
24
  def resource?
25
25
  true
26
26
  end
27
- end
28
- end
27
+ end # Resource
28
+ end # RDF
@@ -24,12 +24,13 @@ module RDF
24
24
  ##
25
25
  # @private
26
26
  # @since 0.2.2
27
- def self.from(statement)
27
+ def self.from(statement, options = {})
28
28
  case statement
29
+ when Array, Query::Pattern
30
+ self.new(statement[0], statement[1], statement[2], options.merge(:context => statement[3] || nil))
29
31
  when Statement then statement
30
- when Hash then self.new(statement)
31
- when Array then self.new(*statement)
32
- else raise ArgumentError.new("expected RDF::Statement, Hash, or Array, but got #{statement.inspect}")
32
+ when Hash then self.new(options.merge(statement))
33
+ else raise ArgumentError, "expected RDF::Statement, Hash, or Array, but got #{statement.inspect}"
33
34
  end
34
35
  end
35
36
 
@@ -45,7 +46,7 @@ module RDF
45
46
  # @return [RDF::URI]
46
47
  attr_accessor :predicate
47
48
 
48
- # @return [RDF::Value]
49
+ # @return [RDF::Term]
49
50
  attr_accessor :object
50
51
 
51
52
  ##
@@ -53,13 +54,13 @@ module RDF
53
54
  # @param [Hash{Symbol => Object}] options
54
55
  # @option options [RDF::Resource] :subject (nil)
55
56
  # @option options [RDF::URI] :predicate (nil)
56
- # @option options [RDF::Value] :object (nil)
57
+ # @option options [RDF::Term] :object (nil)
57
58
  # @option options [RDF::Resource] :context (nil)
58
59
  #
59
60
  # @overload initialize(subject, predicate, object, options = {})
60
61
  # @param [RDF::Resource] subject
61
62
  # @param [RDF::URI] predicate
62
- # @param [RDF::Value] object
63
+ # @param [RDF::Term] object
63
64
  # @param [Hash{Symbol => Object}] options
64
65
  # @option options [RDF::Resource] :context (nil)
65
66
  def initialize(subject = nil, predicate = nil, object = nil, options = {})
@@ -76,7 +77,7 @@ module RDF
76
77
  @object = object
77
78
  end
78
79
  @id = @options.delete(:id) if @options.has_key?(:id)
79
- @context = @options.delete(:context) || @options.delete(:graph)
80
+ @context = @options.delete(:context)
80
81
  initialize!
81
82
  end
82
83
 
@@ -89,7 +90,7 @@ module RDF
89
90
  @object = case @object
90
91
  when nil then nil
91
92
  when Symbol then Node.intern(@object)
92
- when Value then @object
93
+ when Term then @object
93
94
  else Literal.new(@object)
94
95
  end
95
96
  end
@@ -190,7 +191,7 @@ module RDF
190
191
 
191
192
  ##
192
193
  # @param [Integer] index
193
- # @return [RDF::Value]
194
+ # @return [RDF::Term]
194
195
  def [](index)
195
196
  case index
196
197
  when 0 then self.subject
@@ -203,8 +204,8 @@ module RDF
203
204
 
204
205
  ##
205
206
  # @param [Integer] index
206
- # @param [RDF::Value] value
207
- # @return [RDF::Value]
207
+ # @param [RDF::Term] value
208
+ # @return [RDF::Term]
208
209
  def []=(index, value)
209
210
  case index
210
211
  when 0 then self.subject = value
@@ -216,13 +217,13 @@ module RDF
216
217
  end
217
218
 
218
219
  ##
219
- # @return [Array(RDF::Value)]
220
+ # @return [Array(RDF::Term)]
220
221
  def to_quad
221
222
  [subject, predicate, object, context]
222
223
  end
223
224
 
224
225
  ##
225
- # @return [Array(RDF::Value)]
226
+ # @return [Array(RDF::Term)]
226
227
  def to_triple
227
228
  [subject, predicate, object]
228
229
  end
@@ -236,7 +237,7 @@ module RDF
236
237
  # @param [Symbol] subject_key
237
238
  # @param [Symbol] predicate_key
238
239
  # @param [Symbol] object_key
239
- # @return [Hash{Symbol => RDF::Value}]
240
+ # @return [Hash{Symbol => RDF::Term}]
240
241
  def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, context_key = :context)
241
242
  {subject_key => subject, predicate_key => predicate, object_key => object, context_key => context}
242
243
  end
@@ -0,0 +1,45 @@
1
+ module RDF
2
+ ##
3
+ # An RDF term.
4
+ #
5
+ # Terms can be used as subjects, predicates, objects, and contexts of
6
+ # statements.
7
+ #
8
+ # @since 0.3.0
9
+ module Term
10
+ include RDF::Value
11
+ include Comparable
12
+
13
+ ##
14
+ # Compares `self` to `other` for sorting purposes.
15
+ #
16
+ # Subclasses should override this to provide a more meaningful
17
+ # implementation than the default which simply performs a string
18
+ # comparison based on `#to_s`.
19
+ #
20
+ # @abstract
21
+ # @param [Object] other
22
+ # @return [Integer] `-1`, `0`, or `1`
23
+ def <=>(other)
24
+ self.to_s <=> other.to_s
25
+ end
26
+
27
+ ##
28
+ # Returns `true` if this term is constant.
29
+ #
30
+ # @return [Boolean] `true` or `false`
31
+ # @see #variable?
32
+ def constant?
33
+ !(variable?)
34
+ end
35
+
36
+ ##
37
+ # Returns `true` if this term is variable.
38
+ #
39
+ # @return [Boolean] `true` or `false`
40
+ # @see #constant?
41
+ def variable?
42
+ false
43
+ end
44
+ end # Term
45
+ end # RDF
@@ -26,8 +26,6 @@ module RDF
26
26
  # @see RDF::Statement
27
27
  # @see RDF::URI
28
28
  module Value
29
- include Comparable
30
-
31
29
  ##
32
30
  # Returns `true` if `self` is a graph.
33
31
  #
@@ -95,20 +93,6 @@ module RDF
95
93
  false
96
94
  end
97
95
 
98
- ##
99
- # Compares `self` to `other` for sorting purposes.
100
- #
101
- # Subclasses should override this to provide a more meaningful
102
- # implementation than the default which simply performs a string
103
- # comparison based on `#to_s`.
104
- #
105
- # @abstract
106
- # @param [Object] other
107
- # @return [Integer] `-1`, `0`, or `1`
108
- def <=>(other)
109
- self.to_s <=> other.to_s
110
- end
111
-
112
96
  ##
113
97
  # Returns an `RDF::Value` representation of `self`.
114
98
  #
@@ -135,5 +119,5 @@ module RDF
135
119
  def inspect!
136
120
  warn(inspect)
137
121
  end
138
- end
139
- end
122
+ end # Value
123
+ end # RDF
data/lib/rdf/nquads.rb CHANGED
@@ -1,7 +1,19 @@
1
1
  module RDF
2
2
  ##
3
3
  # **`RDF::NQuads`** provides support for the N-Quads serialization format.
4
+ #
5
+ # This has not yet been implemented as of RDF.rb 0.3.x.
4
6
  module NQuads
5
- include NTriples # TODO
6
- end
7
- end
7
+ class Format < NTriples::Format
8
+ # TODO
9
+ end # Format
10
+
11
+ class Reader < NTriples::Reader
12
+ # TODO
13
+ end # Reader
14
+
15
+ class Writer < NTriples::Writer
16
+ # TODO
17
+ end # Writer
18
+ end # NQuads
19
+ end # RDF
@@ -30,7 +30,7 @@ module RDF::NTriples
30
30
 
31
31
  # @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar
32
32
  COMMENT = /^#\s*(.*)$/.freeze
33
- NODEID = /^_:([A-Za-z][A-Za-z0-9]*)/.freeze
33
+ NODEID = /^_:([A-Za-z][A-Za-z0-9\-_]*)/.freeze
34
34
  URIREF = /^<([^>]+)>/.freeze
35
35
  LITERAL_PLAIN = /^"((?:\\"|[^"])*)"/.freeze
36
36
  LITERAL_WITH_LANGUAGE = /^"((?:\\"|[^"])*)"@([a-z]+[\-A-Za-z0-9]*)/.freeze
@@ -56,7 +56,7 @@ module RDF::NTriples
56
56
  # representation.
57
57
  #
58
58
  # @param [String] input
59
- # @return [RDF::Value]
59
+ # @return [RDF::Term]
60
60
  def self.unserialize(input)
61
61
  case input
62
62
  when nil then nil
@@ -80,7 +80,7 @@ module RDF::NTriples
80
80
 
81
81
  ##
82
82
  # @param [String] input
83
- # @return [RDF::Value]
83
+ # @return [RDF::Term]
84
84
  def self.parse_object(input)
85
85
  parse_uri(input) || parse_node(input) || parse_literal(input)
86
86
  end
@@ -155,7 +155,7 @@ module RDF::NTriples
155
155
  end
156
156
 
157
157
  ##
158
- # @return [RDF::Value]
158
+ # @return [RDF::Term]
159
159
  def read_value
160
160
  begin
161
161
  read_statement
@@ -117,14 +117,17 @@ module RDF::NTriples
117
117
  #
118
118
  # @param [RDF::Value] value
119
119
  # @return [String]
120
+ # @raise [ArgumentError] if `value` is not an `RDF::Statement` or `RDF::Term`
120
121
  def self.serialize(value)
121
122
  writer = self.new
122
123
  case value
123
124
  when nil then nil
124
125
  when RDF::Statement
125
126
  writer.format_statement(value) + "\n"
127
+ when RDF::Term
128
+ writer.format_term(value)
126
129
  else
127
- writer.format_value(value)
130
+ raise ArgumentError, "expected an RDF::Statement or RDF::Term, but got #{value.inspect}"
128
131
  end
129
132
  end
130
133
 
@@ -142,7 +145,7 @@ module RDF::NTriples
142
145
  #
143
146
  # @param [RDF::Resource] subject
144
147
  # @param [RDF::URI] predicate
145
- # @param [RDF::Value] object
148
+ # @param [RDF::Term] object
146
149
  # @return [void]
147
150
  def write_triple(subject, predicate, object)
148
151
  puts format_triple(subject, predicate, object)
@@ -162,10 +165,10 @@ module RDF::NTriples
162
165
  #
163
166
  # @param [RDF::Resource] subject
164
167
  # @param [RDF::URI] predicate
165
- # @param [RDF::Value] object
168
+ # @param [RDF::Term] object
166
169
  # @return [String]
167
170
  def format_triple(subject, predicate, object)
168
- "%s %s %s ." % [subject, predicate, object].map { |value| format_value(value) }
171
+ "%s %s %s ." % [subject, predicate, object].map { |value| format_term(value) }
169
172
  end
170
173
 
171
174
  ##
data/lib/rdf/ntriples.rb CHANGED
@@ -84,7 +84,7 @@ module RDF
84
84
  def self.escape(string)
85
85
  Writer.escape(string)
86
86
  end
87
- end
87
+ end # NTriples
88
88
 
89
89
  ##
90
90
  # Extensions for `RDF::Value`.
@@ -100,5 +100,5 @@ module RDF
100
100
  def to_ntriples
101
101
  RDF::NTriples.serialize(self)
102
102
  end
103
- end
104
- end
103
+ end # Value
104
+ end # RDF