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

Inherits:
Binary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/or.rb

Overview

The SPARQL logical or operator.

Constant Summary collapse

NAME =
[:'||', :or]

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Evaluatable

#apply, #memoize, #replace_aggregate!, #replace_vars!

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

cast, #constant?, extension, extensions, for, #invalid?, new, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

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

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



27
28
29
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/or.rb', line 27

def initialize(left, right, options = {})
  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.

Parameters:

  • bindings (RDF::Query::Solution)

    a query solution containing zero or more variable bindings

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

    ({}) options passed from query

Returns:

  • (RDF::Literal::Boolean)

    true or false

Raises:

  • (TypeError)

    if the operands could not be coerced to a boolean literal



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/or.rb', line 43

def evaluate(bindings, options = {})
  begin
    left = boolean(operand(0).evaluate(bindings, options.merge(depth: options[:depth].to_i + 1))).true?
  rescue TypeError
    left = nil
  end
  
  begin
    right = boolean(operand(1).evaluate(bindings, options.merge(depth: options[:depth].to_i + 1))).true?
  rescue TypeError
    right = nil
  end

  # From http://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