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 info.aduna.iteration.CloseableIteration;
09: import info.aduna.iteration.ConvertingIteration;
10:
11: import org.openrdf.model.Value;
12: import org.openrdf.query.BindingSet;
13: import org.openrdf.query.QueryEvaluationException;
14: import org.openrdf.query.algebra.Projection;
15: import org.openrdf.query.algebra.ProjectionElem;
16: import org.openrdf.query.algebra.ProjectionElemList;
17: import org.openrdf.query.algebra.evaluation.QueryBindingSet;
18:
19: public class ProjectionIterator
20: extends
21: ConvertingIteration<BindingSet, BindingSet, QueryEvaluationException> {
22:
23: /*-----------*
24: * Constants *
25: *-----------*/
26:
27: private final Projection projection;
28:
29: private final BindingSet parentBindings;
30:
31: /*--------------*
32: * Constructors *
33: *--------------*/
34:
35: public ProjectionIterator(
36: Projection projection,
37: CloseableIteration<BindingSet, QueryEvaluationException> iter,
38: BindingSet parentBindings) throws QueryEvaluationException {
39: super (iter);
40: this .projection = projection;
41: this .parentBindings = parentBindings;
42: }
43:
44: /*---------*
45: * Methods *
46: *---------*/
47:
48: @Override
49: protected BindingSet convert(BindingSet sourceBindings)
50: throws QueryEvaluationException {
51:
52: return project(projection.getProjectionElemList(),
53: sourceBindings, parentBindings);
54: }
55:
56: public static BindingSet project(ProjectionElemList projElemList,
57: BindingSet sourceBindings, BindingSet parentBindings) {
58: QueryBindingSet resultBindings = new QueryBindingSet(
59: parentBindings);
60:
61: for (ProjectionElem pe : projElemList.getElements()) {
62: Value targetValue = sourceBindings.getValue(pe
63: .getSourceName());
64: if (targetValue != null) {
65: // Potentially overwrites bindings from super
66: resultBindings.setBinding(pe.getTargetName(),
67: targetValue);
68: }
69: }
70:
71: return resultBindings;
72: }
73: }
|