sparql 3.1.8 → 3.2.3

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 (155) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +88 -60
  3. data/VERSION +1 -1
  4. data/bin/sparql +15 -35
  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 +110 -46
  10. data/lib/sparql/algebra/operator/abs.rb +22 -2
  11. data/lib/sparql/algebra/operator/add.rb +21 -2
  12. data/lib/sparql/algebra/operator/adjust.rb +69 -0
  13. data/lib/sparql/algebra/operator/alt.rb +26 -2
  14. data/lib/sparql/algebra/operator/and.rb +25 -3
  15. data/lib/sparql/algebra/operator/asc.rb +20 -1
  16. data/lib/sparql/algebra/operator/ask.rb +17 -1
  17. data/lib/sparql/algebra/operator/avg.rb +19 -1
  18. data/lib/sparql/algebra/operator/base.rb +18 -1
  19. data/lib/sparql/algebra/operator/bgp.rb +13 -1
  20. data/lib/sparql/algebra/operator/bnode.rb +33 -10
  21. data/lib/sparql/algebra/operator/bound.rb +22 -1
  22. data/lib/sparql/algebra/operator/ceil.rb +25 -2
  23. data/lib/sparql/algebra/operator/clear.rb +26 -2
  24. data/lib/sparql/algebra/operator/coalesce.rb +33 -11
  25. data/lib/sparql/algebra/operator/compare.rb +9 -0
  26. data/lib/sparql/algebra/operator/concat.rb +26 -2
  27. data/lib/sparql/algebra/operator/construct.rb +29 -6
  28. data/lib/sparql/algebra/operator/contains.rb +24 -2
  29. data/lib/sparql/algebra/operator/copy.rb +19 -2
  30. data/lib/sparql/algebra/operator/count.rb +52 -6
  31. data/lib/sparql/algebra/operator/create.rb +20 -2
  32. data/lib/sparql/algebra/operator/dataset.rb +37 -2
  33. data/lib/sparql/algebra/operator/datatype.rb +25 -6
  34. data/lib/sparql/algebra/operator/day.rb +25 -7
  35. data/lib/sparql/algebra/operator/delete.rb +29 -2
  36. data/lib/sparql/algebra/operator/delete_data.rb +23 -2
  37. data/lib/sparql/algebra/operator/delete_where.rb +24 -2
  38. data/lib/sparql/algebra/operator/desc.rb +20 -1
  39. data/lib/sparql/algebra/operator/describe.rb +27 -4
  40. data/lib/sparql/algebra/operator/distinct.rb +20 -3
  41. data/lib/sparql/algebra/operator/divide.rb +26 -2
  42. data/lib/sparql/algebra/operator/drop.rb +27 -3
  43. data/lib/sparql/algebra/operator/encode_for_uri.rb +22 -2
  44. data/lib/sparql/algebra/operator/equal.rb +12 -2
  45. data/lib/sparql/algebra/operator/exists.rb +28 -4
  46. data/lib/sparql/algebra/operator/exprlist.rb +15 -2
  47. data/lib/sparql/algebra/operator/extend.rb +95 -7
  48. data/lib/sparql/algebra/operator/filter.rb +27 -5
  49. data/lib/sparql/algebra/operator/floor.rb +25 -2
  50. data/lib/sparql/algebra/operator/function_call.rb +64 -0
  51. data/lib/sparql/algebra/operator/graph.rb +69 -6
  52. data/lib/sparql/algebra/operator/greater_than.rb +12 -3
  53. data/lib/sparql/algebra/operator/greater_than_or_equal.rb +12 -2
  54. data/lib/sparql/algebra/operator/group.rb +133 -8
  55. data/lib/sparql/algebra/operator/group_concat.rb +43 -7
  56. data/lib/sparql/algebra/operator/hours.rb +25 -7
  57. data/lib/sparql/algebra/operator/if.rb +20 -3
  58. data/lib/sparql/algebra/operator/in.rb +18 -1
  59. data/lib/sparql/algebra/operator/insert.rb +24 -2
  60. data/lib/sparql/algebra/operator/insert_data.rb +23 -2
  61. data/lib/sparql/algebra/operator/iri.rb +21 -4
  62. data/lib/sparql/algebra/operator/is_blank.rb +20 -3
  63. data/lib/sparql/algebra/operator/is_iri.rb +20 -3
  64. data/lib/sparql/algebra/operator/is_literal.rb +20 -3
  65. data/lib/sparql/algebra/operator/is_numeric.rb +22 -5
  66. data/lib/sparql/algebra/operator/is_triple.rb +32 -0
  67. data/lib/sparql/algebra/operator/join.rb +58 -3
  68. data/lib/sparql/algebra/operator/lang.rb +25 -0
  69. data/lib/sparql/algebra/operator/lang_matches.rb +22 -1
  70. data/lib/sparql/algebra/operator/lcase.rb +22 -2
  71. data/lib/sparql/algebra/operator/left_join.rb +44 -3
  72. data/lib/sparql/algebra/operator/less_than.rb +12 -3
  73. data/lib/sparql/algebra/operator/less_than_or_equal.rb +12 -2
  74. data/lib/sparql/algebra/operator/load.rb +25 -2
  75. data/lib/sparql/algebra/operator/max.rb +19 -1
  76. data/lib/sparql/algebra/operator/md5.rb +22 -5
  77. data/lib/sparql/algebra/operator/min.rb +21 -3
  78. data/lib/sparql/algebra/operator/minus.rb +65 -7
  79. data/lib/sparql/algebra/operator/minutes.rb +25 -7
  80. data/lib/sparql/algebra/operator/modify.rb +62 -5
  81. data/lib/sparql/algebra/operator/month.rb +25 -7
  82. data/lib/sparql/algebra/operator/move.rb +20 -2
  83. data/lib/sparql/algebra/operator/multiply.rb +26 -3
  84. data/lib/sparql/algebra/operator/negate.rb +23 -3
  85. data/lib/sparql/algebra/operator/not.rb +24 -3
  86. data/lib/sparql/algebra/operator/not_equal.rb +13 -0
  87. data/lib/sparql/algebra/operator/notexists.rb +30 -6
  88. data/lib/sparql/algebra/operator/notin.rb +20 -3
  89. data/lib/sparql/algebra/operator/notoneof.rb +21 -2
  90. data/lib/sparql/algebra/operator/now.rb +24 -5
  91. data/lib/sparql/algebra/operator/object.rb +32 -0
  92. data/lib/sparql/algebra/operator/or.rb +26 -3
  93. data/lib/sparql/algebra/operator/order.rb +64 -1
  94. data/lib/sparql/algebra/operator/path.rb +29 -2
  95. data/lib/sparql/algebra/operator/path_opt.rb +28 -65
  96. data/lib/sparql/algebra/operator/path_plus.rb +37 -10
  97. data/lib/sparql/algebra/operator/path_range.rb +178 -0
  98. data/lib/sparql/algebra/operator/path_star.rb +25 -4
  99. data/lib/sparql/algebra/operator/path_zero.rb +110 -0
  100. data/lib/sparql/algebra/operator/plus.rb +49 -8
  101. data/lib/sparql/algebra/operator/predicate.rb +32 -0
  102. data/lib/sparql/algebra/operator/prefix.rb +24 -3
  103. data/lib/sparql/algebra/operator/project.rb +111 -6
  104. data/lib/sparql/algebra/operator/rand.rb +30 -2
  105. data/lib/sparql/algebra/operator/reduced.rb +20 -3
  106. data/lib/sparql/algebra/operator/regex.rb +26 -18
  107. data/lib/sparql/algebra/operator/replace.rb +26 -6
  108. data/lib/sparql/algebra/operator/reverse.rb +31 -2
  109. data/lib/sparql/algebra/operator/round.rb +25 -2
  110. data/lib/sparql/algebra/operator/same_term.rb +24 -6
  111. data/lib/sparql/algebra/operator/sample.rb +32 -8
  112. data/lib/sparql/algebra/operator/seconds.rb +25 -7
  113. data/lib/sparql/algebra/operator/seq.rb +23 -5
  114. data/lib/sparql/algebra/operator/sequence.rb +14 -11
  115. data/lib/sparql/algebra/operator/sha1.rb +18 -1
  116. data/lib/sparql/algebra/operator/sha256.rb +18 -1
  117. data/lib/sparql/algebra/operator/sha384.rb +18 -1
  118. data/lib/sparql/algebra/operator/sha512.rb +18 -1
  119. data/lib/sparql/algebra/operator/slice.rb +27 -5
  120. data/lib/sparql/algebra/operator/str.rb +21 -1
  121. data/lib/sparql/algebra/operator/strafter.rb +25 -2
  122. data/lib/sparql/algebra/operator/strbefore.rb +25 -2
  123. data/lib/sparql/algebra/operator/strdt.rb +22 -1
  124. data/lib/sparql/algebra/operator/strends.rb +25 -3
  125. data/lib/sparql/algebra/operator/strlang.rb +24 -6
  126. data/lib/sparql/algebra/operator/strlen.rb +23 -2
  127. data/lib/sparql/algebra/operator/strstarts.rb +25 -2
  128. data/lib/sparql/algebra/operator/struuid.rb +29 -9
  129. data/lib/sparql/algebra/operator/subject.rb +32 -0
  130. data/lib/sparql/algebra/operator/substr.rb +23 -2
  131. data/lib/sparql/algebra/operator/subtract.rb +37 -7
  132. data/lib/sparql/algebra/operator/sum.rb +24 -6
  133. data/lib/sparql/algebra/operator/table.rb +85 -4
  134. data/lib/sparql/algebra/operator/timezone.rb +25 -7
  135. data/lib/sparql/algebra/operator/triple.rb +24 -0
  136. data/lib/sparql/algebra/operator/tz.rb +24 -7
  137. data/lib/sparql/algebra/operator/ucase.rb +23 -2
  138. data/lib/sparql/algebra/operator/union.rb +29 -6
  139. data/lib/sparql/algebra/operator/update.rb +46 -4
  140. data/lib/sparql/algebra/operator/using.rb +49 -2
  141. data/lib/sparql/algebra/operator/uuid.rb +27 -8
  142. data/lib/sparql/algebra/operator/with.rb +38 -4
  143. data/lib/sparql/algebra/operator/year.rb +25 -7
  144. data/lib/sparql/algebra/operator.rb +150 -12
  145. data/lib/sparql/algebra/query.rb +5 -3
  146. data/lib/sparql/algebra/sxp_extensions.rb +3 -3
  147. data/lib/sparql/algebra.rb +42 -6
  148. data/lib/sparql/grammar/meta.rb +1367 -267
  149. data/lib/sparql/grammar/parser11.rb +829 -331
  150. data/lib/sparql/grammar/terminals11.rb +2 -2
  151. data/lib/sparql/grammar.rb +6 -4
  152. data/lib/sparql/results.rb +3 -2
  153. data/lib/sparql/server.rb +93 -0
  154. data/lib/sparql.rb +8 -5
  155. metadata +57 -35
