sparql 3.1.6 → 3.2.1

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 (145) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +31 -22
  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 +28 -3
  8. data/lib/sparql/algebra/extensions.rb +109 -45
  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 +20 -2
  16. data/lib/sparql/algebra/operator/base.rb +18 -1
  17. data/lib/sparql/algebra/operator/bgp.rb +13 -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 +26 -2
  22. data/lib/sparql/algebra/operator/coalesce.rb +33 -11
  23. data/lib/sparql/algebra/operator/compare.rb +48 -40
  24. data/lib/sparql/algebra/operator/concat.rb +26 -2
  25. data/lib/sparql/algebra/operator/construct.rb +29 -6
  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 +53 -7
  29. data/lib/sparql/algebra/operator/create.rb +20 -2
  30. data/lib/sparql/algebra/operator/dataset.rb +27 -2
  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 +29 -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 +20 -3
  39. data/lib/sparql/algebra/operator/divide.rb +27 -3
  40. data/lib/sparql/algebra/operator/drop.rb +27 -3
  41. data/lib/sparql/algebra/operator/encode_for_uri.rb +23 -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 +15 -2
  45. data/lib/sparql/algebra/operator/extend.rb +64 -6
  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/function_call.rb +64 -0
  49. data/lib/sparql/algebra/operator/graph.rb +69 -6
  50. data/lib/sparql/algebra/operator/greater_than.rb +14 -4
  51. data/lib/sparql/algebra/operator/greater_than_or_equal.rb +14 -4
  52. data/lib/sparql/algebra/operator/group.rb +105 -8
  53. data/lib/sparql/algebra/operator/group_concat.rb +44 -8
  54. data/lib/sparql/algebra/operator/hours.rb +24 -6
  55. data/lib/sparql/algebra/operator/if.rb +20 -3
  56. data/lib/sparql/algebra/operator/in.rb +18 -1
  57. data/lib/sparql/algebra/operator/insert.rb +24 -2
  58. data/lib/sparql/algebra/operator/insert_data.rb +23 -2
  59. data/lib/sparql/algebra/operator/iri.rb +22 -5
  60. data/lib/sparql/algebra/operator/is_blank.rb +21 -4
  61. data/lib/sparql/algebra/operator/is_iri.rb +21 -4
  62. data/lib/sparql/algebra/operator/is_literal.rb +21 -4
  63. data/lib/sparql/algebra/operator/is_numeric.rb +23 -6
  64. data/lib/sparql/algebra/operator/is_triple.rb +33 -1
  65. data/lib/sparql/algebra/operator/join.rb +56 -1
  66. data/lib/sparql/algebra/operator/lang.rb +26 -1
  67. data/lib/sparql/algebra/operator/lang_matches.rb +23 -2
  68. data/lib/sparql/algebra/operator/lcase.rb +23 -3
  69. data/lib/sparql/algebra/operator/left_join.rb +42 -1
  70. data/lib/sparql/algebra/operator/less_than.rb +14 -4
  71. data/lib/sparql/algebra/operator/less_than_or_equal.rb +14 -4
  72. data/lib/sparql/algebra/operator/load.rb +25 -2
  73. data/lib/sparql/algebra/operator/max.rb +20 -2
  74. data/lib/sparql/algebra/operator/md5.rb +23 -6
  75. data/lib/sparql/algebra/operator/min.rb +22 -4
  76. data/lib/sparql/algebra/operator/minus.rb +65 -7
  77. data/lib/sparql/algebra/operator/minutes.rb +24 -6
  78. data/lib/sparql/algebra/operator/modify.rb +41 -5
  79. data/lib/sparql/algebra/operator/month.rb +24 -6
  80. data/lib/sparql/algebra/operator/move.rb +20 -2
  81. data/lib/sparql/algebra/operator/multiply.rb +27 -4
  82. data/lib/sparql/algebra/operator/negate.rb +24 -4
  83. data/lib/sparql/algebra/operator/not.rb +25 -4
  84. data/lib/sparql/algebra/operator/not_equal.rb +16 -1
  85. data/lib/sparql/algebra/operator/notexists.rb +30 -6
  86. data/lib/sparql/algebra/operator/notin.rb +20 -3
  87. data/lib/sparql/algebra/operator/notoneof.rb +21 -2
  88. data/lib/sparql/algebra/operator/now.rb +25 -6
  89. data/lib/sparql/algebra/operator/object.rb +33 -1
  90. data/lib/sparql/algebra/operator/or.rb +26 -3
  91. data/lib/sparql/algebra/operator/order.rb +71 -2
  92. data/lib/sparql/algebra/operator/path.rb +29 -2
  93. data/lib/sparql/algebra/operator/path_opt.rb +21 -2
  94. data/lib/sparql/algebra/operator/path_plus.rb +21 -2
  95. data/lib/sparql/algebra/operator/path_star.rb +20 -2
  96. data/lib/sparql/algebra/operator/plus.rb +43 -4
  97. data/lib/sparql/algebra/operator/predicate.rb +33 -1
  98. data/lib/sparql/algebra/operator/prefix.rb +24 -3
  99. data/lib/sparql/algebra/operator/project.rb +69 -5
  100. data/lib/sparql/algebra/operator/rand.rb +31 -3
  101. data/lib/sparql/algebra/operator/reduced.rb +20 -3
  102. data/lib/sparql/algebra/operator/regex.rb +27 -19
  103. data/lib/sparql/algebra/operator/replace.rb +27 -7
  104. data/lib/sparql/algebra/operator/reverse.rb +31 -2
  105. data/lib/sparql/algebra/operator/round.rb +26 -3
  106. data/lib/sparql/algebra/operator/same_term.rb +25 -7
  107. data/lib/sparql/algebra/operator/sample.rb +33 -9
  108. data/lib/sparql/algebra/operator/seconds.rb +24 -6
  109. data/lib/sparql/algebra/operator/seq.rb +20 -2
  110. data/lib/sparql/algebra/operator/sequence.rb +4 -11
  111. data/lib/sparql/algebra/operator/sha1.rb +19 -2
  112. data/lib/sparql/algebra/operator/sha256.rb +19 -2
  113. data/lib/sparql/algebra/operator/sha384.rb +19 -2
  114. data/lib/sparql/algebra/operator/sha512.rb +19 -2
  115. data/lib/sparql/algebra/operator/slice.rb +27 -5
  116. data/lib/sparql/algebra/operator/str.rb +22 -2
  117. data/lib/sparql/algebra/operator/strafter.rb +26 -3
  118. data/lib/sparql/algebra/operator/strbefore.rb +26 -3
  119. data/lib/sparql/algebra/operator/strdt.rb +23 -2
  120. data/lib/sparql/algebra/operator/strends.rb +26 -4
  121. data/lib/sparql/algebra/operator/strlang.rb +25 -7
  122. data/lib/sparql/algebra/operator/strlen.rb +24 -3
  123. data/lib/sparql/algebra/operator/strstarts.rb +26 -3
  124. data/lib/sparql/algebra/operator/struuid.rb +30 -10
  125. data/lib/sparql/algebra/operator/subject.rb +33 -1
  126. data/lib/sparql/algebra/operator/substr.rb +24 -3
  127. data/lib/sparql/algebra/operator/subtract.rb +29 -3
  128. data/lib/sparql/algebra/operator/sum.rb +25 -7
  129. data/lib/sparql/algebra/operator/table.rb +76 -4
  130. data/lib/sparql/algebra/operator/timezone.rb +24 -6
  131. data/lib/sparql/algebra/operator/tz.rb +23 -6
  132. data/lib/sparql/algebra/operator/ucase.rb +24 -3
  133. data/lib/sparql/algebra/operator/union.rb +29 -6
  134. data/lib/sparql/algebra/operator/update.rb +46 -4
  135. data/lib/sparql/algebra/operator/using.rb +49 -2
  136. data/lib/sparql/algebra/operator/uuid.rb +28 -9
  137. data/lib/sparql/algebra/operator/with.rb +38 -4
  138. data/lib/sparql/algebra/operator/year.rb +24 -6
  139. data/lib/sparql/algebra/operator.rb +135 -14
  140. data/lib/sparql/algebra/sxp_extensions.rb +3 -3
  141. data/lib/sparql/algebra.rb +20 -3
  142. data/lib/sparql/grammar/meta.rb +1103 -907
  143. data/lib/sparql/grammar/parser11.rb +63 -56
  144. metadata +43 -29
  145. data/lib/sparql/algebra/operator/triple.rb +0 -27
