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.io.File;
21  import java.lang.reflect.Constructor;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import com.ontotext.ordi.exception.ORDIException;
28  import com.ontotext.ordi.exception.ORDIRuntimeException;
29  import com.ontotext.ordi.tripleset.TConnection;
30  import com.ontotext.ordi.tripleset.TFactory;
31  import com.ontotext.ordi.tripleset.impl.TFactoryImpl;
32  import com.ontotext.ordi.tripleset.impl.TSourceImpl;
33  import com.ontotext.trree.owlim_ext.AbstractInferencer;
34  import com.ontotext.trree.owlim_ext.AbstractRepository;
35  import com.ontotext.trree.owlim_ext.EntityPool;
36  import com.ontotext.trree.owlim_ext.InferencerFactory;
37  import com.ontotext.trree.owlim_ext.Repository;
38  
39  public class TRREEAdapter extends TSourceImpl {
40  
41      public static final int KEY_INDEX_SIZE_DEFAULT = 10000000;
42      public static final String KEY_INDEX_SIZE = "entity-key-index-size";
43      public static final String INFERENCER_RULESET = "inferencer-ruleset";
44      public static final String PARTIAL_RDFS = "partial-rdfs";
45      public static final String STORAGE_DIRECTORY = Repository.PARAM_STORAGE_FOLDER;
46      public static final String LOAD_PERSISTED_DATA = "load-persisted-data";
47      private static final Set<String> usedStoragePaths = new HashSet<String>();
48  
49      private Set<TRREEConnection> connections = new HashSet<TRREEConnection>();
50      private AbstractRepository repository;
51      private AbstractInferencer inferencer;
52      private EntityPool pool;
53      private TFactory factory;
54      private boolean isShutDown = false;
55  
56      @SuppressWarnings("unchecked")
57      public TRREEAdapter(Map<Object, Object> props) {
58          super(props);
59  
60          // Storage directory
61          
62          String storageDir = System.getProperty("user.dir");
63          if (props.get(STORAGE_DIRECTORY) instanceof String)
64              storageDir = (String) props.get(STORAGE_DIRECTORY);
65          storageDir = storageDir.endsWith(File.separator) ? storageDir
66                  : storageDir + File.separatorChar;
67          props.put(STORAGE_DIRECTORY, storageDir);
68  
69          // Check if another instance is already running
70          synchronized (usedStoragePaths) {
71              if (usedStoragePaths.contains(storageDir)) {
72                  String msg = String
73                          .format(
74                                  "ORDI model using TRREE is already running in %s. "
75                                          + "Shutdown the other instance or choose a new storage path!",
76                                  storageDir);
77                  throw new ORDIRuntimeException(msg);
78              }
79  
80              // Load the previously saved data
81              boolean loadData = true;
82              if ("false".equals(props.get(LOAD_PERSISTED_DATA))) {
83                  loadData = false;
84              }
85  
86              // Entity pool
87              Object keyIndexSize = props.get(KEY_INDEX_SIZE);
88              try {
89                  keyIndexSize = Integer.parseInt(keyIndexSize.toString());
90              }
91              catch (Exception e) {
92                  keyIndexSize = KEY_INDEX_SIZE_DEFAULT;
93              }
94              pool = new EntityPool(storageDir + "entitypool.bin",
95                      (Integer) keyIndexSize, loadData);
96  
97              // Construct the inferencer rule-set
98              Object inferConf = props.get(INFERENCER_RULESET);
99              Object partRDFSConf = props.get(PARTIAL_RDFS);
100             boolean disableReasoning = "empty".equals(inferConf);
101             if (isAllowedRuleSet(inferConf) == false)
102                 inferConf = "owl-max";
103             if ("false".equals(partRDFSConf))
104                 partRDFSConf = false;
105             else
106                 partRDFSConf = true;
107             Class cls = InferencerFactory.createInferencer(
108                     inferConf.toString(), (Boolean) partRDFSConf);
109             try {
110                 Constructor cons = cls
111                         .getConstructor(new Class[] { EntityPool.class });
112                 inferencer = (AbstractInferencer) cons
113                         .newInstance(new Object[] { pool });
114                 props.put(AbstractInferencer.INFERENCER_PARAM, inferencer);
115             } catch (Exception ex) {
116                 throw new RuntimeException(ex);
117             }
118 
119             // Instantiate the repository in a wrapper
120             Repository trreeRep = new Repository(loadData);
121             trreeRep.setInferencer(inferencer);
122             repository = new TRREEWrapper(trreeRep, pool);
123             Map<Object, Object> params = new HashMap<Object, Object>();
124             params.putAll(props);
125             params.put(STORAGE_DIRECTORY, storageDir);
126             params.put(AbstractInferencer.REPOSITORY_PARAM, repository);
127             params.put(EntityPool.ENTITY_POOL_PARAM, pool);
128             
129             // disable reasoning of ruleset is empty
130             if (disableReasoning)
131                 params.put("ruleset", "empty");
132             
133             repository.initialize(params);
134             inferencer.initialize(params);
135 
136             // Register the storage directory as running
137             usedStoragePaths.add(storageDir);
138         }
139     }
140 
141     public synchronized TConnection getConnection() {
142         if (isShutDown) {
143             throw new IllegalStateException("The instance was shutdown!");
144         }
145         TRREEConnection connection = new TRREEConnection(this, repository,
146                 pool, inferencer);
147         connections.add(connection);
148         return connection;
149     }
150 
151     public TConnection getConnection(String user, String pass) {
152         return getConnection();
153     }
154 
155     public synchronized void shutdown() {
156         if (isShutDown)
157             return;
158 
159         for (TRREEConnection connection : connections) {
160             try {
161                 connection.close();
162             } catch (ORDIException oe) {
163             }
164         }
165 
166         isShutDown = true;
167         synchronized (usedStoragePaths) {
168             usedStoragePaths.remove(getMetaData().get(STORAGE_DIRECTORY));
169         }
170         inferencer.shutdown();
171         repository.shutdown();
172         pool.shutdown();
173         inferencer = null;
174         repository = null;
175         pool = null;
176     }
177 
178     public synchronized TFactory getTriplesetFactory() {
179         if (factory == null) {
180             factory = new TFactoryImpl();
181         }
182         return factory;
183     }
184 
185     public boolean isShutdown() {
186         return isShutDown;
187     }
188 
189     protected void finalize() throws Throwable {
190         if (isShutDown == false)
191             shutdown();
192         super.finalize();
193     }
194     
195     private boolean isAllowedRuleSet(Object ruleSet) {
196         if (ruleSet instanceof String)
197             ruleSet = ((String) ruleSet).toLowerCase();
198         if ("rdfs".equals(ruleSet) || "owl-horst".equals(ruleSet) ||
199                 "owl-max".equals(ruleSet))
200             return true;
201         return false;
202     }
203 }