@@ -3,8 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL relational `<` (less than) comparison operator.
5
5
  #
6
- # @example
7
- # (< ?x ?y)
6
+ # [114] RelationalExpression ::= NumericExpression ('<' NumericExpression)?
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
10
+ # PREFIX : <http://example.org/things#>
11
+ # SELECT ?x
12
+ # WHERE { ?x :p ?v . FILTER ( ?v < 1 ) }
13
+ #
14
+ # @example SSE
15
+ # (prefix
16
+ # ((xsd: <http://www.w3.org/2001/XMLSchema#>) (: <http://example.org/things#>))
17
+ # (project (?x) (filter (< ?v 1) (bgp (triple ?x :p ?v)))))
8
18
  #
9
19
  # @see https://www.w3.org/TR/sparql11-query/#OperatorMapping
10
20
  # @see https://www.w3.org/TR/xpath-functions/#func-compare
@@ -25,7 +35,6 @@ module SPARQL; module Algebra
25
35
  # @return [RDF::Literal::Boolean] `true` or `false`
26
36
  # @raise [TypeError] if either operand is not a literal
27
37
  def apply(left, right, **options)
28
- #require 'byebug'; byebug if super == RDF::Literal(-1)
29
38
  RDF::Literal(super == RDF::Literal(-1))