@@ -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
@@ -22,13 +36,22 @@ module SPARQL; module Algebra
22
36
  # a numeric literal
23
37
  # @return [RDF::Literal::Numeric]
24
38
  # @raise [TypeError] if either operand is not a numeric literal
25
- def apply(left, right)
39
+ def apply(left, right, **options)
26
40
  case
27
41
  when left.is_a?(RDF::Literal::Numeric) && right.is_a?(RDF::Literal::Numeric)
28
42
  left * right
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
@@ -20,12 +31,21 @@ module SPARQL; module Algebra
20
31
  # a numeric literal
21
32
  # @return [RDF::Literal::Numeric]
22
33
  # @raise [TypeError] if the operand is not a numeric literal
23
- def apply(term)
34
+ def apply(term, **options)
24
35
  case term
25
36
  when RDF::Literal::Numeric then -term
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
@@ -23,13 +35,22 @@ module SPARQL; module Algebra
23
35
  # the operand
24
36
  # @return [RDF::Literal::Boolean] `true` or `false`
25
37
  # @raise [TypeError] if the operand could not be coerced to a boolean literal
26
- def apply(operand)
38
+ def apply(operand, **options)
27
39
  case bool = boolean(operand)
28
40
  when RDF::Literal::Boolean
29
41
  RDF::Literal(bool.false?)
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
@@ -12,13 +25,15 @@ module SPARQL; module Algebra
12
25
  # Returns `true` if the operands are not equal; returns `false`
