1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.tripleset.impl;
19
20 import java.lang.reflect.Array;
21
22 import info.aduna.iteration.CloseableIteration;
23 import info.aduna.iteration.Iteration;
24 import info.aduna.iteration.UnionIteration;
25
26 import org.openrdf.model.Resource;
27 import org.openrdf.model.Statement;
28 import org.openrdf.model.URI;
29 import org.openrdf.model.Value;
30 import org.openrdf.model.ValueFactory;
31 import org.openrdf.query.BindingSet;
32 import org.openrdf.query.Dataset;
33 import org.openrdf.query.QueryEvaluationException;
34 import org.openrdf.query.algebra.TupleExpr;
35 import org.openrdf.query.algebra.evaluation.EvaluationStrategy;
36 import org.openrdf.query.algebra.evaluation.TripleSource;
37 import org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl;
38
39 import com.ontotext.ordi.exception.ORDIException;
40 import com.ontotext.ordi.iterator.CloseableIterator;
41 import com.ontotext.ordi.iterator.ConvertingIteratorImpl;
42 import com.ontotext.ordi.iterator.ExplicitStatementIteratorImpl;
43 import com.ontotext.ordi.iterator.WrappedSesameIteratorImpl;
44 import com.ontotext.ordi.tripleset.TConnection;
45 import com.ontotext.ordi.tripleset.TSource;
46 import com.ontotext.ordi.tripleset.TStatement;
47
48 /**
49 * This class implements the evaluation of TupleExpr against the
50 * {@link TConnection}'s search method.
51 *
52 * @author vassil
53 *
54 */
55 public abstract class TQueryConnectionImpl extends TConnectionImpl {
56
57 protected final TSource tsource;
58
59 public TQueryConnectionImpl(TSource source) {
60 super(source);
61 tsource = source;
62 }
63
64 public CloseableIterator<? extends BindingSet> evaluate(
65 TupleExpr tupleExpr, BindingSet bindings, boolean includeInferred)
66 throws ORDIException {
67 TripleSource tripleSource = new ORDITripleSource(tsource, this,
68 includeInferred, null);
69 EvaluationStrategy strategy = new EvaluationStrategyImpl(tripleSource);
70 try {
71 return new WrappedSesameIteratorImpl<BindingSet>(strategy.evaluate(
72 tupleExpr, bindings));
73 } catch (QueryEvaluationException e) {
74 throw new ORDIException(e);
75 }
76 }
77
78 public CloseableIterator<? extends BindingSet> evaluate(
79 TupleExpr tupleExpr, BindingSet bindings, Dataset dataset,
80 boolean includeInferred, URI tripleset) throws ORDIException {
81 TripleSource tripleSource = new ORDITripleSource(tsource, this,
82 includeInferred, tripleset);
83 EvaluationStrategy strategy = new EvaluationStrategyImpl(tripleSource,
84 dataset);
85 try {
86 return new WrappedSesameIteratorImpl<BindingSet>(strategy.evaluate(
87 tupleExpr, bindings));
88 } catch (QueryEvaluationException e) {
89 throw new ORDIException(e);
90 }
91 }
92
93 public class ORDITripleSource implements TripleSource {
94
95 protected final TConnection connection;
96 protected final TSource source;
97 protected final URI tripleset;
98 protected final boolean ignoreTs;
99 protected final boolean includeInferred;
100
101 public ORDITripleSource(TSource source, TConnection connection,
102 boolean includeInferred, URI tripleset) {
103 if (connection == null || source == null) {
104 throw new IllegalArgumentException();
105 }
106 this.connection = connection;
107 this.source = source;
108 this.tripleset = tripleset;
109 this.ignoreTs = tripleset == null ? true : false;
110 this.includeInferred = includeInferred;
111 }
112
113 public ValueFactory getValueFactory() {
114 return source.getTriplesetFactory();
115 }
116
117 @SuppressWarnings("unchecked")
118 public CloseableIteration<? extends Statement, QueryEvaluationException> getStatements(
119 Resource subj, URI pred, Value obj, Resource... contexts) {
120 try {
121 CloseableIteration<? extends Statement, QueryEvaluationException> result = null;
122
123 if (contexts.length == 0) {
124 result = new ConvertingIteratorImpl<TStatement>(connection
125 .search(subj, pred, obj, (URI) null, tripleset));
126 } else if (contexts.length == 1) {
127 URI graph = contexts[0] instanceof URI ? (URI) contexts[0]
128 : null;
129 result = new ConvertingIteratorImpl<TStatement>(connection
130 .search(subj, pred, obj, graph, tripleset));
131 } else {
132 Iteration<? extends Statement, QueryEvaluationException>[] iters =
133 (Iteration<? extends Statement, QueryEvaluationException>[])
134 Array.newInstance(Iteration.class, contexts.length);
135
136 for (int i = 0; i < contexts.length; i++) {
137 iters[i] = new ConvertingIteratorImpl<TStatement>(
138 connection.search(subj, pred, obj,
139 (URI) contexts[i], tripleset));
140 }
141 result = new UnionIteration<Statement, QueryEvaluationException>(
142 iters);
143 }
144
145 if (includeInferred == false)
146 return new ExplicitStatementIteratorImpl(result);
147 return result;
148 } catch (ORDIException e) {
149 throw new RuntimeException(e);
150 }
151 }
152 }
153 }