sparql 3.0.1 → 3.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +188 -73
- data/UNLICENSE +1 -1
- data/VERSION +1 -1
- data/bin/sparql +44 -24
- 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 +17 -16
- 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 +63 -40
- data/lib/sparql/algebra/extensions.rb +180 -33
- 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 +6 -17
- 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 +9 -7
- data/lib/sparql/algebra/operator/delete_data.rb +3 -3
- data/lib/sparql/algebra/operator/delete_where.rb +6 -6
- 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 +2 -2
- 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 +19 -18
- 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 +6 -17
- 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 +7 -5
- 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 +3 -3
- 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 +9 -8
- 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 +7 -7
- 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 +2340 -907
- data/lib/sparql/grammar/parser11.rb +69 -69
- data/lib/sparql/grammar/terminals11.rb +2 -0
- data/lib/sparql/results.rb +73 -46
- data/lib/sparql/version.rb +1 -1
- metadata +38 -65
@@ -6,8 +6,8 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (strlen ?x)
|
8
8
|
#
|
9
|
-
# @see
|
10
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-strlen
|
10
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-string-length
|
11
11
|
class StrLen < Operator::Unary
|
12
12
|
include Evaluatable
|
13
13
|
|
@@ -6,8 +6,8 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (strstarts ?x ?y)
|
8
8
|
#
|
9
|
-
# @see
|
10
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-strstarts
|
10
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-starts-with
|
11
11
|
class StrStarts < Operator::Binary
|
12
12
|
include Evaluatable
|
13
13
|
|
@@ -14,7 +14,7 @@ module SPARQL; module Algebra
|
|
14
14
|
# (extend ((?uuid (struuid)))
|
15
15
|
# (bgp))))))
|
16
16
|
#
|
17
|
-
# @see
|
17
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-struuid
|
18
18
|
class StrUUID < Operator::Nullary
|
19
19
|
include Evaluatable
|
20
20
|
|
@@ -6,8 +6,8 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (substr ?x ?y)
|
8
8
|
#
|
9
|
-
# @see
|
10
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-substr
|
10
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-substring
|
11
11
|
class SubStr < Operator::Ternary
|
12
12
|
include Evaluatable
|
13
13
|
|
@@ -22,7 +22,7 @@ module SPARQL; module Algebra
|
|
22
22
|
# @param [Hash{Symbol => Object}] options
|
23
23
|
# any additional options (see {Operator#initialize})
|
24
24
|
# @raise [TypeError] if any operand is invalid
|
25
|
-
def initialize(source, startingLoc, length = RDF::Literal(""), options
|
25
|
+
def initialize(source, startingLoc, length = RDF::Literal(""), **options)
|
26
26
|
super
|
27
27
|
end
|
28
28
|
|
@@ -71,7 +71,7 @@ module SPARQL; module Algebra
|
|
71
71
|
# Remove the optional argument.
|
72
72
|
#
|
73
73
|
# @return [Array] `self`
|
74
|
-
# @see
|
74
|
+
# @see https://openjena.org/wiki/SSE
|
75
75
|
def to_sxp_bin
|
76
76
|
[NAME] + operands.reject {|o| o.to_s == ""}
|
77
77
|
end
|
@@ -5,7 +5,7 @@ module SPARQL; module Algebra
|
|
5
5
|
# (- ?x ?y)
|
6
6
|
# (subtract ?x ?y)
|
7
7
|
#
|
8
|
-
# @see
|
8
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-numeric-subtract
|
9
9
|
class Subtract < Operator::Binary
|
10
10
|
include Evaluatable
|
11
11
|
|
@@ -6,12 +6,12 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (prefix ((: <http://www.example.org/>))
|
8
8
|
# (project (?sum)
|
9
|
-
# (extend ((?sum
|
10
|
-
# (group () ((
|
9
|
+
# (extend ((?sum ??.0))
|
10
|
+
# (group () ((??.0 (sum ?o)))
|
11
11
|
# (bgp (triple ?s :dec ?o))))))
|
12
12
|
#
|
13
|
-
# @see
|
14
|
-
class Sum < Operator
|
13
|
+
# @see https://www.w3.org/TR/sparql11-query/#defn_aggSum
|
14
|
+
class Sum < Operator
|
15
15
|
include Aggregate
|
16
16
|
|
17
17
|
NAME = :sum
|
@@ -23,6 +23,8 @@ module SPARQL; module Algebra
|
|
23
23
|
# enum of evaluated operand
|
24
24
|
# @return [RDF::Literal::Numeric] The sum of the terms
|
25
25
|
def apply(enum)
|
26
|
+
# FIXME: we don't actually do anything with distinct
|
27
|
+
operands.shift if distinct = (operands.first == :distinct)
|
26
28
|
if enum.empty?
|
27
29
|
RDF::Literal(0)
|
28
30
|
elsif enum.flatten.all? {|n| n.is_a?(RDF::Literal::Numeric)}
|
@@ -13,7 +13,7 @@ module SPARQL; module Algebra
|
|
13
13
|
#
|
14
14
|
# @example empty table
|
15
15
|
# (table unit)
|
16
|
-
# @see
|
16
|
+
# @see https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#inline-data
|
17
17
|
class Table < Operator
|
18
18
|
include Query
|
19
19
|
|
@@ -32,8 +32,8 @@ module SPARQL; module Algebra
|
|
32
32
|
# @yieldreturn [void] ignored
|
33
33
|
# @return [RDF::Query::Solutions]
|
34
34
|
# the resulting solution sequence
|
35
|
-
# @see
|
36
|
-
def execute(queryable, options
|
35
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
36
|
+
def execute(queryable, **options, &block)
|
37
37
|
@solutions = RDF::Query::Solutions()
|
38
38
|
Array(operands[1..-1]).each do |row|
|
39
39
|
next unless row.is_a?(Array)
|
@@ -11,7 +11,7 @@ module SPARQL; module Algebra
|
|
11
11
|
# (extend ((?x (timezone ?date)))
|
12
12
|
# (bgp (triple ?s :date ?date)))))
|
13
13
|
#
|
14
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-timezone
|
15
15
|
class Timezone < Operator::Unary
|
16
16
|
include Evaluatable
|
17
17
|
|
@@ -11,7 +11,7 @@ module SPARQL; module Algebra
|
|
11
11
|
# (extend ((?x (tz ?date)))
|
12
12
|
# (bgp (triple ?s :date ?date)))))
|
13
13
|
#
|
14
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-tz
|
15
15
|
class TZ < Operator::Unary
|
16
16
|
include Evaluatable
|
17
17
|
|
@@ -6,8 +6,8 @@ module SPARQL; module Algebra
|
|
6
6
|
# @example
|
7
7
|
# (ucase ?x)
|
8
8
|
#
|
9
|
-
# @see
|
10
|
-
# @see
|
9
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-ucase
|
10
|
+
# @see https://www.w3.org/TR/xpath-functions/#func-ucase
|
11
11
|
class UCase < Operator::Unary
|
12
12
|
include Evaluatable
|
13
13
|
|
@@ -10,7 +10,7 @@ module SPARQL; module Algebra
|
|
10
10
|
# (graph ?g
|
11
11
|
# (bgp (triple ?s ?p ?o)))))
|
12
12
|
#
|
13
|
-
# @see
|
13
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
14
14
|
class Union < Operator::Binary
|
15
15
|
include Query
|
16
16
|
|
@@ -30,11 +30,11 @@ 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
|
debug(options) {"Union"}
|
36
36
|
@solutions = RDF::Query::Solutions(operands.inject([]) do |memo, op|
|
37
|
-
solns = op.execute(queryable,
|
37
|
+
solns = op.execute(queryable, depth: options[:depth].to_i + 1, **options)
|
38
38
|
debug(options) {"=> (op) #{solns.inspect}"}
|
39
39
|
memo + solns
|
40
40
|
end)
|
@@ -55,13 +55,14 @@ module SPARQL; module Algebra
|
|
55
55
|
end
|
56
56
|
|
57
57
|
##
|
58
|
-
#
|
58
|
+
# Optimizes this query.
|
59
59
|
#
|
60
60
|
# Optimize operands and remove any which are empty.
|
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
|
@@ -9,7 +9,7 @@ module SPARQL; module Algebra
|
|
9
9
|
# (bgp (triple ?s ?p ?o))
|
10
10
|
# (insert ((triple ?s ?p "q")))))
|
11
11
|
#
|
12
|
-
# @see
|
12
|
+
# @see https://www.w3.org/TR/sparql11-update/#graphUpdate
|
13
13
|
class Update < Operator
|
14
14
|
include SPARQL::Algebra::Update
|
15
15
|
|
@@ -30,12 +30,12 @@ module SPARQL; module Algebra
|
|
30
30
|
# If an attempt is made to perform an unsupported operation
|
31
31
|
# @raise [IOError]
|
32
32
|
# If `queryable` is immutable
|
33
|
-
# @see
|
34
|
-
def execute(queryable, options
|
33
|
+
# @see https://www.w3.org/TR/sparql11-update/
|
34
|
+
def execute(queryable, **options)
|
35
35
|
debug(options) {"Update"}
|
36
36
|
raise IOError, "queryable is not mutable" unless queryable.mutable?
|
37
37
|
operands.each do |op|
|
38
|
-
op.execute(queryable,
|
38
|
+
op.execute(queryable, depth: options[:depth].to_i + 1, **options)
|
39
39
|
end
|
40
40
|
queryable
|
41
41
|
end
|
@@ -9,7 +9,7 @@ module SPARQL; module Algebra
|
|
9
9
|
# @example
|
10
10
|
# (using (:g1) (bgp (triple ?s ?p ?o)))
|
11
11
|
#
|
12
|
-
# @see
|
12
|
+
# @see https://www.w3.org/TR/sparql11-update/#add
|
13
13
|
class Using < Operator
|
14
14
|
include SPARQL::Algebra::Query
|
15
15
|
|
@@ -30,10 +30,10 @@ module SPARQL; module Algebra
|
|
30
30
|
# Returns queryable.
|
31
31
|
# @raise [IOError]
|
32
32
|
# If `from` does not exist, unless the `silent` operator is present
|
33
|
-
# @see
|
34
|
-
def execute(queryable, options
|
33
|
+
# @see https://www.w3.org/TR/sparql11-update/
|
34
|
+
def execute(queryable, **options, &block)
|
35
35
|
debug(options) {"Using"}
|
36
|
-
Dataset.new(*operands).execute(queryable,
|
36
|
+
Dataset.new(*operands).execute(queryable, depth: options[:depth].to_i + 1, **options, &block)
|
37
37
|
end
|
38
38
|
end # Using
|
39
39
|
end # Operator
|
@@ -11,7 +11,7 @@ module SPARQL; module Algebra
|
|
11
11
|
# (bgp (triple ?s ?p ?o))
|
12
12
|
# (insert ((triple ?s ?p "z"))))
|
13
13
|
#
|
14
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/sparql11-update/#deleteInsert
|
15
15
|
class With < Operator
|
16
16
|
include SPARQL::Algebra::Update
|
17
17
|
|
@@ -32,17 +32,17 @@ module SPARQL; module Algebra
|
|
32
32
|
# Returns queryable.
|
33
33
|
# @raise [IOError]
|
34
34
|
# If `from` does not exist, unless the `silent` operator is present
|
35
|
-
# @see
|
36
|
-
def execute(queryable, options
|
35
|
+
# @see https://www.w3.org/TR/sparql11-update/
|
36
|
+
def execute(queryable, **options)
|
37
37
|
debug(options) {"With: #{operand.to_sse}"}
|
38
38
|
# Bound variable
|
39
39
|
name = operands.shift
|
40
40
|
|
41
41
|
unless queryable.has_graph?(name)
|
42
42
|
debug(options) {"=> default data source #{name}"}
|
43
|
-
load_opts = {
|
43
|
+
load_opts = {logger: options.fetch(:logger, false), base_uri: name}
|
44
44
|
debug(options) {"=> load #{name}"}
|
45
|
-
queryable.load(name.to_s, load_opts)
|
45
|
+
queryable.load(name.to_s, **load_opts)
|
46
46
|
end
|
47
47
|
|
48
48
|
# Set name for RDF::Graph descendants having no graph_name to the name variable
|
@@ -58,12 +58,12 @@ module SPARQL; module Algebra
|
|
58
58
|
query = operands.shift
|
59
59
|
|
60
60
|
# Restrict query portion to this graph
|
61
|
-
queryable.query(query,
|
61
|
+
queryable.query(query, depth: options[:depth].to_i + 1, **options) do |solution|
|
62
62
|
debug(options) {"(solution)=>#{solution.inspect}"}
|
63
63
|
|
64
64
|
# Execute each operand with queryable and solution
|
65
65
|
operands.each do |op|
|
66
|
-
op.execute(queryable, solution,
|
66
|
+
op.execute(queryable, solutions: solution, depth: options[:depth].to_i + 1, **options)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
@@ -11,7 +11,7 @@ module SPARQL; module Algebra
|
|
11
11
|
# (extend ((?x (year ?date)))
|
12
12
|
# (bgp (triple ?s :date ?date)))))
|
13
13
|
#
|
14
|
-
# @see
|
14
|
+
# @see https://www.w3.org/TR/sparql11-query/#func-year
|
15
15
|
class Year < Operator::Unary
|
16
16
|
include Evaluatable
|
17
17
|
|
data/lib/sparql/algebra/query.rb
CHANGED
@@ -49,8 +49,8 @@ module SPARQL; module Algebra
|
|
49
49
|
# Note, results may be used with {SPARQL.serialize_results} to obtain appropriate output encoding.
|
50
50
|
# @raise [NotImplementedError]
|
51
51
|
# If an attempt is made to perform an unsupported operation
|
52
|
-
# @see
|
53
|
-
def execute(queryable, options
|
52
|
+
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
|
53
|
+
def execute(queryable, **options, &block)
|
54
54
|
raise NotImplementedError, "#{self.class}#execute(#{queryable})"
|
55
55
|
end
|
56
56
|
|
@@ -38,8 +38,8 @@ module SPARQL; module Algebra
|
|
38
38
|
# Returns the dataset.
|
39
39
|
# @raise [NotImplementedError]
|
40
40
|
# If an attempt is made to perform an unsupported operation
|
41
|
-
# @see
|
42
|
-
def execute(queryable, options
|
41
|
+
# @see https://www.w3.org/TR/sparql11-update/
|
42
|
+
def execute(queryable, **options, &block)
|
43
43
|
raise NotImplementedError, "#{self.class}#execute(#{queryable})"
|
44
44
|
end
|
45
45
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module SPARQL; module Algebra
|
2
2
|
module VERSION
|
3
|
-
VERSION_FILE = File.
|
3
|
+
VERSION_FILE = File.expand_path("../../../../VERSION", __FILE__)
|
4
4
|
MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chop.split(".")
|
5
5
|
|
6
6
|
STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
|
data/lib/sparql/extensions.rb
CHANGED
@@ -43,7 +43,7 @@ module RDF::Queryable
|
|
43
43
|
# @yieldreturn [void] ignored
|
44
44
|
# @return [RDF::Graph]
|
45
45
|
#
|
46
|
-
# @see
|
46
|
+
# @see https://www.w3.org/Submission/CBD/
|
47
47
|
def concise_bounded_description(*terms, &block)
|
48
48
|
options = terms.last.is_a?(Hash) ? terms.pop.dup : {}
|
49
49
|
|
@@ -54,11 +54,11 @@ module RDF::Queryable
|
|
54
54
|
|
55
55
|
# Find terms not in self as a subject and recurse with their subjects
|
56
56
|
terms.reject {|term| self.first(subject: term)}.each do |term|
|
57
|
-
self.query(predicate: term) do |statement|
|
57
|
+
self.query({predicate: term}) do |statement|
|
58
58
|
query_terms << statement.subject
|
59
59
|
end
|
60
60
|
|
61
|
-
self.query(object: term) do |statement|
|
61
|
+
self.query({object: term}) do |statement|
|
62
62
|
query_terms << statement.subject
|
63
63
|
end
|
64
64
|
end
|
@@ -69,7 +69,7 @@ module RDF::Queryable
|
|
69
69
|
# Don't consider term if already in graph
|
70
70
|
terms.reject {|term| graph.first(subject: term)}.each do |term|
|
71
71
|
# Find statements from queryiable with term as a subject
|
72
|
-
self.query(subject: term) do |statement|
|
72
|
+
self.query({subject: term}) do |statement|
|
73
73
|
yield(statement) if block_given?
|
74
74
|
graph << statement
|
75
75
|
|
@@ -81,7 +81,7 @@ module RDF::Queryable
|
|
81
81
|
RDF.predicate => statement.predicate,
|
82
82
|
RDF.object => statement.object,
|
83
83
|
}
|
84
|
-
}).execute(self).each do |solution|
|
84
|
+
}, **{}).execute(self).each do |solution|
|
85
85
|
# Recurse to include this subject
|
86
86
|
recurse_opts = options.merge(non_subjects: false, graph: graph)
|
87
87
|
self.concise_bounded_description(solution[:s], recurse_opts, &block)
|
data/lib/sparql/grammar.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
require 'rdf' # @see
|
1
|
+
require 'rdf' # @see https://rubygems.org/gems/rdf
|
2
2
|
require 'sparql/algebra'
|
3
3
|
require 'json'
|
4
|
-
require 'sxp'
|
5
4
|
|
6
5
|
module SPARQL
|
7
6
|
##
|
@@ -21,7 +20,7 @@ module SPARQL
|
|
21
20
|
#
|
22
21
|
# The native SSE representation may be serialized to a textual representation of SSE as
|
23
22
|
# serialized general S-Expressions (SXP).
|
24
|
-
# The SXP generated closely follows that of [OpenJena ARQ](
|
23
|
+
# The SXP generated closely follows that of [OpenJena ARQ](https://openjena.org/wiki/SSE), which is intended principally for
|
25
24
|
# running the SPARQL rules. Additionally, SSE is generated for CONSTRUCT, ASK, DESCRIBE and FROM operators.
|
26
25
|
#
|
27
26
|
# SXP is generated by serializing the parser result as follows:
|
@@ -143,7 +142,108 @@ module SPARQL
|
|
143
142
|
# (using (:g1 :g2)
|
144
143
|
# (bgp (triple ?s ?p ?o)))
|
145
144
|
# (insert ((triple ?s ?p "q"))))))
|
145
|
+
#
|
146
|
+
# SPARQL:
|
147
|
+
#
|
148
|
+
# PREFIX : <http://example.org/>
|
149
|
+
#
|
150
|
+
# SELECT * WHERE
|
151
|
+
# {
|
152
|
+
# ?s :p ?v .
|
153
|
+
# BIND (2*?v AS ?v2) .
|
154
|
+
# ?s :p1 ?v2 .
|
155
|
+
# }
|
156
|
+
#
|
157
|
+
# SXP:
|
158
|
+
#
|
159
|
+
# (prefix ((: <http://example.org/>))
|
160
|
+
# (join
|
161
|
+
# (extend ((?v2 (* 2 ?v)))
|
162
|
+
# (bgp (triple ?s :p ?v)))
|
163
|
+
# (bgp (triple ?s :p1 ?v2))))
|
164
|
+
#
|
165
|
+
# SPARQL:
|
166
|
+
#
|
167
|
+
# PREFIX : <http://bigdata.com>
|
168
|
+
# PREFIX foaf: <http://xmlns.com/foaf/0.1/>
|
169
|
+
# PREFIX dct: <http://purl.org/dc/elements/1.1/>
|
170
|
+
#
|
171
|
+
# SELECT ?age ?src WHERE {
|
172
|
+
# ?bob foaf:name "Bob" .
|
173
|
+
# <<?bob foaf:age ?age>> dct:source ?src .
|
174
|
+
# }
|
175
|
+
#
|
176
|
+
# SXP:
|
146
177
|
#
|
178
|
+
# (prefix
|
179
|
+
# (
|
180
|
+
# (: <http://bigdata.com>)
|
181
|
+
# (foaf: <http://xmlns.com/foaf/0.1/>)
|
182
|
+
# (dct: <http://purl.org/dc/elements/1.1/>))
|
183
|
+
# (project
|
184
|
+
# (?age ?src)
|
185
|
+
# (bgp
|
186
|
+
# (triple ?bob foaf:name "Bob")
|
187
|
+
# (triple (triple ?bob foaf:age ?age) dct:source ?src)) ))
|
188
|
+
#
|
189
|
+
# SPARQL:
|
190
|
+
#
|
191
|
+
# PREFIX : <http://bigdata.com>
|
192
|
+
# PREFIX foaf: <http://xmlns.com/foaf/0.1/>
|
193
|
+
# PREFIX dct: <http://purl.org/dc/elements/1.1/>
|
194
|
+
#
|
195
|
+
# SELECT ?age ?src WHERE {
|
196
|
+
# ?bob foaf:name "Bob" .
|
197
|
+
# BIND( <<?bob foaf:age ?age>> AS ?t ) .
|
198
|
+
# ?t dct:source ?src .
|
199
|
+
# }
|
200
|
+
#
|
201
|
+
# SXP:
|
202
|
+
#
|
203
|
+
# (prefix
|
204
|
+
# (
|
205
|
+
# (: <http://bigdata.com>)
|
206
|
+
# (foaf: <http://xmlns.com/foaf/0.1/>)
|
207
|
+
# (dct: <http://purl.org/dc/elements/1.1/>))
|
208
|
+
# (project
|
209
|
+
# (?age ?src)
|
210
|
+
# (join
|
211
|
+
# (extend ((?t (triple ?bob foaf:age ?age))) (bgp (triple ?bob foaf:name "Bob")))
|
212
|
+
# (bgp (triple ?t dct:source ?src))) ))
|
213
|
+
#
|
214
|
+
# SPARQL:
|
215
|
+
#
|
216
|
+
# PREFIX : <http://bigdata.com>
|
217
|
+
# PREFIX foaf: <http://xmlns.com/foaf/0.1/>
|
218
|
+
# PREFIX dct: <http://purl.org/dc/elements/1.1/>
|
219
|
+
#
|
220
|
+
# CONSTRUCT {
|
221
|
+
# ?bob foaf:name "Bob" .
|
222
|
+
# <<?bob foaf:age ?age>> dct:creator <http://example.com/crawlers#c1>;
|
223
|
+
# dct:source ?src .
|
224
|
+
# }
|
225
|
+
# WHERE {
|
226
|
+
# ?bob foaf:name "Bob" .
|
227
|
+
# <<?bob foaf:age ?age>> dct:source ?src .
|
228
|
+
# }
|
229
|
+
#
|
230
|
+
# SXP:
|
231
|
+
#
|
232
|
+
# (prefix
|
233
|
+
# (
|
234
|
+
# (: <http://bigdata.com>)
|
235
|
+
# (foaf: <http://xmlns.com/foaf/0.1/>)
|
236
|
+
# (dct: <http://purl.org/dc/elements/1.1/>))
|
237
|
+
# (construct
|
238
|
+
# (
|
239
|
+
# (triple ?bob foaf:name "Bob")
|
240
|
+
# (triple (triple ?bob foaf:age ?age) dct:creator <http://example.com/crawlers#c1>)
|
241
|
+
# (triple (triple ?bob foaf:age ?age) dct:source ?src))
|
242
|
+
# (bgp
|
243
|
+
# (triple ?bob foaf:name "Bob")
|
244
|
+
# (triple (triple ?bob foaf:age ?age) dct:source ?src)) ))
|
245
|
+
|
246
|
+
|
147
247
|
# ## Implementation Notes
|
148
248
|
# The parser is driven through a rules table contained in lib/sparql/grammar/meta.rb. This includes branch rules to indicate productions to be taken based on a current production.
|
149
249
|
#
|
@@ -155,7 +255,7 @@ module SPARQL
|
|
155
255
|
# etc/sparql11.bnf
|
156
256
|
#
|
157
257
|
# @see http://www.w3.org/TR/sparql11-query/#grammar
|
158
|
-
# @see
|
258
|
+
# @see https://rubygems.org/gems/ebnf
|
159
259
|
module Grammar
|
160
260
|
autoload :Parser, 'sparql/grammar/parser11'
|
161
261
|
autoload :Terminals, 'sparql/grammar/terminals11'
|
@@ -175,8 +275,8 @@ module SPARQL
|
|
175
275
|
# @param [Hash{Symbol => Object}] options
|
176
276
|
# @return [Parser]
|
177
277
|
# @raise [Parser::Error] on invalid input
|
178
|
-
def self.parse(query, options
|
179
|
-
Parser.new(query, options).parse(options[:update] ? :UpdateUnit : :QueryUnit)
|
278
|
+
def self.parse(query, **options, &block)
|
279
|
+
Parser.new(query, **options).parse(options[:update] ? :UpdateUnit : :QueryUnit)
|
180
280
|
end
|
181
281
|
|
182
282
|
##
|
@@ -190,8 +290,8 @@ module SPARQL
|
|
190
290
|
# @yieldparam [RDF::Reader] reader
|
191
291
|
# @yieldreturn [void] ignored
|
192
292
|
# @raise [RDF::FormatError] if no reader found for the specified format
|
193
|
-
def self.open(filename, options
|
194
|
-
RDF::Util::File.open_file(filename, options) do |file|
|
293
|
+
def self.open(filename, **options, &block)
|
294
|
+
RDF::Util::File.open_file(filename, **options) do |file|
|
195
295
|
self.parse(file, options, &block)
|
196
296
|
end
|
197
297
|
end
|
@@ -206,8 +306,8 @@ module SPARQL
|
|
206
306
|
# @param [String, #to_s] query
|
207
307
|
# @param [Hash{Symbol => Object}] options
|
208
308
|
# @return [Boolean]
|
209
|
-
def self.valid?(query, options
|
210
|
-
Parser.new(query, options).valid?
|
309
|
+
def self.valid?(query, **options)
|
310
|
+
Parser.new(query, **options).valid?
|
211
311
|
end
|
212
312
|
|
213
313
|
##
|
@@ -225,7 +325,7 @@ module SPARQL
|
|
225
325
|
# @yieldparam [Lexer] lexer
|
226
326
|
# @return [Lexer]
|
227
327
|
# @raise [Lexer::Error] on invalid input
|
228
|
-
def self.tokenize(query, options
|
328
|
+
def self.tokenize(query, **options, &block)
|
229
329
|
Lexer.tokenize(query, options, &block)
|
230
330
|
end
|
231
331
|
end # Grammar
|