Class: SPARQL::Algebra::Operator::Union

Inherits:
Binary show all
Includes:
Query
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/union.rb

Overview

The SPARQL GraphPattern union operator.

Examples:

(prefix ((: <http://example/>))
  (union
    (bgp (triple ?s ?p ?o))
    (graph ?g
      (bgp (triple ?s ?p ?o)))))

See Also:

Constant Summary collapse

NAME =
[:union]

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=, #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, #variable?, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, 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

Executes each operand with queryable and performs the union operation by creating a new solution set consiting of all solutions from both operands.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

  • solution (RDF::Query::Solution)

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



34
35
36
37
38
39
40
41
42
43
44
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/union.rb', line 34

def execute(queryable, options = {}, &block)
  debug(options) {"Union"}
  @solutions = RDF::Query::Solutions(operands.inject([]) do |memo, op|
    solns = op.execute(queryable, options.merge(depth: options[:depth].to_i + 1))
    debug(options) {"=> (op) #{solns.inspect}"}
    memo + solns
  end)
  debug(options) {"=> #{@solutions.inspect}"}
  @solutions.each(&block) if block_given?
  @solutions
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

Optimize operands and remove any which are empty.

Returns:



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

def optimize
  ops = operands.map {|o| o.optimize }.select {|o| o.respond_to?(:empty?) && !o.empty?}
  @operands = ops
  self
end

#validate!Object

The same blank node label cannot be used in two different basic graph patterns in the same query



47
48
49
50
51
52
53
54
55
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/operator/union.rb', line 47

def validate!
  left_nodes, right_nodes = operand(0).ndvars.map(&:name), operand(1).ndvars.map(&:name)

  unless (left_nodes.compact & right_nodes.compact).empty?
    raise ArgumentError,
         "sub-operands share non-distinguished variables: #{(left_nodes.compact & right_nodes.compact).to_sse}"
  end
  super
end