13
26
  # otherwise.
14
27
  #
28
+ # Comparing unknown datatypes might have different lexical forms but be the same value.
29
+ #
15
30
  # @param [RDF::Term] term1
16
31
  # an RDF term
17
32
  # @param [RDF::Term] term2
18
33
  # an RDF term
19
34
  # @return [RDF::Literal::Boolean] `true` or `false`
20
35
  # @raise [TypeError] if either operand is not an RDF term
21
- def apply(term1, term2)
36
+ def apply(term1, term2, **options)
22
37
  RDF::Literal(super.false?)
23
38
  end
24
39
  end # NotEqual
@@ -5,12 +5,23 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # There is a filter operator EXISTS that takes a graph pattern. EXISTS returns `true`/`false` depending on whether the pattern matches the dataset given the bindings in the current group graph pattern, the dataset and the active graph at this point in the query evaluation. No additional binding of variables occurs. The `NOT EXISTS` form translates into `fn:not(EXISTS{...})`.
7
7
  #
8
- # @example
9
- # (prefix ((ex: <http://www.example.org/>))
10
- # (filter (exists
11
- # (filter (notexists (bgp (triple ?s ?p ex:o2)))
12
- # (bgp (triple ?s ?p ex:o1))))
13
- # (bgp (triple ?s ?p ex:o))))
8
+ # [126] NotExistsFunc ::= 'NOT' 'EXISTS' GroupGraphPattern
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX ex: <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#>
12
+ # SELECT ?animal {
13
+ # ?animal a ex:Animal
14
+ # FILTER NOT EXISTS { ?animal a ex:Insect }
15
+ # }
16
+ #
17
+ # @example SSE
18
+ # (prefix
19
+ # ((ex: <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/negation#>))
20
+ # (project (?animal)
21
+ # (filter
22
+ # (notexists
23
+ # (bgp (triple ?animal a ex:Insect)))
24
+ # (bgp (triple ?animal a ex:Animal)))) )
14
25
  #
15
26
  # @see https://www.w3.org/TR/sparql11-query/#func-abs
16
27
  # @see https://www.w3.org/TR/xpath-functions/#func-abs
@@ -34,6 +45,19 @@ module SPARQL; module Algebra
34
45
  queryable = options[:queryable]
35
46
  operand(0).execute(queryable, solutions: solutions, **options).empty?
36
47
  end
48
+
49
+ ##
50
+ #
51
+ # Returns a partial SPARQL grammar for this operator.
52
+ #
53
+ # @param [Boolean] top_level (true)
54
+ # Treat this as a top-level, generating SELECT ... WHERE {}
55
+ # @return [String]
56
+ def to_sparql(top_level: true, **options)
57
+ "NOT EXISTS {\n" +
58
+ operands.last.to_sparql(top_level: false, **options) +
59
+ "\n}"
60
+ end
37
61
  end # NotExists
