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.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  
28  import com.ontotext.ordi.tripleset.TStatement;
29  import com.ontotext.trree.owlim_ext.AbstractRepository;
30  import com.ontotext.trree.owlim_ext.EntityPool;
31  
32  public class LazyTStatementImpl implements TStatement {
33  
34      static final long serialVersionUID = 1L;
35  
36      private EntityPool pool;
37      private AbstractRepository repository;
38      private List<URI> tripleSet;
39      private int isubj, ipred, iobj, icontext;
40      private Resource subj;
41      private URI pred, context;
42      private Value obj;
43  
44      public LazyTStatementImpl(int isubj, int ipred, int iobj, int icontext,
45              EntityPool pool, AbstractRepository repository) {
46          if (pool == null || repository == null || isubj == 0 || ipred == 0
47                  || iobj == 0 || icontext == 0) {
48              throw new IllegalArgumentException();
49          }
50          this.isubj = isubj;
51          this.ipred = ipred;
52          this.iobj = iobj;
53          this.icontext = icontext;
54          this.pool = pool;
55          this.repository = repository;
56      }
57  
58      public LazyTStatementImpl(Resource subj, URI pred, Value obj, URI context,
59              EntityPool pool, AbstractRepository repository) {
60          if (pool == null || repository == null) {
61              throw new IllegalArgumentException();
62          }
63          this.subj = subj;
64          this.pred = pred;
65          this.obj = obj;
66          this.context = context;
67          this.pool = pool;
68          this.repository = repository;
69      }
70  
71      public Resource getSubject() {
72          if (subj == null) {
73              Value val = pool.toObject(isubj);
74              assert (val instanceof Resource);
75              subj = (Resource) val;
76          }
77          return subj;
78      }
79  
80      public URI getPredicate() {
81          if (pred == null) {
82              Value val = pool.toObject(ipred);
83              assert (val instanceof Resource);
84              pred = (URI) val;
85          }
86          return pred;
87      }
88  
89      public Value getObject() {
90          if (obj == null) {
91              obj = pool.toObject(iobj);
92          }
93          return obj;
94      }
95  
96      public Resource getContext() {
97          if (context == null) {
98              Value val = pool.toObject(icontext);
99              assert (val instanceof Resource);
100             context = (URI) val;
101         }
102         return context;
103     }
104 
105     public boolean equals(Object other) {
106         if (this == other) {
107             return true;
108         }
109         if (other instanceof TStatement) {
110             TStatement otherSt = (TStatement) other;
111 
112             return getObject().equals(otherSt.getObject())
113                     && getSubject().equals(otherSt.getSubject())
114                     && getPredicate().equals(otherSt.getPredicate())
115                     && getContext().equals(otherSt.getContext());
116         }
117         return false;
118     }
119 
120     public int hashCode() {
121         return 923521 * getContext().hashCode() + 961 * getSubject().hashCode()
122                 + 31 * getPredicate().hashCode() + getObject().hashCode();
123     }
124 
125     public Iterator<URI> getTriplesetIterator() {
126         if (tripleSet == null) {
127             loadTS();
128         }
129         return tripleSet.iterator();
130     }
131 
132     public boolean isMemberOf(URI uri) {
133         if (tripleSet == null) {
134             loadTS();
135         }
136         return tripleSet.contains(uri);
137     }
138 
139     private void loadTS() {
140         tripleSet = new ArrayList<URI>();
141 
142         if (isubj == 0) {
143             isubj = pool.getId(subj);
144             ipred = pool.getId(pred);
145             iobj = pool.getId(obj);
146             icontext = pool.getId(context);
147         }
148 
149         int[] ts = repository.getMappedQuadSets(isubj, ipred, iobj, icontext);
150         for (int i = 0; i < ts.length; i++) {
151             Value val = pool.toObject(ts[i]);
152             assert (val instanceof URI);
153             tripleSet.add((URI) val);
154         }
155     }
156 }