Class: SPARQL::Algebra::Operator::PathPlus

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

Overview

The SPARQL Property Path path+ (OneOrMorePath) operator.

Constant Summary collapse

NAME =
:"path+"

Constants inherited from Unary

Unary::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 Unary

#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, #optimize, #parent, #parent=, prefixes, #prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from Expression

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

Constructor Details

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

Instance Method Details

#execute(queryable, options = {}) {|solution| ... } ⇒ Object

Match on simple relation of subject to object, and then recurse on solutions

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

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

    any additional keyword options

Options Hash (options):

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-683a29666c28/lib/sparql/algebra/operator/path_plus.rb', line 29

def execute(queryable, options = {}, &block)
  subject, object = options[:subject], options[:object]
  debug(options) {"Path+ #{[subject, operands, object].to_sse}"}

  # This turns from
  # (:x :p+ ?y)
  # into
  # (:x :p ?y) UNION solutions(:x :p ?y) do |soln|
  #   if :x.variable?
  #     (soln[:x] :p+ soln[:y])
  # end

  # Solutions where predicate exists
  query = if operand.is_a?(RDF::Term)
    RDF::Query.new do |q|
      q.pattern [subject, operand, object]
    end
  else
    operand
  end

  # Block is given on the first invocation, otherwise, results are returned. This is necessary to stop when an existing solution has already been found
  cumulative_solutions = options.fetch(:accumulator, RDF::Query::Solutions.new)

  # Keep track of solutions
  # Recurse into query
  immediate_solutions = []
  queryable.query(query, options.merge(depth: options[:depth].to_i + 1)) do |solution|
    immediate_solutions << solution
  end

  # For all solutions, if they are not in the accumulator, add them and recurse, otherwise skip
  recursive_solutions = RDF::Query::Solutions.new
  immediate_solutions.reject {|s| cumulative_solutions.include?(s)}.each do |solution|
    debug(options) {"(immediate solution)-> #{solution.to_h.to_sse}"}

    # Recurse on subject, if is a variable
    case
    when subject.variable? && object.variable?
      # Query starting with bound object as subject, but replace result with subject
      rs = queryable.query(self, options.merge(
        subject: solution[object],
        accumulator: (cumulative_solutions + immediate_solutions),
        depth: options[:depth].to_i + 1)).map {|s| s.merge(subject.to_sym => solution[subject])}
      # Query starting with bound subject as object, but replace result with subject
      ro = queryable.query(self, options.merge(
        object: solution[subject],
        accumulator: (cumulative_solutions + immediate_solutions),
        depth: options[:depth].to_i + 1)).map {|s| s.merge(object.to_sym => solution[object])}
      recursive_solutions += (rs + ro).uniq
    when subject.variable?
      recursive_solutions += queryable.query(self, options.merge(
        object: solution[subject],
        accumulator: (cumulative_solutions + immediate_solutions),
        depth: options[:depth].to_i + 1)).uniq
    when object.variable?
      recursive_solutions += queryable.query(self, options.merge(
        subject: solution[object],
        accumulator: (cumulative_solutions + immediate_solutions),
        depth: options[:depth].to_i + 1)).uniq
    end
  end
  debug(options) {"(recursive solutions)-> #{recursive_solutions.map(&:to_h).to_sse}"} unless recursive_solutions.empty?

  solutions = (immediate_solutions + recursive_solutions).uniq
  solutions.each(&block) if block_given? # Only at top-level
  solutions
end