38
62
  end # Operator
39
63
  end; end # SPARQL::Algebra
@@ -5,8 +5,13 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # Used for filters with more than one expression.
7
7
  #
8
- # @example
9
- # (ask (filter (notin ?o 1 2) (bgp)))
8
+ # [114] RelationalExpression ::= NumericExpression ('NOT' 'IN' ExpressionList)?
9
+ #
10
+ # @example SPARQL Grammar
11
+ # ASK { FILTER(2 NOT IN ()) }
12
+ #
13
+ # @example SSE
14
+ # (ask (filter (notin 2) (bgp)))
10
15
  #
11
16
  # @see https://www.w3.org/TR/sparql11-query/#func-notin
12
17
  class NotIn < Operator
@@ -58,6 +63,18 @@ module SPARQL; module Algebra
58
63
  else RDF::Literal::TRUE
59
64
  end
60
65
  end
61
- end # Exprlist
66
+
67
+ ##
68
+ #
69
+ # Returns a partial SPARQL grammar for this operator.
70
+ #
71
+ # @return [String]
72
+ def to_sparql(**options)
73
+ "(" + operands.first.to_sparql(**options) +
74
+ " NOT IN (" +
75
+ operands[1..-1].map {|e| e.to_sparql(**options)}.join(", ") +
76
+ "))"
77
+ end
78
+ end # NotIn
62
79
  end # Operator
63
80
  end; end # SPARQL::Algebra
@@ -3,8 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL Property Path `notoneof` (NegatedPropertySet) operator.
5
5
  #
6
- # @example
7
- # (notoneof ex:p1 ex:p2)
6
+ # [96] PathOneInPropertySet ::= iri | 'a' | '^' ( iri | 'a' )
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX ex: <http://www.example.org/schema#>
10
+ # PREFIX in: <http://www.example.org/instance#>
11
+ # ASK { in:a !(ex:p1|ex:p2) ?x }
12
+ #
13
+ # @example SSE
14
+ # (prefix ((ex: <http://www.example.org/schema#>)
15
+ # (in: <http://www.example.org/instance#>))
16
+ # (ask
17
+ # (path in:a (notoneof ex:p1 ex:p2) ?x)))
8
18
  #
9
19
  # @see https://www.w3.org/TR/sparql11-query/#eval_negatedPropertySet
10
20
  class NotOneOf < Operator
@@ -46,6 +56,15 @@ module SPARQL; module Algebra
46
56
  block.call(solution)
47
57
  end
48
58
  end
59
+
60
+ ##
61
+ #
62
+ # Returns a partial SPARQL grammar for this operator.
63
+ #
64
+ # @return [String]
65
+ def to_sparql(**options)
66
+ "!(" + operands.to_sparql(delimiter: ' | ', **options) + ')'
67
+ end
49
68
  end # NotOneOf
50
69
  end # Operator
51
70
  end; end # SPARQL::Algebra
@@ -7,11 +7,21 @@ module SPARQL; module Algebra
7
7
  #
8
8
  # Returns an XSD dateTime value for the current query execution. All calls to this function in any one query execution must return the same value. The exact moment returned is not specified.
9
9
  #
10
- # @example
11
- # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>))
12
- # (ask (filter (= (datatype ?n) xsd:dateTime)
13
- # (extend ((?n (now)))
14
- # (bgp)))))
10
+ # [121] BuiltInCall ::= ... | 'NOW' NIL
11
+ #
12
+ # @example SPARQL Grammar
13
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
14
+ # ASK {
15
+ # BIND(NOW() AS ?n)
16
+ # FILTER(DATATYPE(?n) = xsd:dateTime)
17
+ # }
18
+ #
19
+ # @example SSE
20
+ # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>))
21
+ # (ask
22
+ # (filter (= (datatype ?n) xsd:dateTime)
23
+ # (extend ((?n (now)))
24
+ # (bgp)))))
15
25
  #
16
26
  # @see https://www.w3.org/TR/sparql11-query/#func-now
17
27
  class Now < Operator::Nullary
@@ -23,9 +33,18 @@ module SPARQL; module Algebra
23
33
  # Returns an XSD dateTime value for the current query execution. All calls to this function in any one query execution must return the same value. The exact moment returned is not specified.
24
34
  #
