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.

[68] Filter ::= ‘FILTER’ Constraint

Examples:

SPARQL Grammar

SELECT ?v
{ 
  ?s <http://example/p> ?v
  FILTER(?v = 2)
}

SSE

(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

Constants included from Expression

Expression::PATTERN_PARENTS

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=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #variable?, #variables, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, 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})

    any additional keyword options

Yields:

  • (solution)

    each matching solution

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sparql/algebra/operator/filter.rb', line 47

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|
    # Re-bind to bindings, if defined, as they might not be found in solution
    options[:bindings].each_binding do |name, value|
      solution[name] ||= value if operands.first.variables.include?(name)
    end if options[:bindings] && operands.first.respond_to?(:variables)

    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

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Provides filters to descendant query.

If filter operation is an Exprlist, then separate into multiple filter ops.

Returns:

  • (String)


95
96
97
98
# File 'lib/sparql/algebra/operator/filter.rb', line 95

def to_sparql(**options)
  filter_ops = operands.first.is_a?(Operator::Exprlist) ? operands.first.operands : [operands.first]
  str = operands.last.to_sparql(filter_ops: filter_ops, **options)
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



77
78
79
80
81
82
83
84
# File 'lib/sparql/algebra/operator/filter.rb', line 77

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