30
39
  end
31
40
  end # LessThan
@@ -3,8 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL relational `<=` (less than or equal) comparison operator.
5
5
  #
6
- # @example
7
- # (<= ?x ?y)
6
+ # [114] RelationalExpression ::= NumericExpression ('<=' NumericExpression)?
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
10
+ # PREFIX : <http://example.org/things#>
11
+ # SELECT ?x
12
+ # WHERE { ?x :p ?v . FILTER ( ?v <= 1 ) }
13
+ #
14
+ # @example SSE
15
+ # (prefix
16
+ # ((xsd: <http://www.w3.org/2001/XMLSchema#>) (: <http://example.org/things#>))
17
+ # (project (?x) (filter (<= ?v 1) (bgp (triple ?x :p ?v)))))
8
18
  #
9
19
  # @see https://www.w3.org/TR/sparql11-query/#OperatorMapping
10
20
  # @see https://www.w3.org/TR/xpath-functions/#func-compare
@@ -6,8 +6,14 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The LOAD operation reads an RDF document from a IRI and inserts its triples into the specified graph in the Graph Store. The specified destination graph should be created if required; again, implementations providing an update service over a fixed set of graphs must return with failure for a request that would create a disallowed graph. If the destination graph already exists, then no data in that graph will be removed.
8
8
  #
9
- # @example
10
- # (load <remote> <g>)
9
+ #
10
+ # [31] Load ::= 'LOAD' 'SILENT'? iri ( 'INTO' GraphRef )?
11
+ #
12
+ # @example SPARQL Grammar
13
+ # LOAD <http://example.org/remote> INTO GRAPH <http://example.org/g> ;
14
+ #
15
+ # @example SSE
16
+ # (update (load <http://example.org/remote> <http://example.org/g>))
11
17
  #
12
18
  # @see https://www.w3.org/TR/sparql11-update/#load
13
19
  class Load < Operator
@@ -43,6 +49,23 @@ module SPARQL; module Algebra
43
49
  ensure
