Class: SPARQL::Algebra::Operator::Filter

Inherits:
Binary show all
Includes:
Query
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/filter.rb

Overview

The SPARQL GraphPattern filter operator.

Examples:

(select (?v)
  (project (?v)
    (filter (= ?v 2)
      (bgp (triple ?s <http://example/p> ?v)))))

See Also:

Constant Summary collapse

NAME =
[:filter]

Constants inherited from Binary

Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

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 Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #boolean, #constant?, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #variable?, #vars

Methods included from Expression

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

Constructor Details

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

Instance Method Details

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

Executes this query on the given queryable graph or repository. Then it passes each solution through one or more filters and removes those that evaluate to false or generate a TypeError.

Note that the last operand returns a solution set, while the first is an expression. This may be a variable, simple expression, or exprlist.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to query

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

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/filter.rb', line 39

def execute(queryable, options = {}, &block)
  debug(options) {"Filter #{operands.first.to_sxp}"}
  opts = options.merge(queryable: queryable, depth: options[:depth].to_i + 1)
  @solutions = RDF::Query::Solutions()
  queryable.query(operands.last, options.merge(depth: options[:depth].to_i + 1)) do |solution|
    begin
      pass = boolean(operands.first.evaluate(solution, opts)).true?
      debug(options) {"(filter) #{pass.inspect} #{solution.to_h.inspect}"}
      @solutions << solution if pass
    rescue
      debug(options) {"(filter) rescue(#{$!}): #{solution.to_h.inspect}"}
    end
  end
  @solutions.each(&block) if block_given?
  @solutions
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

Return optimized query

Returns:



79
80
81
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/filter.rb', line 79

def optimize
  operands = operands.map(&:optimize)
end

#validate!Object

If filtering a join of two BGPs (having the same graph name), don't worry about validating, for shared ndvars, anyway,

  (filter (regex ?homepage "^http://example.org/" "")
    (join
      (bgp (triple ??who :homepage ?homepage))
      (bgp (triple ??who :schoolHomepage ?schoolPage))))))

is legitimate



64
65
66
67
68
69
70
71
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-b66c70fb77a0/lib/sparql/algebra/operator/filter.rb', line 64

def validate!
  unless (join = operands.last).is_a?(Join) &&
          join.operands.all? {|op| op.is_a?(RDF::Query)} &&
          join.operands.map(&:graph_name).uniq.length == 1
    operands.last.validate!
  end
  self
end