sparql 3.2.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/VERSION +1 -1
  4. data/lib/sparql/algebra/expression.rb +28 -3
  5. data/lib/sparql/algebra/extensions.rb +36 -32
  6. data/lib/sparql/algebra/operator/abs.rb +1 -1
  7. data/lib/sparql/algebra/operator/alt.rb +1 -1
  8. data/lib/sparql/algebra/operator/avg.rb +3 -1
  9. data/lib/sparql/algebra/operator/bgp.rb +9 -1
  10. data/lib/sparql/algebra/operator/clear.rb +13 -3
  11. data/lib/sparql/algebra/operator/construct.rb +1 -1
  12. data/lib/sparql/algebra/operator/count.rb +36 -6
  13. data/lib/sparql/algebra/operator/create.rb +5 -4
  14. data/lib/sparql/algebra/operator/dataset.rb +19 -11
  15. data/lib/sparql/algebra/operator/delete.rb +3 -1
  16. data/lib/sparql/algebra/operator/delete_data.rb +1 -1
  17. data/lib/sparql/algebra/operator/delete_where.rb +1 -1
  18. data/lib/sparql/algebra/operator/distinct.rb +2 -2
  19. data/lib/sparql/algebra/operator/divide.rb +1 -1
  20. data/lib/sparql/algebra/operator/drop.rb +15 -6
  21. data/lib/sparql/algebra/operator/encode_for_uri.rb +2 -4
  22. data/lib/sparql/algebra/operator/exprlist.rb +3 -1
  23. data/lib/sparql/algebra/operator/extend.rb +36 -3
  24. data/lib/sparql/algebra/operator/filter.rb +1 -1
  25. data/lib/sparql/algebra/operator/function_call.rb +64 -0
  26. data/lib/sparql/algebra/operator/graph.rb +57 -7
  27. data/lib/sparql/algebra/operator/group.rb +76 -5
  28. data/lib/sparql/algebra/operator/group_concat.rb +25 -1
  29. data/lib/sparql/algebra/operator/if.rb +10 -10
  30. data/lib/sparql/algebra/operator/insert.rb +3 -1
  31. data/lib/sparql/algebra/operator/insert_data.rb +1 -1
  32. data/lib/sparql/algebra/operator/is_blank.rb +1 -2
  33. data/lib/sparql/algebra/operator/is_iri.rb +1 -2
  34. data/lib/sparql/algebra/operator/is_literal.rb +1 -2
  35. data/lib/sparql/algebra/operator/is_numeric.rb +1 -2
  36. data/lib/sparql/algebra/operator/join.rb +37 -3
  37. data/lib/sparql/algebra/operator/lcase.rb +2 -3
  38. data/lib/sparql/algebra/operator/left_join.rb +20 -7
  39. data/lib/sparql/algebra/operator/max.rb +3 -1
  40. data/lib/sparql/algebra/operator/min.rb +4 -2
  41. data/lib/sparql/algebra/operator/minus.rb +46 -6
  42. data/lib/sparql/algebra/operator/multiply.rb +1 -1
  43. data/lib/sparql/algebra/operator/notoneof.rb +12 -3
  44. data/lib/sparql/algebra/operator/order.rb +44 -0
  45. data/lib/sparql/algebra/operator/plus.rb +1 -1
  46. data/lib/sparql/algebra/operator/project.rb +22 -4
  47. data/lib/sparql/algebra/operator/reduced.rb +3 -3
  48. data/lib/sparql/algebra/operator/regex.rb +1 -1
  49. data/lib/sparql/algebra/operator/reverse.rb +12 -1
  50. data/lib/sparql/algebra/operator/sample.rb +3 -1
  51. data/lib/sparql/algebra/operator/seq.rb +1 -1
  52. data/lib/sparql/algebra/operator/sequence.rb +4 -1
  53. data/lib/sparql/algebra/operator/strlang.rb +1 -2
  54. data/lib/sparql/algebra/operator/subtract.rb +1 -1
  55. data/lib/sparql/algebra/operator/sum.rb +9 -7
  56. data/lib/sparql/algebra/operator/table.rb +41 -7
  57. data/lib/sparql/algebra/operator/ucase.rb +1 -1
  58. data/lib/sparql/algebra/operator/update.rb +22 -1
  59. data/lib/sparql/algebra/operator/using.rb +18 -1
  60. data/lib/sparql/algebra/operator/with.rb +1 -1
  61. data/lib/sparql/algebra/operator.rb +46 -18
  62. data/lib/sparql/algebra.rb +20 -3
  63. data/lib/sparql/grammar/parser11.rb +3 -3
  64. metadata +15 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecbe9289bf43f7fa28940a82545ec08b19c54d8bdf64693934405e83ddb4cc53
