Class: SPARQL::Algebra::Operator::If

Inherits:
Ternary show all
Includes:
Evaluatable
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/if.rb

Overview

The SPARQL if function.

Examples:

(base <http://example.org/>
  (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>))
    (project (?o ?integer)
      (extend ((?integer (if (= (lang ?o) "ja") true false)))
        (bgp (triple ?s ?p ?o))))))

See Also:

Constant Summary

NAME =
:if

Constants inherited from Ternary

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

#initialize

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

Instance Method Details

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

The IF function form evaluates the first argument, interprets it as a effective boolean value, then returns the value of expression2 if the EBV is true, otherwise it returns the value of expression3. Only one of expression2 and expression3 is evaluated. If evaluating the first argument raises an error, then an error is raised for the evaluation of the IF expression.

Evaluates the first operand and returns the evaluation of either the second or third operands

Examples:


IF(?x = 2, "yes", "no") #=> "yes"
IF(bound(?y), "yes", "no") #=> "no"
IF(?x=2, "yes", 1/?z) #=> "yes", the expression 1/?z is not evaluated
IF(?x=1, "yes", 1/?z) #=> raises an error
IF("2" > 1, "yes", "no") #=> raises an error

Parameters:

Returns:

Raises:

  • (TypeError)


36
37
38
39
40
41
42
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/if.rb', line 36

def evaluate(bindings, options = {})
  operand(0).evaluate(bindings, options.merge(depth: options[:depth].to_i + 1)) == RDF::Literal::TRUE ?
    operand(1).evaluate(bindings, options.merge(depth: options[:depth].to_i + 1).merge(depth: options[:depth].to_i + 1)) :
    operand(2).evaluate(bindings, options.merge(depth: options[:depth].to_i + 1))
  rescue
    raise TypeError
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

Return optimized query

Returns:



50
51
52
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-e0a915196943/lib/sparql/algebra/operator/if.rb', line 50

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