Class: SPARQL::Algebra::Operator::PathPlus
- Inherits:
-
Unary
- Object
- SPARQL::Algebra::Operator
- Unary
- SPARQL::Algebra::Operator::PathPlus
- Includes:
- Query
- Defined in:
- lib/sparql/algebra/operator/path_plus.rb
Overview
Constant Summary collapse
- NAME =
:"path+"
Constants inherited from Unary
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| ... } ⇒ Object
Match on simple relation of subject to object, and then recurse on solutions.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
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
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))
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, **, &block) subject, object = [:subject], [:object] debug() {"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 = .fetch(:accumulator, RDF::Query::Solutions.new) # Keep track of solutions # Recurse into query immediate_solutions = query.execute(queryable, **.merge(depth: [: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() {"(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, **.merge( subject: solution[object], accumulator: (cumulative_solutions + immediate_solutions), depth: [: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, **.merge( object: solution[subject], accumulator: (cumulative_solutions + immediate_solutions), depth: [: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, **.merge( object: solution[subject], accumulator: (cumulative_solutions + immediate_solutions), depth: [:depth].to_i + 1)).uniq when object.variable? recursive_solutions += self.execute(queryable, **.merge( subject: solution[object], accumulator: (cumulative_solutions + immediate_solutions), depth: [:depth].to_i + 1)).uniq end end debug() {"(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.
121 122 123 |
# File 'lib/sparql/algebra/operator/path_plus.rb', line 121 def to_sparql(**) "(#{operands.first.to_sparql(**)})+" end |