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,7 +3,14 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `avg` set function.
5
5
  #
6
- # @example
6
+ # [127] Aggregate::= ... | 'AVG' '(' 'DISTINCT'? Expression ')'
7
+ #
8
+ # @example SPARQL Query
9
+ # PREFIX : <http://www.example.org/>
10
+ # SELECT (AVG(?o) AS ?avg)
11
+ # WHERE { ?s :dec ?o }
12
+ #
13
+ # @example SSE
7
14
  # (prefix ((: <http://www.example.org/>))
8
15
  # (project (?avg)
9
16
  # (extend ((?avg ??.0))
@@ -40,6 +47,17 @@ module SPARQL; module Algebra
40
47
  raise TypeError, "Averaging non-numeric types: #{enum.flatten}"
41
48
  end
42
49
  end
50
+
51
+ ##
52
+ #
53
+ # Returns a partial SPARQL grammar for this operator.
54
+ #
55
+ # @return [String]
56
+ def to_sparql(**options)
57
+ distinct = operands.first == :distinct
58
+ args = distinct ? operands[1..-1] : operands
59
+ "AVG(#{'DISTINCT ' if distinct}#{args.to_sparql(**options)})"
60
+ end
43
61
  end # Avg
44
62
  end # Operator
45
63
  end; end # SPARQL::Algebra
@@ -3,7 +3,13 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL GraphPattern `base` operator.
5
5
  #
6
- # @example
6
+ # [5] BaseDecl ::= 'BASE' IRIREF
7
+ #
8
+ # @example SPARQL Grammar
9
+ # BASE <http://example.org/>
10
+ # SELECT * { <a> <b> 123.0 }
11
+ #
12
+ # @example SSE
7
13
  # (base <http://example.org/>
8
14
  # (bgp (triple <a> <b> 123.0)))
9
15
  #
@@ -56,6 +62,17 @@ module SPARQL; module Algebra
56
62
  def query_yields_statements?
57
63
  operands.last.query_yields_statements?
58
64
  end
65
+
66
+ ##
67
+ #
68
+ # Returns a partial SPARQL grammar for this term.
69
+ #
70
+ # @return [String]
71
+ def to_sparql(**options)
72
+ str = "BASE #{operands.first.to_sparql}\n"
73
+
74
+ str << operands.last.to_sparql(base_uri: operands.first, **options)
75
+ end
59
76
  end # Base
60
77
  end # Operator
61
78
  end; end # SPARQL::Algebra
@@ -5,10 +5,22 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # Query with `graph_name` set to false.
7
7
  #
8
- # @example
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example/>
10
+ # SELECT * { ?s ?p ?o }
11
+ #
12
+ # @example SSE
9
13
  # (prefix ((: <http://example/>))
10
14
  # (bgp (triple ?s ?p ?o)))
11
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
+ #
12
24
  # @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
13
25
  class BGP < Operator
14
26
  NAME = [:bgp]
@@ -5,16 +5,30 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # The BNODE function constructs a blank node that is distinct from all blank nodes in the dataset being queried and distinct from all blank nodes created by calls to this constructor for other query solutions. If the no argument form is used, every call results in a distinct blank node. If the form with a simple literal is used, every call results in distinct blank nodes for different simple literals, and the same blank node for calls with the same simple literal within expressions for one solution mapping.
7
7
  #
8
- # @example
9
- # (prefix ((: <http://example.org/>)
10
- # (xsd: <http://www.w3.org/2001/XMLSchema#>))
11
- # (project (?s1 ?s2 ?b1 ?b2)
12
- # (extend ((?b1 (bnode ?s1)) (?b2 (bnode ?s2)))
13
- # (filter (exprlist (|| (= ?a :s1) (= ?a :s3)) (|| (= ?b :s1) (= ?b :s3)))
14
- # (bgp
15
- # (triple ?a :str ?s1)
16
- # (triple ?b :str ?s2)
17
- # )))))
8
+ # [121] BuiltInCall ::= ... | 'BNODE' ( '(' Expression ')' | NIL )
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example.org/>
12
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
13
+ # SELECT ?s1 ?s2 (BNODE(?s1) AS ?b1) (BNODE(?s2) AS ?b2)
14
+ # WHERE {
15
+ # ?a :str ?s1 .
16
+ # ?b :str ?s2 .
17
+ # FILTER (?a = :s1 || ?a = :s3)
18
+ # FILTER (?b = :s1 || ?b = :s3)
19
+ # }
20
+ #
21
+ # @example SSE
22
+ # (prefix
23
+ # ((: <http://example.org/>) (xsd: <http://www.w3.org/2001/XMLSchema#>))
24
+ # (project (?s1 ?s2 ?b1 ?b2)
25
+ # (extend
26
+ # ((?b1 (bnode ?s1)) (?b2 (bnode ?s2)))
27
+ # (filter
28
+ # (exprlist
29
+ # (|| (= ?a :s1) (= ?a :s3))
30
+ # (|| (= ?b :s1) (= ?b :s3)))
31
+ # (bgp (triple ?a :str ?s1) (triple ?b :str ?s2))) )) )
18
32
  #
