sparql 3.1.8 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -22
  3. data/VERSION +1 -1
  4. data/bin/sparql +13 -4
  5. data/lib/sparql/algebra/extensions.rb +93 -33
  6. data/lib/sparql/algebra/operator/abs.rb +22 -2
  7. data/lib/sparql/algebra/operator/add.rb +21 -2
  8. data/lib/sparql/algebra/operator/alt.rb +26 -2
  9. data/lib/sparql/algebra/operator/and.rb +25 -3
  10. data/lib/sparql/algebra/operator/asc.rb +20 -1
  11. data/lib/sparql/algebra/operator/ask.rb +17 -1
  12. data/lib/sparql/algebra/operator/avg.rb +17 -1
  13. data/lib/sparql/algebra/operator/base.rb +18 -1
  14. data/lib/sparql/algebra/operator/bgp.rb +5 -1
  15. data/lib/sparql/algebra/operator/bnode.rb +33 -10
  16. data/lib/sparql/algebra/operator/bound.rb +22 -1
  17. data/lib/sparql/algebra/operator/ceil.rb +25 -2
  18. data/lib/sparql/algebra/operator/clear.rb +16 -2
  19. data/lib/sparql/algebra/operator/coalesce.rb +33 -11
  20. data/lib/sparql/algebra/operator/compare.rb +9 -0
  21. data/lib/sparql/algebra/operator/concat.rb +26 -2
  22. data/lib/sparql/algebra/operator/construct.rb +29 -6
  23. data/lib/sparql/algebra/operator/contains.rb +24 -2
  24. data/lib/sparql/algebra/operator/copy.rb +19 -2
  25. data/lib/sparql/algebra/operator/count.rb +17 -1
  26. data/lib/sparql/algebra/operator/create.rb +19 -2
  27. data/lib/sparql/algebra/operator/dataset.rb +17 -0
  28. data/lib/sparql/algebra/operator/datatype.rb +25 -6
  29. data/lib/sparql/algebra/operator/day.rb +23 -5
  30. data/lib/sparql/algebra/operator/delete.rb +27 -2
  31. data/lib/sparql/algebra/operator/delete_data.rb +23 -2
  32. data/lib/sparql/algebra/operator/delete_where.rb +24 -2
  33. data/lib/sparql/algebra/operator/desc.rb +20 -1
  34. data/lib/sparql/algebra/operator/describe.rb +27 -4
  35. data/lib/sparql/algebra/operator/distinct.rb +18 -1
  36. data/lib/sparql/algebra/operator/divide.rb +26 -2
  37. data/lib/sparql/algebra/operator/drop.rb +17 -2
  38. data/lib/sparql/algebra/operator/encode_for_uri.rb +24 -2
  39. data/lib/sparql/algebra/operator/equal.rb +12 -2
  40. data/lib/sparql/algebra/operator/exists.rb +28 -4
  41. data/lib/sparql/algebra/operator/exprlist.rb +12 -1
  42. data/lib/sparql/algebra/operator/extend.rb +31 -6
  43. data/lib/sparql/algebra/operator/filter.rb +27 -5
  44. data/lib/sparql/algebra/operator/floor.rb +25 -2
  45. data/lib/sparql/algebra/operator/graph.rb +13 -0
  46. data/lib/sparql/algebra/operator/greater_than.rb +12 -3
  47. data/lib/sparql/algebra/operator/greater_than_or_equal.rb +12 -2
  48. data/lib/sparql/algebra/operator/group.rb +34 -8
  49. data/lib/sparql/algebra/operator/group_concat.rb +19 -7
  50. data/lib/sparql/algebra/operator/hours.rb +23 -5
  51. data/lib/sparql/algebra/operator/if.rb +21 -4
  52. data/lib/sparql/algebra/operator/in.rb +18 -1
  53. data/lib/sparql/algebra/operator/insert.rb +22 -2
  54. data/lib/sparql/algebra/operator/insert_data.rb +23 -2
  55. data/lib/sparql/algebra/operator/iri.rb +21 -4
  56. data/lib/sparql/algebra/operator/is_blank.rb +19 -1
  57. data/lib/sparql/algebra/operator/is_iri.rb +19 -1
  58. data/lib/sparql/algebra/operator/is_literal.rb +19 -1
  59. data/lib/sparql/algebra/operator/is_numeric.rb +21 -3
  60. data/lib/sparql/algebra/operator/is_triple.rb +32 -0
  61. data/lib/sparql/algebra/operator/join.rb +22 -1
  62. data/lib/sparql/algebra/operator/lang.rb +25 -0
  63. data/lib/sparql/algebra/operator/lang_matches.rb +22 -1
  64. data/lib/sparql/algebra/operator/lcase.rb +23 -2
  65. data/lib/sparql/algebra/operator/left_join.rb +29 -1
  66. data/lib/sparql/algebra/operator/less_than.rb +12 -3
  67. data/lib/sparql/algebra/operator/less_than_or_equal.rb +12 -2
  68. data/lib/sparql/algebra/operator/load.rb +25 -2
  69. data/lib/sparql/algebra/operator/max.rb +17 -1
  70. data/lib/sparql/algebra/operator/md5.rb +22 -5
  71. data/lib/sparql/algebra/operator/min.rb +18 -2
  72. data/lib/sparql/algebra/operator/minus.rb +25 -7
  73. data/lib/sparql/algebra/operator/minutes.rb +23 -5
  74. data/lib/sparql/algebra/operator/modify.rb +41 -5
  75. data/lib/sparql/algebra/operator/month.rb +23 -5
  76. data/lib/sparql/algebra/operator/move.rb +20 -2
  77. data/lib/sparql/algebra/operator/multiply.rb +26 -3
  78. data/lib/sparql/algebra/operator/negate.rb +23 -3
  79. data/lib/sparql/algebra/operator/not.rb +24 -3
  80. data/lib/sparql/algebra/operator/not_equal.rb +13 -0
  81. data/lib/sparql/algebra/operator/notexists.rb +30 -6
  82. data/lib/sparql/algebra/operator/notin.rb +20 -3
  83. data/lib/sparql/algebra/operator/notoneof.rb +12 -2
  84. data/lib/sparql/algebra/operator/now.rb +24 -5
  85. data/lib/sparql/algebra/operator/object.rb +32 -0
  86. data/lib/sparql/algebra/operator/or.rb +26 -3
  87. data/lib/sparql/algebra/operator/order.rb +20 -1
  88. data/lib/sparql/algebra/operator/path.rb +29 -2
  89. data/lib/sparql/algebra/operator/path_opt.rb +21 -2
  90. data/lib/sparql/algebra/operator/path_plus.rb +21 -2
  91. data/lib/sparql/algebra/operator/path_star.rb +20 -2
  92. data/lib/sparql/algebra/operator/plus.rb +42 -3
  93. data/lib/sparql/algebra/operator/predicate.rb +32 -0
  94. data/lib/sparql/algebra/operator/prefix.rb +24 -3
  95. data/lib/sparql/algebra/operator/project.rb +51 -5
  96. data/lib/sparql/algebra/operator/rand.rb +30 -2
  97. data/lib/sparql/algebra/operator/reduced.rb +18 -1
  98. data/lib/sparql/algebra/operator/regex.rb +26 -18
  99. data/lib/sparql/algebra/operator/replace.rb +26 -6
  100. data/lib/sparql/algebra/operator/reverse.rb +20 -2
  101. data/lib/sparql/algebra/operator/round.rb +25 -2
  102. data/lib/sparql/algebra/operator/same_term.rb +24 -6
  103. data/lib/sparql/algebra/operator/sample.rb +30 -8
  104. data/lib/sparql/algebra/operator/seconds.rb +23 -5
  105. data/lib/sparql/algebra/operator/seq.rb +20 -2
  106. data/lib/sparql/algebra/operator/sequence.rb +0 -10
  107. data/lib/sparql/algebra/operator/sha1.rb +18 -1
  108. data/lib/sparql/algebra/operator/sha256.rb +18 -1
  109. data/lib/sparql/algebra/operator/sha384.rb +18 -1
  110. data/lib/sparql/algebra/operator/sha512.rb +18 -1
  111. data/lib/sparql/algebra/operator/slice.rb +27 -5
  112. data/lib/sparql/algebra/operator/str.rb +21 -1
  113. data/lib/sparql/algebra/operator/strafter.rb +25 -2
  114. data/lib/sparql/algebra/operator/strbefore.rb +25 -2
  115. data/lib/sparql/algebra/operator/strdt.rb +22 -1
  116. data/lib/sparql/algebra/operator/strends.rb +25 -3
  117. data/lib/sparql/algebra/operator/strlang.rb +25 -6
  118. data/lib/sparql/algebra/operator/strlen.rb +23 -2
  119. data/lib/sparql/algebra/operator/strstarts.rb +25 -2
  120. data/lib/sparql/algebra/operator/struuid.rb +29 -9
  121. data/lib/sparql/algebra/operator/subject.rb +32 -0
  122. data/lib/sparql/algebra/operator/substr.rb +23 -2
  123. data/lib/sparql/algebra/operator/subtract.rb +28 -2
  124. data/lib/sparql/algebra/operator/sum.rb +17 -1
  125. data/lib/sparql/algebra/operator/table.rb +42 -4
  126. data/lib/sparql/algebra/operator/timezone.rb +23 -5
  127. data/lib/sparql/algebra/operator/tz.rb +22 -5
  128. data/lib/sparql/algebra/operator/ucase.rb +23 -2
  129. data/lib/sparql/algebra/operator/union.rb +29 -6
  130. data/lib/sparql/algebra/operator/update.rb +25 -4
  131. data/lib/sparql/algebra/operator/using.rb +32 -2
  132. data/lib/sparql/algebra/operator/uuid.rb +27 -8
  133. data/lib/sparql/algebra/operator/with.rb +38 -4
  134. data/lib/sparql/algebra/operator/year.rb +23 -5
  135. data/lib/sparql/algebra/operator.rb +99 -6
  136. data/lib/sparql/algebra/sxp_extensions.rb +3 -3
  137. data/lib/sparql/grammar/parser11.rb +4 -4
  138. metadata +22 -35
  139. data/lib/sparql/algebra/operator/triple.rb +0 -27
