Class: LD::Patch::Algebra::Constraint

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/constraint.rb

Overview

The LD Patch constraint operator.

A constraint is a query operator which either ensures that there is a single input node ("!" operator) or finds a set of nodes for a given path, optionally filtering those nodes with a particular predicate value.

Maps input terms to output terms using (path :p) returning those input terms that have at least a single solution.

Maps input terms to output terms using (path :p) and filters the input terms where the output term is 1.

Returns the single term from the input terms if there is a single input term.

Examples:

existence of path solutions

(constraint (path :p))

paths with property value

(constraint (path :p) 1)

unique terms


(constraint unique)

Constant Summary collapse

NAME =
:constraint

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

If the first operand is :unique

Parameters:

Options Hash (options):

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ld-patch-a8edc852261e/lib/ld/patch/algebra/constraint.rb', line 38

def execute(queryable, options = {})
  debug(options) {"Constraint"}
  terms = Array(options.fetch(:terms))
  op, value = operands

  results = if op == :unique
    terms.length == 1 ? terms : []
  else
    # op is a path, filter input terms based on the presense or absense of output terms. Additionally, if a constraint value is given, output terms must equal that value
    terms.select do |term|
      output_terms = op.execute(queryable, options.merge(terms: [term])).map(&:path)
      output_terms = output_terms.select {|t| t == value} if value
      !output_terms.empty?
    end
  end
  RDF::Query::Solutions.new(results.map {|t| RDF::Query::Solution.new(path: t)})
end