19
33
  # @see https://www.w3.org/TR/sparql11-query/#func-bnode
20
34
  class BNode < Operator::Unary
@@ -86,6 +100,15 @@ module SPARQL; module Algebra
86
100
  def to_sxp_bin
87
101
  [NAME] + operands.reject {|o| o == false}
88
102
  end
103
+
104
+ ##
105
+ #
106
+ # Returns a partial SPARQL grammar for this operator.
107
+ #
108
+ # @return [String]
109
+ def to_sparql(**options)
110
+ "BNODE(#{operands.last.to_sparql(**options)})"
111
+ end
89
112
  end # BNode
90
113
  end # Operator
91
114
  end; end # SPARQL::Algebra
@@ -3,7 +3,19 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `bound` operator.
5
5
  #
6
- # @example
6
+ # [121] BuiltInCall ::= ... | 'BOUND' '(' Var ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/ns#>
10
+ # SELECT ?a ?c
11
+ # WHERE
12
+ # { ?a :b ?c .
13
+ # OPTIONAL
14
+ # { ?c :d ?e } .
15
+ # FILTER (! bound(?e))
16
+ # }
17
+ #
18
+ # @example SSE
7
19
  # (prefix ((: <http://example.org/ns#>))
8
20
  # (project (?a ?c)
9
21
  # (filter (! (bound ?e))
@@ -46,6 +58,15 @@ module SPARQL; module Algebra
46
58
  else raise TypeError, "expected an RDF::Query::Variable, but got #{var.inspect}"
47
59
  end
48
60
  end
61
+
62
+ ##
63
+ #
64
+ # Returns a partial SPARQL grammar for this operator.
65
+ #
66
+ # @return [String]
67
+ def to_sparql(**options)
68
+ "bound(" + operands.first.to_sparql(**options) + ")"
69
+ end
49
70
  end # Bound
50
71
  end # Operator
51
72
  end; end # SPARQL::Algebra
@@ -3,8 +3,22 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL logical `ceil` operator.
5
5
  #
6
- # @example
7
- # (ceil ?x)
6
+ # [121] BuiltInCall ::= ... 'CEIL' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
11
+ # SELECT ?s ?num (CEIL(?num) AS ?ceil) WHERE {
12
+ # ?s :num ?num
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (prefix
17
+ # ((: <http://example.org/>)
18
+ # (xsd: <http://www.w3.org/2001/XMLSchema#>))
19
+ # (project (?s ?num ?ceil)
20
+ # (extend ((?ceil (ceil ?num)))
21
+ # (bgp (triple ?s :num ?num)))))
8
22
  #
9
23
  # @see https://www.w3.org/TR/sparql11-query/#func-ceil
10
24
  # @see https://www.w3.org/TR/xpath-functions/#func-ceil
@@ -26,6 +40,15 @@ module SPARQL; module Algebra
26
40
  else raise TypeError, "expected an RDF::Literal::Numeric, but got #{operand.inspect}"
27
41
  end
28
42
  end
43
+
44
+ ##
45
+ #
46
+ # Returns a partial SPARQL grammar for this operator.
47
+ #
48
+ # @return [String]
49
+ def to_sparql(**options)
50
+ "CEIL(#{operands.to_sparql(**options)})"
51
+ end
29
52
  end # Ceil
30
53
  end # Operator
31
54
  end; end # SPARQL::Algebra
@@ -6,8 +6,19 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The CLEAR operation removes all the triples in the specified graph(s) in the Graph Store.
8
8
  #
9
- # @example
10
- # (clear silent default)
9
+ # [32] Clear ::= 'CLEAR' 'SILENT'? GraphRefAll
10
+ #
11
+ # @example SPARQL Grammar (SILENT DEFAULT)
12
+ # CLEAR SILENT DEFAULT
13
+ #
14
+ # @example SSE (SILENT DEFAULT)
15
+ # (update (clear silent default))
16
+ #
17
+ # @example SPARQL Grammar (IRI)
18
+ # CLEAR GRAPH <http://example.com/>
19
+ #
20
+ # @example SSE (IRI)
21
+ # (update (clear <http://example.com/>))
11
22
  #
12
23
  # @see https://www.w3.org/TR/sparql11-update/#clear
13
24
  class Clear < Operator
@@ -58,6 +69,19 @@ module SPARQL; module Algebra
58
69
 
59
70
  queryable
60
71
  end
72
+
73
+ ##
74
+ #
75
+ # Returns a partial SPARQL grammar for this operator.
76
+ #
77
+ # @return [String]
78
+ def 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)
84
+ end
61
85
  end # Clear
