Class: SPARQL::Algebra::Operator::Or

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

Overview

The SPARQL logical or operator.

Constant Summary

NAME =
[:or, :||']

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

#initialize(left, right, options = {}) ⇒ Or

Initializes a new operator instance.

Parameters:

  • left (RDF::Literal::Boolean)

    the left operand

  • right (RDF::Literal::Boolean)

    the right operand

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options (see SPARQL::Algebra::Operator#initialize)

Raises:

  • (TypeError)

    if any operand is invalid



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

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

Instance Method Details

#evaluate(bindings = {}) ⇒ 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.

Parameters:

  • bindings (RDF::Query::Solution, #[]) (defaults to: {})

Returns:

  • (RDF::Literal::Boolean)

    true or false

Raises:

  • (TypeError)

    if the operands could not be coerced to a boolean literal



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/or.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-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