Class: SPARQL::Algebra::Operator::Or
- Includes:
- Evaluatable
- Defined in:
- lib/sparql/algebra/operator/or.rb
Overview
The SPARQL logical or operator.
[111] ConditionalOrExpression ::= ConditionalAndExpression ( '||' ConditionalAndExpression )*
Constant Summary collapse
- NAME =
[:'||', :or]
Instance Method Summary collapse
-
#evaluate(bindings, **options) ⇒ RDF::Literal::Boolean
Returns the logical
ORof the left operand and the right operand. -
#initialize(left, right, **options) ⇒ Or
constructor
Initializes a new operator instance.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
Methods included from Evaluatable
#apply, #memoize, #replace_aggregate!, #replace_vars!
Constructor Details
#initialize(left, right, **options) ⇒ Or
Initializes a new operator instance.
41 42 43 |
# File 'lib/sparql/algebra/operator/or.rb', line 41 def initialize(left, right, **) super end |
Instance Method Details
#evaluate(bindings, **options) ⇒ RDF::Literal::Boolean
Returns the logical OR of the left operand and the right operand.
Note that this operator operates on the effective boolean value (EBV) of its operands.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sparql/algebra/operator/or.rb', line 57 def evaluate(bindings, **) begin left = boolean(operand(0).evaluate(bindings, **.merge(depth: [:depth].to_i + 1))).true? rescue TypeError left = nil end begin right = boolean(operand(1).evaluate(bindings, **.merge(depth: [:depth].to_i + 1))).true? rescue TypeError right = nil end # From https://www.w3.org/TR/sparql11-query/#evaluation # A logical-or that encounters an error on only one branch will return TRUE if the other branch is TRUE # and an error if the other branch is FALSE. case when left.nil? && right.nil? then raise(TypeError) when left.nil? then right ? RDF::Literal::TRUE : raise(TypeError) when right.nil? then left ? RDF::Literal::TRUE : raise(TypeError) else RDF::Literal(left || right) end end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
86 87 88 |
# File 'lib/sparql/algebra/operator/or.rb', line 86 def to_sparql(**) "(#{operands.first.to_sparql(**)} || #{operands.last.to_sparql(**)})" end |