1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.trree;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Set;
24
25 import org.openrdf.model.Resource;
26 import org.openrdf.model.URI;
27 import org.openrdf.model.Value;
28
29 import com.ontotext.ordi.exception.ORDIException;
30 import com.ontotext.ordi.iterator.CloseableIterator;
31 import com.ontotext.ordi.iterator.CloseableIteratorImpl;
32 import com.ontotext.ordi.tripleset.Listener;
33 import com.ontotext.ordi.tripleset.TStatement;
34 import com.ontotext.ordi.tripleset.impl.TQueryConnectionImpl;
35 import com.ontotext.trree.owlim_ext.AbstractInferencer;
36 import com.ontotext.trree.owlim_ext.AbstractRepository;
37 import com.ontotext.trree.owlim_ext.EntityPool;
38 import com.ontotext.trree.owlim_ext.StatementIdIterator;
39
40 public class TRREEConnection extends TQueryConnectionImpl {
41
42 private AbstractRepository repository;
43 private AbstractInferencer inferencer;
44 private EntityPool pool;
45 private boolean isClosed = false;
46
47 public TRREEConnection(TRREEAdapter source, AbstractRepository repository,
48 EntityPool pool, AbstractInferencer inferencer) {
49 super(source);
50 if (repository == null || pool == null || inferencer == null) {
51 throw new IllegalArgumentException();
52 }
53 this.repository = repository;
54 this.inferencer = inferencer;
55 this.pool = pool;
56 }
57
58 public TStatement addStatement(Resource subj, URI pred, Value obj,
59 URI namedGraph) throws ORDIException {
60 if (subj == null || pred == null || obj == null || namedGraph == null) {
61 throw new IllegalArgumentException(
62 "This method does not support wildcard parameters!");
63 }
64 if (isClosed) {
65 throw new IllegalStateException("The connection was closed!");
66 }
67
68 int isubj = pool.createId(subj);
69 int ipred = pool.createId(pred);
70 int iobj = pool.createId(obj);
71 int ing = pool.createId(namedGraph);
72 inferencer.addStatement(isubj, ipred, iobj, ing);
73
74 return new LazyTStatementImpl(isubj, ipred, iobj, ing, pool, repository);
75 }
76
77 public TStatement addStatement(Resource subj, URI pred, Value obj,
78 URI namedGraph, URI... ts) throws ORDIException {
79 if (subj == null || pred == null || obj == null || namedGraph == null
80 || ts == null) {
81 throw new IllegalArgumentException(
82 "This method does not support wildcard parameters!");
83 }
84 if (isClosed) {
85 throw new IllegalStateException("The connection was closed!");
86 }
87 int isubj = pool.createId(subj);
88 int ipred = pool.createId(pred);
89 int iobj = pool.createId(obj);
90 int ing = pool.createId(namedGraph);
91 inferencer.addStatement(isubj, ipred, iobj, ing);
92
93 for (int i = 0; i < ts.length; i++) {
94 repository.mapToQuadSet(isubj, ipred, iobj, ing, pool
95 .createId(ts[i]));
96 }
97 return new LazyTStatementImpl(isubj, ipred, iobj, ing, pool, repository);
98 }
99
100 public int associateTripleset(Resource subj, URI pred, Value obj,
101 URI namedGraph, URI... ts) {
102 if (ts == null || ts.length == 0) {
103 throw new IllegalArgumentException(
104 "You have to specify at least one tripleset!");
105 }
106 if (isClosed) {
107 throw new IllegalStateException("The connection was closed!");
108 }
109 int isubj = subj == null ? 0 : pool.createId(subj);
110 int ipred = pred == null ? 0 : pool.createId(pred);
111 int iobj = obj == null ? 0 : pool.createId(obj);
112 int ing = namedGraph == null ? 0 : pool.createId(namedGraph);
113 Set<Integer> its = new HashSet<Integer>();
114 for (int i = 0; i < ts.length; i++) {
115 if (ts[i] == null) {
116 throw new IllegalArgumentException(
117 "Null values for tripleset not supported!");
118 }
119 its.add(ts[i] == null ? 0 : pool.createId(ts[i]));
120 }
121
122 StatementIdIterator iter = repository.getExplicitStatements(isubj,
123 ipred, iobj, ing);
124 int count = 0;
125
126 while (iter.hasNext()) {
127 boolean mapped = false;
128 for (Iterator<Integer> tsIter = its.iterator(); tsIter.hasNext();) {
129 mapped |= repository.mapToQuadSet(iter.Subj, iter.Pred,
130 iter.Obj, iter.Context, tsIter.next());
131 }
132 iter.next();
133 if (mapped)
134 count++;
135 }
136
137 return count;
138 }
139
140 public int deassociateTripleset(Resource subj, URI pred, Value obj,
141 URI namedGraph, URI... ts) {
142 if (ts == null || ts.length == 0) {
143 throw new IllegalArgumentException(
144 "You have to specify at least one tripleset!");
145 }
146 if (isClosed) {
147 throw new IllegalStateException("The connection was closed!");
148 }
149 int isubj = subj == null ? 0 : pool.createId(subj);
150 int ipred = pred == null ? 0 : pool.createId(pred);
151 int iobj = obj == null ? 0 : pool.createId(obj);
152 int ing = namedGraph == null ? 0 : pool.createId(namedGraph);
153 Set<Integer> its = new HashSet<Integer>();
154 for (int i = 0; i < ts.length; i++) {
155 if (ts[i] == null) {
156 throw new IllegalArgumentException(
157 "Null values for tripleset not supported!");
158 }
159 its.add(ts[i] == null ? 0 : pool.createId(ts[i]));
160 }
161
162 StatementIdIterator iter = repository.getExplicitStatements(isubj,
163 ipred, iobj, ing);
164 int count = 0;
165
166 while (iter.hasNext()) {
167 boolean mapped = false;
168 for (Iterator<Integer> tsIter = its.iterator(); tsIter.hasNext();) {
169 mapped |= repository.removeMappingFromQuadSet(iter.Subj,
170 iter.Pred, iter.Obj, iter.Context, tsIter.next());
171 }
172 iter.next();
173 if (mapped)
174 count++;
175 }
176
177 return count;
178 }
179
180 public CloseableIterator<? extends Value> getValuesOfType(URI uri)
181 throws ORDIException {
182 if (isClosed) {
183 throw new IllegalStateException("The connection was closed!");
184 }
185 return new CloseableIteratorImpl<Value>(new ArrayList<Value>()
186 .iterator());
187 }
188
189 public int removeStatement(Resource subj, URI pred, Value obj,
190 URI namedGraph) throws ORDIException {
191 if (isClosed) {
192 throw new IllegalStateException("The connection was closed!");
193 }
194 int isubj = subj == null ? 0 : pool.createId(subj);
195 int ipred = pred == null ? 0 : pool.createId(pred);
196 int iobj = obj == null ? 0 : pool.createId(obj);
197 int ing = namedGraph == null ? 0 : pool.createId(namedGraph);
198
199 return repository.removeStatements(isubj, ipred, iobj, ing);
200 }
201
202 public CloseableIterator<? extends TStatement> search(Resource subj,
203 URI pred, Value obj, URI namedGraph, URI ts) throws ORDIException {
204 if (isClosed) {
205 throw new IllegalStateException("The connection was closed!");
206 }
207 int isubj = subj == null ? 0 : pool.createId(subj);
208 int ipred = pred == null ? 0 : pool.createId(pred);
209 int iobj = obj == null ? 0 : pool.createId(obj);
210 int ing = namedGraph == null ? 0 : pool.createId(namedGraph);
211 int its = ts == null ? 0 : pool.createId(ts);
212
213 StatementIdIterator iter = null;
214
215 if (its != 0)
216 iter = repository.getFromQuadSet(isubj, ipred, iobj, ing, its);
217 else
218 iter = repository.getStatements(isubj, ipred, iobj, ing);
219
220 return new TRREEIterator(iter, pool, repository, its == 0);
221
222 }
223
224 public void close() throws ORDIException {
225 super.close();
226 isClosed = true;
227 repository = null;
228 pool = null;
229 }
230
231 public boolean isOpen() {
232 return !isClosed;
233 }
234
235 public boolean isReadOnly() {
236 return false;
237 }
238
239 public void addListener(Listener listener) {
240 super.addListener(listener);
241 if (repository instanceof TRREEWrapper) {
242 ((TRREEWrapper) repository).setListener(super.listeners);
243 }
244 }
245
246 public void removeListener(Listener listener) {
247 super.removeListener(listener);
248 if (repository instanceof TRREEWrapper && listListeners().size() == 0) {
249 ((TRREEWrapper) repository).setListener(null);
250 }
251 }
252 }