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.

[112] ConditionalAndExpression::= ValueLogical ( '&&' ValueLogical )*

Examples:

SPARQL Grammar

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

SSE

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

See Also:

Constant Summary collapse

NAME =
[:'&&', :and]

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

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, #inspect, #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?

Constructor Details

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

Initializes a new operator instance.

Parameters:

Raises:

  • (TypeError)

    if any operand is invalid



40
41
42
# File 'lib/sparql/algebra/operator/and.rb', line 40

def initialize(left, right, **options)
  super
end

Instance Method Details

#evaluate(bindings, **options) ⇒ RDF::Literal::Boolean

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

Parameters:

  • bindings (RDF::Query::Solution)

    a query solution containing zero or more variable bindings

  • options (Hash{Symbol => Object})

    ({}) options passed from query

Returns:

  • (RDF::Literal::Boolean)

    true or false

Raises:

  • (TypeError)

    if the operands could not be coerced to boolean literals



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sparql/algebra/operator/and.rb', line 53

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

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


82
83
84
# File 'lib/sparql/algebra/operator/and.rb', line 82

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