rdf 1.1.17.1 → 1.99.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,10 +6,14 @@ module RDF; class Query
6
6
  # @private
7
7
  # @since 0.2.2
8
8
  def self.from(pattern, options = {})
9
+ if options.has_key?(:context)
10
+ warn "[DEPRECATION] the :contexts option to Pattern.from is deprecated in RDF.rb 2.0, use :graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
11
+ options[:graph_name] ||= options.delete(:context)
12
+ end
9
13
  case pattern
10
14
  when Pattern then pattern
11
15
  when Array, Statement
12
- self.new(pattern[0], pattern[1], pattern[2], options.merge(:context => pattern[3]))
16
+ self.new(pattern[0], pattern[1], pattern[2], options.merge(graph_name: pattern[3]))
13
17
  when Hash then self.new(options.merge(pattern))
14
18
  else raise ArgumentError, "expected RDF::Query::Pattern, RDF::Statement, Hash, or Array, but got #{pattern.inspect}"
15
19
  end
@@ -23,6 +27,9 @@ module RDF; class Query
23
27
  # @option options [Variable, Term, Symbol, nil] :object (nil)
24
28
  # @option options [Variable, Resource, Symbol, nil, false] :context (nil)
25
29
  # A context of nil matches any context, a context of false, matches only the default context.
30
+ # The :context option is deprecated in RDF.rb 2.0. Use :graph_name instead.
31
+ # @option options [Variable, Resource, Symbol, nil, false] :graph_name (nil)
32
+ # A graph_name of nil matches any graph, a graph_name of false, matches only the default graph.
26
33
  # @option options [Boolean] :optional (false)
27
34
  #
28
35
  # @overload initialize(subject, predicate, object, options = {})
@@ -32,20 +39,27 @@ module RDF; class Query
32
39
  # @param [Hash{Symbol => Object}] options
33
40
  # @option options [Variable, Resource, Symbol, nil, false] :context (nil)
34
41
  # A context of nil matches any context, a context of false, matches only the default context.
42
+ # The :context option is deprecated in RDF.rb 2.0. Use :graph_name instead.
43
+ # @option options [Variable, Resource, Symbol, nil, false] :graph_name (nil)
44
+ # A graph_name of nil matches any graph, a graph_name of false, matches only the default graph.
35
45
  # @option options [Boolean] :optional (false)
36
46
  #
37
47
  # @note {Statement} treats symbols as interned {Node} instances, in a {Pattern}, they are treated as {Variable}.
38
48
  def initialize(subject = nil, predicate = nil, object = nil, options = {})
49
+ if options.has_key?(:context)
50
+ warn "[DEPRECATION] the :contexts option to Pattern#initialize is deprecated in RDF.rb 2.0, use :graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
51
+ options[:graph_name] ||= options.delete(:context)
52
+ end
39
53
  super
40
54
  end
41
55
 
42
56
  ##
43
57
  # @private
44
58
  def initialize!
45
- @context = Variable.new(@context) if @context.is_a?(Symbol)
46
- @subject = Variable.new(@subject) if @subject.is_a?(Symbol)
47
- @predicate = Variable.new(@predicate) if @predicate.is_a?(Symbol)
48
- @object = Variable.new(@object) if @object.is_a?(Symbol)
59
+ @graph_name = Variable.new(@graph_name) if @graph_name.is_a?(Symbol)
60
+ @subject = Variable.new(@subject) if @subject.is_a?(Symbol)
61
+ @predicate = Variable.new(@predicate) if @predicate.is_a?(Symbol)
62
+ @object = Variable.new(@object) if @object.is_a?(Symbol)
49
63
  super
50
64
  end
51
65
 
@@ -67,7 +81,7 @@ module RDF; class Query
67
81
  # @return [Boolean] `true` or `false`
68
82
  # @since 0.3.0
69
83
  def blank?
70
- subject.nil? && predicate.nil? && object.nil? && context.nil?
84
+ subject.nil? && predicate.nil? && object.nil? && graph_name.nil?
71
85
  end
72
86
 
73
87
  ##
@@ -79,7 +93,7 @@ module RDF; class Query
79
93
  subject.is_a?(Variable) ||
80
94
  predicate.is_a?(Variable) ||
81
95
  object.is_a?(Variable) ||