62
86
  end # Operator
63
87
  end; end # SPARQL::Algebra
@@ -3,17 +3,30 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `coalesce` function.
5
5
  #
6
- # @example
7
- # (prefix ((: <http://example.org/>)
8
- # (xsd: <http://www.w3.org/2001/XMLSchema#>))
9
- # (project (?cx ?div ?def ?err)
10
- # (extend ((?cx (coalesce ?x -1))
11
- # (?div (coalesce (/ ?o ?x) -2))
12
- # (?def (coalesce ?z -3))
13
- # (?err (coalesce ?z)))
14
- # (leftjoin
15
- # (bgp (triple ?s :p ?o))
16
- # (bgp (triple ?s :q ?x))))))
6
+ # [121] BuiltInCall ::= ... | 'COALESCE' ExpressionList
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example/>
10
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
11
+ #
12
+ # SELECT ?X (SAMPLE(?v) AS ?S)
13
+ # {
14
+ # ?s :p ?v .
15
+ # OPTIONAL { ?s :q ?w }
16
+ # }
17
+ # GROUP BY (COALESCE(?w, "1605-11-05"^^xsd:date) AS ?X)
18
+ #
19
+ # @example SSE
20
+ # (prefix
21
+ # ((: <http://example/>) (xsd: <http://www.w3.org/2001/XMLSchema#>))
22
+ # (project (?X ?S)
23
+ # (extend ((?S ??.0))
24
+ # (group
25
+ # ((?X (coalesce ?w "1605-11-05"^^xsd:date)))
26
+ # ((??.0 (sample ?v)))
27
+ # (leftjoin
28
+ # (bgp (triple ?s :p ?v))
29
+ # (bgp (triple ?s :q ?w)))))))
17
30
  #
18
31
  # @see https://www.w3.org/TR/sparql11-query/#func-coalesce
19
32
  class Coalesce < Operator
@@ -50,6 +63,15 @@ module SPARQL; module Algebra
50
63
  end
51
64
  raise TypeError, "None of the operands evaluated"
52
65
  end
66
+
67
+ ##
68
+ #
69
+ # Returns a partial SPARQL grammar for this operator.
70
+ #
71
+ # @return [String]
72
+ def to_sparql(**options)
73
+ "COALESCE(#{operands.to_sparql(delimiter: ', ', **options)})"
74
+ end
53
75
  end # Coalesce
54
76
  end # Operator
55
77
  end; end # SPARQL::Algebra
@@ -35,6 +35,15 @@ module SPARQL; module Algebra
35
35
  RDF::Literal(spaceship(left, right, **options))
36
36
  end
37
37
 
38
+ ##
39
+ #
40
+ # Returns a partial SPARQL grammar for this operator.
41
+ #
42
+ # @return [String]
43
+ def to_sparql(**options)
44
+ "(#{operands.first.to_sparql(**options)} #{self.class.const_get(:NAME)} #{operands.last.to_sparql(**options)})"
45
+ end
46
+
38
47
  private
39
48
  # Define <=> as private for recursive statements
40
49
  def spaceship(left, right, **options)
@@ -5,8 +5,23 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # The CONCAT function corresponds to the XPath fn:concat function. The function accepts string literals as arguments.
7
7
  #