@@ -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,13 @@ 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
12
+ # CLEAR SILENT DEFAULT
13
+ #
14
+ # @example SSE
15
+ # (update (clear silent default))
11
16
  #
12
17
  # @see https://www.w3.org/TR/sparql11-update/#clear
13
18
  class Clear < Operator
@@ -58,6 +63,15 @@ module SPARQL; module Algebra
58
63
 
59
64
  queryable
60
65
  end
66
+
67
+ ##
68
+ #
69
+ # Returns a partial SPARQL grammar for this operator.
70
+ #
71
+ # @return [String]
72
+ def to_sparql(**options)
73
+ "CLEAR " + operands.to_sparql(**options)
74
+ end
61
75
  end # Clear
62
76
  end # Operator
63
77
  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(as_statement: true, 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,7 +3,14 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `count` set function.
5
5
  #
6
- # @example
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
7
14
  # (prefix ((: <http://www.example.org>))
8
15
  # (project (?C)
9
16
  # (extend ((?C ??.0))
@@ -25,6 +32,15 @@ module SPARQL; module Algebra
25
32
  def apply(enum, **options)
26
33
  RDF::Literal(enum.length)
27
34
  end
35
+
36
+ ##
37
+ #
38
+ # Returns a partial SPARQL grammar for this operator.
39
+ #
40
+ # @return [String]
41
+ def to_sparql(**options)
42
+ "COUNT(#{operands.to_sparql(**options)})"
43
+ end
28
44
  end # Count
29
45
  end # Operator
30
46
  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,18 @@ 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
+ *args, last = operands.dup
59
+ args += [:GRAPH, last]
60
+
61
+ "CREATE " + args.to_sparql(**options)
62
+ end
46
63
  end # Create
47
64
  end # Operator
48
65
  end; end # SPARQL::Algebra
@@ -158,6 +158,23 @@ module SPARQL; module Algebra
158
158
  aggregate.default(*default_datasets.select {|name| queryable.has_graph?(name)})
159
159
  aggregate.query(operands.last, depth: options[:depth].to_i + 1, **options, &base)
160
160
  end
161
+
162
+ ##
163
+ #
164
+ # Returns a partial SPARQL grammar for this operator.
165
+ #
166
+ # @return [String]
167
+ 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
177
+ end
161
178
  end # Dataset
162
179
  end # Operator
163
180
  end; end # SPARQL::Algebra
@@ -3,13 +3,23 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `datatype` operator.
5
5
  #
6
- # @example
6
+ # [121] BuiltInCall ::= ... | 'DATATYPE' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
10
+ # PREFIX : <http://example.org/things#>
11
+ # SELECT ?x ?v
12
+ # WHERE
13
+ # { ?x :p ?v .
14
+ # FILTER ( datatype(?v) = xsd:double ) .
15
+ # }
16
+ #
17
+ # @example SSE
7
18
  # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>)
8
- # (rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
9
- # (: <http://example.org/>))
10
- # (project (?s)
11
- # (filter (= (datatype (xsd:double ?v)) xsd:double)
12
- # (bgp (triple ?s :p ?v)))))
19
+ # (: <http://example.org/things#>))
20
+ # (project (?x ?v)
21
+ # (filter (= (datatype ?v) xsd:double)
22
+ # (bgp (triple ?x :p ?v)))))
13
23
  #
14
24
  # @see https://www.w3.org/TR/sparql11-query/#func-datatype
15
25
  class Datatype < Operator::Unary
@@ -33,6 +43,15 @@ module SPARQL; module Algebra
33
43
  else raise TypeError, "expected an RDF::Literal, but got #{literal.inspect}"
34
44
  end
35
45
  end
46
+
47
+ ##
48
+ #
49
+ # Returns a partial SPARQL grammar for this operator.
50
+ #
51
+ # @return [String]
52
+ def to_sparql(**options)
53
+ "DATATYPE(#{operands.last.to_sparql(**options)})"
54
+ end
36
55
  end # Datatype
37
56
  end # Operator
38
57
  end; end # SPARQL::Algebra
@@ -3,11 +3,20 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL logical `day` operator.
5
5
  #
6
- # @example
7
- # (prefix ((: <http://example.org/>))
8
- # (project (?s ?x)
9
- # (extend ((?x (day ?date)))
10
- # (bgp (triple ?s :date ?date)))))
6
+ # [121] BuiltInCall ::= ... | 'DAY' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # SELECT ?s (DAY(?date) AS ?x) WHERE {
11
+ # ?s :date ?date
12
+ # }
13
+ #
14
+ # @example SSE
15
+ # (prefix
16
+ # ((: <http://example.org/>))
17
+ # (project (?s ?x)
18
+ # (extend ((?x (day ?date)))
19
+ # (bgp (triple ?s :date ?date)))))
11
20
  #
12
21
  # @see https://www.w3.org/TR/sparql11-query/#func-day
13
22
  class Day < Operator::Unary
@@ -26,6 +35,15 @@ module SPARQL; module Algebra
26
35
  raise TypeError, "expected an RDF::Literal::DateTime, but got #{operand.inspect}" unless operand.is_a?(RDF::Literal::DateTime)
27
36
  RDF::Literal(operand.object.day)
28
37
  end
38
+
39
+ ##
40
+ #
41
+ # Returns a partial SPARQL grammar for this operator.
42
+ #
43
+ # @return [String]
44
+ def to_sparql(**options)
45
+ "DAY(#{operands.last.to_sparql(**options)})"
46
+ end
29
47
  end # Day
30
48
  end # Operator
31
49
  end; end # SPARQL::Algebra
@@ -6,8 +6,24 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The DELETE operation is a form of the DELETE/INSERT operation having no INSERT section
8
8
  #
9
- # @example
10
- # (delete ((triple ?s ?p ?o))))
9
+ # [42] DeleteClause ::= 'DELETE' QuadPattern
10
+ #
11
+ # @example SPARQL Grammar
12
+ # PREFIX : <http://example.org/>
13
+ # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
14
+ #
15
+ # DELETE { ?s ?p ?o }
16
+ # WHERE { :a foaf:knows ?s . ?s ?p ?o }
17
+ #
18
+ # @example SSE
19
+ # (prefix ((: <http://example.org/>)
20
+ # (foaf: <http://xmlns.com/foaf/0.1/>))
21
+ # (update
22
+ # (modify
23
+ # (bgp
24
+ # (triple :a foaf:knows ?s)
25
+ # (triple ?s ?p ?o))
26
+ # (delete ((triple ?s ?p ?o))))))
11
27
  #
12
28
  # @see https://www.w3.org/TR/sparql11-update/#delete
13
29
  class Delete < Operator::Unary
@@ -52,6 +68,15 @@ module SPARQL; module Algebra
52
68
  end
53
69
  queryable
54
70
  end
71
+
72
+ ##
73
+ #
74
+ # Returns a partial SPARQL grammar for this term.
75
+ #
76
+ # @return [String]
77
+ def to_sparql(**options)
78
+ "DELETE {\n" + operands.first.to_sparql(as_statement: true, **options) + "\n}"
79
+ end
55
80
  end # Delete
56
81
  end # Operator
57
82
  end; end # SPARQL::Algebra
@@ -6,8 +6,18 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The DELETE DATA operation removes some triples, given inline in the request, if the respective graphs in the Graph Store contain those
8
8
  #
9
- # @example
10
- # (deleteData ((triple :a foaf:knows :c)))
9
+ # [39] DeleteData ::= 'DELETE DATA' QuadData
10
+ #
11
+ # @example SPARQL Grammar
12
+ # PREFIX : <http://example.org/>
13
+ # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
14
+ # DELETE DATA {
15
+ # :a foaf:knows :b .
16
+ # }
17
+ #
18
+ # @example SSE
19
+ # (prefix ((: <http://example.org/>) (foaf: <http://xmlns.com/foaf/0.1/>))
20
+ # (update (deleteData ((triple :a foaf:knows :b)))))
11
21
  #
12
22
  # @see https://www.w3.org/TR/sparql11-update/#deleteData
13
23
  class DeleteData < Operator::Unary
@@ -36,6 +46,17 @@ module SPARQL; module Algebra
36
46
  end
37
47
  queryable
38
48
  end
49
+
50
+ ##
51
+ #
52
+ # Returns a partial SPARQL grammar for this term.
53
+ #
54
+ # @return [String]
55
+ def to_sparql(**options)
56
+ "DELETE DATA {\n" +
57
+ operands.first.to_sparql(as_statement: true, top_level: false, delimiter: "\n", **options) +
58
+ "\n}"
59
+ end
39
60
  end # DeleteData
40
61
  end # Operator
41
62
  end; end # SPARQL::Algebra