82
- context.is_a?(Variable)
96
+ graph_name.is_a?(Variable)
83
97
  end
84
98
  alias_method :variables?, :has_variables?
85
99
 
@@ -88,7 +102,7 @@ module RDF; class Query
88
102
  #
89
103
  # @example
90
104
  # Pattern.new(:s, :p, :o).optional? #=> false
91
- # Pattern.new(:s, :p, :o, :optional => true).optional? #=> true
105
+ # Pattern.new(:s, :p, :o, optional: true).optional? #=> true
92
106
  #
93
107
  # @return [Boolean] `true` or `false`
94
108
  # @since 0.3.0
@@ -104,7 +118,7 @@ module RDF; class Query
104
118
  (has_subject? ? (subject.resource? || subject.variable?) && subject.valid? : true) &&
105
119
  (has_predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
106
120
  (has_object? ? (object.term? || object.variable?) && object.valid? : true) &&
107
- (has_context? ? (context.resource? || context.variable?) && context.valid? : true )
121
+ (has_graph? ? (graph_name.resource? || graph_name.variable?) && graph_name.valid? : true )
108
122
  rescue NoMethodError
109
123
  false
110
124
  end
@@ -117,7 +131,7 @@ module RDF; class Query
117
131
  # If the optional `bindings` are given, variables will be substituted with their values
118
132
  # when executing the query.
119
133
  #
120
- # To match triples only in the default context, set context to `false`.
134
+ # To match triples only in the default graph, set graph_name to `false`.
121
135
  #
122
136
  # @example
123
137
  # Pattern.new(:s, :p, :o).execute(RDF::Repository.load('etc/doap.nt'))
@@ -130,16 +144,16 @@ module RDF; class Query
130
144
  # each matching statement
131
145
  # @yieldparam [RDF::Statement] statement
132
146
  # an RDF statement matching this pattern
133
- # @return [Enumerator]
147
+ # @return [Enumerable<RDF::Query::Pattern>]
134
148
  # an enumerator yielding matching statements
135
149
  # @see RDF::Queryable#query
136
150
  # @since 0.3.0
137
151
  def execute(queryable, bindings = {}, &block)
138
152
  query = {
139
- :subject => subject.is_a?(Variable) && bindings[subject.to_sym] ? bindings[subject.to_sym] : subject,
140
- :predicate => predicate.is_a?(Variable) && bindings[predicate.to_sym] ? bindings[predicate.to_sym] : predicate,
141
- :object => object.is_a?(Variable) && bindings[object.to_sym] ? bindings[object.to_sym] : object,
142
- :context => context.is_a?(Variable) && bindings[context.to_sym] ? bindings[context.to_sym] : context,
153
+ subject: subject.is_a?(Variable) && bindings[subject.to_sym] ? bindings[subject.to_sym] : subject,
154
+ predicate: predicate.is_a?(Variable) && bindings[predicate.to_sym] ? bindings[predicate.to_sym] : predicate,
155
+ object: object.is_a?(Variable) && bindings[object.to_sym] ? bindings[object.to_sym] : object,
156
+ graph_name: graph_name.is_a?(Variable) && bindings[graph_name.to_sym] ? bindings[graph_name.to_sym] : graph_name,
143
157
  }.delete_if{|k,v| v.nil?}
144
158
 
145
159
  # Do all the variable terms refer to distinct variables?
@@ -185,10 +199,10 @@ module RDF; class Query
185
199
  # @since 0.3.0
186
200
  def solution(statement)
187
201
  RDF::Query::Solution.new do |solution|
188
- solution[subject.to_sym] = statement.subject if subject.is_a?(Variable)
189
- solution[predicate.to_sym] = statement.predicate if predicate.is_a?(Variable)
190
- solution[object.to_sym] = statement.object if object.is_a?(Variable)
191
- solution[context.to_sym] = statement.context if context.is_a?(Variable)
202
+ solution[subject.to_sym] = statement.subject if subject.is_a?(Variable)
203
+ solution[predicate.to_sym] = statement.predicate if predicate.is_a?(Variable)
204
+ solution[object.to_sym] = statement.object if object.is_a?(Variable)
205
+ solution[graph_name.to_sym] = statement.graph_name if graph_name.is_a?(Variable)
192
206
  end
193
207
  end
194
208
 
@@ -204,10 +218,10 @@ module RDF; class Query
204
218
  # @since 0.3.0
205
219
  def variable_terms(name = nil)
206
220
  terms = []
207
- terms << :subject if subject.is_a?(Variable) && (!name || name.eql?(subject.name))
208
- terms << :predicate if predicate.is_a?(Variable) && (!name || name.eql?(predicate.name))
209
- terms << :object if object.is_a?(Variable) && (!name || name.eql?(object.name))
210
- terms << :context if context.is_a?(Variable) && (!name || name.eql?(context.name))
221
+ terms << :subject if subject.is_a?(Variable) && (!name || name.eql?(subject.name))
222
+ terms << :predicate if predicate.is_a?(Variable) && (!name || name.eql?(predicate.name))
223
+ terms << :object if object.is_a?(Variable) && (!name || name.eql?(object.name))
224
+ terms << :graph_name if graph_name.is_a?(Variable) && (!name || name.eql?(graph_name.name))
211
225
  terms
212
226
  end
213
227
 
@@ -223,7 +237,7 @@ module RDF; class Query
223
237
  count += 1 if subject.is_a?(Variable)
224
238
  count += 1 if predicate.is_a?(Variable)
225
239
  count += 1 if object.is_a?(Variable)
226
- count += 1 if context.is_a?(Variable)
240
+ count += 1 if graph_name.is_a?(Variable)
227
241
  count
228
242
  end
229
243
  alias_method :cardinality, :variable_count
@@ -237,10 +251,10 @@ module RDF; class Query
237
251
  # @return [Hash{Symbol => Variable}]
238
252
  def variables
239
253
  variables = {}
240
- variables.merge!(subject.variables) if subject.is_a?(Variable)
241
- variables.merge!(predicate.variables) if predicate.is_a?(Variable)
242
- variables.merge!(object.variables) if object.is_a?(Variable)
243
- variables.merge!(context.variables) if context.is_a?(Variable)
254
+ variables.merge!(subject.variables) if subject.is_a?(Variable)
255
+ variables.merge!(predicate.variables) if predicate.is_a?(Variable)
256
+ variables.merge!(object.variables) if object.is_a?(Variable)
257
+ variables.merge!(graph_name.variables) if graph_name.is_a?(Variable)
244
258
  variables
245
259
  end
246
260
 
@@ -280,10 +294,10 @@ module RDF; class Query
280
294
  # @return [Hash{Symbol => RDF::Term}]
281
295
  def bindings
282
296
  bindings = {}
283
- bindings.merge!(subject.bindings) if subject.is_a?(Variable)
284
- bindings.merge!(predicate.bindings) if predicate.is_a?(Variable)
285
- bindings.merge!(object.bindings) if object.is_a?(Variable)
286
- bindings.merge!(context.bindings) if context.is_a?(Variable)
297
+ bindings.merge!(subject.bindings) if subject.is_a?(Variable)
298
+ bindings.merge!(predicate.bindings) if predicate.is_a?(Variable)
299
+ bindings.merge!(object.bindings) if object.is_a?(Variable)
300
+ bindings.merge!(graph_name.bindings) if graph_name.is_a?(Variable)
287
301
  bindings
288
302
  end
289
303
 
@@ -329,10 +343,10 @@ module RDF; class Query
329
343
  buffer << [subject, predicate, object].map do |r|
330
344
  r.is_a?(RDF::Query::Variable) ? r.to_s : RDF::NTriples.serialize(r)
331
345
  end.join(" ")
332
- buffer << case context
346
+ buffer << case graph_name
333
347
  when nil, false then " ."
334
- when Variable then " #{context.to_s} ."
335
- else " #{RDF::NTriples.serialize(context)} ."
348
+ when Variable then " #{graph_name.to_s} ."
349
+ else " #{RDF::NTriples.serialize(graph_name)} ."
336
350
  end
337
351
  buffer.string
338
352
  end
@@ -18,7 +18,7 @@ class RDF::Query
18
18
  # solution.mbox
19
19
  #
20
20
  # @example Retrieving all bindings in the solution as a `Hash`
21
- # solution.to_hash #=> {:mbox => "jrhacker@example.org", ...}
21
+ # solution.to_hash #=> {mbox: "jrhacker@example.org", ...}
22
22
  #
23
23
  class Solution
24
24
  # Undefine all superfluous instance methods:
@@ -3,10 +3,10 @@ module RDF; class Query
3
3
  # An RDF basic graph pattern (BGP) query solution sequence.
4
4
  #
5
5
  # @example Filtering solutions using a hash
6
- # solutions.filter(:author => RDF::URI("http://ar.to/#self"))
7
- # solutions.filter(:author => "Gregg Kellogg")
8
- # solutions.filter(:author => [RDF::URI("http://ar.to/#self"), "Gregg Kellogg"])
9
- # solutions.filter(:updated => RDF::Literal(Date.today))
6
+ # solutions.filter(author: RDF::URI("http://ar.to/#self"))
7
+ # solutions.filter(author: "Gregg Kellogg")
8
+ # solutions.filter(author: [RDF::URI("http://ar.to/#self"), "Gregg Kellogg"])
9
+ # solutions.filter(updated: RDF::Literal(Date.today))
10
10
  #
11
11
  # @example Filtering solutions using a block
12
12
  # solutions.filter { |solution| solution.author.literal? }
data/lib/rdf/query.rb CHANGED
@@ -3,10 +3,10 @@ module RDF
3
3
  # An RDF basic graph pattern (BGP) query.
4
4
  #
5
5
  # Named queries either match against a specifically named
6
- # contexts if the name is an RDF::Resource or bound RDF::Query::Variable.
6
+ # graph if the name is an RDF::Resource or bound RDF::Query::Variable.
7
7
  # Names that are against unbound variables match either default
8
- # or named contexts.
9
- # The name of `false` will only match against the default context.
8
+ # or named graphs.
9
+ # The name of `false` will only match against the default graph.
10
10
  #
11
11
  # Variable names cause the variable to be added to the solution set
12
12
  # elements.
@@ -20,7 +20,7 @@ module RDF
20
20
  #
21
21
  # @example Constructing a basic graph pattern query (2)
22
22
  # query = RDF::Query.new({
23
- # :person => {
23
+ # person: {
24
24
  # RDF.type => FOAF.Person,
25
25
  # FOAF.name => :name,
26
26
  # FOAF.mbox => :email,
@@ -40,7 +40,7 @@ module RDF
40
40
  #
41
41
  # @example Constructing and executing a query in one go (2)
42
42
  # solutions = RDF::Query.execute(graph, {
43
- # :person => {
43
+ # person: {
44
44
  # RDF.type => FOAF.Person,
45
45
  # }
46
46
  # })
@@ -147,15 +147,17 @@ module RDF
147
147
  # @param [Hash{Symbol => Object}] options
148
148
  # any additional keyword options
149
149
  # @option options [RDF::Query::Solutions] :solutions (Solutions.new)
150
- # @option options [RDF::Resource, RDF::Query::Variable, false] :context (nil)
151
- # Default context for matching against queryable.
150
+ # @option options [RDF::Resource, RDF::Query::Variable, false] :graph_name (nil)
151
+ # Default graph name for matching against queryable.
152
152
  # Named queries either match against a specifically named
153
153
  # graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
154
154
  # Names that are against unbound variables match either default
155
155
  # or named graphs.
156
- # The name of `false` will only match against the default context.
156
+ # The name of `false` will only match against the default graph.
157
+ # @option options [RDF::Resource, RDF::Query::Variable, false] :context (nil)
158
+ # Alias for `:graph_name`. The :context option is deprecated in RDF.rb 2.0.
157
159
  # @option options [RDF::Resource, RDF::Query::Variable, false] :name (nil)
158
- # Alias for `:context`.
160
+ # Alias for `:graph_name`.
159
161
  # @yield [query]
160
162
  # @yieldparam [RDF::Query] query
161
163
  # @yieldreturn [void] ignored
@@ -166,24 +168,31 @@ module RDF
166
168
  # @param [Hash{Symbol => Object}] options
167
169
  # any additional keyword options
168
170
  # @option options [RDF::Query::Solutions] :solutions (Solutions.new)
169
- # @option options [RDF::Resource, RDF::Query::Variable, false] :context (nil)
170
- # Default context for matching against queryable.
171
+ # @option options [RDF::Resource, RDF::Query::Variable, false] :graph_name (nil)
172
+ # Default graph name for matching against queryable.
171
173
  # Named queries either match against a specifically named
172
174
  # graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
173
175
  # Names that are against unbound variables match either default
174
176
  # or named graphs.
177
+ # The name of `false` will only match against the default graph.
178
+ # @option options [RDF::Resource, RDF::Query::Variable, false] :context (nil)
179
+ # Alias for `:graph_name`. The :context option is deprecated in RDF.rb 2.0.
175
180
  # @option options [RDF::Resource, RDF::Query::Variable, false] :name (nil)
176
- # Alias for `:context`.
181
+ # Alias for `:graph_name`.
177
182
  # @yield [query]
178
183
  # @yieldparam [RDF::Query] query
179
184
  # @yieldreturn [void] ignored
180
185
  def initialize(*patterns, &block)
181
186
  @options = patterns.last.is_a?(Hash) ? patterns.pop.dup : {}
187
+ if @options.has_key?(:context)
188
+ warn "[DEPRECATION] the :contexts option to Query#initialize is deprecated in RDF.rb 2.0, use :graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
189
+ @options[:graph_name] ||= options.delete(:context)
190
+ end
182
191
  patterns << @options if patterns.empty?
183
192
  @variables = {}
184
193
  @solutions = Query::Solutions(@options.delete(:solutions))
185
- context = @options.fetch(:context, @options.fetch(:name, nil))
186
- @options.delete(:context)
194
+ graph_name = @options.fetch(:graph_name, @options.fetch(:name, nil))
195
+ @options.delete(:graph_name)
187
196
  @options.delete(:name)
188
197
 
189
198
  @patterns = case patterns.first
@@ -192,7 +201,7 @@ module RDF
192
201
  else patterns
193
202
  end
194
203
 
195
- self.context = context
204
+ self.graph_name = graph_name
196
205
 
197
206
  if block_given?
198
207
  case block.arity
@@ -245,7 +254,7 @@ module RDF
245
254
  #
246
255
  # @param [Hash{Symbol => Object}] options
247
256
  # any additional options for optimization
248
- # @return [void] `self`
257
+ # @return [self]
249
258
  # @see RDF::Query::Pattern#cost
250
259
  # @since 0.3.0
251
260
  def optimize!(options = {})
@@ -259,10 +268,10 @@ module RDF
259
268
  # Executes this query on the given `queryable` graph or repository.
260
269
  #
261
270
  # Named queries either match against a specifically named
262
- # contexts if the name is an RDF::Resource or bound RDF::Query::Variable.
271
+ # graphs if the name is an RDF::Resource or bound RDF::Query::Variable.
263
272
  # Names that are against unbound variables match either detault
264
- # or named contexts.
265
- # The name of `false` will only match against the default context.
273
+ # or named graphs.
274
+ # The name of `false` will only match against the default graph.
266
275
  #
267
276
  # If the query nas no patterns, it returns a single empty solution as
268
277
  # per SPARQL 1.1 _Empty Group Pattern_.
@@ -274,10 +283,12 @@ module RDF
274
283
  # @option options [Hash{Symbol => RDF::Term}] bindings
275
284
  # optional variable bindings to use
276
285
  # @option options [RDF::Resource, RDF::Query::Variable, false] context (nil)
277
- # Specific context for matching against queryable;
278
- # overrides default context defined on query.
286
+ # Alias for `:graph_name`. The :context option is deprecated in RDF.rb 2.0.
287
+ # @option options [RDF::Resource, RDF::Query::Variable, false] graph_name (nil)
288
+ # Specific graph name for matching against queryable;
289
+ # overrides default graph defined on query.
279
290
  # @option options [RDF::Resource, RDF::Query::Variable, false] name (nil)
280
- # Alias for `:context`.
291
+ # Alias for `:graph_name`.
281
292
  # @option options [RDF::Query::Solutions] solutions
282
293
  # optional initial solutions for chained queries
283
294
  # @yield [solution]
@@ -291,6 +302,10 @@ module RDF
291
302
  def execute(queryable, options = {}, &block)
292
303
  validate!
293
304
  options = options.dup
305
+ if options.has_key?(:context)
306
+ warn "[DEPRECATION] the :contexts option to Query#execute is deprecated in RDF.rb 2.0, use :graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
307
+ options[:graph_name] ||= options.delete(:context)
308
+ end
294
309
 
295
310
  # just so we can call #keys below without worrying
296
311
  options[:bindings] ||= {}
@@ -307,14 +322,14 @@ module RDF
307
322
  end
308
323
 
309
324
  patterns = @patterns
310
- context = options.fetch(:context, options.fetch(:name, self.context))
325
+ graph_name = options.fetch(:graph_name, options.fetch(:name, self.graph_name))
311
326
 
312
- # Add context to pattern, if necessary
313
- unless context.nil?
327
+ # Add graph_name to pattern, if necessary
328
+ unless graph_name.nil?
314
329
  if patterns.empty?
315
- patterns = [Pattern.new(nil, nil, nil, :context => context)]
330
+ patterns = [Pattern.new(nil, nil, nil, graph_name: graph_name)]
316
331
  else
317
- apply_context(context)
332
+ apply_graph_name(graph_name)
318
333
  end
319
334
  end
320
335
 
@@ -402,39 +417,63 @@ module RDF
402
417
  # Is this query scoped to a named graph?
403
418
  # @return [Boolean]
404
419
  def named?
405
- !!options[:context]
420
+ !!options[:graph_name]
406
421
  end
407
422
 
408
423
  # Is this query scoped to the default graph?
409
424
  # @return [Boolean]
410
425
  def default?
411
- options[:context] == false
426
+ options[:graph_name] == false
412
427
  end
413
428
 
414
429
  # Is this query unscoped? This indicates that it can return results from
415
430
  # either a named graph or the default graph.
416
431
  # @return [Boolean]
417
432
  def unnamed?
418
- options[:context].nil?
433
+ options[:graph_name].nil?
419
434
  end
420
-
435
+
421
436
  # Scope the query to named graphs matching value
422
437
  # @param [RDF::IRI, RDF::Query::Variable] value
423
438
  # @return [RDF::IRI, RDF::Query::Variable]
439
+ # @deprecated Use {#graph_name=} instead.
424
440
  def context=(value)
425
- options[:context] = value
441
+ warn "[DEPRECATION] Query#context= is deprecated in RDF.rb 2.0, use Query#graph_name= instead. Called from #{Gem.location_of_caller.join(':')}"
442
+ self.graph_name = value
426
443
  end
427
-
444
+
445
+ # Scope the query to named graphs matching value
446
+ # @param [RDF::IRI, RDF::Query::Variable] value
447
+ # @return [RDF::IRI, RDF::Query::Variable]
448
+ def graph_name=(value)
449
+ options[:graph_name] = value
450
+ end
451
+
428
452
  # Scope of this query, if any
429
453
  # @return [RDF::IRI, RDF::Query::Variable]
454
+ # @deprecated Use {#graph_name} instead.
430
455
  def context
431
- options[:context]
456
+ warn "[DEPRECATION] Query#context is deprecated in RDF.rb 2.0, use Query#graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
457
+ graph_name
458
+ end
459
+
460
+ # Scope of this query, if any
461
+ # @return [RDF::IRI, RDF::Query::Variable]
462
+ def graph_name
463
+ options[:graph_name]
432
464
  end
433
465
 
434
466
  # Apply the context specified (or configured) to all patterns that have no context
435
467
  # @param [RDF::IRI, RDF::Query::Variable] context (self.context)
436
468
  def apply_context(context = options[:context])
437
- patterns.each {|pattern| pattern.context = context if pattern.context.nil?} unless context.nil?
469
+ warn "[DEPRECATION] Query#apply_context is deprecated in RDF.rb 2.0, use Query#apply_graph_name instead. Called from #{Gem.location_of_caller.join(':')}"
470
+ apply_graph_name(context)
471
+ end
472
+
473
+ # Apply the graph name specified (or configured) to all patterns that have no graph name
474
+ # @param [RDF::IRI, RDF::Query::Variable] graph_name (self.graph_name)
475
+ def apply_graph_name(graph_name = options[:graph_name])
476
+ patterns.each {|pattern| pattern.graph_name = graph_name if pattern.graph_name.nil?} unless graph_name.nil?
438
477
  end
439
478
 
440
479
  ##
@@ -442,15 +481,16 @@ module RDF
442
481
  #
443
482
  # @return [Boolean]
444
483
  def variable?
445
- patterns.any?(&:variable?) || context && context.variable?
484
+ patterns.any?(&:variable?) || graph_name && graph_name.variable?
446
485
  end
447
486
 
448
487
  ##
449
488
  # Returns `true` if any pattern contains a blank node.
450
489
  #
451
490
  # @return [Boolean]
452
- def has_blank_nodes?
453
- patterns.any?(&:has_blank_nodes?) || context && context.node?
491
+ # @since 2.0
492
+ def node?
493
+ patterns.any?(&:node?) || graph_name && graph_name.node?
454
494
  end
455
495
 
456
496
  # Query has no patterns
@@ -458,6 +498,7 @@ module RDF
458
498
  def empty?
459
499
  patterns.empty?
460
500
  end
501
+ alias_method :has_blank_nodes?, :node?
461
502
 
462
503
  ##
463
504
  # Enumerates over each matching query solution.
@@ -477,7 +518,7 @@ module RDF
477
518
  # @yieldparam [::Query::Pattern] pattern
478
519
  # @return [Enumerator]
479
520
  def each_statement(&block)
480
- apply_context
521
+ apply_graph_name
481
522
  patterns.each(&block)
482
523
  end
483
524
 
@@ -486,7 +527,7 @@ module RDF
486
527
  # @return [RDF::Query]
487
528
  def dup
488
529
  patterns = @patterns.map {|p| p.dup}
489
- patterns << @options.merge(:solutions => @solutions.dup)
530
+ patterns << @options.merge(solutions: @solutions.dup)
490
531
  Query.new(*patterns)
491
532
  end
492
533
 
data/lib/rdf/reader.rb CHANGED
@@ -11,9 +11,9 @@ module RDF
11
11
  # @example Obtaining an RDF reader class
12
12
  # RDF::Reader.for(:ntriples) #=> RDF::NTriples::Reader
13
13
  # RDF::Reader.for("etc/doap.nt")
14
- # RDF::Reader.for(:file_name => "etc/doap.nt")
15
- # RDF::Reader.for(:file_extension => "nt")
16
- # RDF::Reader.for(:content_type => "text/plain")
14
+ # RDF::Reader.for(file_name: "etc/doap.nt")
15
+ # RDF::Reader.for(file_extension: "nt")
16
+ # RDF::Reader.for(content_type: "application/n-triples")
17
17
  #
18
18
  # @example Instantiating an RDF reader class
19
19
  # RDF::Reader.for(:ntriples).new($stdin) { |reader| ... }
@@ -87,7 +87,7 @@ module RDF
87
87
  #
88
88
  # @return [Class]
89
89
  def self.for(options = {}, &block)
90
- options = options.merge(:has_reader => true) if options.is_a?(Hash)
90
+ options = options.merge(has_reader: true) if options.is_a?(Hash)
91
91
  if format = self.format || Format.for(options, &block)
92
92
  format.reader
93
93
  end
@@ -246,7 +246,7 @@ module RDF
246
246
  #
247
247
  # @example
248
248
  # reader.prefixes = {
249
- # :dc => RDF::URI('http://purl.org/dc/terms/'),
249
+ # dc: RDF::URI('http://purl.org/dc/terms/'),
250
250
  # }
251
251
  #
252
252
  # @param [Hash{Symbol => RDF::URI}] prefixes
@@ -298,6 +298,7 @@ module RDF
298
298
  # @return [Enumerator]
299
299
  #
300
300
  # @return [void]
301
+ # @raise [RDF::ReaderError] on invalid data
301
302
  # @see RDF::Enumerable#each_statement
302
303
  def each_statement(&block)
303
304
  if block_given?
@@ -376,6 +377,22 @@ module RDF
376
377
  @input.lineno
377
378
  end
378
379
 
380
+ ##
381
+ # @return [Boolean]
382
+ #
383
+ # @note this parses the full input.
384
+ # Use `Reader.new(input, validate: true)` if you intend to capture the
385
+ # result.
386
+ #
387
+ # @see RDF::Value#validate! for Literal & URI validation relevant to
388
+ # error handling.
389
+ # @see Enumerable#valid?
390
+ def valid?
391
+ super
392
+ rescue ArgumentError, RDF::ReaderError
393
+ false
394
+ end
395
+
379
396
  protected
380
397
 
381
398
  ##