Class: LD::Patch::Algebra::Bind

Inherits:
SPARQL::Algebra::Operator::Ternary 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/bind.rb

Overview

The LD Patch bind operator.

The Bind operation is used to bind a single term to a variable.

Examples:

simple value to variable binding

Bind ?x <http://example.org/s> . #=>
(bind ?x <http://example.org/s> ())

constant path (filter-forward.ldpatch)

Bind ?x <http://example.org/s> / <http://example.org/p1> . #=>
(bind ?x <http://example.org/s> (<http://example.org/p1>))

list index (path-at.ldpatch)

Bind ?x <http://example.org/s> / 1 . #=>
(bind ?x <http://example.org/s> ((index 1)))

reverse (path-backward.ldpath)

Bind ?x <http://example.org/s> / ^<http://example.org/p1> . #=>
(bind ?x <http://example.org/s> ((reverse <http://example.org/p1>)))

constraint (path-filter-equal.ldpatch)

Bind ?x <http://example.org/s> / <http://example.org/p2> [  / <http://example.org/l> = "b" ] . #=>
(bind ?x <http://example.org/s> (
  <http://example.org/p2>
  (constraint (<http://example.org/l>) "b")))

constraint (path-filter.ldpatch)

Bind ?x <http://example.org/s> / <http://example.org/p2> [  / <http://example.org/p1> ] . #=>
(bind ?x <http://example.org/s> (
  <http://example.org/p2>
  (constraint (<http://example.org/l>))))

starting with a literal

Bind ?x "a" / ^<http://example.org/l> / ^<http://example.org/p2> . #=>
(bind ?x "a" (
  (reverse <http://example.org/l>)
  (reverse <http://example.org/p2>)))

unicity (path-unicity.ldpath)

Bind ?x <http://example.org/s> / <http://example.org/p1> ! . #=>
SELECT ?x
WHERE {<http//example.org/s> <http://example.org/p1> ?x}
GROUP BY ?x
HAVING COUNT(?x) = 1
(bind ?x ?0
  ((pattern <http://example.org/s> <http://example.org/p1> ??0)
   (unique ??0)))

Constant Summary collapse

NAME =
:bind

Constants inherited from SPARQL::Algebra::Operator::Ternary

SPARQL::Algebra::Operator::Ternary::ARITY

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

#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, #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::Ternary

Instance Method Details

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

Maps the value to a single output term by executing the path and updates bindings with var set to that output term

Parameters:

Options Hash (options):

Returns:

  • (RDF::Query::Solutions)

    A single solution including passed bindings with var bound to the solution.

Raises:

  • (Error)

    If path does not evaluate to a single term or if unbound variables are used.

See Also:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/algebra/bind.rb', line 69

def execute(queryable, options = {})
  debug(options) {"Bind"}
  bindings = options.fetch(:bindings)
  solution = bindings.first
  var, value, path = operands

  # Bind variables to path
  if value.variable?
    raise LD::Patch::Error.new("Operand uses unbound variable #{value.inspect}", code: 400) unless solution.bound?(value)
    value = solution[value]
  end

  path = path.dup.replace_vars! do |v|
    raise Error, "Operand uses unbound variable #{v.inspect}" unless solution.bound?(v)
    solution[v]
  end
  results = path.execute(queryable, terms: [value])
  raise LD::Patch::Error, "Bind path bound to #{results.length} terms, expected just one" unless results.length == 1
  RDF::Query::Solutions.new [solution.merge(var.to_sym => results.first.path)]
end