class SPARQL::Algebra::Operator::And

The SPARQL logical `and` operator.

@example

(&& ?x ?y)
(and ?x ?y)

@see www.w3.org/TR/sparql11-query/#func-logical-and @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/and.rb, line 27
def initialize(left, right, **options)
  super
end

Public Instance Methods

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

Returns a logical `AND` of `left` and `right`. Note that logical-and operates on the effective boolean value of its arguments.

@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 boolean literals

# File lib/sparql/algebra/operator/and.rb, line 40
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-and that encounters an error on only one branch will return an error if the other branch is
  # TRUE and FALSE if the other branch is FALSE.
  case
  when left.nil? && right.nil? then raise(TypeError)
  when left.nil?               then right ? raise(TypeError) : RDF::Literal::FALSE
  when right.nil?              then left  ? raise(TypeError) : RDF::Literal::FALSE
  else                              RDF::Literal(left && right)
  end
end