Class: SPARQL::Algebra::Operator::PathPlus
- Inherits:
-
Unary
- Object
- SPARQL::Algebra::Operator
- Unary
- SPARQL::Algebra::Operator::PathPlus
- Includes:
- Query
- Defined in:
- vendor/bundler/ruby/3.0.0/bundler/gems/sparql-ffa592c8f275/lib/sparql/algebra/operator/path_plus.rb
Overview
The SPARQL Property Path path+
(OneOrMorePath) operator.
Constant Summary collapse
- NAME =
:"path+"
Constants inherited from Unary
Constants inherited from SPARQL::Algebra::Operator
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.
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, #formulae, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #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, #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
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/3.0.0/bundler/gems/sparql-ffa592c8f275/lib/sparql/algebra/operator/path_plus.rb', line 29 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 = [] queryable.query(query, depth: [: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() {"(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, **.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 = queryable.query(self, **.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 += queryable.query(self, **.merge( object: solution[subject], accumulator: (cumulative_solutions + immediate_solutions), depth: [:depth].to_i + 1)).uniq when object.variable? recursive_solutions += queryable.query(self, **.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 |