sparql 3.2.0 → 3.2.4

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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +59 -38
  3. data/VERSION +1 -1
  4. data/bin/sparql +2 -31
  5. data/lib/rack/sparql/conneg.rb +22 -1
  6. data/lib/sinatra/sparql/extensions.rb +1 -1
  7. data/lib/sinatra/sparql.rb +57 -12
  8. data/lib/sparql/algebra/expression.rb +63 -10
  9. data/lib/sparql/algebra/extensions.rb +39 -35
  10. data/lib/sparql/algebra/operator/abs.rb +1 -1
  11. data/lib/sparql/algebra/operator/adjust.rb +69 -0
  12. data/lib/sparql/algebra/operator/alt.rb +1 -1
  13. data/lib/sparql/algebra/operator/avg.rb +3 -1
  14. data/lib/sparql/algebra/operator/bgp.rb +9 -1
  15. data/lib/sparql/algebra/operator/clear.rb +13 -3
  16. data/lib/sparql/algebra/operator/construct.rb +1 -1
  17. data/lib/sparql/algebra/operator/count.rb +36 -6
  18. data/lib/sparql/algebra/operator/create.rb +5 -4
  19. data/lib/sparql/algebra/operator/dataset.rb +29 -11
  20. data/lib/sparql/algebra/operator/day.rb +2 -2
  21. data/lib/sparql/algebra/operator/delete.rb +3 -1
  22. data/lib/sparql/algebra/operator/delete_data.rb +1 -1
  23. data/lib/sparql/algebra/operator/delete_where.rb +1 -1
  24. data/lib/sparql/algebra/operator/distinct.rb +2 -2
  25. data/lib/sparql/algebra/operator/divide.rb +1 -1
  26. data/lib/sparql/algebra/operator/drop.rb +15 -6
  27. data/lib/sparql/algebra/operator/encode_for_uri.rb +2 -4
  28. data/lib/sparql/algebra/operator/exprlist.rb +3 -1
  29. data/lib/sparql/algebra/operator/extend.rb +73 -5
  30. data/lib/sparql/algebra/operator/filter.rb +6 -1
  31. data/lib/sparql/algebra/operator/function_call.rb +64 -0
  32. data/lib/sparql/algebra/operator/graph.rb +57 -7
  33. data/lib/sparql/algebra/operator/group.rb +105 -6
  34. data/lib/sparql/algebra/operator/group_concat.rb +25 -1
  35. data/lib/sparql/algebra/operator/hours.rb +2 -2
  36. data/lib/sparql/algebra/operator/if.rb +10 -10
  37. data/lib/sparql/algebra/operator/insert.rb +3 -1
  38. data/lib/sparql/algebra/operator/insert_data.rb +1 -1
  39. data/lib/sparql/algebra/operator/is_blank.rb +1 -2
  40. data/lib/sparql/algebra/operator/is_iri.rb +1 -2
  41. data/lib/sparql/algebra/operator/is_literal.rb +1 -2
  42. data/lib/sparql/algebra/operator/is_numeric.rb +1 -2
  43. data/lib/sparql/algebra/operator/join.rb +39 -5
  44. data/lib/sparql/algebra/operator/lcase.rb +2 -3
  45. data/lib/sparql/algebra/operator/left_join.rb +27 -9
  46. data/lib/sparql/algebra/operator/max.rb +3 -1
  47. data/lib/sparql/algebra/operator/min.rb +4 -2
  48. data/lib/sparql/algebra/operator/minus.rb +46 -6
  49. data/lib/sparql/algebra/operator/minutes.rb +2 -2
  50. data/lib/sparql/algebra/operator/modify.rb +21 -0
  51. data/lib/sparql/algebra/operator/month.rb +2 -2
  52. data/lib/sparql/algebra/operator/multiply.rb +1 -1
  53. data/lib/sparql/algebra/operator/notoneof.rb +12 -3
  54. data/lib/sparql/algebra/operator/order.rb +44 -0
  55. data/lib/sparql/algebra/operator/path_opt.rb +9 -65
  56. data/lib/sparql/algebra/operator/path_plus.rb +18 -10
  57. data/lib/sparql/algebra/operator/path_range.rb +178 -0
  58. data/lib/sparql/algebra/operator/path_star.rb +7 -4
  59. data/lib/sparql/algebra/operator/path_zero.rb +110 -0
  60. data/lib/sparql/algebra/operator/plus.rb +8 -6
  61. data/lib/sparql/algebra/operator/project.rb +64 -5
  62. data/lib/sparql/algebra/operator/reduced.rb +3 -3
  63. data/lib/sparql/algebra/operator/regex.rb +1 -1
  64. data/lib/sparql/algebra/operator/reverse.rb +12 -1
  65. data/lib/sparql/algebra/operator/sample.rb +3 -1
  66. data/lib/sparql/algebra/operator/seconds.rb +2 -2
  67. data/lib/sparql/algebra/operator/seq.rb +4 -4
  68. data/lib/sparql/algebra/operator/sequence.rb +14 -1
  69. data/lib/sparql/algebra/operator/service.rb +86 -0
  70. data/lib/sparql/algebra/operator/strlang.rb +1 -2
  71. data/lib/sparql/algebra/operator/subtract.rb +10 -6
  72. data/lib/sparql/algebra/operator/sum.rb +9 -7
  73. data/lib/sparql/algebra/operator/table.rb +50 -7
  74. data/lib/sparql/algebra/operator/timezone.rb +2 -2
  75. data/lib/sparql/algebra/operator/triple.rb +51 -0
  76. data/lib/sparql/algebra/operator/tz.rb +2 -2
  77. data/lib/sparql/algebra/operator/ucase.rb +1 -1
  78. data/lib/sparql/algebra/operator/update.rb +22 -1
  79. data/lib/sparql/algebra/operator/using.rb +18 -1
  80. data/lib/sparql/algebra/operator/with.rb +1 -1
  81. data/lib/sparql/algebra/operator/year.rb +2 -2
  82. data/lib/sparql/algebra/operator.rb +69 -22
  83. data/lib/sparql/algebra/query.rb +5 -3
  84. data/lib/sparql/algebra.rb +42 -6
  85. data/lib/sparql/grammar/meta.rb +1367 -267
  86. data/lib/sparql/grammar/parser11.rb +842 -331
  87. data/lib/sparql/grammar/terminals11.rb +2 -2
  88. data/lib/sparql/grammar.rb +6 -4
  89. data/lib/sparql/results.rb +3 -2
  90. data/lib/sparql/server.rb +93 -0
  91. data/lib/sparql.rb +8 -5
  92. metadata +49 -13
