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

Inherits:
Unary show all
Includes:
Query
Defined in:
lib/sparql/algebra/operator/path_plus.rb

Overview

Examples:

SPARQL Grammar

PREFIX : <http://example/> 
SELECT * WHERE {
  :a :p+ ?z
} 

SSE

(prefix ((: <http://example/>))
 (path :a (path+ :p) ?z))

See Also:

Constant Summary collapse

NAME =
:"path+"

Constants inherited from Unary

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

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

Methods included from Expression

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

Path including at least one:

(path :a (path+ :p) :b)

into

(union (bgp (triple :a :p :b)) (path :a (path* :p) :b))

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

  • options (Hash{Symbol => Object})

    any additional keyword options

Options Hash (**options):

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

See Also:



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sparql/algebra/operator/path_plus.rb', line 49

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 = 
    query.execute(queryable, **options.merge(depth: options[:depth].to_i + 1))

  # 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 = self.execute(queryable, **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 = self.execute(queryable, **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 += self.execute(queryable, **options.merge(
        object: solution[subject],
        accumulator: (cumulative_solutions + immediate_solutions),
        depth: options[:depth].to_i + 1)).uniq
    when object.variable?
      recursive_solutions += self.execute(queryable, **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

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


121
122
123
# File 'lib/sparql/algebra/operator/path_plus.rb', line 121

def to_sparql(**options)
  "(#{operands.first.to_sparql(**options)})+"
end