Class: SPARQL::Algebra::Operator::Extend

Inherits:
Binary show all
Includes:
Query
Defined in:
vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/operator/extend.rb

Overview

The SPARQL Extend operator.

Extends a solution

Examples:

(select (?z)
  (project (?z)
    (extend ((?z (+ ?o 10)))
      (bgp (triple ?s <http://example/p> ?o)))))

See Also:

Constant Summary collapse

NAME =
[:extend]

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Query

#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift, #variables

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #variable?, #vars

Methods included from Expression

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

Constructor Details

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

Instance Method Details

#execute(queryable, **options) {|solution| ... } ⇒ RDF::Query::Solutions

Let μ be a solution mapping, Ω a multiset of solution mappings, var a variable and expr be an expression, then we define:

Extend(μ, var, expr) = μ ∪ { (var,value) | var not in dom(μ) and value = expr(μ) }

Extend(μ, var, expr) = μ if var not in dom(μ) and expr(μ) is an error

Extend is undefined when var in dom(μ).

Extend(Ω, var, expr) = { Extend(μ, var, expr) | μ in Ω }

For SPARQL*, expr may be an embedded tiple pattern

(extend ((?t (triple ?bob foaf:age ?age))) (bgp (triple ?t dct:source ?src)))

Parameters:

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/operator/extend.rb', line 48

def execute(queryable, **options, &block)
  debug(options) {"Extend"}
  @solutions = operand(1).execute(queryable, depth: options[:depth].to_i + 1, **options)
  @solutions.each do |solution|
    debug(options) {"===> soln #{solution.to_h.inspect}"}
    operand(0).each do |(var, expr)|
      begin
        val = expr.evaluate(solution, queryable: queryable,
                                      depth: options[:depth].to_i + 1,
                                      **options)
        debug(options) {"===> + #{var} => #{val.inspect}"}
        solution.bindings[var.to_sym] = if val.is_a?(RDF::Query::Pattern)
          # Variable is bound to the solution
          val.bind(solution)
        else
          val
        end
      rescue TypeError => e
        # Evaluates to error, ignore
        debug(options) {"===> #{var} error: #{e.message}"}
      end
    end
  end
  @solutions.each(&block) if block_given?
  @solutions
end

#validate!Object

The variable introduced by the BIND clause must not have been used in the group graph pattern up to the point of use in BIND



76
77
78
79
80
81
82
83
84
85
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/sparql-cd4915d97d78/lib/sparql/algebra/operator/extend.rb', line 76

def validate!
  bind_vars = operand(0).map(&:first).map(&:name)
  query_vars = operand(1).vars.map(&:name)
  
  unless (bind_vars.compact & query_vars.compact).empty?
    raise ArgumentError,
         "bound variable used in query: #{(bind_vars.compact & query_vars.compact).to_sse}"
  end
  super
end