44
50
  queryable
45
51
  end
52
+
53
+ ##
54
+ #
55
+ # Returns a partial SPARQL grammar for this operator.
56
+ #
57
+ # @return [String]
58
+ def to_sparql(**options)
59
+ silent = operands.first == :silent
60
+ ops = silent ? operands[1..-1] : operands
61
+ location, name = ops
62
+
63
+ str = "LOAD "
64
+ str << "SILENT " if silent
65
+ str << location.to_sparql(**options)
66
+ str << " INTO GRAPH " + name.to_sparql(**options) if name
67
+ str
68
+ end
46
69
  end # Load
47
70
  end # Operator
48
71
  end; end # SPARQL::Algebra
@@ -3,7 +3,14 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `max` set function.
5
5
  #
6
- # @example
6
+ # [127] Aggregate::= ... | 'MAX' '(' 'DISTINCT'? Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://www.example.org/>
10
+ # SELECT (MAX(?o) AS ?max)
11
+ # WHERE { ?s ?p ?o }
12
+ #
13
+ # @example SSE
7
14
  # (prefix ((: <http://www.example.org/>))
8
15
  # (project (?max)
9
16
  # (extend ((?max ??.0))
@@ -42,6 +49,17 @@ module SPARQL; module Algebra
42
49
  raise TypeError, "Maximum of non-literals: #{enum.flatten}"
43
50
  end
44
51
  end
52
+
53
+ ##
54
+ #
55
+ # Returns a partial SPARQL grammar for this operator.
56
+ #
57
+ # @return [String]
58
+ def to_sparql(**options)
59
+ distinct = operands.first == :distinct
60
+ args = distinct ? operands[1..-1] : operands
61
+ "MAX(#{'DISTINCT ' if distinct}#{args.to_sparql(**options)})"
62
+ end
45
63
  end # Max
46
64
  end # Operator
47
65
  end; end # SPARQL::Algebra
@@ -7,11 +7,19 @@ module SPARQL; module Algebra
7
7
  #
8
8
  # Returns the MD5 checksum, as a hex digit string, calculated on the UTF-8 representation of the simple literal or lexical form of the `xsd:string`. Hex digits `SHOULD` be in lower case.
9
9
  #
10
- # @example
11
- # (prefix ((: <http://example.org/>))
12
- # (project (?hash)
13
- # (extend ((?hash (md5 ?l)))
14
- # (bgp (triple :s1 :str ?l)))))
10
+ # [121] BuiltInCall ::= ... | 'MD5' '(' Expression ')'
11
+ #
12
+ # @example SPARQL Grammar
13
+ # PREFIX : <http://example.org/>
14
+ # SELECT (MD5(?l) AS ?hash) WHERE {
15
+ # :s1 :str ?l
16
+ # }
17
+ #
18
+ # @example SSE
19
+ # (prefix ((: <http://example.org/>))
20
+ # (project (?hash)
21
+ # (extend ((?hash (md5 ?l)))
22
+ # (bgp (triple :s1 :str ?l)))))
15
23
  #
16
24
  # @see https://www.w3.org/TR/sparql11-query/#func-md5
17
25
  class MD5 < Operator::Unary
@@ -31,6 +39,15 @@ module SPARQL; module Algebra
31
39
  raise TypeError, "expected simple literal or xsd:string, but got #{operand.inspect}" unless (operand.datatype || RDF::XSD.string) == RDF::XSD.string
32
40
  RDF::Literal(Digest::MD5.new.hexdigest(operand.to_s))
33
41
  end
42
+
43
+ ##
44
+ #
45
+ # Returns a partial SPARQL grammar for this operator.
46
+ #
47
+ # @return [String]
48
+ def to_sparql(**options)
49
+ "MD5(" + operands.to_sparql(**options) + ")"
50
+ end
34
51
  end # MD5
35
52
  end # Operator
36
53
  end; end # SPARQL::Algebra
@@ -3,12 +3,19 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `min` set function.
5
5
  #
6
- # @example
6
+ # [127] Aggregate::= ... | 'MIN' '(' 'DISTINCT'? Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://www.example.org/>
10
+ # SELECT (MIN(?o) AS ?min)
11
+ # WHERE { ?s :dec ?o }
12
+ #
13
+ # @example SSE
7
14
  # (prefix ((: <http://www.example.org/>))
8
- # (project (?max)
15
+ # (project (?min)
9
16
  # (extend ((?min ??.0))
10
17
  # (group () ((??.0 (min ?o)))
11
- # (bgp (triple ?s ?p ?o))))))
18
+ # (bgp (triple ?s :dec ?o))))))
12
19
  #
13
20
  # @see https://www.w3.org/TR/sparql11-query/#defn_aggMin
14
21
  class Min < Operator