25
35
  # @return [RDF::Literal::Double] random value
26
- def apply
36
+ def apply(**options)
27
37
  RDF::Literal(DateTime.now)
28
38
  end
39
+
40
+ ##
41
+ #
42
+ # Returns a partial SPARQL grammar for this operator.
43
+ #
44
+ # @return [String]
45
+ def to_sparql(**options)
46
+ "NOW()"
47
+ end
29
48
  end # Now
30
49
  end # Operator
31
50
  end; end # SPARQL::Algebra
@@ -5,6 +5,29 @@ module SPARQL; module Algebra
5
5
  #
6
6
  # If triple is an RDF-star triple, the function returns the object of this triple. Passing anything other than an RDF-star triple is an error.
7
7
  #
8
+ # [121] BuiltInCall ::= ... | 'OBJECT' '(' Expression ')'
9
+ #
10
+ # @example SPARQL Grammar
11
+ # PREFIX : <http://example.com/ns#>
12
+ # SELECT * {
13
+ # ?t :source :g
14
+ # FILTER(isTriple(?t))
15
+ # FILTER(SUBJECT(?t) = :s)
16
+ # FILTER(PREDICATE(?t) = :p)
17
+ # FILTER(OBJECT(?t) = :o)
18
+ # }
19
+ #
20
+ # @example SSE
21
+ # (prefix
22
+ # ((: <http://example.com/ns#>))
23
+ # (filter
24
+ # (exprlist
25
+ # (isTRIPLE ?t)
26
+ # (= (subject ?t) :s)
27
+ # (= (predicate ?t) :p)
28
+ # (= (object ?t) :o))
29
+ # (bgp (triple ?t :source :g))) )
30
+ #
8
31
  # @see https://w3c.github.io/rdf-star/rdf-star-cg-spec.html#object
9
32
  class Object < Operator::Unary
10
33
  include Evaluatable
@@ -18,10 +41,19 @@ module SPARQL; module Algebra
18
41
  # the operand
19
42
  # @return [RDF::Literal]
20
43
  # @raise [TypeError] if the operand is not a statement
21
- def apply(operand)
44
+ def apply(operand, **options)
22
45
  raise TypeError, "expected an RDF::Statement, but got #{operand.inspect}" unless operand.is_a?(RDF::Statement)
23
46
  operand.object
24
47
  end
48
+
49
+ ##
50
+ #
51
+ # Returns a partial SPARQL grammar for this operator.
52
+ #
53
+ # @return [String]
54
+ def to_sparql(**options)
55
+ "OBJECT(" + operands.last.to_sparql(**options) + ")"
56
+ end
25
57
  end # Object
26
58
  end # Operator
27
59
  end; end # SPARQL::Algebra
@@ -3,9 +3,23 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL logical `or` operator.
5
5
  #
6
- # @example
7
- # (|| ?x ?y)
8
- # (or ?x ?y)
6
+ # [111] ConditionalOrExpression ::= ConditionalAndExpression ( '||' ConditionalAndExpression )*
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
10
+ # PREFIX : <http://example.org/ns#>
11
+ # SELECT ?a
12
+ # WHERE {
13
+ # ?a :p ?v .
14
+ # FILTER ("false"^^xsd:boolean || ?v) .
15
+ # }
16
+ #
17
+ # @example SSE
18
+ # (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>)
19
+ # (: <http://example.org/ns#>))
20
+ # (project (?a)
21
+ # (filter (|| false ?v)
22
+ # (bgp (triple ?a :p ?v)))))
9
23
  #
10
24
  # @see https://www.w3.org/TR/sparql11-query/#func-logical-or
11
25
  # @see https://www.w3.org/TR/sparql11-query/#evaluation
@@ -63,6 +77,15 @@ module SPARQL; module Algebra
63
77
  else RDF::Literal(left || right)
64
78
  end
65
79
  end
80
+
81
+ ##
82
+ #
83
+ # Returns a partial SPARQL grammar for this operator.
84
+ #
85
+ # @return [String]
86
+ def to_sparql(**options)
87
+ "(#{operands.first.to_sparql(**options)} || #{operands.last.to_sparql(**options)})"
88
+ end
66
89
  end # Or
67
90
  end # Operator
68
91
  end; end # SPARQL::Algebra
