Class: SPARQL::Algebra::Operator::And

Inherits:
Binary show all
Includes:
Evaluatable
Defined in:
lib/sparql/algebra/operator/and.rb

Overview

The SPARQL logical and operator.

Examples:

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

See Also:

Constant Summary

NAME =
[:and, :&&']

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands, #options

Instance Method Summary (collapse)

Methods included from Evaluatable

#apply, #memoize

Methods inherited from SPARQL::Algebra::Operator

arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #eql?, #evaluatable?, evaluate, #executable?, for, #inspect, #operand, #optimize, prefixes, #prefixes, prefixes=, #to_sse, #to_sxp, #variable?

Methods included from Expression

cast, #constant?, for, new, open, #optimize, parse, #to_sse, #variable?

Constructor Details

- (And) initialize(left, right, options = {})

Initializes a new operator instance.

Raises:

  • (TypeError)

    if any operand is invalid



27
28
29
# File 'lib/sparql/algebra/operator/and.rb', line 27

def initialize(left, right, options = {})
  super
end

Instance Method Details

- (RDF::Literal::Boolean) evaluate(bindings = {})

Returns the logical AND of the left operand and the right operand.

Note that this operator operates on the effective boolean value (EBV) of its operands.

Raises:

  • (TypeError)

    if the operands could not be coerced to boolean literals



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sparql/algebra/operator/and.rb', line 40

def evaluate(bindings = {})
  begin
    left = boolean(operand(0).evaluate(bindings)).true?
  rescue TypeError
    left = nil
  end
  
  begin
    right = boolean(operand(1).evaluate(bindings)).true?
  rescue TypeError
    right = nil
  end

  # From http://www.w3.org/TR/rdf-sparql-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