@@ -42,6 +49,17 @@ module SPARQL; module Algebra
42
49
  raise TypeError, "Minumuim of non-literals: #{enum.flatten}"
43
50
  end
44
51
  end
52
+
53
+ ##
54
+ #
55
+ # Returns a partial SPARQL grammar for this operator.
56
+ #
57
+ # @return [String]
58
+ def to_sparql(**options)
59
+ distinct = operands.first == :distinct
60
+ args = distinct ? operands[1..-1] : operands
61
+ "MIN(#{'DISTINCT ' if distinct}#{args.to_sparql(**options)})"
62
+ end
45
63
  end # Min
46
64
  end # Operator
47
65
  end; end # SPARQL::Algebra
@@ -3,13 +3,42 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL GraphPattern `minus` operator.
5
5
  #
6
- # @example
7
- # (prefix ((ex: <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#>))
8
- # (project (?animal)
9
- # (minus
10
- # (bgp (triple ?animal <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ex:Animal))
11
- # (filter (|| (= ?type ex:Reptile) (= ?type ex:Insect))
12
- # (bgp (triple ?animal <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type))))))
6
+ # [66] MinusGraphPattern ::= 'MINUS' GroupGraphPattern
7
+ #
8
+ # @example SPARQL Grammar
9
+ # SELECT * { ?s ?p ?o MINUS { ?s ?q ?v } }
10
+ #
11
+ # @example SSE
12
+ # (minus
13
+ # (bgp
14
+ # (triple ?s ?p ?o))
15
+ # (bgp (triple ?s ?q ?v)))
16
+ #
17
+ # @example SPARQL Grammar (inline filter)
18
+ # PREFIX : <http://example/>
19
+ # SELECT (?s1 AS ?subset) (?s2 AS ?superset)
20
+ # WHERE {
21
+ # ?s2 a :Set .
22
+ # ?s1 a :Set .
23
+ # FILTER(?s1 != ?s2)
24
+ # MINUS {
25
+ # ?s1 a :Set .
26
+ # ?s2 a :Set .
27
+ # FILTER(?s1 != ?s2)
28
+ # }
29
+ # }
30
+ #
31
+ # @example SSE (inline filter)
32
+ # (prefix ((: <http://example/>))
33
+ # (project (?subset ?superset)
34
+ # (extend ((?subset ?s1) (?superset ?s2))
35
+ # (filter (!= ?s1 ?s2)
36
+ # (minus
37
+ # (bgp (triple ?s2 a :Set) (triple ?s1 a :Set))
38
+ # (filter (!= ?s1 ?s2)
39
+ # (bgp
40
+ # (triple ?s1 a :Set)
41
+ # (triple ?s2 a :Set))))))))
13
42
  #
14
43
  # @see https://www.w3.org/TR/xpath-functions/#func-numeric-unary-minus
15
44
  # @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
@@ -66,6 +95,35 @@ module SPARQL; module Algebra
66
95
  @operands = ops
67
96
  self
68
97
  end
98
+
99
+ ##
100
+ #
101
+ # Returns a partial SPARQL grammar for this operator.
102
+ #
103
+ # @param [Hash{String => Operator}] extensions
104
+ # Variable bindings
105
+ # @param [Array<Operator>] filter_ops ([])
106
+ # Filter Operations
107
+ # @param [Boolean] top_level (true)
108
+ # Treat this as a top-level, generating SELECT ... WHERE {}
109
+ # @return [String]
110
+ def to_sparql(top_level: true, filter_ops: [], extensions: {}, **options)
111
+ lhs, *rhs = operands
112
+ str = "{\n" + lhs.to_sparql(top_level: false, extensions: {}, **options)
113
+
114
+ # Any accrued filters go here.
115
+ filter_ops.each do |op|
116
+ str << "\nFILTER (#{op.to_sparql(**options)}) ."
117
+ end
118
+
119
+ rhs.each do |minus|
120
+ str << "\nMINUS {\n"
121
+ str << minus.to_sparql(top_level: false, extensions: {}, **options)
122
+ str << "\n}"
123
+ end
124
+ str << "}"
125
+ top_level ? Operator.to_sparql(str, extensions: extensions, **options) : str
126
+ end
69
127
  end # Minus
70
128
  end # Operator
71
129
  end; end # SPARQL::Algebra
@@ -5,11 +5,20 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # Returns the minutes part of the lexical form of `arg`. The value is as given in the lexical form of the XSD dateTime.
7
7
  #
8
- # @example
9
- # (prefix ((: <http://example.org/>))
10
- # (project (?s ?x)
11
- # (extend ((?x (minutes ?date)))
12
- # (bgp (triple ?s :date ?date)))))
8
+ # [121] BuiltInCall ::= ... | 'MINUTES' '(' Expression ')'
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example.org/>
12
+ # SELECT ?s (MINUTES(?date) AS ?x) WHERE {
13
+ # ?s :date ?date
14
+ # }
15
+ #
16
+ # @example SSE
17
+ # (prefix
18
+ # ((: <http://example.org/>))
19
+ # (project (?s ?x)
20
+ # (extend ((?x (minutes ?date)))
21
+ # (bgp (triple ?s :date ?date)))))
13
22
  #
