Class: SPARQL::Algebra::Operator::Join

Inherits:
Binary show all
Includes:
Query
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/join.rb

Overview

The SPARQL GraphPattern join operator.

Examples:

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

See Also:

Constant Summary collapse

NAME =
[:join]

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 join operation by creating a new solution set containing the merge of all solutions from each set that are compatible with each other.

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:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/join.rb', line 37

def execute(queryable, options = {}, &block)
  # Join(Ω1, Ω2) = { merge(μ1, μ2) | μ1 in Ω1 and μ2 in Ω2, and μ1 and μ2 are compatible }
  # eval(D(G), Join(P1, P2)) = Join(eval(D(G), P1), eval(D(G), P2))
  #
  # Generate solutions independently, merge based on solution compatibility
  debug(options) {"Join #{operands.to_sse}"}
 
  left = queryable.query(operand(0), options.merge(depth: options[:depth].to_i + 1))
  debug(options) {"(join)=>(left) #{left.map(&:to_h).to_sse}"}

  right = queryable.query(operand(1), options.merge(depth: options[:depth].to_i + 1))
  debug(options) {"(join)=>(right) #{right.map(&:to_h).to_sse}"}

  @solutions = RDF::Query::Solutions(left.map do |s1|
    right.map { |s2| s2.merge(s1) if s2.compatible?(s1) }
  end.flatten.compact)
  debug(options) {"(join)=> #{@solutions.map(&:to_h).to_sse}"}
  @solutions.each(&block) if block_given?
  @solutions
end

#optimizeJoin, RDF::Query

Returns an optimized version of this query.

Groups of one graph pattern (not a filter) become join(Z, A) and can be replaced by A. The empty graph pattern Z is the identity for join: Replace join(Z, A) by A Replace join(A, Z) by A

Returns:



78
79
80
81
82
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/join.rb', line 78

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



59
60
61
62
63
64
65
66
67
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/join.rb', line 59

def validate!
  left_nodes, right_nodes = operand(0).ndvars, operand(1).ndvars

  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