Class: SPARQL::Algebra::Operator::Filter
- Inherits:
-
Binary
- Object
- SPARQL::Algebra::Operator
- Binary
- SPARQL::Algebra::Operator::Filter
- Includes:
- Query
- Defined in:
- lib/sparql/algebra/operator/filter.rb
Overview
The SPARQL GraphPattern filter
operator.
[68] Filter ::= ‘FILTER’ Constraint
Constant Summary collapse
- NAME =
[:filter]
Constants inherited from Binary
Constants inherited from SPARQL::Algebra::Operator
Constants included from Expression
Instance Attribute Summary
Attributes included from Query
Attributes inherited from SPARQL::Algebra::Operator
Instance Method Summary collapse
-
#execute(queryable, **options) {|solution| ... } ⇒ RDF::Query::Solutions
Executes this query on the given
queryable
graph or repository. -
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this operator.
-
#validate! ⇒ Object
If filtering a join of two BGPs (having the same graph name), don’t worry about validating, for shared ndvars, anyway,.
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
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.
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, **, &block) debug() {"Filter #{operands.first.to_sxp}"} opts = .merge(queryable: queryable, depth: [:depth].to_i + 1) @solutions = RDF::Query::Solutions() queryable.query(operands.last, **.merge(depth: [:depth].to_i + 1)) do |solution| # Re-bind to bindings, if defined, as they might not be found in solution [:bindings].each_binding do |name, value| solution[name] ||= value if operands.first.variables.include?(name) end if [:bindings] && operands.first.respond_to?(:variables) begin pass = boolean(operands.first.evaluate(solution, **opts)).true? debug() {"(filter) #{pass.inspect} #{solution.to_h.inspect}"} @solutions << solution if pass rescue debug() {"(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.
95 96 97 98 |
# File 'lib/sparql/algebra/operator/filter.rb', line 95 def to_sparql(**) filter_ops = operands.first.is_a?(Operator::Exprlist) ? operands.first.operands : [operands.first] str = operands.last.to_sparql(filter_ops: filter_ops, **) 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 |