14
23
  # @see https://www.w3.org/TR/sparql11-query/#func-minutes
15
24
  class Minutes < Operator::Unary
@@ -22,12 +31,21 @@ module SPARQL; module Algebra
22
31
  #
23
32
  # @param [RDF::Literal] operand
24
33
  # the operand
25
- # @return [RDF::Literal]
34
+ # @return [RDF::Literal::Temporal]
26
35
  # @raise [TypeError] if the operand is not a simple literal
27
36
  def apply(operand, **options)
28
- raise TypeError, "expected an RDF::Literal::DateTime, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::DateTime)
37
+ raise TypeError, "expected an RDF::Literal::Temporal, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::Temporal)
29
38
  RDF::Literal(operand.object.minute)
30
39
  end
40
+
41
+ ##
42
+ #
43
+ # Returns a partial SPARQL grammar for this operator.
44
+ #
45
+ # @return [String]
46
+ def to_sparql(**options)
47
+ "MINUTES(#{operands.last.to_sparql(**options)})"
48
+ end
31
49
  end # Minutes
32
50
  end # Operator
33
51
  end; end # SPARQL::Algebra
@@ -6,11 +6,28 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # Wraps delete/insert
8
8
  #
9
- # @example
10
- # (modify
11
- # (bgp (triple ?a foaf:knows ?b))
12
- # (delete ((triple ?a foaf:knows ?b)))
13
- # (insert ((triple ?b foaf:knows ?a)))
9
+ # If `options` contains any of the Protocol attributes, it is treated as if there is a USING or USING NAMED clause inserted.
10
+ #
11
+ # * `using-graph-uri`
12
+ # * `using-named-graph-uri`
13
+ #
14
+ # [41] Modify ::= ( 'WITH' iri )? ( DeleteClause InsertClause? | InsertClause ) UsingClause* 'WHERE' GroupGraphPattern
15
+ #
16
+ # @example SPARQL Grammar
17
+ # PREFIX : <http://example.org/>
18
+ # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
19
+ # DELETE { ?a foaf:knows ?b }
20
+ # INSERT { ?b foaf:knows ?a }
21
+ # WHERE { ?a foaf:knows ?b }
22
+ #
23
+ # @example SSE
24
+ # (prefix ((: <http://example.org/>)
25
+ # (foaf: <http://xmlns.com/foaf/0.1/>))
26
+ # (update
27
+ # (modify
28
+ # (bgp (triple ?a foaf:knows ?b))
29
+ # (delete ((triple ?a foaf:knows ?b)))
30
+ # (insert ((triple ?b foaf:knows ?a)))) ))
14
31
  #
15
32
  # @see XXX
16
33
  class Modify < Operator
@@ -23,6 +40,10 @@ module SPARQL; module Algebra
23
40
  #
24
41
  # Execute the first operand to get solutions, and apply those solutions to the subsequent operators.
25
42
  #
43
+ # If `options` contains any of the Protocol attributes, any `using` clause is removed and a new `using` clause is added with entries taken from the `using-graph-uri` and `using-named-graph-uri`.
44
+ #
45
+ # It is an error to supply the using-graph-uri or using-named-graph-uri parameters when using this protocol to convey a SPARQL 1.1 Update request that contains an operation that uses the USING, USING NAMED, or WITH clause.
46
+ #
26
47
  # @param [RDF::Queryable] queryable
27
48
  # the graph or repository to write
28
49
  # @param [Hash{Symbol => Object}] options
@@ -38,6 +59,18 @@ module SPARQL; module Algebra
38
59
  debug(options) {"Modify"}
39
60
  query = operands.shift
40
61
 
62
+ if %i(using-graph-uri using-named-graph-uri).any? {|k| options.key?(k)}
63
+ raise ArgumentError,
64
+ "query contains USING/WITH clause, which is incompatible with using-graph-uri or using-named-graph-uri query parameters" if
65
+ query.is_a?(Operator::Using) || query.is_a?(Operator::With)
66
+
67
+ debug("=> Insert USING clause", options)
68
+ defaults = Array(options.delete(:'using-graph-uri')).map {|uri| RDF::URI(uri)}
69
+ named = Array(options.delete(:'using-named-graph-uri')).map {|uri| [:named, RDF::URI(uri)]}
70
+
71
+ query = Operator::Using.new((defaults + named), query, **options)
72
+ end
73
+
41
74
  queryable.query(query, depth: options[:depth].to_i + 1, **options) do |solution|