4
- data.tar.gz: 24ab0942be18a9d9d281452ba9a7195dcad2a31755bda6d84afe4f49fe7c34c1
3
+ metadata.gz: e9bcab10eb6706316b68b44a0fd917d7b735b18a8353b51e49ef124fc966176d
4
+ data.tar.gz: 6da59b4e90fd1b7a7d80c42d941f7c27f6bf95892d17302caf16a13809b03afa
5
5
  SHA512:
6
- metadata.gz: 89a77c749eb99b0fe83f27d5f413556ac2f6cf22af7e06b8e0588155bbf5a50eb053b1f87ffe11a13b5fd873d5fcaaee1166cdf336aacd3029380898a82da382
7
- data.tar.gz: 13be95c405b9fdf7e678bc9bbaf6dcc45075a692a08e1762deacc0779948996fdc992376f3a564644f50270282c76a759023d7393e28db07ec7828a73324a948
6
+ metadata.gz: d2f99aee7d5b9482bbd653333eeefd215da872291993a2ba138ed9bcfc3f92812672cd1508dfbd11a729d527c9724f0a80ec816c5c84fd2df9933b4b91b5a296
7
+ data.tar.gz: 41a11caf8e85ca4bd01213bb0c0c236314560338dbd2825bd2437afbf4fd9855b994150737dbcad00838dc5ac50e368d72e5bfe2e516566a6319f824bdcacc56
data/README.md CHANGED
@@ -281,6 +281,8 @@ a full set of RDF formats.
281
281
 
282
282
  ### Parsing a SSE to SPARQL query or update string to SPARQL
283
283
 
284
+ # Note: if the SSE uses extension functions, they either must be XSD casting functions, or custom functions which are registered extensions. (See [SPARQL Extension Functions](#sparql-extension-functions))
285
+
284
286
  query = SPARQL::Algebra.parse(%{(bgp (triple ?s ?p ?o))})
285
287
  sparql = query.to_sparql #=> "SELECT * WHERE { ?s ?p ?o }"
286
288
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.2.0
1
+ 3.2.1
@@ -66,9 +66,11 @@ module SPARQL; module Algebra
66
66
  #
67
67
  # @param [Array] sse
68
68
  # a SPARQL S-Expression (SSE) form
69
+ # @param [Hash{Symbol => Object}] options
70
+ # any additional options (see {Operator#initialize})
69
71
  # @return [Expression]
70
- def self.for(*sse)
71
- self.new(sse)
72
+ def self.for(*sse, **options)
73
+ self.new(sse, **options)
72
74
  end
73
75
  class << self; alias_method :[], :for; end
74
76
 
@@ -86,6 +88,13 @@ module SPARQL; module Algebra
86
88
  raise ArgumentError, "invalid SPARQL::Algebra::Expression form: #{sse.inspect}" unless sse.is_a?(Array)
87
89
 
88
90
  operator = Operator.for(sse.first, sse.length - 1)
91
+
92
+ # If we don't find an operator, and sse.first is an extension IRI, use a function call
93
+ if !operator && sse.first.is_a?(RDF::URI) && self.extension?(sse.first)
94
+ operator = Operator.for(:function_call, sse.length)
95
+ sse.unshift(:function_call)
96
+ end
97
+
89
98
  unless operator
90
99
  return case sse.first
91
100
  when Array
@@ -115,11 +124,16 @@ module SPARQL; module Algebra
115
124
  end
116
125
 
117
126
  debug(options) {"#{operator.inspect}(#{operands.map(&:inspect).join(',')})"}
127
+ logger = options[:logger]
118
128
  options.delete_if {|k, v| [:debug, :logger, :depth, :prefixes, :base_uri, :update, :validate].include?(k) }
119
129
  begin
120
130
  operator.new(*operands, **options)
121
131
  rescue ArgumentError => e
122
- error(options) {"Operator=#{operator.inspect}: #{e}"}
132
+ if logger
133
+ logger.error("Operator=#{operator.inspect}: #{e}")
134
+ else
135
+ raise "Operator=#{operator.inspect}: #{e}"
136
+ end
123
137
  end
124
138
  end
125
139
 
@@ -163,6 +177,17 @@ module SPARQL; module Algebra
163
177
  @extensions ||= {}
164
178
  end
165
179
 
180
+ ##
181
+ # Is an extension function available?
182
+ #
183
+ # It's either a registered extension, or an XSD casting function
184
+ #
185
+ # @param [RDF::URI] function
186
+ # @return [Boolean]
187
+ def self.extension?(function)
188
+ function.to_s.start_with?(RDF::XSD.to_s) || self.extensions[function]
189
+ end
190
+
166
191
  ##
167
192
  # Invoke an extension function.
168
193
  #
@@ -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
@@ -302,7 +286,6 @@ module RDF::Term
302
286
  end
303
287
  end # RDF::Term
304
288
 
305
-
306
289
  # Override RDF::Queryable to execute against SPARQL::Algebra::Query elements as well as RDF::Query and RDF::Pattern
307
290
  module RDF::Queryable
308
291
  alias_method :query_without_sparql, :query
