Class: SPARQL::Algebra::Operator::In

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

Overview

The SPARQL GraphPattern in operator.

Examples:

(ask (filter (in 2 1 2 3) (bgp)))

See Also:

Constant Summary collapse

NAME =
:in

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, #initialize, #inspect, #ndvars, #node?, #operand, #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, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator

Instance Method Details

#evaluate(bindings, options = {}) ⇒ RDF::Literal::Boolean

The IN operator tests whether the RDF term on the left-hand side is found in the values of list of expressions on the right-hand side. The test is done with "=" operator, which tests for the same value, as determined by the operator mapping.

A list of zero terms on the right-hand side is legal.

Errors in comparisons cause the IN expression to raise an error if the RDF term being tested is not found elsewhere in the list of terms.

The IN operator is equivalent to the SPARQL expression:

(lhs = expression1) || (lhs = expression2) || ...

Examples:


2 IN (1, 2, 3) #=> true
2 IN () #=> false
2 IN (<http://example/iri>, "str", 2.0) #=> true
2 IN (1/0, 2) #=> true
2 IN (2, 1/0) #=> true
2 IN (3, 1/0) #=> raises an error

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 term is not found and any operand raises an error



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/in.rb', line 41

def evaluate(bindings, options = {})
  lhs = operands.first.evaluate(bindings, options)
  error_found = false
  found = operands[1..-1].any? do |op|
    begin
      lhs == op.evaluate(bindings, options.merge(depth: options[:depth].to_i + 1))
    rescue TypeError
      error_found = true
    end
  end
  case
  when found then RDF::Literal::TRUE
  when error_found then raise TypeError
  else RDF::Literal::FALSE
  end
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

Return optimized query

Returns:



64
65
66
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/in.rb', line 64

def optimize
  operands = operands.map(&:optimize)
end