42
75
  debug(options) {"(solution)=>#{solution.inspect}"}
43
76
 
@@ -48,6 +81,30 @@ module SPARQL; module Algebra
48
81
  end
49
82
  queryable
50
83
  end
84
+
85
+ ##
86
+ #
87
+ # Returns a partial SPARQL grammar for this operator.
88
+ #
89
+ # @return [String]
90
+ def to_sparql(**options)
91
+ if operands.first.is_a?(With)
92
+ operands.first.to_sparql(**options)
93
+ else
94
+ # The content of the WHERE clause, may be USING
95
+ content = operands.first.to_sparql(top_level: false, **options)
96
+
97
+ # DELETE | INSERT | DELETE INSERT
98
+ str = operands[1..-1].to_sparql(top_level: false, delimiter: "\n", **options) + "\n"
99
+
100
+ # Append the WHERE or USING clause
101
+ str << if operands.first.is_a?(Using)
102
+ content
103
+ else
104
+ Operator.to_sparql(content, project: nil, **options)
105
+ end
106
+ end
107
+ end
51
108
  end # Modify
52
109
  end # Operator
53
110
  end; end # SPARQL::Algebra
@@ -5,11 +5,20 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # Returns the month part of `arg` as an integer.
7
7
  #
8
- # @example
9
- # (prefix ((: <http://example.org/>))
10
- # (project (?s ?x)
11
- # (extend ((?x (month ?date)))
12
- # (bgp (triple ?s :date ?date)))))
8
+ # [121] BuiltInCall ::= ... | 'MONTH' '(' Expression ')'
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example.org/>
12
+ # SELECT ?s (MONTH(?date) AS ?x) WHERE {
13
+ # ?s :date ?date
14
+ # }
15
+ #
16
+ # @example SSE
17
+ # (prefix
18
+ # ((: <http://example.org/>))
19
+ # (project (?s ?x)
20
+ # (extend ((?x (month ?date)))
21
+ # (bgp (triple ?s :date ?date)))))
13
22
  #
14
23
  # @see https://www.w3.org/TR/sparql11-query/#func-month
15
24
  class Month < Operator::Unary
@@ -22,12 +31,21 @@ module SPARQL; module Algebra
22
31
  #
23
32
  # @param [RDF::Literal] operand
24
33
  # the operand
25
- # @return [RDF::Literal]
34
+ # @return [RDF::Literal::Temporal]
26
35
  # @raise [TypeError] if the operand is not a simple literal
27
36
  def apply(operand, **options)
28
- raise TypeError, "expected an RDF::Literal::DateTime, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::DateTime)
37
+ raise TypeError, "expected an RDF::Literal::Temporal, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::Temporal)
29
38
  RDF::Literal(operand.object.month)
30
39
  end
40
+
41
+ ##
42
+ #
43
+ # Returns a partial SPARQL grammar for this operator.
44
+ #
45
+ # @return [String]
46
+ def to_sparql(**options)
47
+ "MONTH(#{operands.last.to_sparql(**options)})"
48
+ end
31
49
  end # Month
32
50
  end # Operator
33
51
  end; end # SPARQL::Algebra
@@ -6,8 +6,14 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The MOVE operation is a shortcut for moving all data from an input graph into a destination graph. The input graph is removed after insertion and data from the destination graph, if any, is removed before insertion.
8
8
  #
9
- # @example
10
- # (move silent <iri> to default)
9
+ # [36] Move ::= 'MOVE' 'SILENT'? GraphOrDefault 'TO' GraphOrDefault
10
+ #
11
+ # @example SPARQL Grammar
12
+ # MOVE SILENT GRAPH <http://www.example.com/g1> TO DEFAULT
13
+ #
14
+ # @example SSE
15
+ # (update
16
+ # (move silent <http://www.example.com/g1> default))
11
17
  #
12
18
  # @see https://www.w3.org/TR/sparql11-update/#move
13
19
  class Move < Operator
@@ -62,6 +68,18 @@ module SPARQL; module Algebra
62
68
  end
63
69
  queryable
64
70
  end
71
+
72
+ ##
73
+ #
74
+ # Returns a partial SPARQL grammar for this operator.
75
+ #
76
+ # @return [String]
77
+ def to_sparql(**options)
78
+ *args, last = operands.dup
79
+ args += [:TO, last]
80
+
81
+ "MOVE " + args.to_sparql(**options)
82
+ end
65
83
  end # Move
66
84
  end # Operator
67
85
  end; end # SPARQL::Algebra
@@ -3,9 +3,23 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL numeric `multiply` operator.
5
5
  #
