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

Inherits:
Binary show all
Includes:
Query
Defined in:
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

Instance Attribute Summary

Attributes included from Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands, #options

Instance Method Summary collapse

Methods included from Query

#context=, #each_solution, #failed?, #matched?, #unshift, #variables

Methods inherited from Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

arity, base_uri, #base_uri, base_uri=, #boolean, #constant?, #eql?, #evaluatable?, evaluate, #executable?, for, #initialize, #inspect, #operand, prefixes, #prefixes, prefixes=, #to_sse, #to_sxp, #variable?

Methods included from Evaluatable

#apply, #evaluate, #memoize

Methods included from Expression

cast, #constant?, #evaluate, for, new, open, parse, #to_sse, #variable?

Constructor Details

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

Instance Method Details

#execute(queryable, options = {}) ⇒ 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

Returns:

See Also:


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sparql/algebra/operator/filter.rb', line 35

def execute(queryable, options = {})
  debug("Filter #{operands.first}", options)
  @solutions = operands.last.execute(queryable, options.merge(:depth => options[:depth].to_i + 1))
  debug("=>(before) #{@solutions.map(&:to_hash).inspect}", options)
  @solutions = @solutions.filter do |solution|
    # Evaluate the solution, which will return true or false
    #debug("===>(evaluate) #{operands.first.inspect} against #{solution.to_hash.inspect}", options)
    
    # From http://www.w3.org/TR/rdf-sparql-query/#tests
    # FILTERs eliminate any solutions that, when substituted into the expression, either
    # result in an effective boolean value of false or produce an error.
    begin
      res = boolean(operands.first.evaluate(solution)).true?
      debug("===>#{res} #{solution.to_hash.inspect}", options)
      res
    rescue
      debug("rescue(#{$!}): #{solution.to_hash.inspect}", options)
      false
    end
  end
  @solutions = RDF::Query::Solutions.new(@solutions)
  debug("=>(after) #{@solutions.map(&:to_hash).inspect}", options)
  @solutions
end

#optimizeUnion, RDF::Query

Returns an optimized version of this query.

Return optimized query

Returns:


66
67
68
# File 'lib/sparql/algebra/operator/filter.rb', line 66

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