001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.parser.sparql;
007:
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.Collections;
011: import java.util.List;
012:
013: import org.openrdf.query.algebra.Join;
014: import org.openrdf.query.algebra.LeftJoin;
015: import org.openrdf.query.algebra.Filter;
016: import org.openrdf.query.algebra.SingletonSet;
017: import org.openrdf.query.algebra.StatementPattern;
018: import org.openrdf.query.algebra.TupleExpr;
019: import org.openrdf.query.algebra.ValueExpr;
020: import org.openrdf.query.algebra.Var;
021:
022: /**
023: * A graph pattern consisting of (required and optional) tuple expressions and
024: * boolean constraints.
025: *
026: * @author Arjohn Kampman
027: */
028: class GraphPattern {
029:
030: /**
031: * The context of this graph pattern.
032: */
033: private Var contextVar;
034:
035: /**
036: * The StatementPattern-scope of this graph pattern.
037: */
038: private StatementPattern.Scope spScope = StatementPattern.Scope.DEFAULT_CONTEXTS;
039:
040: /**
041: * The required tuple expressions in this graph pattern.
042: */
043: private List<TupleExpr> requiredTEs = new ArrayList<TupleExpr>();
044:
045: /**
046: * The optional tuple expressions in this graph pattern.
047: */
048: private List<TupleExpr> optionalTEs = new ArrayList<TupleExpr>();
049:
050: /**
051: * The boolean constraints in this graph pattern.
052: */
053: private List<ValueExpr> constraints = new ArrayList<ValueExpr>();
054:
055: /**
056: * Creates a new graph pattern.
057: */
058: public GraphPattern() {
059: }
060:
061: /**
062: * Creates a new graph pattern that inherits the context and scope from a
063: * parent graph pattern.
064: */
065: public GraphPattern(GraphPattern parent) {
066: contextVar = parent.contextVar;
067: spScope = parent.spScope;
068: }
069:
070: public void setContextVar(Var contextVar) {
071: this .contextVar = contextVar;
072: }
073:
074: public Var getContextVar() {
075: return contextVar;
076: }
077:
078: public void setStatementPatternScope(StatementPattern.Scope spScope) {
079: this .spScope = spScope;
080: }
081:
082: public StatementPattern.Scope getStatementPatternScope() {
083: return spScope;
084: }
085:
086: public void addRequiredTE(TupleExpr te) {
087: requiredTEs.add(te);
088: }
089:
090: public void addRequiredSP(Var subjVar, Var predVar, Var objVar) {
091: addRequiredTE(new StatementPattern(spScope, subjVar, predVar,
092: objVar, contextVar));
093: }
094:
095: public List<TupleExpr> getRequiredTEs() {
096: return Collections.unmodifiableList(requiredTEs);
097: }
098:
099: public void addOptionalTE(TupleExpr te) {
100: optionalTEs.add(te);
101: }
102:
103: public List<TupleExpr> getOptionalTEs() {
104: return Collections.unmodifiableList(optionalTEs);
105: }
106:
107: public void addConstraint(ValueExpr constraint) {
108: constraints.add(constraint);
109: }
110:
111: public void addConstraints(Collection<ValueExpr> constraints) {
112: this .constraints.addAll(constraints);
113: }
114:
115: public List<ValueExpr> getConstraints() {
116: return Collections.unmodifiableList(constraints);
117: }
118:
119: public List<ValueExpr> removeAllConstraints() {
120: List<ValueExpr> constraints = this .constraints;
121: this .constraints = new ArrayList<ValueExpr>();
122: return constraints;
123: }
124:
125: /**
126: * Removes all tuple expressions and constraints.
127: */
128: public void clear() {
129: requiredTEs.clear();
130: optionalTEs.clear();
131: constraints.clear();
132: }
133:
134: /**
135: * Builds a combined tuple expression from the tuple expressions and
136: * constraints in this graph pattern.
137: *
138: * @return A tuple expression for this graph pattern.
139: */
140: public TupleExpr buildTupleExpr() {
141: TupleExpr result;
142:
143: if (requiredTEs.isEmpty()) {
144: result = new SingletonSet();
145: } else {
146: result = requiredTEs.get(0);
147:
148: for (int i = 1; i < requiredTEs.size(); i++) {
149: result = new Join(result, requiredTEs.get(i));
150: }
151: }
152:
153: for (TupleExpr optTE : optionalTEs) {
154: result = new LeftJoin(result, optTE);
155: }
156:
157: for (ValueExpr constraint : constraints) {
158: result = new Filter(result, constraint);
159: }
160:
161: return result;
162: }
163: }
|