@@ -3,12 +3,64 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL GraphPattern `order` operator.
5
5
  #
6
- # @example
6
+ # [23] OrderClause ::= 'ORDER' 'BY' OrderCondition+
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX foaf: <http://xmlns.com/foaf/0.1/>
10
+ # SELECT ?name
11
+ # WHERE { ?x foaf:name ?name }
12
+ # ORDER BY ASC(?name)
13
+ #
14
+ # @example SSE
7
15
  # (prefix ((foaf: <http://xmlns.com/foaf/0.1/>))
8
16
  # (project (?name)
9
17
  # (order ((asc ?name))
10
18
  # (bgp (triple ?x foaf:name ?name)))))
11
19
  #
20
+ # @example SPARQL Grammar (with builtin)
21
+ # PREFIX : <http://example.org/>
22
+ # SELECT ?s WHERE {
23
+ # ?s :p ?o .
24
+ # }
25
+ # ORDER BY str(?o)
26
+ #
27
+ # @example SSE (with builtin)
28
+ # (prefix ((: <http://example.org/>))
29
+ # (project (?s)
30
+ # (order ((str ?o))
31
+ # (bgp (triple ?s :p ?o)))))
32
+ #
33
+ # @example SPARQL Grammar (with bracketed expression)
34
+ # PREFIX : <http://example.org/>
35
+ # SELECT ?s WHERE {
36
+ # ?s :p ?o1 ; :q ?o2 .
37
+ # } ORDER BY (?o1 + ?o2)
38
+ #
39
+ # @example SSE (with bracketed expression)
40
+ # (prefix
41
+ # ((: <http://example.org/>))
42
+ # (project (?s)
43
+ # (order ((+ ?o1 ?o2))
44
+ # (bgp
45
+ # (triple ?s :p ?o1)
46
+ # (triple ?s :q ?o2)))))
47
+ #
48
+ # @example SPARQL Grammar (with function call)
49
+ # PREFIX : <http://example.org/ns#>
50
+ # PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
51
+ # SELECT *
52
+ # { ?s ?p ?o }
53
+ # ORDER BY
54
+ # DESC(?o+57) xsd:string(?o) ASC(?s)
55
+ #
56
+ # @example SSE (with function call)
57
+ # (prefix ((: <http://example.org/ns#>)
58
+ # (xsd: <http://www.w3.org/2001/XMLSchema#>))
59
+ # (order ((desc (+ ?o 57))
60
+ # (xsd:string ?o)
61
+ # (asc ?s))
62
+ # (bgp (triple ?s ?p ?o))))
63
+ #
12
64
  # @see https://www.w3.org/TR/sparql11-query/#modOrderBy
13
65
  class Order < Operator::Binary
14
66
  include Query
@@ -40,7 +92,13 @@ module SPARQL; module Algebra
40
92
  memo = begin
41
93
  a_eval = op.evaluate(a, queryable: queryable, depth: options[:depth].to_i + 1, **options) rescue nil
42
94
  b_eval = op.evaluate(b, queryable: queryable, depth: options[:depth].to_i + 1, **options) rescue nil
43
- comp = Operator::Compare.evaluate(a_eval, b_eval).to_i
95
+ comp = begin
96
+ Operator::Compare.evaluate(a_eval, b_eval, order_by: true).to_s.to_i
97
+ rescue TypeError
98
+ # Type sError is effectively zero
99
+ debug(options) {"(order) rescue(#{$!}): #{a_eval.inspect}, #{b_eval.inspect}"}
100
+ RDF::Literal(0)
101
+ end
44
102
  comp = -comp if op.is_a?(Operator::Desc)
45
103
  comp
46
104
  end if memo == 0
@@ -50,6 +108,17 @@ module SPARQL; module Algebra
50
108
  @solutions.each(&block) if block_given?
51
109
  @solutions
52
110
  end
111
+
112
+ ##
113
+ #
114
+ # Returns a partial SPARQL grammar for this operator.
115
+ #
116
+ # Provides order to descendant query.
117
+ #
118
+ # @return [String]
119
+ def to_sparql(**options)
120
+ operands.last.to_sparql(order_ops: operands.first, **options)
121
+ end
53
122
  end # Order
54
123
  end # Operator
55
124
  end; end # SPARQL::Algebra
@@ -3,8 +3,23 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL Property Path `path` operator.
5
5
  #
