Class: LD::Patch::Algebra::Path

Inherits:
SPARQL::Algebra::Operator show all
Includes:
SPARQL::Algebra::Evaluatable, SPARQL::Algebra::Query
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/algebra/path.rb

Overview

The LD Patch path operator.

The Path creates a closure over path operands querying queryable for terms having a relationship with the input terms based on each operand. The terms extracted from the first operand are used as inputs for the next operand until a final set of terms is found. These terms are returned as RDF:Query::Solution bound the the variable ?path

Returns input terms

Queries queryable for objects where the input terms are subjects and the predicate is :p

Queries queryable for subjects where input terms are objects and the predicate is :p, by executing the reverse operand using input terms to get a set of output terms.

Returns the input terms satisfying the constrant.

Maps terms using (path :p), using them as terms for (path :q), then subsets these based on the constraint.

Examples:

empty path

(path)

forward path

(path :p)

reverse path

(path (reverse :p))

constraint

(path (constraint (path) :c, 1))

chained path elements

(path :p :q (constraint (path) :c, 1))

Constant Summary collapse

NAME =
:path

Constants inherited from SPARQL::Algebra::Operator

SPARQL::Algebra::Operator::ARITY, SPARQL::Algebra::Operator::IsURI, SPARQL::Algebra::Operator::URI

Constants included from SPARQL::Algebra::Expression

SPARQL::Algebra::Expression::PATTERN_PARENTS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

Instance Attribute Summary

Attributes included from SPARQL::Algebra::Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from SPARQL::Algebra::Evaluatable

#apply, #evaluate, #memoize, #replace_aggregate!, #replace_vars!

Methods included from SPARQL::Algebra::Query

#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift, #variables

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

Methods included from SPARQL::Algebra::Expression

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

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

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

Instance Method Details

#execute(queryable, options = {}) ⇒ RDF::Query::Solutions

Executes this operator using the given variable bindings and a starting term, returning zero or more terms at the end of the path.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

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

    ({}) options passed from query

Options Hash (options):

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/algebra/path.rb', line 47

def execute(queryable, options = {})
  solutions = RDF::Query::Solutions.new

  # Iterate updating terms, then create solutions from matched terms
  operands.inject(Array(options.fetch(:terms))) do |terms, op|
    case op
    when RDF::URI
      terms.map do |subject|
        queryable.query({subject: subject, predicate: op}).map(&:object)
      end.flatten
    when SPARQL::Algebra::Query
      # Get path solutions for each term for op
      op.execute(queryable, **options.merge(terms: terms)).map do |soln|
        soln.path
      end.flatten
    else
      raise NotImplementedError, "Unknown path operand #{op.inspect}"
    end
  end.each do |term|
    solutions << RDF::Query::Solution.new(path: term)
  end
  solutions
end