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

Inherits:
Binary show all
Includes:
Query
Defined in:
lib/sparql/algebra/operator/union.rb

Overview

The SPARQL GraphPattern union operator.

[67] GroupOrUnionGraphPattern::= GroupGraphPattern ( ‘UNION’ GroupGraphPattern )*

Examples:

SPARQL Grammar

SELECT * {
  { ?s ?p ?o }
  UNION
  { GRAPH ?g { ?s ?p ?o } }}

SSE

(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

Constants included from Expression

Expression::PATTERN_PARENTS

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?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #variable?, #variables, #vars

Methods included from Expression

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

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})

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sparql/algebra/operator/union.rb', line 41

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

#optimize!(**options) ⇒ self

Optimizes this query.

Optimize operands and remove any which are empty.

Returns:

  • (self)

See Also:



71
72
73
74
75
# File 'lib/sparql/algebra/operator/union.rb', line 71

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

#to_sparql(top_level: true, **options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Parameters:

  • top_level (Boolean) (defaults to: true)

    (true) Treat this as a top-level, generating SELECT … WHERE {}

Returns:

  • (String)


84
85
86
87
88
89
90
91
# File 'lib/sparql/algebra/operator/union.rb', line 84

def to_sparql(top_level: true, **options)
  str = "{\n"
  str << operands[0].to_sparql(top_level: false, **options)
  str << "\n} UNION {\n"
  str << operands[1].to_sparql(top_level: false, **options)
  str << "\n}"
  top_level ? Operator.to_sparql(str, **options) : str
end

#validate!Object

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



54
55
56
57
58
59
60
61
62
# File 'lib/sparql/algebra/operator/union.rb', line 54

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