@@ -70,28 +70,12 @@ class Array
70
70
  # Returns a partial SPARQL grammar for this array.
71
71
  #
72
72
  # @param [String] delimiter (" ")
73
+ # If the first element is an IRI, treat it as an extension function
73
74
  # @return [String]
74
- def to_sparql(delimiter: " ", **options)
75
+ def to_sparql(delimiter: " ", **options)
75
76
  map {|e| e.to_sparql(**options)}.join(delimiter)
76
77
  end
77
78
 
78
- ##
79
- # Evaluates the array using the given variable `bindings`.
80
- #
81
- # In this case, the Array has two elements, the first of which is
82
- # an XSD datatype, and the second is the expression to be evaluated.
83
- # The result is cast as a literal of the appropriate type
84
- #
85
- # @param [RDF::Query::Solution] bindings
86
- # a query solution containing zero or more variable bindings
87
- # @param [Hash{Symbol => Object}] options ({})
88
- # options passed from query
89
- # @return [RDF::Term]
90
- # @see SPARQL::Algebra::Expression.evaluate
91
- def evaluate(bindings, **options)
92
- SPARQL::Algebra::Expression.extension(*self.map {|o| o.evaluate(bindings, **options)})
93
- end
94
-
95
79
  ##
96
80
  # If `#execute` is invoked, it implies that a non-implemented Algebra operator
97
81
  # is being invoked
@@ -140,6 +124,14 @@ class Array
140
124
  end
141
125
  def constant?; !(variable?); end
