01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.algebra.evaluation.impl;
07:
08: import org.openrdf.model.Value;
09: import org.openrdf.query.BindingSet;
10: import org.openrdf.query.Dataset;
11: import org.openrdf.query.algebra.TupleExpr;
12: import org.openrdf.query.algebra.Var;
13: import org.openrdf.query.algebra.evaluation.QueryOptimizer;
14: import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
15:
16: /**
17: * Assigns values to variables based on a supplied set of bindings.
18: *
19: * @author Arjohn Kampman
20: */
21: public class BindingAssigner implements QueryOptimizer {
22:
23: public void optimize(TupleExpr tupleExpr, Dataset dataset,
24: BindingSet bindings) {
25: if (bindings.size() > 0) {
26: tupleExpr.visit(new VarVisitor(bindings));
27: }
28: }
29:
30: protected class VarVisitor extends
31: QueryModelVisitorBase<RuntimeException> {
32:
33: protected BindingSet bindings;
34:
35: public VarVisitor(BindingSet bindings) {
36: this .bindings = bindings;
37: }
38:
39: @Override
40: public void meet(Var var) {
41: if (!var.hasValue() && bindings.hasBinding(var.getName())) {
42: Value value = bindings.getValue(var.getName());
43: var.setValue(value);
44: }
45: }
46: }
47: }
|