Class: LD::Patch::Algebra::Reverse

Inherits:
SPARQL::Algebra::Operator::Unary
  • Object
show all
Includes:
SPARQL::Algebra::Evaluatable, SPARQL::Algebra::Query
Defined in:
lib/ld/patch/algebra/reverse.rb

Overview

The LD Patch ‘reverse` operator

Finds all the terms which are the subject of triples where the ‘operand` is the predicate and input terms are objects.

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.

Examples:

(reverse :p)

Constant Summary collapse

NAME =
:reverse

Instance Method Summary collapse

Instance Method Details

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

Executes this upate on the given ‘writable` graph or repository.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to write

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

    any additional options

Options Hash (options):

  • starting (Array<RDF::Term>)

    terms

Returns:

  • (RDF::Query::Solutions)

    solutions with ‘:term` mapping



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ld/patch/algebra/reverse.rb', line 27

def execute(queryable, options = {})
  debug(options) {"Reverse"}
  op = operand(0)
  terms = Array(options.fetch(:terms))

  results = terms.map do |object|
    queryable.query({object: object, predicate: op}).map(&:subject)
  end.flatten

  RDF::Query::Solutions.new(results.map {|t| RDF::Query::Solution.new(path: t)})
end