142
126
 
127
+ ##
128
+ # The variables used in this array.
129
+ #
130
+ # @return [Hash{Symbol => RDF::Query::Variable}]
131
+ def variables
132
+ self.inject({}) {|hash, o| o.respond_to?(:variables) ? hash.merge(o.variables) : hash}
133
+ end
134
+
143
135
  ##
144
136
  # Does this contain any nodes?
145
137
  #
@@ -302,7 +294,6 @@ module RDF::Term
302
294
  end
303
295
  end # RDF::Term
304
296
 
305
-
306
297
  # Override RDF::Queryable to execute against SPARQL::Algebra::Query elements as well as RDF::Query and RDF::Pattern
307
298
  module RDF::Queryable
308
299
  alias_method :query_without_sparql, :query
@@ -364,7 +355,7 @@ class RDF::Statement
364
355
  # Transform Statement Pattern into an SXP
365
356
  # @return [Array]
366
357
  def to_sxp_bin
367
- [ (has_graph? ? :quad : :triple),
358
+ [ (has_graph? ? :quad : (quoted? ? :qtriple : :triple)),
368
359
  (:inferred if inferred?),
369
360
  subject,
370
361
  predicate,
@@ -387,18 +378,10 @@ class RDF::Statement
387
378
  #
388
379
  # Returns a partial SPARQL grammar for this term.
389
380
  #
390
- # @param [Boolean] as_statement (false) serialize as < ... >, otherwise TRIPLE(...)
391
381
  # @return [String]
392
- def to_sparql(as_statement: false, **options)
393
- return "TRIPLE(#{to_triple.to_sparql(as_statement: true, **options)})" unless as_statement
394
-
395
- to_triple.map do |term|
396
- if term.is_a?(::RDF::Statement)
397
- "<<" + term.to_sparql(**options) + ">>"
398
- else
399
- term.to_sparql(**options)
400
- end
401
- end.join(" ") + " ."
382
+ def to_sparql(**options)
383
+ str = to_triple.map {|term| term.to_sparql(**options)}.join(" ")
384
+ quoted? ? ('<<' + str + '>>') : str
402
385
  end
403
386
 
404
387
  ##
@@ -448,15 +431,36 @@ class RDF::Query
448
431
 
449
432
  ##
450
433
  #
451
- # Returns a partial SPARQL grammar for this term.
434
+ # Returns a partial SPARQL grammar for this query.
452
435
  #
453
436
  # @param [Boolean] top_level (true)
454
437
  # Treat this as a top-level, generating SELECT ... WHERE {}
438
+ # @param [Array<Operator>] filter_ops ([])
439
+ # Filter Operations
455
440
  # @return [String]
456
- def to_sparql(top_level: true, **options)
457
- str = @patterns.map { |e| e.to_sparql(as_statement: true, top_level: false, **options) }.join("\n")
441
+ def to_sparql(top_level: true, filter_ops: [], **options)
442
+ str = @patterns.map do |e|
443
+ e.to_sparql(top_level: false, **options) + " . \n"
444
+ end.join("")
458
445
  str = "GRAPH #{graph_name.to_sparql(**options)} {\n#{str}\n}\n" if graph_name
459
- top_level ? SPARQL::Algebra::Operator.to_sparql(str, **options) : str
446
+ if top_level
447
+ SPARQL::Algebra::Operator.to_sparql(str, filter_ops: filter_ops, **options)
448
+ else
449
+ # Filters
450
+ filter_ops.each do |op|
451
+ str << "\nFILTER (#{op.to_sparql(**options)}) ."
452
+ end
453
+
454
+ # Extensons
455
+ extensions = options.fetch(:extensions, [])
456
+ extensions.each do |as, expression|
457
+ v = expression.to_sparql(**options)
458
+ pp = RDF::Query::Variable.new(as).to_sparql(**options)
459
+ str << "\nBIND (" << v << " AS " << pp << ") ."
460
+ end
461
+ str = "{#{str}}" unless filter_ops.empty? && extensions.empty?
462
+ str
463
+ end
460
464
  end
461
465
 
462
466
  ##
@@ -5,7 +5,7 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # [121] BuiltInCall ::= ... | 'ABS' '(' Expression ')'
7
7
  #
8
- # @example SPARQL Query
8
+ # @example SPARQL Grammar
9
9
  # PREFIX : <http://example.org/>
10
10
  # SELECT * WHERE {
11
11
  # ?s :num ?num
@@ -0,0 +1,69 @@
1
+ module SPARQL; module Algebra
2
+ class Operator
3
+ ##
4
+ # The SPARQL `adjust` operator.
5
+ #
6
+ # [121] BuiltInCall ::= ... | 'ADJUST' '(' Expression ',' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
10
+ # SELECT ?id (ADJUST(?d, ?tz) AS ?adjusted) WHERE {
11
+ # VALUES (?id ?tz ?d) {
12
+ # (1 "-PT10H"^^xsd:dayTimeDuration "2002-03-07"^^xsd:date)
13
+ # }
14
+ # }
15
+ #
16
+ # @example SSE
17
+ # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>))
18
+ # (project (?id ?adjusted)
19
+ # (extend ((?adjusted (adjust ?d ?tz)))
20
+ # (table (vars ?id ?tz ?d)
21
+ # (row
22
+ # (?id 1)
23
+ # (?tz "-PT10H"^^xsd:dayTimeDuration)
24
+ # (?d "2002-03-07"^^xsd:date))))))
25
+ #
26
+ # @see https://www.w3.org/TR/sparql11-query/#func-abs
27
+ # @see https://www.w3.org/TR/xpath-functions/#func-abs
28
+ class Adjust < Operator::Binary
29
+ include Evaluatable
30
+
31
+ NAME = [:adjust]
32
+
33
+ ##
34
+ # Returns the first operand adjusted by the dayTimeDuration of the second operand
35
+ #
36
+ # @param [RDF::Literal::Temporal] operand
37
+ # the operand
38
+ # @param [RDF::Literal, String] duration
39
+ # the dayTimeDuration or an empty string.
40
+ # @return [RDF::Literal] literal of same type
41
+ # @raise [TypeError] if the operand is not a numeric value
42
+ def apply(operand, duration, **options)
43
+ case operand
44
+ when RDF::Literal::Temporal
45
+ case duration
46
+ when RDF::Literal::DayTimeDuration
47
+ operand.adjust_to_timezone(duration)
48
+ when RDF::Literal
49
+ raise TypeError, "expected second operand to be an empty literal, but got #{duration.inspect}" unless duration.to_s.empty?
50
+ operand.adjust_to_timezone(nil)
51
+ else
52
+ raise TypeError, "expected second operand to be an RDF::Literal::DayTimeDuration, but got #{duration.inspect}"
53
+ end
54
+ else
55
+ raise TypeError, "expected first operand to be an RDF::Literal::Temporal, but got #{operand.inspect}"
56
+ end
57
+ end
58
+
59
+ ##
60
+ #
61
+ # Returns a partial SPARQL grammar for this operator.
62
+ #
63
+ # @return [String]
64
+ def to_sparql(**options)
65
+ "ADJUST(#{operands.to_sparql(delimiter: ', ', **options)})"
66
+ end
67
+ end # Abs
68
+ end # Operator
69
+ end; end # SPARQL::Algebra
@@ -79,7 +79,7 @@ module SPARQL; module Algebra
79
79
  #
80
80
  # @return [String]
81
81
  def to_sparql(**options)
82
- "#{operands.first.to_sparql(**options)}|#{operands.last.to_sparql(**options)}"
82
+ "(#{operands.first.to_sparql(**options)}|#{operands.last.to_sparql(**options)})"
83
83
  end
84
84
  end # Alt
85
85
  end # Operator
@@ -54,7 +54,9 @@ module SPARQL; module Algebra
54
54
  #
55
55
  # @return [String]
56
56
  def to_sparql(**options)
57
- "AVG(#{operands.to_sparql(**options)})"
57
+ distinct = operands.first == :distinct
58
+ args = distinct ? operands[1..-1] : operands
59
+ "AVG(#{'DISTINCT ' if distinct}#{args.to_sparql(**options)})"
58
60
  end
59
61
  end # Avg
60
62
  end # Operator
@@ -7,12 +7,20 @@ module SPARQL; module Algebra
7
7
  #
8
8
  # @example SPARQL Grammar
9
9
  # PREFIX : <http://example/>
10
- # SELECT * { :s :p :o }
10
+ # SELECT * { ?s ?p ?o }
11
11
  #
12
12
  # @example SSE
13
13
  # (prefix ((: <http://example/>))
14
14
  # (bgp (triple ?s ?p ?o)))
15
15
  #
16
+ # @example SPARQL Grammar (sparql-star)
17
+ # PREFIX : <http://example.com/ns#>
18
+ # SELECT * {<< :a :b :c >> :p1 :o1.}
19
+ #
20
+ # @example SSE (sparql-star)
21
+ # (prefix ((: <http://example.com/ns#>))
22
+ # (bgp (triple (qtriple :a :b :c) :p1 :o1)))
23
+ #
16
24
  # @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
17
25
  class BGP < Operator
18
26
  NAME = [:bgp]
@@ -8,12 +8,18 @@ module SPARQL; module Algebra
8
8
  #
9
9
  # [32] Clear ::= 'CLEAR' 'SILENT'? GraphRefAll
10
10
  #
11
- # @example SPARQL Grammar
11
+ # @example SPARQL Grammar (SILENT DEFAULT)
12
12
  # CLEAR SILENT DEFAULT
13
13
  #
14
- # @example SSE
14
+ # @example SSE (SILENT DEFAULT)
15
15
  # (update (clear silent default))
16
16
  #
17
+ # @example SPARQL Grammar (IRI)
18
+ # CLEAR GRAPH <http://example.com/>
19
+ #
20
+ # @example SSE (IRI)
21
+ # (update (clear <http://example.com/>))
22
+ #
17
23
  # @see https://www.w3.org/TR/sparql11-update/#clear
18
24
  class Clear < Operator
19
25
  include SPARQL::Algebra::Update
@@ -70,7 +76,11 @@ module SPARQL; module Algebra
70
76
  #
71
77
  # @return [String]
72
78
  def to_sparql(**options)
73
- "CLEAR " + operands.to_sparql(**options)
79
+ silent = operands.first == :silent
80
+ str = "CLEAR "
81
+ str << "SILENT " if operands.first == :silent
82
+ str << "GRAPH " if operands.last.is_a?(RDF::URI)
83
+ str << operands.last.to_sparql(**options)
74
84
  end
75
85
  end # Clear
76
86
  end # Operator
@@ -99,7 +99,7 @@ module SPARQL; module Algebra
99
99
  # @return [String]
100
100
  def to_sparql(**options)
101
101
  str = "CONSTRUCT {\n" +
102
- operands[0].map { |e| e.to_sparql(as_statement: true, top_level: false, **options) }.join("\n") +
102
+ operands[0].map { |e| e.to_sparql(top_level: false, **options) }.join(". \n") +
103
103
  "\n}\n"
104
104
 
105
105
  str << operands[1].to_sparql(top_level: true, project: nil, **options)
@@ -11,11 +11,39 @@ module SPARQL; module Algebra
11
11
  # WHERE { ?S ?P ?O }
12
12
  #
13
13
  # @example SSE
14
- # (prefix ((: <http://www.example.org>))
15
- # (project (?C)
16
- # (extend ((?C ??.0))
17
- # (group () ((??.0 (count ?O)))
18
- # (bgp (triple ?S ?P ?O))))))
14
+ # (prefix ((: <http://www.example.org/>))
15
+ # (project (?C)
16
+ # (extend ((?C ??.0))
17
+ # (group () ((??.0 (count ?O)))
18
+ # (bgp (triple ?S ?P ?O))))))
19
+ #
20
+ # @example SPARQL Grammar (count(*))
21
+ # PREFIX : <http://www.example.org>
22
+ #
23
+ # SELECT (COUNT(*) AS ?C)
24
+ # WHERE { ?S ?P ?O }
25
+ #
26
+ # @example SSE (count(*))
27
+ # (prefix
28
+ # ((: <http://www.example.org>))
29
+ # (project (?C)
30
+ # (extend ((?C ??.0))
31
+ # (group () ((??.0 (count)))
32
+ # (bgp (triple ?S ?P ?O))))))
33
+ #
34
+ # @example SPARQL Grammar (count(distinct *))
35
+ # PREFIX : <http://www.example.org>
36
+ #
37
+ # SELECT (COUNT(DISTINCT *) AS ?C)
38
+ # WHERE { ?S ?P ?O }
39
+ #
40
+ # @example SSE (count(distinct *))
41
+ # (prefix
42
+ # ((: <http://www.example.org>))
43
+ # (project (?C)
44
+ # (extend ((?C ??.0))
45
+ # (group () ((??.0 (count distinct)))
46
+ # (bgp (triple ?S ?P ?O))))))
19
47
  #
20
48
  # @see https://www.w3.org/TR/sparql11-query/#defn_aggCount
21
49
  class Count < Operator
@@ -39,7 +67,9 @@ module SPARQL; module Algebra
39
67
  #
40
68
  # @return [String]
41
69
  def to_sparql(**options)
42
- "COUNT(#{operands.to_sparql(**options)})"
70
+ distinct = operands.first == :distinct
71
+ args = distinct ? operands[1..-1] : operands
72
+ "COUNT(#{'DISTINCT ' if distinct}#{args.empty? ? '*' : args.to_sparql(**options)})"
43
73
  end
44
74
  end # Count
45
75
  end # Operator
@@ -55,10 +55,11 @@ module SPARQL; module Algebra
55
55
  #
56
56
  # @return [String]
57
57
  def to_sparql(**options)
58
- *args, last = operands.dup
59
- args += [:GRAPH, last]
60
-
61
- "CREATE " + args.to_sparql(**options)
58
+ silent = operands.first == :silent
59
+ str = "CREATE "
60
+ str << "SILENT " if operands.first == :silent
61
+ str << "GRAPH " if operands.last.is_a?(RDF::URI)
62
+ str << operands.last.to_sparql(**options)
62
63
  end
63
64
  end # Create
64
65
  end # Operator
@@ -73,7 +73,7 @@ module SPARQL; module Algebra
73
73
  # @example Dataset with two default data sources
74
74
  #
75
75
  # (prefix ((: <http://example/>))
76
- # (dataset (<data-g1.ttl> <data-g1.ttl)
76
+ # (dataset (<data-g1.ttl> <data-g2.ttl)
77
77
  # (bgp (triple ?s ?p ?o))))
78
78
  #
79
79
  # is effectively re-written to the following:
@@ -101,7 +101,21 @@ module SPARQL; module Algebra
101
101
  # (filter ((= ?g <data-g1.ttl>) || (= ?g <data-g2.ttl>))
102
102
  # (graph ?g (bgp (triple ?s ?p ?o))))))
103
103
  #
104
- # @example Dataset with multiple named graphs
104
+ #
105
+ # @example SPARQL Grammar
106
+ # BASE <http://example.org/>
107
+ # PREFIX : <http://example.com/>
108
+ #
109
+ # SELECT *
110
+ # FROM <data-g1.ttl>
111
+ # { ?s ?p ?o }
112
+ #
113
+ # @example SSE
114
+ # (base <http://example.org/>
115
+ # (prefix ((: <http://example.com/>))
116
+ # (dataset (<data-g1.ttl>)
117
+ # (bgp (triple ?s ?p ?o)))))
118
+ #
105
119
  # @see https://www.w3.org/TR/sparql11-query/#specifyingDataset
106
120
  class Dataset < Binary
107
121
  include Query
@@ -115,6 +129,11 @@ module SPARQL; module Algebra
115
129
  #
116
130
  # Datasets are specified in operand(1), which is an array of default or named graph URIs.
117
131
  #
132
+ # If `options` contains any of the Protocol attributes, the dataset is constructed on creation, and these operations should be ignored:
133
+ #
134
+ # * `default-graph-uri`
135
+ # * `named-graph-uri`
136
+ #
118
137
  # @param [RDF::Queryable] queryable
119
138
  # the graph or repository to query
120
139
  # @param [Hash{Symbol => Object}] options
@@ -128,6 +147,11 @@ module SPARQL; module Algebra
128
147
  # @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
129
148
  def execute(queryable, **options, &base)
130
149
  debug(options) {"Dataset"}
150
+ if %i(default-graph-uri named-graph-uri).any? {|k| options.key?(k)}
151
+ debug("=> Skip constructing merge repo due to options", options)
152
+ return queryable.query(operands.last, depth: options[:depth].to_i + 1, **options, &base)
153
+ end
154
+
131
155
  default_datasets = []
132
156
  named_datasets = []
133
157
  operand(0).each do |uri|
@@ -163,17 +187,11 @@ module SPARQL; module Algebra
163
187
  #
164
188
  # Returns a partial SPARQL grammar for this operator.
165
189
  #
190
+ # Extracts datasets
191
+ #
166
192
  # @return [String]
167
193
  def to_sparql(**options)
168
- operands[0].each_with_object('') do |graph, str|
169
- str << if graph.is_a?(Array)
170
- "FROM #{graph[0].upcase} #{graph[1].to_sparql(**options)}\n"
171
- else
172
- "FROM #{graph.to_sparql(**options)}\n"
173
- end
174
- end.tap do |str|
175
- str << operands[1].to_sparql(**options)
176
- end
194
+ operands.last.to_sparql(datasets: operands.first, **options)
177
195
  end
178
196
  end # Dataset
179
197
  end # Operator
@@ -29,10 +29,10 @@ module SPARQL; module Algebra
29
29
  #
30
30
  # @param [RDF::Literal] operand
31
31
  # the operand
32
- # @return [RDF::Literal]
32
+ # @return [RDF::Literal::Temporal]
33
33
  # @raise [TypeError] if the operand is not a simple literal
34
34
  def apply(operand, **options)
35
- raise TypeError, "expected an RDF::Literal::DateTime, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::DateTime)
35
+ raise TypeError, "expected an RDF::Literal::Temporal, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::Temporal)
36
36
  RDF::Literal(operand.object.day)
37
37
  end
38
38
 
@@ -75,7 +75,9 @@ module SPARQL; module Algebra
75
75
  #
76
76
  # @return [String]
77
77
  def to_sparql(**options)
78
- "DELETE {\n" + operands.first.to_sparql(as_statement: true, **options) + "\n}"
78
+ "DELETE {\n" +
79
+ operands.first.to_sparql(delimiter: " .\n", **options) +
80
+ "\n}"
79
81
  end
80
82
  end # Delete
81
83
  end # Operator
@@ -54,7 +54,7 @@ module SPARQL; module Algebra
54
54
  # @return [String]
55
55
  def to_sparql(**options)
56
56
  "DELETE DATA {\n" +
57
- operands.first.to_sparql(as_statement: true, top_level: false, delimiter: "\n", **options) +
57
+ operands.first.to_sparql(top_level: false, delimiter: ". \n", **options) +
58
58
  "\n}"
59
59
  end
60
60
  end # DeleteData
@@ -71,7 +71,7 @@ module SPARQL; module Algebra
71
71
  # @return [String]
72
72
  def to_sparql(**options)
73
73
  "DELETE WHERE {\n" +
74
- operands.first.to_sparql(as_statement: true, top_level: false, delimiter: "\n", **options) +
74
+ operands.first.to_sparql(top_level: false, delimiter: ". \n", **options) +
75
75
  "\n}"
76
76
  end
77
77
  end # DeleteWhere
@@ -12,8 +12,8 @@ module SPARQL; module Algebra
12
12
  # WHERE { ?x ?p ?v }
13
13
  #
14
14
  # @example SSE
15
- # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>)
16
- # (: <http://example/>))
15
+ # (prefix ((: <http://example.org/>)
16
+ # (xsd: <http://www.w3.org/2001/XMLSchema#>))
17
17
  # (distinct
18
18
  # (project (?v)
19
19
  # (bgp (triple ?x ?p ?v)))))
@@ -60,7 +60,7 @@ module SPARQL; module Algebra
60
60
  #
61
61
  # @return [String]
62
62
  def to_sparql(**options)
63
- "#{operands.first.to_sparql(**options)} / #{operands.last.to_sparql(**options)}"
63
+ "(#{operands.first.to_sparql(**options)} / #{operands.last.to_sparql(**options)})"
64
64
  end
65
65
  end # Divide
66
66
  end # Operator
@@ -10,12 +10,18 @@ module SPARQL; module Algebra
10
10
  #
11
11
  # [33] Drop ::= 'DROP' 'SILENT'? GraphRefAll
12
12
  #
13
- # @example SPARQL Grammar
14
- # DROP DEFAULT
13
+ # @example SPARQL Grammar (SILENT DEFAULT)
14
+ # DROP SILENT DEFAULT
15
15
  #
16
- # @example SSE
16
+ # @example SSE (SILENT DEFAULT)
17
17
  # (update
18
- # (drop default))
18
+ # (drop silent default))
19
+ #
20
+ # @example SPARQL Grammar (IRI)
21
+ # DROP GRAPH <http://example.com/>
22
+ #
23
+ # @example SSE (IRI)
24
+ # (update (drop <http://example.com/>))
19
25
  #
20
26
  # @see https://www.w3.org/TR/sparql11-update/#drop
21
27
  class Drop < Operator
@@ -40,7 +46,6 @@ module SPARQL; module Algebra
40
46
  def execute(queryable, **options)
41
47
  debug(options) {"Drop"}
42
48
  silent = operands.first == :silent
43
- silent = operands.first == :silent
44
49
  operands.shift if silent
45
50
 
46
51
  raise ArgumentError, "drop expected operand to be 'default', 'named', 'all', or an IRI" unless operands.length == 1
@@ -74,7 +79,11 @@ module SPARQL; module Algebra
74
79
  #
75
80
  # @return [String]
76
81
  def to_sparql(**options)
77
- "DROP " + operands.to_sparql(**options)
82
+ silent = operands.first == :silent
83
+ str = "DROP "
84
+ str << "SILENT " if operands.first == :silent
85
+ str << "GRAPH " if operands.last.is_a?(RDF::URI)
86
+ str << operands.last.to_sparql(**options)
78
87
  end
79
88
  end # Drop
80
89
  end # Operator
@@ -9,15 +9,13 @@ module SPARQL; module Algebra
9
9
  #
10
10
  # @example SPARQL Grammar
11
11
  # PREFIX : <http://example.org/>
12
- # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
13
12
  # SELECT ?s ?str (ENCODE_FOR_URI(?str) AS ?encoded) WHERE {
14
13
  # ?s :str ?str
15
14
  # }
16
15
  #
17
16
  # @example SSE
18
- # (prefix
19
- # ((: <http://example.org/>))
20
- # (project (?str ?encoded)
17
+ # (prefix ((: <http://example.org/>))
18
+ # (project (?s ?str ?encoded)
21
19
  # (extend ((?encoded (encode_for_uri ?str)))
22
20
  # (bgp (triple ?s :str ?str)))))
23
21
  #
@@ -8,6 +8,8 @@ module SPARQL; module Algebra
8
8
  # [72] ExpressionList ::= NIL | '(' Expression ( ',' Expression )* ')'
9
9
  #
10
10
  # @example SPARQL Grammar
11
+ # PREFIX : <http://example.org/>
12
+ #
11
13
  # SELECT ?v ?w
12
14
  # {
13
15
  # FILTER (?v = 2)
@@ -17,7 +19,7 @@ module SPARQL; module Algebra
17
19
  # }
18
20
  #
19
21
  # @example SSE
20
- # (prefix ((: <http://example/>))
22
+ # (prefix ((: <http://example.org/>))
21
23
  # (project (?v ?w)
22
24
  # (filter (exprlist (= ?v 2) (= ?w 3))
23
25
  # (bgp