8
- # @example
9
- # (concat ?a ?b ...)
8
+ # [121] BuiltInCall ::= ... 'CONCAT' ExpressionList
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example.org/>
12
+ # SELECT (CONCAT(?str1,?str2) AS ?str) WHERE {
13
+ # :s6 :str ?str1 .
14
+ # :s7 :str ?str2 .
15
+ # }
16
+ #
17
+ # @example SSE
18
+ # (prefix
19
+ # ((: <http://example.org/>))
20
+ # (project (?str)
21
+ # (extend ((?str (concat ?str1 ?str2)))
22
+ # (bgp
23
+ # (triple :s6 :str ?str1)
24
+ # (triple :s7 :str ?str2)))))
10
25
  #
11
26
  # @see https://www.w3.org/TR/sparql11-query/#func-concat
12
27
  # @see https://www.w3.org/TR/xpath-functions/#func-concat
@@ -48,6 +63,15 @@ module SPARQL; module Algebra
48
63
  end
49
64
  end
50
65
  end
66
+
67
+ ##
68
+ #
69
+ # Returns a partial SPARQL grammar for this operator.
70
+ #
71
+ # @return [String]
72
+ def to_sparql(**options)
73
+ "CONCAT(#{operands.to_sparql(delimiter: ', ', **options)})"
74
+ end
51
75
  end # Concat
52
76
  end # Operator
53
77
  end; end # SPARQL::Algebra
@@ -5,12 +5,22 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # The CONSTRUCT query form returns a single RDF graph specified by a graph template. The result is an RDF graph formed by taking each query solution in the solution sequence, substituting for the variables in the graph template, and combining the triples into a single RDF graph by set union.
7
7
  #
8
- # @example
9
- # (prefix ((rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
10
- # (foaf: <http://xmlns.com/foaf/0.1/>))
11
- # (construct ((triple ?s ?p ?o))
12
- # (project (?s ?p ?o)
13
- # (bgp (triple ?s ?p ?o)))))
8
+ # [10] ConstructQuery ::= 'CONSTRUCT' ( ConstructTemplate DatasetClause* WhereClause SolutionModifier | DatasetClause* 'WHERE' '{' TriplesTemplate? '}' SolutionModifier ) ValuesClause
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example/>
12
+ # CONSTRUCT { ?x :p2 ?v }
13
+ # WHERE {
14
+ # ?x :p ?o .
15
+ # OPTIONAL {?o :q ?v }
16
+ # }
17
+ #
18
+ # @example SSE
19
+ # (prefix ((: <http://example/>))
20
+ # (construct ((triple ?x :p2 ?v))
21
+ # (leftjoin
22
+ # (bgp (triple ?x :p ?o))
23
+ # (bgp (triple ?o :q ?v)))))
14
24
  #
15
25
  # @see https://www.w3.org/TR/sparql11-query/#construct
16
26
  class Construct < Operator::Binary
@@ -81,6 +91,19 @@ module SPARQL; module Algebra
81
91
  def query_yields_statements?
82
92
  true
83
93
  end
94
+
95
+ ##
96
+ #
97
+ # Returns a partial SPARQL grammar for this term.
98
+ #
99
+ # @return [String]
100
+ def to_sparql(**options)
101
+ str = "CONSTRUCT {\n" +
102
+ operands[0].map { |e| e.to_sparql(top_level: false, **options) }.join(". \n") +
103
+ "\n}\n"
104
+
105
+ str << operands[1].to_sparql(top_level: true, project: nil, **options)
106
+ end
84
107
  end # Construct
85
108
  end # Operator
86
109
  end; end # SPARQL::Algebra
@@ -3,8 +3,21 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # A SPARQL `contains` operator.
5
5
  #
