Class: SPARQL::Algebra::Operator::Union
- Inherits:
-
Binary
- Object
- SPARQL::Algebra::Operator
- Binary
- SPARQL::Algebra::Operator::Union
- Includes:
- Query
- Defined in:
- lib/sparql/algebra/operator/union.rb
Overview
The SPARQL GraphPattern union
operator.
[67] GroupOrUnionGraphPattern::= GroupGraphPattern ( ‘UNION’ GroupGraphPattern )*
Constant Summary collapse
- NAME =
[:union]
Constants inherited from Binary
Constants inherited from SPARQL::Algebra::Operator
Constants included from Expression
Instance Attribute Summary
Attributes included from Query
Attributes inherited from SPARQL::Algebra::Operator
Instance Method Summary collapse
-
#execute(queryable, **options) {|solution| ... } ⇒ RDF::Query::Solutions
Executes each operand with
queryable
and performs theunion
operation by creating a new solution set consiting of all solutions from both operands. -
#optimize!(**options) ⇒ self
Optimizes this query.
-
#to_sparql(top_level: true, **options) ⇒ String
Returns a partial SPARQL grammar for this operator.
-
#validate! ⇒ Object
The same blank node label cannot be used in two different basic graph patterns in the same query.
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
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.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sparql/algebra/operator/union.rb', line 41 def execute(queryable, **, &block) debug() {"Union"} @solutions = RDF::Query::Solutions(operands.inject([]) do |memo, op| solns = op.execute(queryable, **.merge(depth: [:depth].to_i + 1)) debug() {"=> (op) #{solns.inspect}"} memo + solns end) debug() {"=> #{@solutions.inspect}"} @solutions.each(&block) if block_given? @solutions end |
#optimize!(**options) ⇒ self
Optimizes this query.
Optimize operands and remove any which are empty.
71 72 73 74 75 |
# File 'lib/sparql/algebra/operator/union.rb', line 71 def optimize!(**) ops = operands.map {|o| o.optimize(**) }.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.
84 85 86 87 88 89 90 91 |
# File 'lib/sparql/algebra/operator/union.rb', line 84 def to_sparql(top_level: true, **) str = "{\n" str << operands[0].to_sparql(top_level: false, **) str << "\n} UNION {\n" str << operands[1].to_sparql(top_level: false, **) str << "\n}" top_level ? Operator.to_sparql(str, **) : 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 |