sparql 3.1.5 → 3.2.0

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 (144) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -28
  3. data/VERSION +1 -1
  4. data/bin/sparql +14 -4
  5. data/lib/sparql/algebra/aggregate.rb +1 -1
  6. data/lib/sparql/algebra/evaluatable.rb +4 -4
  7. data/lib/sparql/algebra/expression.rb +4 -1
  8. data/lib/sparql/algebra/extensions.rb +96 -29
  9. data/lib/sparql/algebra/operator/abs.rb +23 -3
  10. data/lib/sparql/algebra/operator/add.rb +21 -2
  11. data/lib/sparql/algebra/operator/alt.rb +26 -2
  12. data/lib/sparql/algebra/operator/and.rb +25 -3
  13. data/lib/sparql/algebra/operator/asc.rb +20 -1
  14. data/lib/sparql/algebra/operator/ask.rb +17 -1
  15. data/lib/sparql/algebra/operator/avg.rb +18 -2
  16. data/lib/sparql/algebra/operator/base.rb +18 -1
  17. data/lib/sparql/algebra/operator/bgp.rb +5 -1
  18. data/lib/sparql/algebra/operator/bnode.rb +34 -11
  19. data/lib/sparql/algebra/operator/bound.rb +22 -1
  20. data/lib/sparql/algebra/operator/ceil.rb +26 -3
  21. data/lib/sparql/algebra/operator/clear.rb +16 -2
  22. data/lib/sparql/algebra/operator/coalesce.rb +33 -11
  23. data/lib/sparql/algebra/operator/compare.rb +48 -33
  24. data/lib/sparql/algebra/operator/concat.rb +26 -2
  25. data/lib/sparql/algebra/operator/construct.rb +31 -7
  26. data/lib/sparql/algebra/operator/contains.rb +25 -3
  27. data/lib/sparql/algebra/operator/copy.rb +19 -2
  28. data/lib/sparql/algebra/operator/count.rb +18 -2
  29. data/lib/sparql/algebra/operator/create.rb +19 -2
  30. data/lib/sparql/algebra/operator/dataset.rb +17 -0
  31. data/lib/sparql/algebra/operator/datatype.rb +26 -7
  32. data/lib/sparql/algebra/operator/day.rb +24 -6
  33. data/lib/sparql/algebra/operator/delete.rb +27 -2
  34. data/lib/sparql/algebra/operator/delete_data.rb +23 -2
  35. data/lib/sparql/algebra/operator/delete_where.rb +24 -2
  36. data/lib/sparql/algebra/operator/desc.rb +20 -1
  37. data/lib/sparql/algebra/operator/describe.rb +27 -4
  38. data/lib/sparql/algebra/operator/distinct.rb +18 -1
  39. data/lib/sparql/algebra/operator/divide.rb +27 -3
  40. data/lib/sparql/algebra/operator/drop.rb +17 -2
  41. data/lib/sparql/algebra/operator/encode_for_uri.rb +25 -3
  42. data/lib/sparql/algebra/operator/equal.rb +13 -3
  43. data/lib/sparql/algebra/operator/exists.rb +28 -4
  44. data/lib/sparql/algebra/operator/exprlist.rb +12 -1
  45. data/lib/sparql/algebra/operator/extend.rb +33 -18
  46. data/lib/sparql/algebra/operator/filter.rb +27 -5
  47. data/lib/sparql/algebra/operator/floor.rb +26 -3
  48. data/lib/sparql/algebra/operator/graph.rb +13 -0
  49. data/lib/sparql/algebra/operator/greater_than.rb +14 -4
  50. data/lib/sparql/algebra/operator/greater_than_or_equal.rb +14 -4
  51. data/lib/sparql/algebra/operator/group.rb +34 -8
  52. data/lib/sparql/algebra/operator/group_concat.rb +20 -8
  53. data/lib/sparql/algebra/operator/hours.rb +24 -6
  54. data/lib/sparql/algebra/operator/if.rb +21 -4
  55. data/lib/sparql/algebra/operator/in.rb +18 -1
  56. data/lib/sparql/algebra/operator/insert.rb +22 -2
  57. data/lib/sparql/algebra/operator/insert_data.rb +23 -2
  58. data/lib/sparql/algebra/operator/iri.rb +22 -5
  59. data/lib/sparql/algebra/operator/is_blank.rb +20 -2
  60. data/lib/sparql/algebra/operator/is_iri.rb +20 -2
  61. data/lib/sparql/algebra/operator/is_literal.rb +20 -2
  62. data/lib/sparql/algebra/operator/is_numeric.rb +22 -4
  63. data/lib/sparql/algebra/operator/is_triple.rb +62 -0
  64. data/lib/sparql/algebra/operator/join.rb +22 -1
  65. data/lib/sparql/algebra/operator/lang.rb +26 -1
  66. data/lib/sparql/algebra/operator/lang_matches.rb +23 -2
  67. data/lib/sparql/algebra/operator/lcase.rb +24 -3
  68. data/lib/sparql/algebra/operator/left_join.rb +29 -1
  69. data/lib/sparql/algebra/operator/less_than.rb +14 -4
  70. data/lib/sparql/algebra/operator/less_than_or_equal.rb +14 -4
  71. data/lib/sparql/algebra/operator/load.rb +25 -2
  72. data/lib/sparql/algebra/operator/max.rb +18 -2
  73. data/lib/sparql/algebra/operator/md5.rb +23 -6
  74. data/lib/sparql/algebra/operator/min.rb +19 -3
  75. data/lib/sparql/algebra/operator/minus.rb +25 -7
  76. data/lib/sparql/algebra/operator/minutes.rb +24 -6
  77. data/lib/sparql/algebra/operator/modify.rb +41 -5
  78. data/lib/sparql/algebra/operator/month.rb +24 -6
  79. data/lib/sparql/algebra/operator/move.rb +20 -2
  80. data/lib/sparql/algebra/operator/multiply.rb +27 -4
  81. data/lib/sparql/algebra/operator/negate.rb +24 -4
  82. data/lib/sparql/algebra/operator/not.rb +25 -4
  83. data/lib/sparql/algebra/operator/not_equal.rb +16 -1
  84. data/lib/sparql/algebra/operator/notexists.rb +30 -6
  85. data/lib/sparql/algebra/operator/notin.rb +20 -3
  86. data/lib/sparql/algebra/operator/notoneof.rb +12 -2
  87. data/lib/sparql/algebra/operator/now.rb +25 -6
  88. data/lib/sparql/algebra/operator/object.rb +59 -0
  89. data/lib/sparql/algebra/operator/or.rb +26 -3
  90. data/lib/sparql/algebra/operator/order.rb +27 -2
  91. data/lib/sparql/algebra/operator/path.rb +29 -2
  92. data/lib/sparql/algebra/operator/path_opt.rb +21 -2
  93. data/lib/sparql/algebra/operator/path_plus.rb +21 -2
  94. data/lib/sparql/algebra/operator/path_star.rb +20 -2
  95. data/lib/sparql/algebra/operator/plus.rb +43 -4
  96. data/lib/sparql/algebra/operator/predicate.rb +59 -0
  97. data/lib/sparql/algebra/operator/prefix.rb +24 -3
  98. data/lib/sparql/algebra/operator/project.rb +51 -5
  99. data/lib/sparql/algebra/operator/rand.rb +31 -3
  100. data/lib/sparql/algebra/operator/reduced.rb +18 -1
  101. data/lib/sparql/algebra/operator/regex.rb +27 -19
  102. data/lib/sparql/algebra/operator/replace.rb +27 -7
  103. data/lib/sparql/algebra/operator/reverse.rb +20 -2
  104. data/lib/sparql/algebra/operator/round.rb +26 -3
  105. data/lib/sparql/algebra/operator/same_term.rb +25 -7
  106. data/lib/sparql/algebra/operator/sample.rb +31 -9
  107. data/lib/sparql/algebra/operator/seconds.rb +24 -6
  108. data/lib/sparql/algebra/operator/seq.rb +20 -2
  109. data/lib/sparql/algebra/operator/sequence.rb +0 -10
  110. data/lib/sparql/algebra/operator/sha1.rb +19 -2
  111. data/lib/sparql/algebra/operator/sha256.rb +19 -2
  112. data/lib/sparql/algebra/operator/sha384.rb +19 -2
  113. data/lib/sparql/algebra/operator/sha512.rb +19 -2
  114. data/lib/sparql/algebra/operator/slice.rb +27 -5
  115. data/lib/sparql/algebra/operator/str.rb +22 -2
  116. data/lib/sparql/algebra/operator/strafter.rb +26 -3
  117. data/lib/sparql/algebra/operator/strbefore.rb +26 -3
  118. data/lib/sparql/algebra/operator/strdt.rb +23 -2
  119. data/lib/sparql/algebra/operator/strends.rb +26 -4
  120. data/lib/sparql/algebra/operator/strlang.rb +26 -7
  121. data/lib/sparql/algebra/operator/strlen.rb +24 -3
  122. data/lib/sparql/algebra/operator/strstarts.rb +26 -3
  123. data/lib/sparql/algebra/operator/struuid.rb +30 -10
  124. data/lib/sparql/algebra/operator/subject.rb +61 -0
  125. data/lib/sparql/algebra/operator/substr.rb +24 -3
  126. data/lib/sparql/algebra/operator/subtract.rb +29 -3
  127. data/lib/sparql/algebra/operator/sum.rb +18 -2
  128. data/lib/sparql/algebra/operator/table.rb +42 -4
  129. data/lib/sparql/algebra/operator/timezone.rb +24 -6
  130. data/lib/sparql/algebra/operator/tz.rb +23 -6
  131. data/lib/sparql/algebra/operator/ucase.rb +24 -3
  132. data/lib/sparql/algebra/operator/union.rb +29 -6
  133. data/lib/sparql/algebra/operator/update.rb +25 -4
  134. data/lib/sparql/algebra/operator/using.rb +32 -2
  135. data/lib/sparql/algebra/operator/uuid.rb +28 -9
  136. data/lib/sparql/algebra/operator/with.rb +38 -4
  137. data/lib/sparql/algebra/operator/year.rb +24 -6
  138. data/lib/sparql/algebra/operator.rb +112 -7
  139. data/lib/sparql/algebra/sxp_extensions.rb +3 -3
  140. data/lib/sparql/grammar/meta.rb +5390 -2410
  141. data/lib/sparql/grammar/parser11.rb +119 -53
  142. data/lib/sparql/grammar/terminals11.rb +2 -0
  143. data/lib/sparql/grammar.rb +0 -25
  144. metadata +40 -34