@@ -390,15 +373,17 @@ class RDF::Statement
390
373
  # @param [Boolean] as_statement (false) serialize as < ... >, otherwise TRIPLE(...)
391
374
  # @return [String]
392
375
  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(" ") + " ."
376
+ if as_statement
377
+ to_triple.map do |term|
378
+ if term.is_a?(::RDF::Statement)
379
+ "<<" + term.to_sparql(as_statement: true, **options) + ">>"
380
+ else
381
+ term.to_sparql(**options)
382
+ end
383
+ end.join(" ")
384
+ else
385
+ "TRIPLE(#{to_triple.to_sparql(as_statement: true, **options)})"
386
+ end
402
387
  end
403
388
 
404
389
  ##
@@ -448,15 +433,34 @@ class RDF::Query
448
433
 
449
434
  ##
450
435
  #
451
- # Returns a partial SPARQL grammar for this term.
436
+ # Returns a partial SPARQL grammar for this query.
452
437
  #
453
438
  # @param [Boolean] top_level (true)
454
439
  # Treat this as a top-level, generating SELECT ... WHERE {}
440
+ # @param [Array<Operator>] filter_ops ([])
441
+ # Filter Operations
455
442
  # @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")
443
+ def to_sparql(top_level: true, filter_ops: [], **options)
444
+ str = @patterns.map { |e| e.to_sparql(as_statement: true, top_level: false, **options) }.join(". \n")
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(as_statement: true, **options)
458
+ v = "<< #{v} >>" if expression.is_a?(RDF::Statement)
459
+ str << "\nBIND (" << v << " AS " << as.to_sparql(**options) << ") ."
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
@@ -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 (triple :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(as_statement: true, 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
@@ -163,17 +177,11 @@ module SPARQL; module Algebra
163
177
  #
164
178
  # Returns a partial SPARQL grammar for this operator.
165
179
  #
180
+ # Extracts datasets
181
+ #
166
182
  # @return [String]
167
183
  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
184
+ operands.last.to_sparql(datasets: operands.first, **options)
177
185
  end
178
186
  end # Dataset
179
187
  end # Operator
@@ -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(as_statement: true, 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(as_statement: true, 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(as_statement: true, 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
@@ -10,14 +10,47 @@ module SPARQL; module Algebra
10
10
  # @example SPARQL Grammar
11
11
  # SELECT ?z
12
12
  # {
13
- # ?x <http://example/p> ?o
14
- # BIND(?o+1 AS ?z)
13
+ # ?x <http://example.org/p> ?o
14
+ # BIND(?o+10 AS ?z)
15
15
  # }
16
16
  #
17
17
  # @example SSE
18
18
  # (project (?z)
19
19
  # (extend ((?z (+ ?o 10)))
20
- # (bgp (triple ?s <http://example/p> ?o))))
20
+ # (bgp (triple ?x <http://example.org/p> ?o))))
21
+ #
22
+ # @example SPARQL Grammar (cast as boolean)
23
+ # PREFIX : <http://example.org/>
24
+ # PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
25
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
26
+ # SELECT ?a ?v (xsd:boolean(?v) AS ?boolean)
27
+ # WHERE { ?a :p ?v . }
28
+ #
29
+ # @example SSE (cast as boolean)
30
+ # (prefix ((: <http://example.org/>)
31
+ # (rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
32
+ # (xsd: <http://www.w3.org/2001/XMLSchema#>))
33
+ # (project (?a ?v ?boolean)
34
+ # (extend ((?boolean (xsd:boolean ?v)))
35
+ # (bgp (triple ?a :p ?v)))))
36
+ #
37
+ # @example SPARQL Grammar (inner bind)
38
+ # PREFIX : <http://example.org/>
39
+ #
40
+ # SELECT ?z ?s1
41
+ # {
42
+ # ?s ?p ?o .
43
+ # BIND(?o+1 AS ?z)
44
+ # ?s1 ?p1 ?z
45
+ # }
46
+ #
47
+ # @example SSE (inner bind)
48
+ # (prefix ((: <http://example.org/>))
49
+ # (project (?z ?s1)
50
+ # (join
51
+ # (extend ((?z (+ ?o 1)))
52
+ # (bgp (triple ?s ?p ?o)))
53
+ # (bgp (triple ?s1 ?p1 ?z)))))
21
54
  #
22
55
  # @see https://www.w3.org/TR/sparql11-query/#evaluation
23
56
  class Extend < Operator::Binary
@@ -89,7 +89,7 @@ module SPARQL; module Algebra
89
89
  # @return [String]
90
90
  def to_sparql(**options)
91
91
  filter_ops = operands.first.is_a?(Operator::Exprlist) ? operands.first.operands : [operands.first]
92
- operands.last.to_sparql(filter_ops: filter_ops, **options)
92
+ str = operands.last.to_sparql(filter_ops: filter_ops, **options)
93
93
  end
94
94
  end # Filter
95
95
  end # Operator