rdf 1.1.17.1 → 1.99.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.
- checksums.yaml +4 -4
- data/README +9 -9
- data/VERSION +1 -1
- data/lib/rdf/cli.rb +10 -10
- data/lib/rdf/format.rb +9 -9
- data/lib/rdf/mixin/enumerable.rb +66 -38
- data/lib/rdf/mixin/mutable.rb +11 -5
- data/lib/rdf/mixin/queryable.rb +2 -2
- data/lib/rdf/model/graph.rb +60 -40
- data/lib/rdf/model/list.rb +9 -9
- data/lib/rdf/model/literal/boolean.rb +1 -1
- data/lib/rdf/model/literal/date.rb +1 -1
- data/lib/rdf/model/literal/datetime.rb +5 -5
- data/lib/rdf/model/literal/decimal.rb +1 -1
- data/lib/rdf/model/literal/double.rb +3 -1
- data/lib/rdf/model/literal/integer.rb +1 -1
- data/lib/rdf/model/literal/time.rb +5 -5
- data/lib/rdf/model/literal/token.rb +1 -1
- data/lib/rdf/model/literal.rb +40 -6
- data/lib/rdf/model/node.rb +1 -1
- data/lib/rdf/model/statement.rb +88 -30
- data/lib/rdf/model/term.rb +10 -1
- data/lib/rdf/model/uri.rb +24 -25
- data/lib/rdf/nquads.rb +13 -22
- data/lib/rdf/ntriples/format.rb +4 -5
- data/lib/rdf/ntriples/reader.rb +10 -10
- data/lib/rdf/ntriples/writer.rb +6 -6
- data/lib/rdf/query/pattern.rb +49 -35
- data/lib/rdf/query/solution.rb +1 -1
- data/lib/rdf/query/solutions.rb +4 -4
- data/lib/rdf/query.rb +81 -40
- data/lib/rdf/reader.rb +22 -5
- data/lib/rdf/repository.rb +86 -37
- data/lib/rdf/transaction.rb +41 -20
- data/lib/rdf/util/file.rb +35 -18
- data/lib/rdf/vocab/schema.rb +5129 -5127
- data/lib/rdf/vocabulary.rb +43 -60
- data/lib/rdf/writer.rb +22 -12
- data/lib/rdf.rb +19 -4
- metadata +19 -5
data/lib/rdf/query/pattern.rb
CHANGED
@@ -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(:
|
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
|
-
@
|
46
|
-
@subject
|
47
|
-
@predicate
|
48
|
-
@object
|
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? &&
|
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
|
-
|
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, :
|
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
|
-
(
|
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
|
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 [
|
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
|
140
|
-
:predicate
|
141
|
-
:object
|
142
|
-
:
|
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]
|
189
|
-
solution[predicate.to_sym]
|
190
|
-
solution[object.to_sym]
|
191
|
-
solution[
|
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
|
208
|
-
terms << :predicate
|
209
|
-
terms << :object
|
210
|
-
terms << :
|
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
|
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)
|
241
|
-
variables.merge!(predicate.variables)
|
242
|
-
variables.merge!(object.variables)
|
243
|
-
variables.merge!(
|
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)
|
284
|
-
bindings.merge!(predicate.bindings)
|
285
|
-
bindings.merge!(object.bindings)
|
286
|
-
bindings.merge!(
|
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
|
346
|
+
buffer << case graph_name
|
333
347
|
when nil, false then " ."
|
334
|
-
when Variable then " #{
|
335
|
-
else " #{RDF::NTriples.serialize(
|
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
|
data/lib/rdf/query/solution.rb
CHANGED
@@ -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 #=> {:
|
21
|
+
# solution.to_hash #=> {mbox: "jrhacker@example.org", ...}
|
22
22
|
#
|
23
23
|
class Solution
|
24
24
|
# Undefine all superfluous instance methods:
|
data/lib/rdf/query/solutions.rb
CHANGED
@@ -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(:
|
7
|
-
# solutions.filter(:
|
8
|
-
# solutions.filter(:
|
9
|
-
# solutions.filter(:
|
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
|
-
#
|
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
|
9
|
-
# The name of `false` will only match against the default
|
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
|
-
# :
|
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
|
-
# :
|
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] :
|
151
|
-
# Default
|
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
|
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 `:
|
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] :
|
170
|
-
# Default
|
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 `:
|
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
|
-
|
186
|
-
@options.delete(:
|
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.
|
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 [
|
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
|
-
#
|
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
|
265
|
-
# The name of `false` will only match against the default
|
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
|
-
#
|
278
|
-
#
|
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 `:
|
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
|
-
|
325
|
+
graph_name = options.fetch(:graph_name, options.fetch(:name, self.graph_name))
|
311
326
|
|
312
|
-
# Add
|
313
|
-
unless
|
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, :
|
330
|
+
patterns = [Pattern.new(nil, nil, nil, graph_name: graph_name)]
|
316
331
|
else
|
317
|
-
|
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[:
|
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[:
|
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[:
|
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
|
-
|
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
|
-
|
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
|
-
|
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?) ||
|
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
|
-
|
453
|
-
|
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
|
-
|
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(:
|
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(:
|
15
|
-
# RDF::Reader.for(:
|
16
|
-
# RDF::Reader.for(:
|
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(:
|
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
|
-
# :
|
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
|
##
|