@@ -5,11 +5,19 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # Extends a solution
7
7
  #
8
- # @example
9
- # (select (?z)
10
- # (project (?z)
11
- # (extend ((?z (+ ?o 10)))
12
- # (bgp (triple ?s <http://example/p> ?o)))))
8
+ # [60] Bind ::= 'BIND' '(' Expression 'AS' Var ')'
9
+ #
10
+ # @example SPARQL Grammar
11
+ # SELECT ?z
12
+ # {
13
+ # ?x <http://example/p> ?o
14
+ # BIND(?o+1 AS ?z)
15
+ # }
16
+ #
17
+ # @example SSE
18
+ # (project (?z)
19
+ # (extend ((?z (+ ?o 10)))
20
+ # (bgp (triple ?s <http://example/p> ?o))))
13
21
  #
14
22
  # @see https://www.w3.org/TR/sparql11-query/#evaluation
15
23
  class Extend < Operator::Binary
@@ -28,12 +36,6 @@ module SPARQL; module Algebra
28
36
  #
29
37
  # Extend(Ω, var, expr) = { Extend(μ, var, expr) | μ in Ω }
30
38
  #
31
- # For SPARQL*, expr may be an embedded tiple pattern
32
- #
33
- # (extend
34
- # ((?t (triple ?bob foaf:age ?age)))
35
- # (bgp (triple ?t dct:source ?src)))
36
- #
37
39
  # @param [RDF::Queryable] queryable
