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.trree;
19  
20  import java.util.NoSuchElementException;
21  
22  import org.openrdf.model.Resource;
23  import org.openrdf.model.URI;
24  import org.openrdf.model.Value;
25  import org.openrdf.model.impl.URIImpl;
26  
27  import com.ontotext.ordi.exception.ORDIRuntimeException;
28  import com.ontotext.ordi.iterator.CloseableIterator;
29  import com.ontotext.ordi.tripleset.ORDIConst;
30  import com.ontotext.ordi.tripleset.TStatement;
31  import com.ontotext.ordi.tripleset.impl.TStatementImpl;
32  import com.ontotext.trree.owlim_ext.AbstractRepository;
33  import com.ontotext.trree.owlim_ext.EntityPool;
34  import com.ontotext.trree.owlim_ext.StatementIdIterator;
35  
36  public class TRREEIterator implements CloseableIterator<TStatement> {
37  
38      private boolean isClosed = false;
39      private boolean isLazyTS = false;
40      private StatementIdIterator iter;
41      private EntityPool pool;
42      private AbstractRepository repository;
43  
44      public TRREEIterator(StatementIdIterator iter, EntityPool pool,
45              AbstractRepository repository, boolean isLazyTs) {
46          if (iter == null || pool == null || repository == null) {
47              throw new IllegalArgumentException();
48          }
49          this.iter = iter;
50          this.pool = pool;
51          this.repository = repository;
52      }
53  
54      public void close() {
55          isClosed = true;
56          iter = null;
57          pool = null;
58          repository = null;
59      }
60  
61      public boolean hasNext() {
62          return !isClosed && iter != null && iter.hasNext();
63      }
64  
65      public TStatement next() {
66          TStatement result = null;
67  
68          if (isClosed)
69              throw new NoSuchElementException("Iterator is closed!");
70  
71          if (isLazyTS) {
72              result = new LazyTStatementImpl(iter.Subj, iter.Pred, iter.Obj,
73                      iter.Context, pool, repository);
74          } else {
75              URI[] uris = null;
76              URI context = null;
77              
78              if (iter.Context == 0) {
79                  // Implicit statement
80                  uris = new URI[] { new URIImpl(ORDIConst.IMPLICIT_STATEMENT) };
81                  context = new URIImpl(ORDIConst.DEFAULT_GRAPH);
82              } else {
83                  // Explicit statement
84                  int[] ts = repository.getMappedQuadSets(iter.Subj, iter.Pred,
85                          iter.Obj, iter.Context);
86                  uris = new URI[ts.length];
87                  context = (URI) pool.toObject(iter.Context);
88                  for (int i = 0; i < ts.length; i++) {
89                      uris[i] = (URI) pool.toObject(ts[i]);
90                  }
91              }
92              try {
93                  Resource subj = (Resource) pool.toObject(iter.Subj);
94                  URI pred = (URI) pool.toObject(iter.Pred);
95                  Value obj = pool.toObject(iter.Obj);
96                  if (subj == null || pred == null || obj == null
97                          || context == null)
98                      throw new ORDIRuntimeException(ERROR);
99  
100                 result = new TStatementImpl(subj, pred, obj, context, uris);
101             } catch (ClassCastException ce) {
102                 throw new ORDIRuntimeException(ERROR);
103             }
104         }
105         iter.next();
106         return result;
107     }
108 
109     public void remove() {
110         if (!isClosed)
111             throw new NoSuchElementException("Iterator is closed!");
112         iter.remove();
113     }
114 
115     private static final String ERROR = "Internal IDs synchronization problem! "
116             + "Please check if the storage files have been damaged or incorrectly used!";
117 }