Skip to content

Commit

Permalink
Preliminary support for isTriple, Triple, Subject, Predicate, and Obj…
Browse files Browse the repository at this point in the history
…ect builtin operators.
  • Loading branch information
gkellogg committed Feb 28, 2021
1 parent ac8a0f3 commit dafc247
Show file tree
Hide file tree
Showing 13 changed files with 5,638 additions and 2,427 deletions.
5 changes: 5 additions & 0 deletions etc/sparql11.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@
| 'isBLANK' '(' Expression ')'
| 'isLITERAL' '(' Expression ')'
| 'isNUMERIC' '(' Expression ')'
| 'TRIPLE' '(' Expression ',' Expression ',' Expression ')'
| 'isTriple' '(' Expression ')'
| 'SUBJECT' '(' Expression ')'
| 'PREDICATE' '(' Expression ')'
| 'OBJECT' '(' Expression ')'
| RegexExpression
| ExistsFunc
| NotExistsFunc
Expand Down
25 changes: 25 additions & 0 deletions etc/sparql11.html
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,31 @@
<td>|</td>
<td><code>(</code> &quot;isNUMERIC&quot; "<code class="grammar-literal">(</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">)</code>"<code>)</code></td>
</tr>
<tr>
<td colspan=2></td>
<td>|</td>
<td><code>(</code> &quot;TRIPLE&quot; "<code class="grammar-literal">(</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">,</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">,</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">)</code>"<code>)</code></td>
</tr>
<tr>
<td colspan=2></td>
<td>|</td>
<td><code>(</code> &quot;isTriple&quot; "<code class="grammar-literal">(</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">)</code>"<code>)</code></td>
</tr>
<tr>
<td colspan=2></td>
<td>|</td>
<td><code>(</code> &quot;SUBJECT&quot; "<code class="grammar-literal">(</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">)</code>"<code>)</code></td>
</tr>
<tr>
<td colspan=2></td>
<td>|</td>
<td><code>(</code> &quot;PREDICATE&quot; "<code class="grammar-literal">(</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">)</code>"<code>)</code></td>
</tr>
<tr>
<td colspan=2></td>
<td>|</td>
<td><code>(</code> &quot;OBJECT&quot; "<code class="grammar-literal">(</code>" <a href="#grammar-production-Expression">Expression</a> "<code class="grammar-literal">)</code>"<code>)</code></td>
</tr>
<tr>
<td colspan=2></td>
<td>|</td>
Expand Down
4,447 changes: 2,403 additions & 2,044 deletions etc/sparql11.ll1.sxp

Large diffs are not rendered by default.

17 changes: 12 additions & 5 deletions etc/sparql11.sxp
Original file line number Diff line number Diff line change
Expand Up @@ -435,18 +435,19 @@
_BuiltInCall_36 _BuiltInCall_37 _BuiltInCall_38 _BuiltInCall_39
_BuiltInCall_40 _BuiltInCall_41 _BuiltInCall_42 _BuiltInCall_43
_BuiltInCall_44 _BuiltInCall_45 _BuiltInCall_46 _BuiltInCall_47
_BuiltInCall_48 _BuiltInCall_49 _BuiltInCall_50 RegexExpression ExistsFunc
NotExistsFunc ))
_BuiltInCall_48 _BuiltInCall_49 _BuiltInCall_50 _BuiltInCall_51
_BuiltInCall_52 _BuiltInCall_53 _BuiltInCall_54 _BuiltInCall_55
RegexExpression ExistsFunc NotExistsFunc ))
(rule _BuiltInCall_1 "121.1" (seq "STR" "(" Expression ")"))
(rule _BuiltInCall_2 "121.2" (seq "LANG" "(" Expression ")"))
(rule _BuiltInCall_3 "121.3" (seq "LANGMATCHES" "(" Expression "," Expression ")"))
(rule _BuiltInCall_4 "121.4" (seq "DATATYPE" "(" Expression ")"))
(rule _BuiltInCall_5 "121.5" (seq "BOUND" "(" Var ")"))
(rule _BuiltInCall_6 "121.6" (seq "IRI" "(" Expression ")"))
(rule _BuiltInCall_7 "121.7" (seq "URI" "(" Expression ")"))
(rule _BuiltInCall_8 "121.8" (seq "BNODE" _BuiltInCall_51))
(rule _BuiltInCall_51 "121.51" (alt _BuiltInCall_52 NIL))
(rule _BuiltInCall_52 "121.52" (seq "(" Expression ")"))
(rule _BuiltInCall_8 "121.8" (seq "BNODE" _BuiltInCall_56))
(rule _BuiltInCall_56 "121.56" (alt _BuiltInCall_57 NIL))
(rule _BuiltInCall_57 "121.57" (seq "(" Expression ")"))
(rule _BuiltInCall_9 "121.9" (seq "RAND" NIL))
(rule _BuiltInCall_10 "121.10" (seq "ABS" "(" Expression ")"))
(rule _BuiltInCall_11 "121.11" (seq "CEIL" "(" Expression ")"))
Expand Down Expand Up @@ -490,6 +491,12 @@
(rule _BuiltInCall_48 "121.48" (seq "isBLANK" "(" Expression ")"))
(rule _BuiltInCall_49 "121.49" (seq "isLITERAL" "(" Expression ")"))
(rule _BuiltInCall_50 "121.50" (seq "isNUMERIC" "(" Expression ")"))
(rule _BuiltInCall_51 "121.51"
(seq "TRIPLE" "(" Expression "," Expression "," Expression ")"))
(rule _BuiltInCall_52 "121.52" (seq "isTriple" "(" Expression ")"))
(rule _BuiltInCall_53 "121.53" (seq "SUBJECT" "(" Expression ")"))
(rule _BuiltInCall_54 "121.54" (seq "PREDICATE" "(" Expression ")"))
(rule _BuiltInCall_55 "121.55" (seq "OBJECT" "(" Expression ")"))
(rule RegexExpression "122"
(seq "REGEX" "(" Expression "," Expression _RegexExpression_1 ")"))
(rule _RegexExpression_1 "122.1" (cleanup opt) (alt _empty _RegexExpression_2))
Expand Down
12 changes: 12 additions & 0 deletions lib/sparql/algebra/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class Operator
autoload :Timezone, 'sparql/algebra/operator/timezone'
autoload :TZ, 'sparql/algebra/operator/tz'
autoload :Year, 'sparql/algebra/operator/year'
autoload :IsTriple, 'sparql/algebra/operator/is_triple'
autoload :Subject, 'sparql/algebra/operator/subject'
autoload :Predicate, 'sparql/algebra/operator/predicate'
autoload :Object, 'sparql/algebra/operator/object'

