Skip to content

Commit 656494d

Browse files
committed
Fix documentation on RDF::Query#initialize and #execuite to note that variable graph names do not match the default graph due to SPARQL semantics.
Fixes #442.
1 parent a889ba9 commit 656494d

5 files changed

Lines changed: 49 additions & 11 deletions

File tree

lib/rdf/mixin/queryable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def query_execute(query, **options, &block)
146146
#
147147
# Patterns may also have embedded patterns as either a subject or object, recursively.
148148
#
149+
# Patterns with a variable `graph_name` do not match the default graph.
150+
#
149151
# When matching, match an embedded pattern against embedded statements, recursively. (see {RDF::Query::Pattern#eql?})
150152
#
151153
# @param [RDF::Query::Pattern] pattern

lib/rdf/query.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ def self.Solutions(*args)
151151
# @option options [RDF::Query::Solutions] :solutions (Solutions.new)
152152
# @option options [RDF::Resource, RDF::Query::Variable, false] :graph_name (nil)
153153
# Default graph name for matching against queryable.
154-
# Named queries either match against a specifically named
154+
# Queries with a graph name match against a specifically named
155155
# graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
156-
# Names that are against unbound variables match either default
157-
# or named graphs.
156+
# Queries using an unbound variable as a graph name only match against named graphs, and will not match the default graph.
158157
# The name of `false` will only match against the default graph.
159158
# @option options [RDF::Resource, RDF::Query::Variable, false] :name (nil)
160159
# Alias for `:graph_name`.
@@ -168,10 +167,9 @@ def self.Solutions(*args)
168167
# @param [RDF::Query::Solutions] solutions (Solutions.new)
169168
# @param [RDF::Resource, RDF::Query::Variable, false] graph_name (false)
170169
# Default graph name for matching against queryable.
171-
# Named queries either match against a specifically named
170+
# Queries with a graph name match against a specifically named
172171
# graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
173-
# Names that are against unbound variables match either default
174-
# or named graphs.
172+
# Queries using an unbound variable as a graph name only match against named graphs, and will not match the default graph.
175173
# The name of `false` will only match against the default graph.
176174
# @param [RDF::Resource, RDF::Query::Variable, false] name (false)
177175
# Alias for `:graph_name`.
@@ -285,10 +283,9 @@ def optimize!(**options)
285283
# @param [RDF::Query::Solutions] solutions (Solutions.new)
286284
# @param [RDF::Resource, RDF::Query::Variable, false] graph_name (nil)
287285
# Default graph name for matching against queryable.
288-
# Named queries either match against a specifically named
286+
# Queries with a graph name match against a specifically named
289287
# graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
290-
# Names that are against unbound variables match either default
291-
# or named graphs.
288+
# Queries using an unbound variable as a graph name only match against named graphs, and will not match the default graph.
292289
# The name of `false` will only match against the default graph.
293290
# @param [RDF::Resource, RDF::Query::Variable, false] name (nil)
294291
# Alias for `:graph_name`.

lib/rdf/query/pattern.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def self.from(pattern, graph_name: nil, **options)
2323
# @option options [Variable, URI, Symbol, nil] :predicate (nil)
2424
# @option options [Variable, Term, Symbol, nil] :object (nil)
2525
# @option options [Variable, Resource, Symbol, nil, false] :graph_name (nil)
26-
# A graph_name of nil matches any graph, a graph_name of false, matches only the default graph.
26+
# A graph_name of nil matches any graph, a graph_name of false, matches only the default graph. (See {RDF::Query#initialize})
2727
# @option options [Boolean] :optional (false)
2828
#
2929
# @overload initialize(subject, predicate, object, options = {})
@@ -32,7 +32,7 @@ def self.from(pattern, graph_name: nil, **options)
3232
# @param [Variable, Termm, Symbol, nil] object
3333
# @param [Hash{Symbol => Object}] options
3434
# @option options [Variable, Resource, Symbol, nil, false] :graph_name (nil)
35-
# A graph_name of nil matches any graph, a graph_name of false, matches only the default graph.
35+
# A graph_name of nil matches any graph, a graph_name of false, matches only the default graph. (See {RDF::Query#initialize})
3636
# @option options [Boolean] :optional (false)
3737
#
3838
# @note {Statement} treats symbols as interned {Node} instances, in a {Pattern}, they are treated as {Variable}.

lib/rdf/query/variable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def hash
233233
# Returns `true` if this variable is equivalent to a given `other`
234234
# variable. Or, to another Term if bound, or to any other Term
235235
#
236+
# @note when comparing against the default graph in an {RDF::Dataset}, `other` will be `false` and not be equal to an unbound variable.
237+
#
236238
# @param [Object] other
237239
# @return [Boolean] `true` or `false`
238240
# @since 0.3.0

spec/query_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,43 @@
965965
end
966966
end
967967

968+
context "Issues" do
969+
it "issue #442" do
970+
repository = RDF::Repository.new do |r|
971+
# Adding a statement in the default graph
972+
r << RDF::Statement.new(
973+
RDF::URI('http://www.example.com#alice'),
974+
RDF::URI('http://www.example.com#knows'),
975+
RDF::URI('http://www.example.com#bob')
976+
)
977+
978+
# Adding a statement in a named graph
979+
r << RDF::Statement.new(
980+
RDF::URI('http://www.example.com#alice'),
981+
RDF::URI('http://www.example.com#knows'),
982+
RDF::URI('http://www.example.com#charlie'),
983+
graph_name: RDF::URI('http://www.example.com#named_graph')
984+
)
985+
end
986+
987+
query = RDF::Query.new(
988+
{
989+
RDF::URI('http://www.example.com#alice') => {
990+
RDF::URI('http://www.example.com#knows') => :friend
991+
}
992+
},
993+
graph_name: RDF::Query::Variable.new(:graph)
994+
)
995+
996+
solutions = query.execute(repository)
997+
expect(solutions.count).to eql(1)
998+
expect(solutions.first).to eql(RDF::Query::Solution.new(
999+
friend: RDF::URI('http://www.example.com#charlie'),
1000+
graph: RDF::URI('http://www.example.com#named_graph')
1001+
))
1002+
end
1003+
end
1004+
9681005
context "Examples" do
9691006
let!(:graph) {RDF::Graph.new.insert(RDF::Spec.triples.extend(RDF::Enumerable))}
9701007
subject {

0 commit comments

Comments
 (0)