View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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  
24  import org.openrdf.model.Resource;
25  import org.openrdf.model.URI;
26  import org.openrdf.model.Value;
27  import org.wsmo.common.IRI;
28  import org.wsmo.common.Identifier;
29  
30  import com.ontotext.ordi.exception.ORDIException;
31  import com.ontotext.ordi.iterator.CloseableIterator;
32  import com.ontotext.ordi.tripleset.TConnection;
33  import com.ontotext.ordi.tripleset.TStatement;
34  
35  /**
36   * This class is responsible to manage all transformation task between triples
37   * (composed by RDF types) and triples (composed by WSML types).
38   */
39  public class WSMLConnectionAdapterImpl {
40  
41      protected final TConnection conection;
42  
43      public WSMLConnectionAdapterImpl(TConnection connection) {
44          if (connection == null) {
45              throw new IllegalArgumentException();
46          }
47          this.conection = connection;
48      }
49  
50      public TConnection getConnection() {
51          return conection;
52      }
53  
54      public int removeStatement(Identifier subj, IRI pred, Object obj,
55              IRI namedGraph) {
56          try {
57              return conection
58                      .removeStatement(Util.toURI(subj), Util.toURI(pred), Util
59                              .toValue(obj), Util.toURI(namedGraph));
60          } catch (ORDIException e) {
61              throw new RuntimeException("Error while deleting statements!", e);
62          }
63      }
64  
65      public boolean hasStatement(Identifier subj, IRI pred, Object obj) {
66          return hasStatement(subj, pred, obj, null, null);
67      }
68  
69      public boolean hasStatement(Identifier subj, IRI pred, Object obj, IRI ng,
70              IRI ts) {
71          return search(subj, pred, obj, ng, ts).hasNext();
72      }
73  
74      public IteratorAdapter search(Identifier subj, IRI pred, Object obj) {
75          return search(subj, pred, obj, null, null);
76      }
77  
78      public IteratorAdapter search(Identifier subj, IRI pred, Object obj,
79              IRI ng, IRI ts) {
80          try {
81              CloseableIterator<? extends TStatement> iterator = conection
82                      .search(Util.toURI(subj), Util.toURI(pred), Util
83                              .toValue(obj), Util.toURI(ng), Util.toURI(ts));
84              return new IteratorAdapter(iterator);
85          } catch (ORDIException e) {
86              throw new RuntimeException(
87                      "Could not perform the requested operation!", e);
88          }
89      }
90  
91      /**
92       * Retrieve the first object by specified subject and predicates, which is
93       * assignable to a specified class. If an argument is null then it will
94       * match all values.
95       * 
96       * @param subject
97       *            to filter or null for wildcard.
98       * @param predicate
99       *            to filter or null for wildcard.
100      * @param assignableTo
101      *            verify if the result may be casted to the specified type
102      * @return The first object of a triple to match the subject and predicate.
103      */
104     @SuppressWarnings("unchecked")
105     public <T> T searchForObject(Identifier subject, IRI predicate,
106             Class<T> assignableTo) {
107         WSMLConnectionAdapterImpl.IteratorAdapter iter = search(subject,
108                 predicate, (Identifier) null);
109         try {
110             if (iter.hasNext()) {
111                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
112                 if (assignableTo == null
113                         || assignableTo.isInstance(t.getObject()))
114                     return (T) t.getObject();
115             }
116         } finally {
117             iter.close();
118         }
119         return null;
120     }
121 
122     /**
123      * Retrieve the first ?result to bind the pattern <?s, ?p1, ?o> <?o, ?p2,
124      * ?result>. This is regarded as a primitive join operation between triples.
125      * 
126      * @param subject
127      *            the value of ?s, may be null for wildcard.
128      * @param predicate
129      *            the value of ?p1, may be null for wildcard.
130      * @param predicate2
131      *            the value of ?p2, may be null for wildcard.
132      * @param object
133      *            the value of ?o, may be null for wildcard.
134      * @return the value of ?return or null if no match is possible
135      */
136     public Identifier searchForObjectByJoin(Identifier subject, IRI predicate,
137             IRI predicate2, Identifier object) {
138         WSMLConnectionAdapterImpl.IteratorAdapter iter = search(subject,
139                 predicate, (Identifier) null);
140         WSMLConnectionAdapterImpl.IteratorAdapter iterInner = null;
141         try {
142             while (iter.hasNext()) {
143                 WSMLConnectionAdapterImpl.WSMLTriple t = iter.next();
144                 if (t.getObject() instanceof Identifier == false)
145                     continue;
146                 iterInner = search((Identifier) t.getObject(), predicate2,
147                         object);
148                 while (iterInner.hasNext()) {
149                     WSMLConnectionAdapterImpl.WSMLTriple t2 = iterInner.next();
150                     if (t2.getPredicate().equals(predicate2)
151                             && t2.getObject().equals(object)) {
152                         return (Identifier) t.getObject();
153                     }
154                 }
155             }
156         } finally {
157             if (iterInner != null)
158                 iterInner.close();
159             if (iter != null)
160                 iter.close();
161         }
162         return null;
163     }
164 
165     public class IteratorAdapter implements CloseableIterator<WSMLTriple> {
166 
167         private CloseableIterator<? extends TStatement> iterator;
168 
169         public IteratorAdapter(CloseableIterator<? extends TStatement> iterator) {
170             if (iterator == null) {
171                 throw new IllegalArgumentException();
172             }
173             this.iterator = iterator;
174         }
175 
176         public boolean hasNext() {
177             return iterator.hasNext();
178         }
179 
180         public WSMLTriple next() {
181             TStatement statement = iterator.next();
182             List<URI> tsList = new ArrayList<URI>();
183             Iterator<URI> iterator = statement.getTriplesetIterator();
184             while (iterator.hasNext()) {
185                 tsList.add(iterator.next());
186             }
187             URI ng = null;
188             if (statement.getContext() instanceof URI)
189                 ng = (URI) statement.getContext();
190             return new WSMLTriple(statement.getSubject(), statement
191                     .getPredicate(), statement.getObject(), ng, tsList
192                     .toArray(new URI[0]));
193         }
194 
195         public void remove() {
196             iterator.remove();
197         }
198 
199         public void close() {
200             iterator.close();
201         }
202     }
203 
204     public class WSMLTriple {
205 
206         private Resource subj;
207         private URI pred;
208         private Value obj;
209         private URI ng;
210         private URI[] tripleSets;
211 
212         public WSMLTriple(Resource subj, URI pred, Value obj, URI ng,
213                 URI... tripleSets) {
214             if (subj == null || pred == null || obj == null) {
215                 throw new IllegalArgumentException();
216             }
217             this.subj = subj;
218             this.pred = pred;
219             this.obj = obj;
220             this.ng = ng;
221             this.tripleSets = tripleSets;
222         }
223 
224         public Identifier getSubject() {
225             return Util.toIdentifier(subj);
226         }
227 
228         public IRI getPredicate() {
229             return (IRI) Util.toIdentifier(pred);
230         }
231 
232         public Object getObject() {
233             return Util.toValue(obj);
234         }
235 
236         public URI getNamedGraph() {
237             return ng;
238         }
239 
240         public URI[] tripleSets() {
241             if (tripleSets == null)
242                 return new URI[0];
243             return tripleSets;
244         }
245 
246         public boolean hasTripleSet(URI ts) {
247             for (int i = 0; i < tripleSets.length; i++) {
248                 if (tripleSets[i].equals(ts))
249                     return true;
250             }
251             return false;
252         }
253     }
254 }