sparql 3.0.0 → 3.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +184 -70
- data/UNLICENSE +1 -1
- data/VERSION +1 -1
- data/bin/sparql +28 -17
- data/lib/rack/sparql.rb +1 -1
- data/lib/rack/sparql/conneg.rb +3 -3
- data/lib/sinatra/sparql.rb +5 -5
- data/lib/sparql.rb +14 -10
- data/lib/sparql/algebra.rb +15 -23
- data/lib/sparql/algebra/aggregate.rb +4 -4
- data/lib/sparql/algebra/evaluatable.rb +2 -2
- data/lib/sparql/algebra/expression.rb +28 -21
- data/lib/sparql/algebra/extensions.rb +142 -16
- data/lib/sparql/algebra/operator.rb +37 -16
- data/lib/sparql/algebra/operator/abs.rb +2 -2
- data/lib/sparql/algebra/operator/add.rb +3 -3
- data/lib/sparql/algebra/operator/alt.rb +4 -4
- data/lib/sparql/algebra/operator/and.rb +7 -7
- data/lib/sparql/algebra/operator/asc.rb +3 -3
- data/lib/sparql/algebra/operator/ask.rb +4 -14
- data/lib/sparql/algebra/operator/avg.rb +6 -4
- data/lib/sparql/algebra/operator/base.rb +10 -10
- data/lib/sparql/algebra/operator/bgp.rb +1 -1
- data/lib/sparql/algebra/operator/bnode.rb +5 -5
- data/lib/sparql/algebra/operator/bound.rb +3 -3
- data/lib/sparql/algebra/operator/ceil.rb +2 -2
- data/lib/sparql/algebra/operator/clear.rb +3 -3
- data/lib/sparql/algebra/operator/coalesce.rb +3 -13
- data/lib/sparql/algebra/operator/compare.rb +8 -8
- data/lib/sparql/algebra/operator/concat.rb +4 -4
- data/lib/sparql/algebra/operator/construct.rb +4 -14
- data/lib/sparql/algebra/operator/contains.rb +2 -2
- data/lib/sparql/algebra/operator/copy.rb +3 -3
- data/lib/sparql/algebra/operator/count.rb +3 -3
- data/lib/sparql/algebra/operator/create.rb +3 -3
- data/lib/sparql/algebra/operator/dataset.rb +5 -16
- data/lib/sparql/algebra/operator/datatype.rb +1 -1
- data/lib/sparql/algebra/operator/day.rb +1 -1
- data/lib/sparql/algebra/operator/delete.rb +7 -5
- data/lib/sparql/algebra/operator/delete_data.rb +3 -3
- data/lib/sparql/algebra/operator/delete_where.rb +5 -5
- data/lib/sparql/algebra/operator/desc.rb +1 -1
- data/lib/sparql/algebra/operator/describe.rb +3 -13
- data/lib/sparql/algebra/operator/distinct.rb +4 -14
- data/lib/sparql/algebra/operator/divide.rb +1 -1
- data/lib/sparql/algebra/operator/drop.rb +3 -3
- data/lib/sparql/algebra/operator/encode_for_uri.rb +3 -3
- data/lib/sparql/algebra/operator/equal.rb +3 -3
- data/lib/sparql/algebra/operator/exists.rb +5 -5
- data/lib/sparql/algebra/operator/exprlist.rb +3 -13
- data/lib/sparql/algebra/operator/extend.rb +21 -20
- data/lib/sparql/algebra/operator/filter.rb +6 -16
- data/lib/sparql/algebra/operator/floor.rb +2 -2
- data/lib/sparql/algebra/operator/graph.rb +5 -16
- data/lib/sparql/algebra/operator/greater_than.rb +5 -5
- data/lib/sparql/algebra/operator/greater_than_or_equal.rb +5 -5
- data/lib/sparql/algebra/operator/group.rb +25 -25
- data/lib/sparql/algebra/operator/group_concat.rb +6 -6
- data/lib/sparql/algebra/operator/hours.rb +1 -1
- data/lib/sparql/algebra/operator/if.rb +5 -15
- data/lib/sparql/algebra/operator/in.rb +4 -14
- data/lib/sparql/algebra/operator/insert.rb +6 -4
- data/lib/sparql/algebra/operator/insert_data.rb +3 -3
- data/lib/sparql/algebra/operator/iri.rb +1 -1
- data/lib/sparql/algebra/operator/is_blank.rb +1 -1
- data/lib/sparql/algebra/operator/is_iri.rb +1 -1
- data/lib/sparql/algebra/operator/is_literal.rb +1 -1
- data/lib/sparql/algebra/operator/is_numeric.rb +1 -1
- data/lib/sparql/algebra/operator/join.rb +12 -10
- data/lib/sparql/algebra/operator/lang.rb +1 -1
- data/lib/sparql/algebra/operator/lang_matches.rb +3 -3
- data/lib/sparql/algebra/operator/lcase.rb +2 -2
- data/lib/sparql/algebra/operator/left_join.rb +15 -12
- data/lib/sparql/algebra/operator/less_than.rb +5 -5
- data/lib/sparql/algebra/operator/less_than_or_equal.rb +5 -5
- data/lib/sparql/algebra/operator/load.rb +3 -3
- data/lib/sparql/algebra/operator/max.rb +6 -4
- data/lib/sparql/algebra/operator/md5.rb +1 -1
- data/lib/sparql/algebra/operator/min.rb +6 -4
- data/lib/sparql/algebra/operator/minus.rb +12 -11
- data/lib/sparql/algebra/operator/minutes.rb +1 -1
- data/lib/sparql/algebra/operator/modify.rb +4 -4
- data/lib/sparql/algebra/operator/month.rb +1 -1
- data/lib/sparql/algebra/operator/move.rb +3 -3
- data/lib/sparql/algebra/operator/multiply.rb +1 -1
- data/lib/sparql/algebra/operator/negate.rb +1 -1
- data/lib/sparql/algebra/operator/not.rb +1 -1
- data/lib/sparql/algebra/operator/not_equal.rb +2 -2
- data/lib/sparql/algebra/operator/notexists.rb +4 -4
- data/lib/sparql/algebra/operator/notin.rb +4 -14
- data/lib/sparql/algebra/operator/notoneof.rb +5 -6
- data/lib/sparql/algebra/operator/now.rb +1 -1
- data/lib/sparql/algebra/operator/or.rb +7 -7
- data/lib/sparql/algebra/operator/order.rb +6 -16
- data/lib/sparql/algebra/operator/path.rb +6 -5
- data/lib/sparql/algebra/operator/path_opt.rb +16 -16
- data/lib/sparql/algebra/operator/path_plus.rb +8 -8
- data/lib/sparql/algebra/operator/path_star.rb +4 -4
- data/lib/sparql/algebra/operator/plus.rb +3 -3
- data/lib/sparql/algebra/operator/prefix.rb +10 -10
- data/lib/sparql/algebra/operator/project.rb +4 -14
- data/lib/sparql/algebra/operator/rand.rb +1 -1
- data/lib/sparql/algebra/operator/reduced.rb +4 -14
- data/lib/sparql/algebra/operator/regex.rb +6 -6
- data/lib/sparql/algebra/operator/replace.rb +4 -4
- data/lib/sparql/algebra/operator/reverse.rb +4 -4
- data/lib/sparql/algebra/operator/round.rb +2 -2
- data/lib/sparql/algebra/operator/same_term.rb +8 -5
- data/lib/sparql/algebra/operator/sample.rb +3 -3
- data/lib/sparql/algebra/operator/seconds.rb +1 -1
- data/lib/sparql/algebra/operator/seq.rb +5 -6
- data/lib/sparql/algebra/operator/sequence.rb +4 -4
- data/lib/sparql/algebra/operator/sha1.rb +1 -1
- data/lib/sparql/algebra/operator/sha256.rb +1 -1
- data/lib/sparql/algebra/operator/sha384.rb +1 -1
- data/lib/sparql/algebra/operator/sha512.rb +1 -1
- data/lib/sparql/algebra/operator/slice.rb +4 -14
- data/lib/sparql/algebra/operator/str.rb +1 -1
- data/lib/sparql/algebra/operator/strafter.rb +2 -2
- data/lib/sparql/algebra/operator/strbefore.rb +2 -2
- data/lib/sparql/algebra/operator/strdt.rb +2 -2
- data/lib/sparql/algebra/operator/strends.rb +2 -2
- data/lib/sparql/algebra/operator/strlang.rb +2 -2
- data/lib/sparql/algebra/operator/strlen.rb +2 -2
- data/lib/sparql/algebra/operator/strstarts.rb +2 -2
- data/lib/sparql/algebra/operator/struuid.rb +1 -1
- data/lib/sparql/algebra/operator/substr.rb +4 -4
- data/lib/sparql/algebra/operator/subtract.rb +1 -1
- data/lib/sparql/algebra/operator/sum.rb +6 -4
- data/lib/sparql/algebra/operator/table.rb +6 -4
- data/lib/sparql/algebra/operator/timezone.rb +1 -1
- data/lib/sparql/algebra/operator/tz.rb +1 -1
- data/lib/sparql/algebra/operator/ucase.rb +2 -2
- data/lib/sparql/algebra/operator/union.rb +10 -9
- data/lib/sparql/algebra/operator/update.rb +4 -4
- data/lib/sparql/algebra/operator/using.rb +4 -4
- data/lib/sparql/algebra/operator/uuid.rb +1 -1
- data/lib/sparql/algebra/operator/with.rb +6 -6
- data/lib/sparql/algebra/operator/year.rb +1 -1
- data/lib/sparql/algebra/query.rb +2 -2
- data/lib/sparql/algebra/update.rb +2 -2
- data/lib/sparql/algebra/version.rb +1 -1
- data/lib/sparql/extensions.rb +5 -5
- data/lib/sparql/grammar.rb +111 -11
- data/lib/sparql/grammar/meta.rb +1372 -333
- data/lib/sparql/grammar/parser11.rb +56 -42
- data/lib/sparql/grammar/terminals11.rb +1 -0
- data/lib/sparql/results.rb +63 -44
- data/lib/sparql/version.rb +1 -1
- metadata +40 -47
@@ -11,8 +11,8 @@ module SPARQL; module Algebra
|
|
11
11
|
# (filter (|| (= ?type ex:Reptile) (= ?type ex:Insect))
|
12
12
|
# (bgp (triple ?animal <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type))))))
|
13
13
|
#
|
14
|
-
# @see
|
15
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-numeric-unary-minus
|
15
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
16
16
|
class Minus < Operator::Binary
|
17
17
|
include Query
|
18
18
|
|
@@ -33,18 +33,18 @@ module SPARQL; module Algebra
|
|
33
33
|
# @yieldreturn [void] ignored
|
34
34
|
# @return [RDF::Query::Solutions]
|
35
35
|
# the resulting solution sequence
|
36
|
-
# @see
|
37
|
-
# @see
|
38
|
-
def execute(queryable, options
|
36
|
+
# @see https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
|
37
|
+
# @see https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#negation
|
38
|
+
def execute(queryable, **options, &block)
|
39
39
|
# Let Ω1 and Ω2 be multisets of solution mappings. We define:
|
40
40
|
#
|
41
41
|
# Minus(Ω1, Ω2) = { μ | μ in Ω1 . ∀ μ' in Ω2, either μ and μ' are not compatible or dom(μ) and dom(μ') are disjoint }
|
42
42
|
#
|
43
43
|
# card[Minus(Ω1, Ω2)](μ) = card[Ω1](μ)
|
44
44
|
debug(options) {"Minus"}
|
45
|
-
left = queryable.query(operand(0),
|
45
|
+
left = queryable.query(operand(0), depth: options[:depth].to_i + 1, **options)
|
46
46
|
debug(options) {"(minus left) #{left.inspect}"}
|
47
|
-
right = queryable.query(operand(1),
|
47
|
+
right = queryable.query(operand(1), depth: options[:depth].to_i + 1, **options)
|
48
48
|
debug(options) {"(minus right) #{right.inspect}"}
|
49
49
|
@solutions = left.minus(right)
|
50
50
|
@solutions.each(&block) if block_given?
|
@@ -52,16 +52,17 @@ module SPARQL; module Algebra
|
|
52
52
|
end
|
53
53
|
|
54
54
|
##
|
55
|
-
#
|
55
|
+
# Optimizes this query.
|
56
56
|
#
|
57
57
|
# Groups of one graph pattern (not a filter) become join(Z, A) and can be replaced by A.
|
58
58
|
# The empty graph pattern Z is the identity for join:
|
59
59
|
# Replace join(Z, A) by A
|
60
60
|
# Replace join(A, Z) by A
|
61
61
|
#
|
62
|
-
# @return [
|
63
|
-
|
64
|
-
|
62
|
+
# @return [self]
|
63
|
+
# @see SPARQL::Algebra::Expression#optimize!
|
64
|
+
def optimize!(**options)
|
65
|
+
ops = operands.map {|o| o.optimize(**options) }.select {|o| o.respond_to?(:empty?) && !o.empty?}
|
65
66
|
@operands = ops
|
66
67
|
self
|
67
68
|
end
|
@@ -11,7 +11,7 @@ module SPARQL; module Algebra
|
|
11
11
|
# (extend ((?x (minutes ?date)))
|
12
12
|
# (bgp (triple ?s :date ?date)))))
|
13
13
|
#
|
14
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-minutes
|
15
15
|
class Minutes < Operator::Unary
|
16
16
|
include Evaluatable
|
17
17
|
|
@@ -33,17 +33,17 @@ module SPARQL; module Algebra
|
|
33
33
|
# Returns queryable.
|
34
34
|
# @raise [IOError]
|
35
35
|
# If `from` does not exist, unless the `silent` operator is present
|
36
|
-
# @see
|
37
|
-
def execute(queryable, options
|
36
|
+
# @see https://www.w3.org/TR/sparql11-update/
|
37
|
+
def execute(queryable, **options)
|
38
38
|
debug(options) {"Modify"}
|
39
39
|
query = operands.shift
|
40
40
|
|
41
|
-
queryable.query(query,
|
41
|
+
queryable.query(query, depth: options[:depth].to_i + 1, **options) do |solution|
|
42
42
|
debug(options) {"(solution)=>#{solution.inspect}"}
|
43
43
|
|
44
44
|
# Execute each operand with queryable and solution
|
45
45
|
operands.each do |op|
|
46
|
-
op.execute(queryable, solution,
|
46
|
+
op.execute(queryable, solutions: solution, depth: options[:depth].to_i + 1, **options)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
queryable
|
@@ -11,7 +11,7 @@ module SPARQL; module Algebra
|
|
11
11
|
# (extend ((?x (month ?date)))
|
12
12
|
# (bgp (triple ?s :date ?date)))))
|
13
13
|
#
|
14
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-month
|
15
15
|
class Month < Operator::Unary
|
16
16
|
include Evaluatable
|
17
17
|
|
@@ -9,7 +9,7 @@ module SPARQL; module Algebra
|
|
9
9
|
# @example
|
10
10
|
# (move silent <iri> to default)
|
11
11
|
#
|
12
|
-
# @see
|
12
|
+
# @see https://www.w3.org/TR/sparql11-update/#move
|
13
13
|
class Move < Operator
|
14
14
|
include SPARQL::Algebra::Update
|
15
15
|
|
@@ -28,8 +28,8 @@ module SPARQL; module Algebra
|
|
28
28
|
# Returns queryable.
|
29
29
|
# @raise [IOError]
|
30
30
|
# If `from` does not exist, unless the `silent` operator is present
|
31
|
-
# @see
|
32
|
-
def execute(queryable, options
|
31
|
+
# @see https://www.w3.org/TR/sparql11-update/
|
32
|
+
def execute(queryable, **options)
|
33
33
|
debug(options) {"Move"}
|
34
34
|
silent = operands.first == :silent
|
35
35
|
operands.shift if silent
|
@@ -7,7 +7,7 @@ module SPARQL; module Algebra
|
|
7
7
|
# (* ?x ?y)
|
8
8
|
# (multiply ?x ?y)
|
9
9
|
#
|
10
|
-
# @see
|
10
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-numeric-multiply
|
11
11
|
class Multiply < Operator::Binary
|
12
12
|
include Evaluatable
|
13
13
|
|
@@ -7,7 +7,7 @@ module SPARQL; module Algebra
|
|
7
7
|
# (- ?x)
|
8
8
|
# (negate ?x)
|
9
9
|
#
|
10
|
-
# @see
|
10
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-numeric-unary-minus
|
11
11
|
class Negate < Operator::Unary
|
12
12
|
include Evaluatable
|
13
13
|
|
@@ -3,8 +3,8 @@ module SPARQL; module Algebra
|
|
3
3
|
##
|
4
4
|
# The SPARQL relational `!=` (not equal) comparison operator.
|
5
5
|
#
|
6
|
-
# @see
|
7
|
-
# @see
|
6
|
+
# @see https://www.w3.org/TR/sparql11-query/#OperatorMapping
|
7
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-RDFterm-equal
|
8
8
|
class NotEqual < Equal
|
9
9
|
NAME = :'!='
|
10
10
|
|
@@ -12,8 +12,8 @@ module SPARQL; module Algebra
|
|
12
12
|
# (bgp (triple ?s ?p ex:o1))))
|
13
13
|
# (bgp (triple ?s ?p ex:o))))
|
14
14
|
#
|
15
|
-
# @see
|
16
|
-
# @see
|
15
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-abs
|
16
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-abs
|
17
17
|
class NotExists < Operator::Unary
|
18
18
|
include Evaluatable
|
19
19
|
|
@@ -29,10 +29,10 @@ module SPARQL; module Algebra
|
|
29
29
|
# @option options[RDF::Queryable] queryable
|
30
30
|
# queryable to execute, using bindings as an initial solution.
|
31
31
|
# @return [RDF::Literal::Boolean] `true` or `false`
|
32
|
-
def evaluate(bindings, options
|
32
|
+
def evaluate(bindings, **options)
|
33
33
|
solutions = RDF::Query::Solutions(bindings)
|
34
34
|
queryable = options[:queryable]
|
35
|
-
operand(0).execute(queryable,
|
35
|
+
operand(0).execute(queryable, solutions: solutions, **options).empty?
|
36
36
|
end
|
37
37
|
end # NotExists
|
38
38
|
end # Operator
|
@@ -8,7 +8,7 @@ module SPARQL; module Algebra
|
|
8
8
|
# @example
|
9
9
|
# (ask (filter (notin ?o 1 2) (bgp)))
|
10
10
|
#
|
11
|
-
# @see
|
11
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-notin
|
12
12
|
class NotIn < Operator
|
13
13
|
include Evaluatable
|
14
14
|
|
@@ -42,12 +42,12 @@ module SPARQL; module Algebra
|
|
42
42
|
# options passed from query
|
43
43
|
# @return [RDF::Literal::Boolean] `true` or `false`
|
44
44
|
# @raise [TypeError] if term is not found and any operand raises an error
|
45
|
-
def evaluate(bindings, options
|
46
|
-
lhs = operands.first.evaluate(bindings,
|
45
|
+
def evaluate(bindings, **options)
|
46
|
+
lhs = operands.first.evaluate(bindings, depth: options[:depth].to_i + 1, **options)
|
47
47
|
error_found = false
|
48
48
|
found = operands[1..-1].any? do |op|
|
49
49
|
begin
|
50
|
-
lhs == op.evaluate(bindings,
|
50
|
+
lhs == op.evaluate(bindings, depth: options[:depth].to_i + 1, **options)
|
51
51
|
rescue TypeError
|
52
52
|
error_found = true
|
53
53
|
end
|
@@ -58,16 +58,6 @@ module SPARQL; module Algebra
|
|
58
58
|
else RDF::Literal::TRUE
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
62
|
-
##
|
63
|
-
# Returns an optimized version of this query.
|
64
|
-
#
|
65
|
-
# Return optimized query
|
66
|
-
#
|
67
|
-
# @return [Union, RDF::Query] `self`
|
68
|
-
def optimize
|
69
|
-
operands = operands.map(&:optimize)
|
70
|
-
end
|
71
61
|
end # Exprlist
|
72
62
|
end # Operator
|
73
63
|
end; end # SPARQL::Algebra
|
@@ -6,7 +6,7 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (notoneof ex:p1 ex:p2)
|
8
8
|
#
|
9
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#eval_negatedPropertySet
|
10
10
|
class NotOneOf < Operator
|
11
11
|
include Query
|
12
12
|
|
@@ -30,18 +30,17 @@ module SPARQL; module Algebra
|
|
30
30
|
# each matching solution
|
31
31
|
# @yieldparam [RDF::Query::Solution] solution
|
32
32
|
# @yieldreturn [void] ignored
|
33
|
-
# @see
|
34
|
-
def execute(queryable, options
|
33
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
34
|
+
def execute(queryable, **options, &block)
|
35
35
|
debug(options) {"NotOneOf #{operands.to_sse}"}
|
36
36
|
subject, object = options[:subject], options[:object]
|
37
37
|
|
38
|
-
v = RDF::Query::Variable.new
|
39
|
-
v.distinguished = false
|
38
|
+
v = RDF::Query::Variable.new(distinguished: false)
|
40
39
|
bgp = RDF::Query.new do |q|
|
41
40
|
q.pattern [subject, v, object]
|
42
41
|
end
|
43
42
|
query = Filter.new(NotIn.new(v, *operands), bgp)
|
44
|
-
queryable.query(query,
|
43
|
+
queryable.query(query, depth: options[:depth].to_i + 1, **options) do |solution|
|
45
44
|
solution.bindings.delete(v.to_sym)
|
46
45
|
debug(options) {"(solution)-> #{solution.to_h.to_sse}"}
|
47
46
|
block.call(solution)
|
@@ -7,8 +7,8 @@ module SPARQL; module Algebra
|
|
7
7
|
# (|| ?x ?y)
|
8
8
|
# (or ?x ?y)
|
9
9
|
#
|
10
|
-
# @see
|
11
|
-
# @see
|
10
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-logical-or
|
11
|
+
# @see https://www.w3.org/TR/sparql11-query/#evaluation
|
12
12
|
class Or < Operator::Binary
|
13
13
|
include Evaluatable
|
14
14
|
|
@@ -24,7 +24,7 @@ module SPARQL; module Algebra
|
|
24
24
|
# @param [Hash{Symbol => Object}] options
|
25
25
|
# any additional options (see {Operator#initialize})
|
26
26
|
# @raise [TypeError] if any operand is invalid
|
27
|
-
def initialize(left, right, options
|
27
|
+
def initialize(left, right, **options)
|
28
28
|
super
|
29
29
|
end
|
30
30
|
|
@@ -40,20 +40,20 @@ module SPARQL; module Algebra
|
|
40
40
|
# options passed from query
|
41
41
|
# @return [RDF::Literal::Boolean] `true` or `false`
|
42
42
|
# @raise [TypeError] if the operands could not be coerced to a boolean literal
|
43
|
-
def evaluate(bindings, options
|
43
|
+
def evaluate(bindings, **options)
|
44
44
|
begin
|
45
|
-
left = boolean(operand(0).evaluate(bindings,
|
45
|
+
left = boolean(operand(0).evaluate(bindings, depth: options[:depth].to_i + 1, **options)).true?
|
46
46
|
rescue TypeError
|
47
47
|
left = nil
|
48
48
|
end
|
49
49
|
|
50
50
|
begin
|
51
|
-
right = boolean(operand(1).evaluate(bindings,
|
51
|
+
right = boolean(operand(1).evaluate(bindings, depth: options[:depth].to_i + 1, **options)).true?
|
52
52
|
rescue TypeError
|
53
53
|
right = nil
|
54
54
|
end
|
55
55
|
|
56
|
-
# From
|
56
|
+
# From https://www.w3.org/TR/sparql11-query/#evaluation
|
57
57
|
# A logical-or that encounters an error on only one branch will return TRUE if the other branch is TRUE
|
58
58
|
# and an error if the other branch is FALSE.
|
59
59
|
case
|
@@ -9,7 +9,7 @@ module SPARQL; module Algebra
|
|
9
9
|
# (order ((asc ?name))
|
10
10
|
# (bgp (triple ?x foaf:name ?name)))))
|
11
11
|
#
|
12
|
-
# @see
|
12
|
+
# @see https://www.w3.org/TR/sparql11-query/#modOrderBy
|
13
13
|
class Order < Operator::Binary
|
14
14
|
include Query
|
15
15
|
|
@@ -30,16 +30,16 @@ module SPARQL; module Algebra
|
|
30
30
|
# @yieldreturn [void] ignored
|
31
31
|
# @return [RDF::Query::Solutions]
|
32
32
|
# the resulting solution sequence
|
33
|
-
# @see
|
34
|
-
def execute(queryable, options
|
33
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
34
|
+
def execute(queryable, **options, &block)
|
35
35
|
|
36
36
|
debug(options) {"Order"}
|
37
|
-
@solutions = queryable.query(operands.last,
|
37
|
+
@solutions = queryable.query(operands.last, depth: options[:depth].to_i + 1, **options).order do |a, b|
|
38
38
|
operand(0).inject(0) do |memo, op|
|
39
39
|
debug(options) {"(order) #{op.inspect}"}
|
40
40
|
memo = begin
|
41
|
-
a_eval = op.evaluate(a,
|
42
|
-
b_eval = op.evaluate(b,
|
41
|
+
a_eval = op.evaluate(a, queryable: queryable, depth: options[:depth].to_i + 1, **options) rescue nil
|
42
|
+
b_eval = op.evaluate(b, queryable: queryable, depth: options[:depth].to_i + 1, **options) rescue nil
|
43
43
|
comp = Operator::Compare.evaluate(a_eval, b_eval).to_i
|
44
44
|
comp = -comp if op.is_a?(Operator::Desc)
|
45
45
|
comp
|
@@ -50,16 +50,6 @@ module SPARQL; module Algebra
|
|
50
50
|
@solutions.each(&block) if block_given?
|
51
51
|
@solutions
|
52
52
|
end
|
53
|
-
|
54
|
-
##
|
55
|
-
# Returns an optimized version of this query.
|
56
|
-
#
|
57
|
-
# Return optimized query
|
58
|
-
#
|
59
|
-
# @return [Union, RDF::Query] `self`
|
60
|
-
def optimize
|
61
|
-
operands = operands.map(&:optimize)
|
62
|
-
end
|
63
53
|
end # Order
|
64
54
|
end # Operator
|
65
55
|
end; end # SPARQL::Algebra
|
@@ -6,7 +6,7 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (path :a (path+ :p) ?z)
|
8
8
|
#
|
9
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlTranslatePathExpressions
|
10
10
|
class Path < Operator::Ternary
|
11
11
|
include Query
|
12
12
|
|
@@ -25,17 +25,18 @@ module SPARQL; module Algebra
|
|
25
25
|
# @yieldreturn [void] ignored
|
26
26
|
# @return [RDF::Query::Solutions]
|
27
27
|
# the resulting solution sequence
|
28
|
-
# @see
|
29
|
-
def execute(queryable, options
|
28
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
29
|
+
def execute(queryable, **options, &block)
|
30
30
|
debug(options) {"Path #{operands.to_sse}"}
|
31
31
|
subject, path_op, object = operands
|
32
32
|
|
33
33
|
@solutions = RDF::Query::Solutions.new
|
34
|
-
path_op.execute(queryable,
|
34
|
+
path_op.execute(queryable,
|
35
35
|
subject: subject,
|
36
36
|
object: object,
|
37
37
|
graph_name: options.fetch(:graph_name, false),
|
38
|
-
depth: options[:depth].to_i + 1
|
38
|
+
depth: options[:depth].to_i + 1,
|
39
|
+
**options
|
39
40
|
) do |solution|
|
40
41
|
@solutions << solution
|
41
42
|
end
|
@@ -6,7 +6,7 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (path? :p)
|
8
8
|
#
|
9
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#defn_evalPP_ZeroOrOnePath
|
10
10
|
class PathOpt < Operator::Unary
|
11
11
|
include Query
|
12
12
|
|
@@ -29,8 +29,8 @@ module SPARQL; module Algebra
|
|
29
29
|
# each matching solution
|
30
30
|
# @yieldparam [RDF::Query::Solution] solution
|
31
31
|
# @yieldreturn [void] ignored
|
32
|
-
# @see
|
33
|
-
def execute(queryable, options
|
32
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
33
|
+
def execute(queryable, **options, &block)
|
34
34
|
subject, object = options[:subject], options[:object]
|
35
35
|
debug(options) {"Path? #{[subject, operands, object].to_sse}"}
|
36
36
|
|
@@ -41,48 +41,48 @@ module SPARQL; module Algebra
|
|
41
41
|
# Nodes is the set of all subjects and objects in queryable
|
42
42
|
# FIXME: should this be Queryable#enum_nodes?
|
43
43
|
# All subjects which are `object`
|
44
|
-
query = RDF::Query.new {|q| q.pattern(subject: subject)}
|
45
|
-
queryable.query(query, options) do |solution|
|
44
|
+
query = RDF::Query.new {|q| q.pattern({subject: subject})}
|
45
|
+
queryable.query(query, **options) do |solution|
|
46
46
|
solution.merge!(object.to_sym => solution[subject])
|
47
47
|
debug(options) {"(solution-s0)-> #{solution.to_h.to_sse}"}
|
48
48
|
solutions << solution
|
49
49
|
end if query.valid?
|
50
50
|
|
51
51
|
# All objects which are `object`
|
52
|
-
query = RDF::Query.new {|q| q.pattern(object: object)}
|
53
|
-
queryable.query(query, options) do |solution|
|
52
|
+
query = RDF::Query.new {|q| q.pattern({object: object})}
|
53
|
+
queryable.query(query, **options) do |solution|
|
54
54
|
solution.merge!(subject.to_sym => solution[object])
|
55
55
|
debug(options) {"(solution-o0)-> #{solution.to_h.to_sse}"}
|
56
56
|
solutions << solution
|
57
57
|
end if query.valid?
|
58
58
|
when subject.variable?
|
59
59
|
# All subjects which are `object`
|
60
|
-
query = RDF::Query.new {|q| q.pattern(subject: object)}
|
61
|
-
queryable.query(query, options) do |solution|
|
60
|
+
query = RDF::Query.new {|q| q.pattern({subject: object})}
|
61
|
+
queryable.query(query, **options) do |solution|
|
62
62
|
solution.merge!(subject.to_sym => object)
|
63
63
|
debug(options) {"(solution-s0)-> #{solution.to_h.to_sse}"}
|
64
64
|
solutions << solution
|
65
65
|
end if query.valid?
|
66
66
|
|
67
67
|
# All objects which are `object`
|
68
|
-
query = RDF::Query.new {|q| q.pattern(object: object)}
|
69
|
-
queryable.query(query, options) do |solution|
|
68
|
+
query = RDF::Query.new {|q| q.pattern({object: object})}
|
69
|
+
queryable.query(query, **options) do |solution|
|
70
70
|
solution.merge!(subject.to_sym => object)
|
71
71
|
debug(options) {"(solution-o0)-> #{solution.to_h.to_sse}"}
|
72
72
|
solutions << solution
|
73
73
|
end if query.valid?
|
74
74
|
when object.variable?
|
75
75
|
# All subjects which are `subject`
|
76
|
-
query = RDF::Query.new {|q| q.pattern(subject: subject)}
|
77
|
-
queryable.query(query, options) do |solution|
|
76
|
+
query = RDF::Query.new {|q| q.pattern({subject: subject})}
|
77
|
+
queryable.query(query, **options) do |solution|
|
78
78
|
solution.merge!(object.to_sym => subject)
|
79
79
|
debug(options) {"(solution-s0)-> #{solution.to_h.to_sse}"}
|
80
80
|
solutions << solution
|
81
81
|
end if query.valid?
|
82
82
|
|
83
83
|
# All objects which are `subject
|
84
|
-
query = RDF::Query.new {|q| q.pattern(object: subject)}
|
85
|
-
queryable.query(query, options) do |solution|
|
84
|
+
query = RDF::Query.new {|q| q.pattern({object: subject})}
|
85
|
+
queryable.query(query, **options) do |solution|
|
86
86
|
solution.merge!(object.to_sym => subject)
|
87
87
|
debug(options) {"(solution-o0)-> #{solution.to_h.to_sse}"}
|
88
88
|
solutions << solution
|
@@ -103,7 +103,7 @@ module SPARQL; module Algebra
|
|
103
103
|
|
104
104
|
# Recurse into query
|
105
105
|
solutions +=
|
106
|
-
queryable.query(query,
|
106
|
+
queryable.query(query, depth: options[:depth].to_i + 1, **options)
|
107
107
|
solutions.each(&block) if block_given?
|
108
108
|
solutions
|
109
109
|
end
|