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

Inherits:
Binary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/or.rb

Overview

The SPARQL logical or operator.

[111] ConditionalOrExpression ::= ConditionalAndExpression ( '||' ConditionalAndExpression )*

Examples:

SPARQL Grammar

PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX  : <http://example.org/ns#>
SELECT  ?a
WHERE {
  ?a :p ?v . 
  FILTER ("false"^^xsd:boolean || ?v) .
}

SSE

(prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>)
         (: <http://example.org/ns#>))
 (project (?a)
  (filter (|| false ?v)
   (bgp (triple ?a :p ?v)))))

See Also:

Constant Summary collapse

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

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

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=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #formulae, #inspect, #mergable?, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars

Methods included from Expression

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

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

#initialize(left, right, **options) ⇒ Or

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



41
42
43
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/or.rb', line 41

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:

Returns:

Raises:

  • (TypeError)

    if the operands could not be coerced to a boolean literal



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/or.rb', line 57

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 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

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



86
87
88
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-796d3be4aa08/lib/sparql/algebra/operator/or.rb', line 86

def to_sparql(**options)
  "(#{operands.first.to_sparql(**options)} || #{operands.last.to_sparql(**options)})"
end