View Javadoc

1   /*
2    * Copyright James Leigh (c) 2006, 2007.
3    *
4    * Licensed under the Aduna BSD-style license.
5    */
6   package org.openrdf.repository.augur.model;
7   
8   import org.openrdf.model.Resource;
9   import org.openrdf.model.URI;
10  import org.openrdf.model.Value;
11  import org.openrdf.query.algebra.Filter;
12  import org.openrdf.query.algebra.Or;
13  import org.openrdf.query.algebra.SameTerm;
14  import org.openrdf.query.algebra.StatementPattern;
15  import org.openrdf.query.algebra.TupleExpr;
16  import org.openrdf.query.algebra.ValueConstant;
17  import org.openrdf.query.algebra.ValueExpr;
18  import org.openrdf.query.algebra.Var;
19  import org.openrdf.query.algebra.StatementPattern.Scope;
20  
21  /**
22   * AugurNode that was created with a statement pattern.
23   * 
24   * @author James Leigh
25   * 
26   */
27  public class AugurStatementNode extends AugurNode {
28  
29  	private String subjectName;
30  
31  	private String predicateName;
32  
33  	private String objectName;
34  
35  	private String contextName;
36  
37  	public String getSubjectName() {
38  		return subjectName;
39  	}
40  
41  	protected void setSubjectName(String subjectName) {
42  		this.subjectName = subjectName;
43  	}
44  
45  	public String getPredicateName() {
46  		return predicateName;
47  	}
48  
49  	public String getObjectName() {
50  		return objectName;
51  	}
52  
53  	protected void setObjectName(String objectName) {
54  		this.objectName = objectName;
55  	}
56  
57  	public String getContextName() {
58  		return contextName;
59  	}
60  
61  	public TupleExpr setStatementPattern(Resource subj, URI pred, Value obj,
62  			Resource... contexts) {
63  		subjectName = "_subj" + getDepth();
64  		predicateName = "_pred" + getDepth();
65  		objectName = "_obj" + getDepth();
66  		contextName = "_ctx" + getDepth();
67  		Var subjVar = new Var(subjectName, subj);
68  		Var predVar = new Var(predicateName, pred);
69  		Var objVar = new Var(objectName, obj);
70  		return createTuplePattern(subjVar, predVar, objVar, contextName, contexts);
71  	}
72  
73  	protected TupleExpr createTuplePattern(Var subjVar, Var predVar, Var objVar, String contextName, Resource... contexts) {
74  		if (contexts.length > 1) {
75  			StatementPattern sp;
76  			ValueExpr condition = null;
77  			Scope scope = Scope.NAMED_CONTEXTS;
78  			Var ctxVar = new Var(contextName);
79  			sp = new StatementPattern(scope, subjVar, predVar, objVar, ctxVar);
80  			for (Resource ctx : contexts) {
81  				ValueConstant val = new ValueConstant(ctx);
82  				SameTerm eq = new SameTerm(ctxVar, val);
83  				if (condition == null) {
84  					condition = eq;
85  				} else {
86  					condition = new Or(condition, eq);
87  				}
88  			}
89  			return new Filter(sp, condition);
90  		} else if (contexts.length == 1) {
91  			Scope scope = Scope.NAMED_CONTEXTS;
92  			Var ctxVar = new Var(contextName, contexts[0]);
93  			return new StatementPattern(scope, subjVar, predVar, objVar, ctxVar);
94  		} else {
95  			Scope scope = Scope.DEFAULT_CONTEXTS;
96  			return new StatementPattern(scope, subjVar, predVar, objVar, new Var(contextName));
97  		}
98  	}
99  
100 }