38
40
  # the graph or repository to query
39
41
  # @param [Hash{Symbol => Object}] options
@@ -56,12 +58,8 @@ module SPARQL; module Algebra
56
58
  depth: options[:depth].to_i + 1,
57
59
  **options)
58
60
  debug(options) {"===> + #{var} => #{val.inspect}"}
59
- solution.bindings[var.to_sym] = if val.is_a?(RDF::Query::Pattern)
60
- # Variable is bound to the solution
61
- val.bind(solution)
62
- else
63
- val
64
- end
61
+ val = val.dup.bind(solution) if val.is_a?(RDF::Query::Pattern)
62
+ solution.bindings[var.to_sym] = val
65
63
  rescue TypeError => e
66
64
  # Evaluates to error, ignore
67
65
  debug(options) {"===> #{var} error: #{e.message}"}
@@ -83,6 +81,23 @@ module SPARQL; module Algebra
83
81
  end
84
82
  super
85
83
  end
86
- end # Filter
84
+
85
+ ##
86
+ #
87
+ # Returns a partial SPARQL grammar for this operator.
88
+ #
89
+ # Extracts bindings.
90
+ #
91
+ # @return [String]
92
+ def to_sparql(**options)
93
+ extensions = operands.first.inject({}) do |memo, (as, expression)|
94
+ memo.merge(as => expression)
95
+ end
96
+
97
+ # Merge any inherited extensions from options
98
+ extensions = options.delete(:extensions).merge(extensions) if options.key?(:extensions)
99
+ operands.last.to_sparql(extensions: extensions, **options)
100
+ end
101
+ end # Extend
87
102
  end # Operator
