Class: SPARQL::Algebra::Operator::NotIn

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/notin.rb

Overview

The SPARQL GraphPattern in operator.

Used for filters with more than one expression.

[114] RelationalExpression ::= NumericExpression ('NOT' 'IN' ExpressionList)?

Examples:

SPARQL Grammar

ASK { FILTER(2 NOT IN ()) }

SSE

(ask (filter (notin 2) (bgp)))

See Also:

Constant Summary collapse

NAME =
:notin

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

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

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

Instance Method Details

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

The NOT IN operator tests whether the RDF term on the left-hand side is not found in the values of list of expressions on the right-hand side. The test is done with "!=" operator, which tests for not 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 NOT IN expression to raise an error if the RDF term being tested is not found to be in the list elsewhere in the list of terms.

The NOT IN operator is equivalent to the SPARQL expression:

(lhs != expression1) && (lhs != expression2) && ...

NOT IN (...) is equivalent to !(IN (...)).

Examples:


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

Parameters:

Returns:

Raises:

  • (TypeError)

    if term is not found and any operand raises an error



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/notin.rb', line 50

def evaluate(bindings, **options)
  lhs = operands.first.evaluate(bindings, **options.merge(depth: options[:depth].to_i + 1))
  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::FALSE
  when error_found then raise TypeError
  else RDF::Literal::TRUE
  end
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:



72
73
74
75
76
77
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/operator/notin.rb', line 72

def to_sparql(**options)
  "(" + operands.first.to_sparql(**options) +
  " NOT IN (" +
  operands[1..-1].map {|e| e.to_sparql(**options)}.join(", ") +
  "))"
end