6
- # @example
7
- # (* ?x ?y)
8
- # (multiply ?x ?y)
6
+ # [117] MultiplicativeExpression::= UnaryExpression ( '*' UnaryExpression | '/' UnaryExpression )*
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # SELECT ?s WHERE {
11
+ # ?s :p ?o .
12
+ # ?s2 :p ?o2 .
13
+ # FILTER(?o * ?o2 = 4) .
14
+ # }
15
+ #
16
+ # @example SSE
17
+ # (prefix ((: <http://example.org/>))
18
+ # (project (?s)
19
+ # (filter (= (* ?o ?o2) 4)
20
+ # (bgp
21
+ # (triple ?s :p ?o)
22
+ # (triple ?s2 :p ?o2)))))
9
23
  #
10
24
  # @see https://www.w3.org/TR/xpath-functions/#func-numeric-multiply
11
25
  class Multiply < Operator::Binary
@@ -29,6 +43,15 @@ module SPARQL; module Algebra
29
43
  else raise TypeError, "expected two RDF::Literal::Numeric operands, but got #{left.inspect} and #{right.inspect}"
30
44
  end
31
45
  end
46
+
47
+ ##
48
+ #
49
+ # Returns a partial SPARQL grammar for this operator.
50
+ #
51
+ # @return [String]
52
+ def to_sparql(**options)
53
+ "(#{operands.first.to_sparql(**options)} * #{operands.last.to_sparql(**options)})"
54
+ end
32
55
  end # Multiply
33
56
  end # Operator
34
57
  end; end # SPARQL::Algebra
@@ -3,9 +3,20 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL numeric unary `-` (negation) operator.
5
5
  #
6
- # @example
7
- # (- ?x)
8
- # (negate ?x)
6
+ # [118] UnaryExpression ::= ... | '-' PrimaryExpression
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # SELECT ?s WHERE {
11
+ # ?s :p ?o .
12
+ # FILTER(-?o = -2) .
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (prefix ((: <http://example.org/>))
17
+ # (project (?s)
18
+ # (filter (= (- ?o) -2)
19
+ # (bgp (triple ?s :p ?o)))))
9
20
  #
10
21
  # @see https://www.w3.org/TR/xpath-functions/#func-numeric-unary-minus
11
22
  class Negate < Operator::Unary
@@ -26,6 +37,15 @@ module SPARQL; module Algebra
26
37
  else raise TypeError, "expected an RDF::Literal::Numeric, but got #{term.inspect}"
27
38
  end
28
39
  end
40
+
41
+ ##
42
+ #
43
+ # Returns a partial SPARQL grammar for this operator.
44
+ #
45
+ # @return [String]
46
+ def to_sparql(**options)
47
+ "(-#{operands.to_sparql(**options)})"
48
+ end
29
49
  end # Negate
30
50
  end # Operator
31
51
  end; end # SPARQL::Algebra
@@ -3,9 +3,21 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL logical `not` operator.
5
5
  #
6
- # @example
7
- # (! ?x)
8
- # (not ?x)
6
+ # [118] UnaryExpression ::= ... | '!' PrimaryExpression
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/ns#>
10
+ # SELECT ?a
11
+ # WHERE {
12
+ # ?a :p ?v .
13
+ # FILTER ( ! ?v ) .
14
+ # }
15
+ #
16
+ # @example SSE
17
+ # (prefix ((: <http://example.org/ns#>))
18
+ # (project (?a)
19
+ # (filter (! ?v)
20
+ # (bgp (triple ?a :p ?v)))))
9
21
  #
10
22
  # @see https://www.w3.org/TR/xpath-functions/#func-not
11
23
  class Not < Operator::Unary
@@ -30,6 +42,15 @@ module SPARQL; module Algebra
30
42
  else super
31
43
  end
32
44
  end
45
+
46
+ ##
47
+ #
48
+ # Returns a partial SPARQL grammar for this operator.
49
+ #
50
+ # @return [String]
51
+ def to_sparql(**options)
52
+ "(!" + operands.first.to_sparql(**options) + ")"
53
+ end
33
54
  end # Not
34
55
  end # Operator
35
56
  end; end # SPARQL::Algebra
@@ -3,6 +3,19 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL relational `!=` (not equal) comparison operator.
5
5
  #
6
+ # [114] RelationalExpression ::= NumericExpression ('!=' NumericExpression)?
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
10
+ # PREFIX : <http://example.org/things#>
11
+ # SELECT ?x
12
+ # WHERE { ?x :p ?v . FILTER ( ?v != 1 ) }
13
+ #
14
+ # @example SSE
15
+ # (prefix
16
+ # ((xsd: <http://www.w3.org/2001/XMLSchema#>) (: <http://example.org/things#>))
17
+ # (project (?x) (filter (!= ?v 1) (bgp (triple ?x :p ?v)))))
18
+ #
6
19
  # @see https://www.w3.org/TR/sparql11-query/#OperatorMapping
7
20
  # @see https://www.w3.org/TR/sparql11-query/#func-RDFterm-equal
8
21
  class NotEqual < Equal