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.algebra;
007:
008: import java.util.HashSet;
009: import java.util.Set;
010:
011: /**
012: * A tuple expression that matches a statement pattern against an RDF graph.
013: * Statement patterns can be targeted at one of three context scopes: all
014: * contexts, null context only, or named contexts only.
015: */
016: public class StatementPattern extends QueryModelNodeBase implements
017: TupleExpr {
018:
019: /*------------*
020: * enum Scope *
021: *------------*/
022:
023: /**
024: * Indicates the scope of the statement pattern.
025: */
026: public enum Scope {
027: /**
028: * Scope for patterns that should be matched against statements from the
029: * default contexts.
030: */
031: DEFAULT_CONTEXTS,
032:
033: /**
034: * Scope for patterns that should be matched against statements from named
035: * contexts only.
036: */
037: NAMED_CONTEXTS
038: }
039:
040: /*-----------*
041: * Variables *
042: *-----------*/
043:
044: private Scope scope;
045:
046: private Var subjectVar;
047:
048: private Var predicateVar;
049:
050: private Var objectVar;
051:
052: private Var contextVar;
053:
054: /*--------------*
055: * Constructors *
056: *--------------*/
057:
058: public StatementPattern() {
059: }
060:
061: /**
062: * Creates a statement pattern that matches a subject-, predicate- and object
063: * variable against statements from all contexts.
064: */
065: public StatementPattern(Var subject, Var predicate, Var object) {
066: this (Scope.DEFAULT_CONTEXTS, subject, predicate, object);
067: }
068:
069: /**
070: * Creates a statement pattern that matches a subject-, predicate- and object
071: * variable against statements from the specified context scope.
072: */
073: public StatementPattern(Scope scope, Var subject, Var predicate,
074: Var object) {
075: this (scope, subject, predicate, object, null);
076: }
077:
078: /**
079: * Creates a statement pattern that matches a subject-, predicate-, object-
080: * and context variable against statements from all contexts.
081: */
082: public StatementPattern(Var subject, Var predicate, Var object,
083: Var context) {
084: this (Scope.DEFAULT_CONTEXTS, subject, predicate, object,
085: context);
086: }
087:
088: /**
089: * Creates a statement pattern that matches a subject-, predicate-, object-
090: * and context variable against statements from the specified context scope.
091: */
092: public StatementPattern(Scope scope, Var subjVar, Var predVar,
093: Var objVar, Var conVar) {
094: setScope(scope);
095: setSubjectVar(subjVar);
096: setPredicateVar(predVar);
097: setObjectVar(objVar);
098: setContextVar(conVar);
099: }
100:
101: /*---------*
102: * Methods *
103: *---------*/
104:
105: /**
106: * Gets the context scope for the statement pattern.
107: */
108: public Scope getScope() {
109: return scope;
110: }
111:
112: /**
113: * Sets the context scope for the statement pattern.
114: */
115: public void setScope(Scope scope) {
116: assert scope != null : "scope must not be null";
117: this .scope = scope;
118: }
119:
120: public Var getSubjectVar() {
121: return subjectVar;
122: }
123:
124: public void setSubjectVar(Var subject) {
125: assert subject != null : "subject must not be null";
126: subject.setParentNode(this );
127: subjectVar = subject;
128: }
129:
130: public Var getPredicateVar() {
131: return predicateVar;
132: }
133:
134: public void setPredicateVar(Var predicate) {
135: assert predicate != null : "predicate must not be null";
136: predicate.setParentNode(this );
137: predicateVar = predicate;
138: }
139:
140: public Var getObjectVar() {
141: return objectVar;
142: }
143:
144: public void setObjectVar(Var object) {
145: assert object != null : "object must not be null";
146: object.setParentNode(this );
147: objectVar = object;
148: }
149:
150: /**
151: * Returns the context variable, if available.
152: *
153: * @return
154: */
155: public Var getContextVar() {
156: return contextVar;
157: }
158:
159: public void setContextVar(Var context) {
160: if (context != null) {
161: context.setParentNode(this );
162: }
163: contextVar = context;
164: }
165:
166: public Set<String> getBindingNames() {
167: Set<String> bindingNames = new HashSet<String>(8);
168:
169: if (subjectVar != null) {
170: bindingNames.add(subjectVar.getName());
171: }
172: if (predicateVar != null) {
173: bindingNames.add(predicateVar.getName());
174: }
175: if (objectVar != null) {
176: bindingNames.add(objectVar.getName());
177: }
178: if (contextVar != null) {
179: bindingNames.add(contextVar.getName());
180: }
181:
182: return bindingNames;
183: }
184:
185: public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
186: throws X {
187: visitor.meet(this );
188: }
189:
190: @Override
191: public <X extends Exception> void visitChildren(
192: QueryModelVisitor<X> visitor) throws X {
193: if (subjectVar != null) {
194: subjectVar.visit(visitor);
195: }
196: if (predicateVar != null) {
197: predicateVar.visit(visitor);
198: }
199: if (objectVar != null) {
200: objectVar.visit(visitor);
201: }
202: if (contextVar != null) {
203: contextVar.visit(visitor);
204: }
205:
206: super .visitChildren(visitor);
207: }
208:
209: @Override
210: public void replaceChildNode(QueryModelNode current,
211: QueryModelNode replacement) {
212: if (subjectVar == current) {
213: setSubjectVar((Var) replacement);
214: } else if (predicateVar == current) {
215: setPredicateVar((Var) replacement);
216: } else if (objectVar == current) {
217: setObjectVar((Var) replacement);
218: } else if (contextVar == current) {
219: setContextVar((Var) replacement);
220: } else {
221: super .replaceChildNode(current, replacement);
222: }
223: }
224:
225: @Override
226: public String getSignature() {
227: StringBuilder sb = new StringBuilder(128);
228:
229: sb.append(super .getSignature());
230:
231: if (scope == Scope.NAMED_CONTEXTS) {
232: sb.append(" FROM NAMED CONTEXT");
233: }
234:
235: return sb.toString();
236: }
237:
238: @Override
239: public StatementPattern clone() {
240: StatementPattern clone = (StatementPattern) super.clone();
241: clone.setSubjectVar(getSubjectVar().clone());
242: clone.setPredicateVar(getPredicateVar().clone());
243: clone.setObjectVar(getObjectVar().clone());
244:
245: if (getContextVar() != null) {
246: clone.setContextVar(getContextVar().clone());
247: }
248:
249: return clone;
250: }
251: }
|