# Binary operators
autoload :And, 'sparql/algebra/operator/and'
Expand Down Expand Up @@ -294,6 +298,14 @@ def self.for(name, arity = nil)
when :update then Update
when :using then Using
when :with then With

# RDF-star
when :istriple then IsTriple
when :triple then Triple
when :subject then Subject
when :predicate then Predicate
when :object then Object

else nil # not found
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/sparql/algebra/operator/is_triple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module SPARQL; module Algebra
class Operator
##
# The SPARQL `isTriple` operator.
#
# @see xxx
class IsTriple < Operator::Unary
include Evaluatable

NAME = :isTriple

##
# Returns `true` if the operand is an `RDF::Statement`, `false` otherwise.
#
# @param [RDF::Term] term
# an RDF term
# @return [RDF::Literal::Boolean] `true` or `false`
# @raise [TypeError] if the operand is not an RDF term
def apply(term)
case term
when RDF::Statement then RDF::Literal::TRUE
when RDF::Term then RDF::Literal::FALSE
else raise TypeError, "expected an RDF::Term, but got #{term.inspect}"
end
end
end # IsTriple
end # Operator
end; end # SPARQL::Algebra
27 changes: 27 additions & 0 deletions lib/sparql/algebra/operator/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module SPARQL; module Algebra
class Operator
##
# The SPARQL logical `object` operator.
#
# Returns the object part of `arg` as a term.
#
# @see xxx
class Object < Operator::Unary
include Evaluatable

NAME = :object

##
# Returns the object part of arg.
#
# @param [RDF::Statement] operand
# the operand
# @return [RDF::Literal]
# @raise [TypeError] if the operand is not a statement
def apply(operand)
raise TypeError, "expected an RDF::Statement, but got #{operand.inspect}" unless operand.is_a?(RDF::Statement)
operand.object
end
end # Object
end # Operator
end; end # SPARQL::Algebra
27 changes: 27 additions & 0 deletions lib/sparql/algebra/operator/predicate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module SPARQL; module Algebra
class Operator
##
# The SPARQL logical `predicate` operator.
#
# Returns the predicate part of `arg` as a term.
#
# @see xxx
class Predicate < Operator::Unary
include Evaluatable

NAME = :predicate

##
# Returns the predicate part of arg.
#
# @param [RDF::Statement] operand
# the operand
# @return [RDF::Literal]
# @raise [TypeError] if the operand is not a statement
def apply(operand)
raise TypeError, "expected an RDF::Statement, but got #{operand.inspect}" unless operand.is_a?(RDF::Statement)
operand.predicate
end
end # Predicate
end # Operator
end; end # SPARQL::Algebra
27 changes: 27 additions & 0 deletions lib/sparql/algebra/operator/subject.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module SPARQL; module Algebra
class Operator
##
# The SPARQL logical `subject` operator.
#
# Returns the subject part of `arg` as a term.
#
# @see xxx
class Subject < Operator::Unary
include Evaluatable

NAME = :subject

##
# Returns the subject part of arg.
#
# @param [RDF::Statement] operand
# the operand
# @return [RDF::Literal]
# @raise [TypeError] if the operand is not a statement
def apply(operand)
raise TypeError, "expected an RDF::Statement, but got #{operand.inspect}" unless operand.is_a?(RDF::Statement)
operand.subject
end
end # Subject
end # Operator
end; end # SPARQL::Algebra
25 changes: 25 additions & 0 deletions lib/sparql/algebra/operator/triple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module SPARQL; module Algebra
class Operator
##
# The SPARQL `triple` operator.
#
# @see xxx
class Triple < Operator::Ternary
include Evaluatable

NAME = :triple

##
# @param [RDF::Term] subject
# @param [RDF::Term] predicate
# @param [RDF::Term] object
# @return [RDF::URI]
# @raise [TypeError] if the operand is not a simple literal
def apply(subject, predicate, object)
triple = RDF::Statement(subject, predicate, object)
raise TypeError, "valid components, but got #{triple.inspect}" unless triple.valid?
triple
end
end # Triple
end # Operator
end; end # SPARQL::Algebra

0 comments on commit dafc247

Please sign in to comment.