01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.algebra.evaluation.iterator;
07:
08: import java.util.Set;
09:
10: import info.aduna.iteration.CloseableIteration;
11: import info.aduna.iteration.FilterIteration;
12:
13: import org.openrdf.query.BindingSet;
14: import org.openrdf.query.QueryEvaluationException;
15: import org.openrdf.query.algebra.Filter;
16: import org.openrdf.query.algebra.evaluation.QueryBindingSet;
17: import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
18: import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
19:
20: public class FilterIterator extends
21: FilterIteration<BindingSet, QueryEvaluationException> {
22:
23: /*-----------*
24: * Constants *
25: *-----------*/
26:
27: private final Filter filter;
28:
29: private final EvaluationStrategy strategy;
30:
31: /**
32: * The set of binding names that are "in scope" for the filter. The filter
33: * must not include bindings that are (only) included because of the
34: * depth-first evaluation strategy in the evaluation of the constraint.
35: */
36: private final Set<String> scopeBindingNames;
37:
38: /*--------------*
39: * Constructors *
40: *--------------*/
41:
42: public FilterIterator(
43: Filter filter,
44: CloseableIteration<BindingSet, QueryEvaluationException> iter,
45: EvaluationStrategy strategy)
46: throws QueryEvaluationException {
47: super (iter);
48: this .filter = filter;
49: this .strategy = strategy;
50: this .scopeBindingNames = filter.getBindingNames();
51: }
52:
53: /*---------*
54: * Methods *
55: *---------*/
56:
57: @Override
58: protected boolean accept(BindingSet bindings)
59: throws QueryEvaluationException {
60: try {
61: // Limit the bindings to the ones that are in scope for this filter
62: QueryBindingSet scopeBindings = new QueryBindingSet(
63: bindings);
64: scopeBindings.retainAll(scopeBindingNames);
65:
66: return strategy
67: .isTrue(filter.getCondition(), scopeBindings);
68: } catch (ValueExprEvaluationException e) {
69: // failed to evaluate condition
70: return false;
71: }
72: }
73: }
|