class SPARQL::Algebra::Operator::Or

The SPARQL logical `or` operator.

@example

(|| ?x ?y)
(or ?x ?y)

@see www.w3.org/TR/sparql11-query/#func-logical-or @see www.w3.org/TR/sparql11-query/#evaluation

Constants

NAME

Public Class Methods

new(left, right, **options) click to toggle source

Initializes a new operator instance.

@param [RDF::Literal::Boolean] left

the left operand

@param [RDF::Literal::Boolean] right

the right operand

@param [Hash{Symbol => Object}] options

any additional options (see {Operator#initialize})

@raise [TypeError] if any operand is invalid

Calls superclass method SPARQL::Algebra::Operator::Binary::new
# File lib/sparql/algebra/operator/or.rb, line 27
def initialize(left, right, **options)
  super
end

Public Instance Methods

evaluate(bindings, **options) click to toggle source

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.

@param [RDF::Query::Solution] bindings

a query solution containing zero or more variable bindings

@param [Hash{Symbol => Object}] options ({})

options passed from query

@return [RDF::Literal::Boolean] `true` or `false` @raise [TypeError] if the operands could not be coerced to a boolean literal

# File lib/sparql/algebra/operator/or.rb, line 43
def evaluate(bindings, **options)
  begin
    left = boolean(operand(0).evaluate(bindings, depth: options[:depth].to_i + 1, **options)).true?
  rescue TypeError
    left = nil
  end
  
  begin
    right = boolean(operand(1).evaluate(bindings, depth: options[:depth].to_i + 1, **options)).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