6
- # @example
7
- # (path :a (path+ :p) ?z)
6
+ # [88] Path ::= PathAlternative
7
+ #
8
+ # @example SPARQL Grammar
9
+ # PREFIX : <http://www.example.org/>
10
+ # SELECT ?t
11
+ # WHERE {
12
+ # :a :p1|:p2/:p3|:p4 ?t
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (prefix ((: <http://www.example.org/>))
17
+ # (project (?t)
18
+ # (path :a
19
+ # (alt
20
+ # (alt :p1 (seq :p2 :p3))
21
+ # :p4)
22
+ # ?t)))
8
23
  #
9
24
  # @see https://www.w3.org/TR/sparql11-query/#sparqlTranslatePathExpressions
10
25
  class Path < Operator::Ternary
@@ -45,6 +60,18 @@ module SPARQL; module Algebra
45
60
  @solutions.each(&block) if block_given?
46
61
  @solutions
47
62
  end
63
+
64
+ ##
65
+ #
66
+ # Returns a partial SPARQL grammar for this operator.
67
+ #
68
+ # @param [Boolean] top_level (true)
69
+ # Treat this as a top-level, generating SELECT ... WHERE {}
70
+ # @return [String]
71
+ def to_sparql(top_level: true, **options)
72
+ str = operands.to_sparql(top_level: false, **options) + " ."
73
+ top_level ? Operator.to_sparql(str, **options) : str
74
+ end
48
75
  end # Path
49
76
  end # Operator
50
77
  end; end # SPARQL::Algebra
@@ -3,8 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL Property Path `path?` (ZeroOrOnePath) operator.
5
5
  #
6
- # @example
7
- # (path? :p)
6
+ # [91] PathElt ::= PathPrimary PathMod?
7
+ # [93] PathMod ::= '*' | '?' | '+'
8
+
9
+ # @example SPARQL Grammar
10
+ # PREFIX : <http://example/>
11
+ # SELECT * WHERE {
12
+ # :a (:p/:p)? ?t
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (prefix ((: <http://example/>))
17
+ # (path :a (path? (seq :p :p)) ?t))
8
18
  #
9
19
  # @see https://www.w3.org/TR/sparql11-query/#defn_evalPP_ZeroOrOnePath
10
20
  class PathOpt < Operator::Unary
@@ -107,6 +117,15 @@ module SPARQL; module Algebra
107
117
  solutions.each(&block) if block_given?
108
118
  solutions
109
119
  end
120
+
121
+ ##
122
+ #
123
+ # Returns a partial SPARQL grammar for this operator.
124
+ #
125
+ # @return [String]
126
+ def to_sparql(**options)
127
+ "(#{operands.first.to_sparql(**options)})?"
128
+ end
110
129
  end # PathOpt
111
130
  end # Operator
112
131
  end; end # SPARQL::Algebra
@@ -3,8 +3,18 @@ module SPARQL; module Algebra
3
3
  ##
4
4
  # The SPARQL Property Path `path+` (OneOrMorePath) operator.
5
5
  #
6
- # @example
7
- # (path+ :p)
6
+ # [91] PathElt ::= PathPrimary PathMod?
7
+ # [93] PathMod ::= '*' | '?' | '+'
8
+
9
+ # @example SPARQL Grammar
10
+ # PREFIX : <http://example/>
11
+ # SELECT * WHERE {
12
+ # :a :p+ ?z
13
+ # }
14
+ #
15
+ # @example SSE
16
+ # (prefix ((: <http://example/>))
17
+ # (path :a (path+ :p) ?z))
8
18
  #
9
19
  # @see https://www.w3.org/TR/sparql11-query/#defn_evalPP_OneOrMorePath
10
20
  class PathPlus < Operator::Unary
@@ -94,6 +104,15 @@ module SPARQL; module Algebra
94
104
  solutions.each(&block) if block_given? # Only at top-level
95
105
  solutions
96
106
  end
107
+
108
+ ##
109
+ #
110
+ # Returns a partial SPARQL grammar for this operator.
111
+ #
112
+ # @return [String]
113
+ def to_sparql(**options)
114
+ "(#{operands.first.to_sparql(**options)})+"
115
+ end
97
116
  end # PathPlus
98
117
  end # Operator
99
118
  end; end # SPARQL::Algebra