6
- # @example
7
- # (contains ?x ?y)
6
+ # [121] BuiltInCall ::= ... | 'CONTAINS' '(' Expression ',' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # SELECT ?s ?str WHERE {
11
+ # ?s :str ?str
12
+ # FILTER CONTAINS(?str, "a")
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (prefix
17
+ # ((: <http://example.org/>))
18
+ # (project (?s ?str)
19
+ # (filter (contains ?str "a")
20
+ # (bgp (triple ?s :str ?str)))))
8
21
  #
9
22
  # @see https://www.w3.org/TR/sparql11-query/#func-contains
10
23
  # @see https://www.w3.org/TR/xpath-functions/#func-contains
@@ -39,6 +52,15 @@ module SPARQL; module Algebra
39
52
  else RDF::Literal::FALSE
40
53
  end
41
54
  end
55
+
56
+ ##
57
+ #
58
+ # Returns a partial SPARQL grammar for this operator.
59
+ #
60
+ # @return [String]
61
+ def to_sparql(**options)
62
+ "contains(" + operands.to_sparql(delimiter: ', ', **options) + ")"
63
+ end
42
64
  end # Contains
43
65
  end # Operator
44
66
  end; end # SPARQL::Algebra
@@ -6,8 +6,13 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The COPY operation is a shortcut for inserting all data from an input graph into a destination graph. Data from the input graph is not affected, but data from the destination graph, if any, is removed before insertion.
8
8
  #
9
- # @example
10
- # (copy silent <iri> to default)
9
+ # [37] Copy ::= 'COPY' 'SILENT'? GraphOrDefault 'TO' GraphOrDefault
10
+ #
11
+ # @example SPARQL Grammar
12
+ # COPY SILENT GRAPH <http://www.example.com/g1> TO DEFAULT
13
+ #
14
+ # @example SSE
15
+ # (update (copy silent <http://www.example.com/g1> default))
11
16
  #
12
17
  # @see https://www.w3.org/TR/sparql11-update/#copy
13
18
  class Copy < Operator
@@ -59,6 +64,18 @@ module SPARQL; module Algebra
59
64
  end
60
65
  queryable
61
66
  end
67
+
68
+ ##
69
+ #
70
+ # Returns a partial SPARQL grammar for this operator.
71
+ #
72
+ # @return [String]
73
+ def to_sparql(**options)
74
+ *args, last = operands.dup
75
+ args += [:TO, last]
76
+
77
+ "COPY " + args.to_sparql(**options)
78
+ end
62
79
  end # Copy
63
80
  end # Operator
64
81
  end; end # SPARQL::Algebra
@@ -3,12 +3,47 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `count` set function.
5
5
  #
6
- # @example
7
- # (prefix ((: <http://www.example.org>))
8
- # (project (?C)
9
- # (extend ((?C ??.0))
10
- # (group () ((??.0 (count ?O)))
11
- # (bgp (triple ?S ?P ?O))))))
6
+ # [127] Aggregate::= 'COUNT' '(' 'DISTINCT'? ( '*' | Expression ) ')' ...
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://www.example.org/>
10
+ # SELECT (COUNT(?O) AS ?C)
11
+ # WHERE { ?S ?P ?O }
12
+ #
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))))))
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))))))
12
47
  #
13
48
  # @see https://www.w3.org/TR/sparql11-query/#defn_aggCount
14
49
  class Count < Operator
@@ -25,6 +60,17 @@ module SPARQL; module Algebra
25
60
  def apply(enum, **options)
26
61
  RDF::Literal(enum.length)
27
62
  end
63
+
64
+ ##
65
+ #
66
+ # Returns a partial SPARQL grammar for this operator.
67
+ #
68
+ # @return [String]
69
+ def 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)})"
73
+ end
28
74
  end # Count
29
75
  end # Operator
30
76
  end; end # SPARQL::Algebra
@@ -8,8 +8,13 @@ module SPARQL; module Algebra
8
8
  #
9
9
  # This is a no-op for RDF.rb implementations, unless the graph exists
10
10
  #
11
- # @example
12
- # (create silent <graph>)
11
+ # [34] Create ::= 'CREATE' 'SILENT'? GraphRef
12
+ #
13
+ # @example SPARQL Grammar
14
+ # CREATE SILENT GRAPH <http://example.org/g1>
15
+ #
16
+ # @example SSE
17
+ # (update (create silent <http://example.org/g1>))
13
18
  #
14
19
  # @see https://www.w3.org/TR/sparql11-update/#create
15
20
  class Create < Operator
@@ -43,6 +48,19 @@ module SPARQL; module Algebra
43
48
  end
44
49
  queryable
45
50
  end
51
+
52
+ ##
53
+ #
54
+ # Returns a partial SPARQL grammar for this operator.
55
+ #
56
+ # @return [String]
57
+ def 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)
63
+ end
46
64
  end # Create
47
65
  end # Operator
48
66
  end; end # SPARQL::Algebra