88
103
  end; end # SPARQL::Algebra
@@ -3,11 +3,19 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL GraphPattern `filter` operator.
5
5
  #
6
- # @example
7
- # (select (?v)
8
- # (project (?v)
9
- # (filter (= ?v 2)
10
- # (bgp (triple ?s <http://example/p> ?v)))))
6
+ # [68] Filter ::= 'FILTER' Constraint
7
+ #
8
+ # @example SPARQL Grammar
9
+ # SELECT ?v
10
+ # {
11
+ # ?s <http://example/p> ?v
12
+ # FILTER(?v = 2)
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (project (?v)
17
+ # (filter (= ?v 2)
18
+ # (bgp (triple ?s <http://example/p> ?v))))
11
19
  #
12
20
  # @see https://www.w3.org/TR/sparql11-query/#evaluation
13
21
  class Filter < Operator::Binary
@@ -69,6 +77,20 @@ module SPARQL; module Algebra
69
77
  end
70
78
  self
71
79
  end
80
+
81
+ ##
82
+ #
83
+ # Returns a partial SPARQL grammar for this operator.
84
+ #
85
+ # Provides filters to descendant query.
86
+ #
87
+ # If filter operation is an Exprlist, then separate into multiple filter ops.
88
+ #
89
+ # @return [String]
90
+ def to_sparql(**options)
91
+ filter_ops = operands.first.is_a?(Operator::Exprlist) ? operands.first.operands : [operands.first]
92
+ operands.last.to_sparql(filter_ops: filter_ops, **options)
93
+ end
72
94
  end # Filter
73
95
  end # Operator
74
96
  end; end # SPARQL::Algebra
@@ -3,8 +3,22 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL logical `floor` operator.
5
5
  #
6
- # @example
7
- # (floor ?x)
6
+ # [121] BuiltInCall ::= ... 'FLOOR' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
11
+ # SELECT ?s ?num (FLOOR(?num) AS ?floor) 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 ?floor)
20
+ # (extend ((?floor (floor ?num)))
21
+ # (bgp (triple ?s :num ?num)))))
8
22
  #
9
23
  # @see https://www.w3.org/TR/sparql11-query/#func-floor
10
24
  # @see https://www.w3.org/TR/xpath-functions/#func-floor
@@ -24,12 +38,21 @@ module SPARQL; module Algebra
24
38
  # the operand
25
39
  # @return [RDF::Literal] literal of same type
26
40
  # @raise [TypeError] if the operand is not a numeric value
27
- def apply(operand)
41
+ def apply(operand, **options)
28
42
  case operand
29
43
  when RDF::Literal::Numeric then operand.floor
30
44
  else raise TypeError, "expected an RDF::Literal::Numeric, but got #{operand.inspect}"
31
45
  end
32
46
  end
47
+
48
+ ##
49
+ #
50
+ # Returns a partial SPARQL grammar for this operator.
51
+ #
52
+ # @return [String]
53
+ def to_sparql(**options)
54
+ "FLOOR(#{operands.to_sparql(**options)})"
55
+ end
33
56
  end # Floor
34
57
  end # Operator
35
58
  end; end # SPARQL::Algebra
@@ -5,6 +5,19 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # This is a wrapper to add a `graph_name` to the query, or an array of statements.
7
7
  #
8
+ # [58] GraphGraphPattern ::= 'GRAPH' VarOrIri GroupGraphPattern
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example/>
12
+ # SELECT * {
13
+ # GRAPH ?g { ?s ?p ?o }
14
+ # }
15
+ #
16
+ # @example SSE
17
+ # (prefix ((: <http://example/>))
18
+ # (graph ?g
19
+ # (bgp (triple ?s ?p ?o))))
20
+ #
8
21
  # @example of a query
9
22
  # (prefix ((: <http://example/>))
10
23
  # (graph ?g
@@ -3,8 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL relational `>` (greater 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
@@ -24,8 +34,8 @@ module SPARQL; module Algebra
24
34
  # a literal
25
35
  # @return [RDF::Literal::Boolean] `true` or `false`
26
36
  # @raise [TypeError] if either operand is not a literal
27
- def apply(left, right)
28
- super
37
+ def apply(left, right, **options)
38
+ RDF::Literal(super == RDF::Literal(1))
29
39
  end
30
40
  end # GreaterThan
31
41
  end # Operator
@@ -4,8 +4,18 @@ module SPARQL; module Algebra
4
4
  # The SPARQL relational `>=` (greater than or equal) comparison
5
5
  # operator.
6
6
  #
7
- # @example
8
- # (>= ?x ?y)
7
+ # [114] RelationalExpression ::= NumericExpression ('>=' NumericExpression)?
8
+ #
9
+ # @example SPARQL Grammar
10
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
11
+ # PREFIX : <http://example.org/things#>
12
+ # SELECT ?x
13
+ # WHERE { ?x :p ?v . FILTER ( ?v >= 1 ) }
14
+ #
15
+ # @example SSE
16
+ # (prefix
17
+ # ((xsd: <http://www.w3.org/2001/XMLSchema#>) (: <http://example.org/things#>))
18
+ # (project (?x) (filter (>= ?v 1) (bgp (triple ?x :p ?v)))))
9
19
  #
10
20
  # @see https://www.w3.org/TR/sparql11-query/#OperatorMapping
11
21
  # @see https://www.w3.org/TR/xpath-functions/#func-compare
@@ -25,8 +35,8 @@ module SPARQL; module Algebra
25
35
  # a literal
26
36
  # @return [RDF::Literal::Boolean] `true` or `false`
27
37
  # @raise [TypeError] if either operand is not a literal
28
- def apply(left, right)
29
- super
38
+ def apply(left, right, **options)
39
+ RDF::Literal(super >= RDF::Literal(0))
30
40
  end
31
41
  end # GreaterThanOrEqual
32
42
  end # Operator
@@ -8,14 +8,22 @@ module SPARQL; module Algebra
8
8
  # query to be grouped. If three operands are provided,
9
9
  # the second is an array of temporary bindings.
10
10
  #
11
- # @example
12
- # (prefix ((: <http://example/>))
13
- # (project (?w ?S)
14
- # (extend ((?S ??.0))
15
- # (group (?w) ((??.0 (sample ?v)))
16
- # (leftjoin
17
- # (bgp (triple ?s :p ?v))
18
- # (bgp (triple ?s :q ?w)))))))
11
+ # [19] GroupClause ::= 'GROUP' 'BY' GroupCondition+
12
+ #
13
+ # @example SPARQL Grammar
14
+ # PREFIX : <http://www.example.org>
15
+ #
16
+ # SELECT ?P (COUNT(?O) AS ?C)
17
+ # WHERE { ?S ?P ?O }
18
+ # GROUP BY ?P
19
+ #
20
+ # @example SSE
21
+ # (prefix
22
+ # ((: <http://www.example.org>))
23
+ # (project (?P ?C)
24
+ # (extend ((?C ??.0))
25
+ # (group (?P) ((??.0 (count ?O)))
26
+ # (bgp (triple ?S ?P ?O))))))
19
27
  #
20
28
  # @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
21
29
  class Group < Operator
@@ -122,6 +130,24 @@ module SPARQL; module Algebra
122
130
  end
123
131
  super
124
132
  end
133
+
134
+ ##
135
+ #
136
+ # Returns a partial SPARQL grammar for this operator.
137
+ #
138
+ # @param [Hash{Symbol => Operator}] extensions
139
+ # Variable bindings
140
+ # @return [String]
141
+ def to_sparql(extensions: {}, **options)
142
+ if operands.length > 2
143
+ # Replace extensions from temporary bindings
144
+ operands[1].each do |var, op|
145
+ ext_var = extensions.invert.fetch(var)
146
+ extensions[ext_var] = op
147
+ end
148
+ end
149
+ operands.last.to_sparql(extensions: extensions, group_ops: operands.first, **options)
150
+ end
125
151
  end # Group
126
152
  end # Operator
127
153
  end; end # SPARQL::Algebra
@@ -5,13 +5,16 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # GroupConcat is a set function which performs a string concatenation across the values of an expression with a group. The order of the strings is not specified. The separator character used in the concatenation may be given with the scalar argument SEPARATOR.
7
7
  #
8
- # @example
9
- # (prefix ((: <http://www.example.org/>))
10
- # (filter (|| (= ?g "1 22") (= ?g "22 1"))
11
- # (project (?g)
12
- # (extend ((?g ??.0))
13
- # (group () ((??.0 (group_concat ?o)))
14
- # (bgp (triple ??.0 :p1 ?o)))))))
8
+ # [127] Aggregate::= ... | 'GROUP_CONCAT' '(' 'DISTINCT'? Expression ( ';' 'SEPARATOR' '=' String )? ')'
9
+ #
10
+ # @example SPARQL Grammar
11
+ # SELECT (GROUP_CONCAT(?x) AS ?y) {}
12
+ #
13
+ # @example SSE
14
+ # (project (?y)
15
+ # (extend ((?y ??.0))
16
+ # (group () ((??.0 (group_concat ?x)))
17
+ # (bgp))))
15
18
  #
16
19
  # @see https://www.w3.org/TR/sparql11-query/#defn_aggGroupConcat
17
20
  class GroupConcat < Operator
@@ -50,9 +53,18 @@ module SPARQL; module Algebra
50
53
  # enum of evaluated operand
51
54
  # @return [RDF::Term] An arbitrary term
52
55
  # @raise [TypeError] If enum is empty
53
- def apply(enum, separator)
56
+ def apply(enum, separator, **options)
54
57
  RDF::Literal(enum.flatten.map(&:to_s).join(separator.to_s))
55
58
  end
59
+
60
+ ##
61
+ #
62
+ # Returns a partial SPARQL grammar for this operator.
63
+ #
64
+ # @return [String]
65
+ def to_sparql(**options)
66
+ "GROUP_CONCAT(#{operands.to_sparql(delimiter: ', ', **options)})"
67
+ end
56
68
  end # GroupConcat
57
69
  end # Operator
58
70
  end; end # SPARQL::Algebra
@@ -3,11 +3,20 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL logical `hours` operator.
5
5
  #
6
- # @example
7
- # (prefix ((: <http://example.org/>))
8
- # (project (?s ?x)
9
- # (extend ((?x (hours ?date)))
10
- # (bgp (triple ?s :date ?date)))))
6
+ # [121] BuiltInCall ::= ... | 'HOURS' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/>
10
+ # SELECT ?s (HOURS(?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 (hours ?date)))
19
+ # (bgp (triple ?s :date ?date)))))
11
20
  #
12
21
  # @see https://www.w3.org/TR/sparql11-query/#func-hours
13
22
  class Hours < Operator::Unary
@@ -22,10 +31,19 @@ module SPARQL; module Algebra
22
31
  # the operand
23
32
  # @return [RDF::Literal]
24
33
  # @raise [TypeError] if the operand is not a simple literal
25
- def apply(operand)
34
+ def apply(operand, **options)
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.hour)
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
+ "HOURS(#{operands.last.to_sparql(**options)})"
46
+ end
29
47
  end # Hours
30
48
  end # Operator
31
49
  end; end # SPARQL::Algebra
@@ -3,7 +3,15 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `if` function.
5
5
  #
6
- # @example
6
+ # [121] BuiltInCall ::= ... | 'IF' '(' Expression ',' Expression ',' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # BASE <http://example.org/>
10
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
11
+ # SELECT ?o (IF(lang(?o) = "ja", true, false) AS ?integer)
12
+ # WHERE { ?s ?p ?o }
13
+ #
14
+ # @example SSE
7
15
  # (base <http://example.org/>
8
16
  # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>))
9
17
  # (project (?o ?integer)
@@ -37,9 +45,18 @@ module SPARQL; module Algebra
37
45
  operand(0).evaluate(bindings, depth: options[:depth].to_i + 1, **options) == RDF::Literal::TRUE ?
38
46
  operand(1).evaluate(bindings, depth: options[:depth].to_i + 1, **options) :
39
47
  operand(2).evaluate(bindings, depth: options[:depth].to_i + 1, **options)
40
- rescue
41
- raise TypeError
48
+ rescue
49
+ raise TypeError
42
50
  end
43
51
  end # If
44
- end # Operator
52
+
53
+ ##
54
+ #
55
+ # Returns a partial SPARQL grammar for this operator.
56
+ #
57
+ # @return [String]
58
+ def to_sparql(**options)
59
+ "IF(" + operands.to_sparql(delimiter: ', ', **options) + ")"
60
+ end
61
+ end # If
45
62
  end; end # SPARQL::Algebra
@@ -3,7 +3,12 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL GraphPattern `in` operator.
5
5
  #
6
- # @example
6
+ # [114] RelationalExpression ::= NumericExpression ('IN' ExpressionList)?
7
+ #
8
+ # @example SPARQL Grammar
9
+ # ASK { FILTER(2 IN (1, 2, 3)) }
10
+ #
11
+ # @example SSE
7
12
  # (ask (filter (in 2 1 2 3) (bgp)))
8
13
  #
9
14
  # @see https://www.w3.org/TR/sparql11-query/#func-in
@@ -54,6 +59,18 @@ module SPARQL; module Algebra
54
59
  else RDF::Literal::FALSE
55
60
  end
56
61
  end
62
+
63
+ ##
64
+ #
65
+ # Returns a partial SPARQL grammar for this operator.
66
+ #
67
+ # @return [String]
68
+ def to_sparql(**options)
69
+ "(" + operands.first.to_sparql(**options) +
70
+ " IN (" +
71
+ operands[1..-1].map {|e| e.to_sparql(**options)}.join(", ") +
72
+ "))"
73
+ end
57
74
  end # In
58
75
  end # Operator
59
76
  end; end # SPARQL::Algebra
@@ -6,8 +6,19 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The INSERT operation is a form of the DELETE/INSERT operation having no DELETE section
8
8
  #
9
- # @example
10
- # (insert ((triple ?s ?p "q")))
9
+ # [43] InsertClause ::= 'INSERT' QuadPattern
10
+ #
11
+ # @example SPARQL Grammar
12
+ # PREFIX : <http://example.org/>
13
+ # INSERT { ?s ?p "q" }
14
+ # WHERE { ?s ?p ?o }
15
+ #
16
+ # @example SSE
17
+ # (prefix
18
+ # ((: <http://example.org/>))
19
+ # (update
20
+ # (modify (bgp (triple ?s ?p ?o))
21
+ # (insert ((triple ?s ?p "q"))))))
11
22
  #
12
23
  # @see https://www.w3.org/TR/sparql11-update/#insert
13
24
  class Insert < Operator::Unary
@@ -51,6 +62,15 @@ module SPARQL; module Algebra
51
62
  end
52
63
  queryable
53
64
  end
65
+
66
+ ##
67
+ #
68
+ # Returns a partial SPARQL grammar for this term.
69
+ #
70
+ # @return [String]
71
+ def to_sparql(**options)
72
+ "INSERT {\n" + operands.first.to_sparql(as_statement: true, **options) + "\n}"
73
+ end
54
74
  end # Insert
55
75
  end # Operator
56
76
  end; end # SPARQL::Algebra
@@ -6,8 +6,18 @@ module SPARQL; module Algebra
6
6
  #
7
7
  # The INSERT DATA operation adds some triples, given inline in the request, into the Graph Store
8
8
  #
9
- # @example
10
- # (insertData ((graph <http://example.org/g1> ((triple :s :p :o)))))
9
+ # [38] InsertData ::= 'INSERT DATA' QuadData
10
+ #
11
+ # @example SPARQL Grammar
12
+ # PREFIX : <http://example.org/ns#>
13
+ # INSERT DATA { GRAPH <http://example.org/g1> { :s :p :o } }
14
+ #
15
+ # @example SSE
16
+ # (prefix
17
+ # ((: <http://example.org/ns#>))
18
+ # (update
19
+ # (insertData (
20
+ # (graph <http://example.org/g1> ((triple :s :p :o)))))))
11
21
  #
12
22
  # @see https://www.w3.org/TR/sparql11-update/#insertData
13
23
  class InsertData < 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
+ "INSERT DATA {\n" +
57
+ operands.first.to_sparql(as_statement: true, top_level: false, delimiter: "\n", **options) +
58
+ "\n}"
59
+ end
39
60
  end # InsertData
40
61
  end # Operator
41
62
  end; end # SPARQL::Algebra
@@ -3,10 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `iri` operator.
5
5
  #
6
- # @example
7
- # (base <http://example.org/> (project (?uri ?iri)
8
- # (extend ((?uri (uri "uri")) (?iri (iri "iri")))
9
- # (bgp))))
6
+ # [121] BuiltInCall ::= ... | 'IRI' '(' Expression ')' | 'URI' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # BASE <http://example.org/>
10
+ # SELECT (URI("uri") AS ?uri) (IRI("iri") AS ?iri)
11
+ # WHERE {}
12
+ #
13
+ # @example SSE
14
+ # (base <http://example.org/>
15
+ # (project (?uri ?iri)
16
+ # (extend ((?uri (iri "uri")) (?iri (iri "iri")))
17
+ # (bgp))))
10
18
  #
11
19
  # @see https://www.w3.org/TR/sparql11-query/#func-iri
12
20
  class IRI < Operator::Unary
@@ -29,12 +37,21 @@ module SPARQL; module Algebra
29
37
  # a simple literal
30
38
  # @return [RDF::URI]
31
39
  # @raise [TypeError] if the operand is not a simple literal
32
- def apply(literal)
40
+ def apply(literal, **options)
33
41
  raise TypeError, "expected an simple literal, but got #{literal.inspect}" unless literal.literal? && literal.simple?
34
42
  base = Operator.base_uri || RDF::URI("")
35
43
  base.join(literal.to_s)
36
44
  end
37
45
 
46
+ ##
47
+ #
48
+ # Returns a partial SPARQL grammar for this operator.
49
+ #
50
+ # @return [String]
51
+ def to_sparql(**options)
52
+ "IRI(" + operands.last.to_sparql(**options) + ")"
53
+ end
54
+
38
55
  Operator::URI = IRI
39
56
  end # IRI
40
57
  end # Operator
@@ -3,7 +3,16 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL `isBlank` operator.
5
5
  #
6
- # @example
6
+ # [121] BuiltInCall ::= ... | 'isBlank' '(' Expression ')'
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://example.org/things#>
10
+ # SELECT ?x ?v WHERE {
11
+ # ?x :p ?v .
12
+ # FILTER isBlank(?v) .
13
+ # }
14
+ #
15
+ # @example SSE
7
16
  # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>)
8
17
  # (: <http://example.org/things#>))
9
18
  # (project (?x ?v)
@@ -23,13 +32,22 @@ module SPARQL; module Algebra
23
32
  # an RDF term
24
33
  # @return [RDF::Literal::Boolean] `true` or `false`
25
34
  # @raise [TypeError] if the operand is not an RDF term
26
- def apply(term)
35
+ def apply(term, **options)
27
36
  case term
28
37
  when RDF::Node then RDF::Literal::TRUE
29
38
  when RDF::Term then RDF::Literal::FALSE
30
39
  else raise TypeError, "expected an RDF::Term, but got #{term.inspect}"
31
40
  end
32
41
  end
42
+
43
+ ##
44
+ #
45
+ # Returns a partial SPARQL grammar for this operator.
46
+ #
47
+ # @return [String]
48
+ def to_sparql(**options)
49
+ "isBlank(" + operands.first.to_sparql(**options) + ")"
50
+ end
33
51
  end # IsBlank
34
52
  end # Operator
35
53
  end; end # SPARQL::Algebra