1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.wsmo4rdf.impl;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.log4j.Logger;
26 import org.omwg.ontology.Attribute;
27 import org.omwg.ontology.Ontology;
28 import org.openrdf.model.URI;
29 import org.openrdf.query.Binding;
30 import org.openrdf.query.BindingSet;
31 import org.openrdf.query.MalformedQueryException;
32 import org.openrdf.query.impl.EmptyBindingSet;
33 import org.openrdf.query.parser.ParsedQuery;
34 import org.openrdf.query.parser.sparql.SPARQLParser;
35 import org.wsmo.common.Entity;
36 import org.wsmo.common.IRI;
37 import org.wsmo.common.Identifier;
38 import org.wsmo.common.TopEntity;
39 import org.wsmo.common.exception.SynchronisationException;
40 import org.wsmo.factory.LogicalExpressionFactory;
41 import org.wsmo.factory.WsmoFactory;
42 import org.wsmo.mediator.GGMediator;
43 import org.wsmo.mediator.Mediator;
44 import org.wsmo.mediator.OOMediator;
45 import org.wsmo.mediator.WGMediator;
46 import org.wsmo.mediator.WWMediator;
47 import org.wsmo.service.Goal;
48 import org.wsmo.service.WebService;
49
50 import com.ontotext.ordi.IsolationLevel;
51 import com.ontotext.ordi.exception.ORDIException;
52 import com.ontotext.ordi.exception.ORDIWarning;
53 import com.ontotext.ordi.iterator.CloseableIterator;
54 import com.ontotext.ordi.tripleset.TConnection;
55 import com.ontotext.ordi.tripleset.TStatement;
56 import com.ontotext.ordi.wsmo4rdf.Constants;
57 import com.ontotext.ordi.wsmo4rdf.QueryResultListener;
58 import com.ontotext.ordi.wsmo4rdf.WSMLTripleHandler;
59 import com.ontotext.ordi.wsmo4rdf.WSMLfromTriples;
60 import com.ontotext.ordi.wsmo4rdf.WSMLtoTriples;
61 import com.ontotext.ordi.wsmo4rdf.WSMO4RDFRuntimeException;
62 import com.ontotext.ordi.wsmo4rdf.WsmoConnection;
63 import com.ontotext.ordi.wsmo4rdf.WsmoSource;
64 import com.ontotext.ordi.wsmo4rdf.impl.WSMLConnectionAdapterImpl.IteratorAdapter;
65
66 public class WsmoConnectionImpl implements WsmoConnection {
67
68 private WsmoSource source;
69 private TConnection ordiConnection;
70 private WSMLTripleHandler handler;
71 private WsmoFactory wsmoFactory;
72 private LogicalExpressionFactory leFactory;
73 private String description;
74 private WSMLfromTriples wsmlBuilder;
75 private WSMLConnectionAdapterImpl wsmlConnection;
76 private Logger logger = Logger.getLogger(WsmoConnectionImpl.class);
77
78 public WsmoConnectionImpl(WsmoSource source, TConnection ordiConnection,
79 WsmoFactory wsmoFactory, LogicalExpressionFactory leFactory) {
80 if (source == null || ordiConnection == null || wsmoFactory == null
81 || leFactory == null) {
82 throw new IllegalArgumentException();
83 }
84 this.source = source;
85 this.ordiConnection = ordiConnection;
86 this.wsmoFactory = wsmoFactory;
87 this.leFactory = leFactory;
88 handler = new Handler2TConnectionImpl(ordiConnection);
89 wsmlConnection = new WSMLConnectionAdapterImpl(ordiConnection);
90 wsmlBuilder = new WSMLfromTriplesImpl(this.wsmoFactory, this.leFactory,
91 ordiConnection);
92 }
93
94 public WsmoSource getDataSource() {
95 return source;
96 }
97
98 public IsolationLevel getTransactionIsolationLevel() {
99 return ordiConnection.getTransactionIsolationLevel();
100 }
101
102 public boolean isAutoCommit() {
103 return ordiConnection.isAutoCommit();
104 }
105
106 public void setAutoCommit(boolean mode) throws ORDIException {
107 ordiConnection.setAutoCommit(mode);
108 }
109
110 public void setTransactionIsolationLevel(IsolationLevel isolationLevel)
111 throws ORDIException {
112 ordiConnection.setTransactionIsolationLevel(isolationLevel);
113 }
114
115 public boolean isWrapperFor(Class<?> iface) throws ORDIException {
116 if (TConnection.class.isAssignableFrom(iface))
117 return true;
118 return false;
119 }
120
121 @SuppressWarnings("unchecked")
122 public <T> T unwrap(Class<T> iface) throws ORDIException {
123 if (TConnection.class.isAssignableFrom(iface))
124 return (T) ordiConnection;
125 return null;
126 }
127
128 public boolean isOpen() {
129 return ordiConnection.isOpen();
130 }
131
132 public void rollback() throws ORDIException {
133 ordiConnection.rollback();
134 }
135
136 public void commit() throws ORDIException {
137 ordiConnection.commit();
138 }
139
140 public void close() throws ORDIException {
141 ordiConnection.close();
142 }
143
144 public boolean isReadOnly() {
145 return ordiConnection.isReadOnly();
146 }
147
148 public ORDIWarning getWarning() {
149 return ordiConnection.getWarning();
150 }
151
152 public void clearWarnings() {
153 ordiConnection.clearWarnings();
154 }
155
156 public void evaluate(String query, QueryResultListener listener)
157 throws MalformedQueryException {
158
159 CloseableIterator<? extends BindingSet> iterator = null;
160 try {
161 SPARQLParser parser = new SPARQLParser();
162 ParsedQuery parsedQuery = parser.parseQuery(query,
163 Constants.RDFS_NS);
164 iterator = ordiConnection.evaluate(parsedQuery.getTupleExpr(),
165 new EmptyBindingSet(), true);
166 } catch (ORDIException e) {
167 throw new WSMO4RDFRuntimeException(
168 "Error durint the executio of the query!", e);
169 }
170
171 while (iterator.hasNext()) {
172 BindingSet set = iterator.next();
173 Iterator<Binding> iterator2 = set.iterator();
174 while (iterator2.hasNext()) {
175 Binding binding = iterator2.next();
176 listener.nextBinding(binding.getName(), Util.toValue(binding
177 .getValue()));
178 }
179 }
180 }
181
182 public void addGoal(Goal arg0) throws SynchronisationException {
183 save(arg0);
184 }
185
186 public void addMediator(Mediator arg0) throws SynchronisationException {
187 save(arg0);
188 }
189
190 public void addOntology(Ontology arg0) throws SynchronisationException {
191 save(arg0);
192 }
193
194 public void addWebService(WebService arg0) throws SynchronisationException {
195 save(arg0);
196 }
197
198 public void deleteGoal(IRI arg0) throws SynchronisationException {
199 remove(arg0, Goal.class);
200 }
201
202 public void deleteMediator(IRI arg0) throws SynchronisationException {
203 remove(arg0, Mediator.class);
204 }
205
206 public void deleteOntology(IRI arg0) throws SynchronisationException {
207 remove(arg0, Ontology.class);
208 }
209
210 public void deleteWebService(IRI arg0) throws SynchronisationException {
211 remove(arg0, WebService.class);
212 }
213
214 public String getDescription() {
215 if (description != null) {
216 return description;
217 }
218 return String.format("%s %s", this.getClass().getName(), getVersion());
219 }
220
221 public Goal getGoal(IRI arg0) throws SynchronisationException {
222 return (Goal) load(arg0, Goal.class);
223 }
224
225 public Mediator getMediator(IRI arg0) throws SynchronisationException {
226 Class<?>[] mediatorTypes = new Class[] { OOMediator.class,
227 GGMediator.class, WGMediator.class, WWMediator.class };
228 Mediator result = null;
229 for (int i = 0; i < mediatorTypes.length; i++) {
230 try {
231 result = (Mediator) load(arg0, mediatorTypes[i]);
232 if (result != null)
233 break;
234 } catch (RuntimeException e) {
235
236 }
237 }
238 return result;
239 }
240
241 public Ontology getOntology(IRI arg0) throws SynchronisationException {
242 return (Ontology) load(arg0, Ontology.class);
243 }
244
245 public String getVersion() throws SynchronisationException {
246 return "ORDI v0.5";
247 }
248
249 public WebService getWebService(IRI arg0) throws SynchronisationException {
250 return (WebService) load(arg0, WebService.class);
251 }
252
253 public List<IRI> listGoals() throws SynchronisationException {
254 return listTypes(Constants.WSML_goal);
255 }
256
257 public List<IRI> listMediators() throws SynchronisationException {
258 List<IRI> result = new ArrayList<IRI>();
259 String[] mediatorTypes = new String[] { Constants.WSML_ooMediator,
260 Constants.WSML_ggMediator, Constants.WSML_wgMediator,
261 Constants.WSML_wwMediator };
262 for (int i = 0; i < mediatorTypes.length; i++) {
263 result.addAll(listTypes(mediatorTypes[i]));
264 }
265 return result;
266 }
267
268 public List<IRI> listOntologies() throws SynchronisationException {
269 return listTypes(Constants.WSML_ontology);
270 }
271
272 public List<IRI> listWebServices() throws SynchronisationException {
273 return listTypes(Constants.WSML_webService);
274 }
275
276 public void saveGoal(Goal arg0) throws SynchronisationException {
277 remove(arg0.getIdentifier(), Goal.class);
278 save(arg0);
279 }
280
281 public void saveMediator(Mediator arg0) throws SynchronisationException {
282 remove(arg0.getIdentifier(), Mediator.class);
283 save(arg0);
284 }
285
286 public void saveOntology(Ontology arg0) throws SynchronisationException {
287 remove(arg0.getIdentifier(), Ontology.class);
288 save(arg0);
289 }
290
291 public void saveWebService(WebService arg0) throws SynchronisationException {
292 remove(arg0.getIdentifier(), WebService.class);
293 save(arg0);
294 }
295
296 public void setDescription(String arg0) {
297 if (arg0 == null) {
298 throw new IllegalArgumentException("Null is not allowed!");
299 }
300 description = arg0;
301 }
302
303 public Set<Entity> load(Identifier arg0) {
304 if (logger.isTraceEnabled()) {
305 logger.trace(String.format("Loading entities identified by %s...",
306 arg0));
307 }
308 Set<Entity> result = wsmlBuilder.construct(arg0);
309 if (logger.isTraceEnabled()) {
310 logger.trace(String.format("%d entities identified by %s loaded",
311 result.size(), arg0));
312 }
313 return result;
314 }
315
316 @SuppressWarnings("unchecked")
317 public Entity load(Identifier arg0, Class arg1) {
318 if (logger.isTraceEnabled()) {
319 logger.trace(String.format(
320 "Loading entity of type %s identified by %s...", arg1
321 .getSimpleName(), arg0));
322 }
323 if (Entity.class.isAssignableFrom(arg1) == false)
324 return null;
325 Entity result = wsmlBuilder.construct(arg0, arg1);
326 if (logger.isTraceEnabled()) {
327 logger
328 .trace(String
329 .format(
330 arg0 == null ? "No entity identified by %s and type %s was found!"
331 : "Entity identified by %s and type %s was found!",
332 arg0, arg1.getSimpleName()));
333 }
334 return result;
335 }
336
337 /**
338 * Removes all associated statements to a specific identifier. There are two
339 * types of removes: 1) Entity - delete all statements where TS = Entity ID.
340 * 2) TopEntity - delete all statements where namedGraph = TopEntity ID in
341 * addition to procedure for Entity
342 *
343 * @param arg0
344 * identifier to be deleted (anonymous identifiers are ignored)
345 */
346 public void remove(Identifier arg0) {
347 Class<? extends Entity> type = getClassFor(arg0);
348 if (type == null) {
349 return;
350 }
351 try {
352 URI id = Util.toURI(arg0);
353 if (TopEntity.class.isAssignableFrom(type)) {
354 wsmlConnection.getConnection().removeStatement(null, null,
355 null, id);
356 }
357 if (Entity.class.isAssignableFrom(type)) {
358
359 CloseableIterator<? extends TStatement> iterator = wsmlConnection.conection
360 .search(null, null, null, null, id);
361 while (iterator.hasNext()) {
362 TStatement statement = iterator.next();
363 wsmlConnection.conection.removeStatement(statement
364 .getSubject(), statement.getPredicate(), statement
365 .getObject(), null);
366 }
367 }
368 } catch (ORDIException e) {
369 throw new RuntimeException(
370 "Cannot delete statements from ORDI model!", e);
371 }
372 }
373
374 @SuppressWarnings("unchecked")
375 public void remove(Identifier arg0, Class arg1) {
376 if (Entity.class.isAssignableFrom(arg1) == false)
377 return;
378
379
380 remove(arg0);
381 }
382
383 public void save(Entity arg0) {
384 if (logger.isTraceEnabled()) {
385 logger.trace(String.format("Adding %s %s in repository...", arg0
386 .getClass().getSimpleName(), arg0.getIdentifier()));
387 }
388 WSMLtoTriples tripileser = new WSML2TriplesImpl(wsmoFactory, handler);
389 tripileser.process(arg0);
390
391 if (logger.isTraceEnabled()) {
392 logger.trace(String.format("%s %s added to repository", arg0
393 .getClass().getSimpleName(), arg0.getIdentifier()));
394 }
395 }
396
397 private List<IRI> listTypes(String type) {
398 List<IRI> result = new ArrayList<IRI>();
399 IteratorAdapter iterator = wsmlConnection.search(null, wsmoFactory
400 .createIRI(Constants.RDF_type), wsmoFactory.createIRI(type));
401 while (iterator.hasNext()) {
402 Identifier id = iterator.next().getSubject();
403 if (id instanceof IRI)
404 result.add((IRI) id);
405 }
406 return result;
407 }
408
409 /**
410 * Returns Java class to be created for the specified identifier.
411 *
412 * @param id
413 * to be checked in the repository
414 * @return Java class for the id or null if it is invalid or does not exist
415 */
416 private Class<? extends Entity> getClassFor(Identifier id) {
417 if (id instanceof IRI == false) {
418 return null;
419 }
420 Identifier iriType = wsmlConnection.searchForObject(id, wsmoFactory
421 .createIRI(Constants.RDF_type), Identifier.class);
422 Class<? extends Entity> cls = Util.identifierToClass(iriType);
423 if (iriType == null) {
424
425 IteratorAdapter iterator = wsmlConnection.search(null, wsmoFactory
426 .createIRI(Constants.WSML_forAttribute), id);
427 if (iterator.hasNext()) {
428 iterator.close();
429 return Attribute.class;
430 }
431 }
432 return cls;
433 }
434 }