diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-29 18:35:51 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-29 18:35:51 +0100 |
| commit | 4b7253559c290b6fdd1c4122830f153fda85dd62 (patch) | |
| tree | 37c1487e4682be719dec532ca3d7e1af353fb9a1 /src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | |
| parent | 6a559a415b3bdb3048021839e5bcf5bbf5aa4bbd (diff) | |
| download | ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.tar.gz ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.zip | |
Disposable.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | 436 |
1 files changed, 225 insertions, 211 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java index 118c1b2..962a78f 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | |||
| @@ -9,6 +9,8 @@ import uk.ac.ox.cs.pagoda.query.QueryRecord; | |||
| 9 | import uk.ac.ox.cs.pagoda.util.PagodaProperties; | 9 | import uk.ac.ox.cs.pagoda.util.PagodaProperties; |
| 10 | import uk.ac.ox.cs.pagoda.util.Timer; | 10 | import uk.ac.ox.cs.pagoda.util.Timer; |
| 11 | import uk.ac.ox.cs.pagoda.util.Utility; | 11 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 12 | import uk.ac.ox.cs.pagoda.util.disposable.Disposable; | ||
| 13 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 12 | 14 | ||
| 13 | import java.io.BufferedWriter; | 15 | import java.io.BufferedWriter; |
| 14 | import java.io.File; | 16 | import java.io.File; |
| @@ -18,230 +20,242 @@ import java.nio.file.Paths; | |||
| 18 | import java.util.Collection; | 20 | import java.util.Collection; |
| 19 | 21 | ||
| 20 | // TODO clean APIs | 22 | // TODO clean APIs |
| 21 | public abstract class QueryReasoner { | 23 | public abstract class QueryReasoner extends Disposable { |
| 22 | 24 | ||
| 23 | public static final String ImportDataFileSeparator = ";"; | 25 | public static final String ImportDataFileSeparator = ";"; |
| 24 | private static final boolean DEFAULT_MULTI_STAGES = true; | 26 | private static final boolean DEFAULT_MULTI_STAGES = true; |
| 25 | private static final boolean DEFAULT_EQUALITIES = true; | 27 | private static final boolean DEFAULT_EQUALITIES = true; |
| 26 | public boolean fullReasoner = this instanceof MyQueryReasoner; | 28 | public boolean fullReasoner = this instanceof MyQueryReasoner; |
| 27 | protected StringBuilder importedData = new StringBuilder(); | 29 | protected StringBuilder importedData = new StringBuilder(); |
| 28 | // protected boolean forSemFacet = false; | 30 | // protected boolean forSemFacet = false; |
| 29 | PagodaProperties properties; | 31 | PagodaProperties properties; |
| 30 | BufferedWriter answerWriter = null; | 32 | BufferedWriter answerWriter = null; |
| 31 | private QueryManager m_queryManager = new QueryManager(); | 33 | private QueryManager m_queryManager = new QueryManager(); |
| 32 | 34 | ||
| 33 | public static QueryReasoner getInstance(PagodaProperties p) { | 35 | public static QueryReasoner getInstance(PagodaProperties p) { |
| 34 | OWLOntology ontology = OWLHelper.loadOntology(p.getOntologyPath()); | 36 | OWLOntology ontology = OWLHelper.loadOntology(p.getOntologyPath()); |
| 35 | QueryReasoner pagoda = getInstance(ontology, p); | 37 | QueryReasoner pagoda = getInstance(ontology, p); |
| 36 | pagoda.properties = p; | 38 | pagoda.properties = p; |
| 37 | pagoda.loadOntology(ontology); | 39 | pagoda.loadOntology(ontology); |
| 38 | pagoda.importData(p.getDataPath()); | 40 | pagoda.importData(p.getDataPath()); |
| 39 | if (pagoda.preprocess()) { | 41 | if(pagoda.preprocess()) { |
| 40 | Utility.logInfo("The ontology is consistent!"); | 42 | Utility.logInfo("The ontology is consistent!"); |
| 41 | return pagoda; | 43 | return pagoda; |
| 42 | } | 44 | } |
| 43 | else { | 45 | else { |
| 44 | System.out.println("The ontology is inconsistent!"); | 46 | System.out.println("The ontology is inconsistent!"); |
| 45 | pagoda.dispose(); | 47 | pagoda.dispose(); |
| 46 | return null; | 48 | return null; |
| 47 | } | 49 | } |
| 48 | } | 50 | } |
| 49 | 51 | ||
| 50 | public static QueryReasoner getInstance(OWLOntology o) { | 52 | public static QueryReasoner getInstance(OWLOntology o) { |
| 51 | QueryReasoner pagoda = getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES); | 53 | QueryReasoner pagoda = getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES); |
| 52 | pagoda.properties = new PagodaProperties(); | 54 | pagoda.properties = new PagodaProperties(); |
| 53 | return pagoda; | 55 | return pagoda; |
| 54 | } | 56 | } |
| 55 | 57 | ||
| 56 | private static QueryReasoner getInstance(OWLOntology o, PagodaProperties p) { | 58 | private static QueryReasoner getInstance(OWLOntology o, PagodaProperties p) { |
| 57 | return getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES); | 59 | return getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES); |
| 58 | } | 60 | } |
| 59 | 61 | ||
| 60 | public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) { | 62 | public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) { |
| 61 | // Utility.initialise(); | 63 | // Utility.initialise(); |
| 62 | QueryReasoner reasoner; | 64 | QueryReasoner reasoner; |
| 63 | if (OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner(); | 65 | if(OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner(); |
| 64 | else if (OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner(); | 66 | else if(OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner(); |
| 65 | else | 67 | else |
| 66 | switch (type) { | 68 | switch(type) { |
| 67 | case RLU: | 69 | case RLU: |
| 68 | reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities); | 70 | reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities); |
| 69 | break; | 71 | break; |
| 70 | case ELHOU: | 72 | case ELHOU: |
| 71 | reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities); | 73 | reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities); |
| 72 | break; | 74 | break; |
| 73 | default: | 75 | default: |
| 74 | reasoner = new MyQueryReasoner(performMultiStages, considerEqualities); | 76 | reasoner = new MyQueryReasoner(performMultiStages, considerEqualities); |
| 75 | } | 77 | } |
| 76 | return reasoner; | 78 | return reasoner; |
| 77 | } | 79 | } |
| 78 | 80 | ||
| 79 | public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) { | 81 | public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) { |
| 80 | return new HermiTReasoner(toCheckSatisfiability); | 82 | return new HermiTReasoner(toCheckSatisfiability); |
| 81 | } | 83 | } |
| 82 | 84 | ||
| 83 | public void setToClassify(boolean flag) { | 85 | public void setToClassify(boolean flag) { |
| 84 | properties.setToClassify(flag); | 86 | if(isDisposed()) throw new DisposedException(); |
| 85 | } | 87 | properties.setToClassify(flag); |
| 86 | 88 | } | |
| 87 | public void setToCallHermiT(boolean flag) { | 89 | |
| 88 | properties.setToCallHermiT(flag); | 90 | public void setToCallHermiT(boolean flag) { |
| 89 | } | 91 | if(isDisposed()) throw new DisposedException(); |
| 90 | 92 | properties.setToCallHermiT(flag); | |
| 91 | public void importData(String datafile) { | 93 | } |
| 92 | if (datafile != null && !datafile.equalsIgnoreCase("null")) | 94 | |
| 93 | importData(datafile.split(ImportDataFileSeparator)); | 95 | public void importData(String datafile) { |
| 94 | } | 96 | if(isDisposed()) throw new DisposedException(); |
| 95 | 97 | if(datafile != null && !datafile.equalsIgnoreCase("null")) | |
| 96 | public void importData(String[] datafiles) { | 98 | importData(datafile.split(ImportDataFileSeparator)); |
| 97 | if (datafiles != null) { | 99 | } |
| 98 | for (String datafile: datafiles) { | 100 | |
| 99 | File file = new File(datafile); | 101 | public void importData(String[] datafiles) { |
| 100 | if (file.exists()) { | 102 | if(isDisposed()) throw new DisposedException(); |
| 101 | if (file.isFile()) importDataFile(file); | 103 | if(datafiles != null) { |
| 102 | else importDataDirectory(file); | 104 | for(String datafile : datafiles) { |
| 103 | } | 105 | File file = new File(datafile); |
| 104 | else { | 106 | if(file.exists()) { |
| 105 | Utility.logError("warning: file " + datafile + " doesn't exists."); | 107 | if(file.isFile()) importDataFile(file); |
| 106 | } | 108 | else importDataDirectory(file); |
| 107 | } | 109 | } |
| 108 | } | 110 | else { |
| 109 | } | 111 | Utility.logError("warning: file " + datafile + " doesn't exists."); |
| 110 | 112 | } | |
| 111 | private void importDataDirectory(File file) { | 113 | } |
| 112 | for (File child: file.listFiles()) | 114 | } |
| 113 | if (child.isFile()) importDataFile(child); | 115 | } |
| 114 | else importDataDirectory(child); | 116 | |
| 115 | } | 117 | public abstract void loadOntology(OWLOntology ontology); |
| 116 | 118 | ||
| 117 | private void importDataFile(File file) { | 119 | public abstract boolean preprocess(); |
| 118 | String datafile; | 120 | |
| 119 | try { | 121 | public abstract boolean isConsistent(); |
| 120 | datafile = file.getCanonicalPath(); | 122 | |
| 121 | } catch (IOException e) { | 123 | public abstract void evaluate(QueryRecord record); |
| 122 | e.printStackTrace(); | 124 | |
| 123 | return; | 125 | public abstract void evaluateUpper(QueryRecord record); |
| 124 | } | 126 | |
| 125 | importDataFile(datafile); | 127 | public AnswerTuples evaluate(String queryText, boolean forFacetGeneration) { |
| 126 | } | 128 | if(isDisposed()) throw new DisposedException(); |
| 127 | 129 | if(forFacetGeneration) { | |
| 128 | protected final void importDataFile(String datafile) { | 130 | QueryRecord record = m_queryManager.create(queryText); |
| 129 | if (importedData.length() == 0) | 131 | Utility.logInfo("---------- start evaluating upper bound for Query " + record.getQueryID() + " ----------", queryText); |
| 130 | importedData.append(datafile); | 132 | if(!record.isProcessed()) |
| 131 | else | 133 | evaluateUpper(record); |
| 132 | importedData.append(ImportDataFileSeparator).append(datafile); | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | public abstract void loadOntology(OWLOntology ontology); | ||
| 137 | |||
| 138 | public abstract boolean preprocess(); | ||
| 139 | |||
| 140 | public abstract boolean isConsistent(); | ||
| 141 | |||
| 142 | public abstract void evaluate(QueryRecord record); | ||
| 143 | |||
| 144 | public abstract void evaluateUpper(QueryRecord record); | ||
| 145 | |||
| 146 | public AnswerTuples evaluate(String queryText, boolean forFacetGeneration) { | ||
| 147 | if (forFacetGeneration) { | ||
| 148 | QueryRecord record = m_queryManager.create(queryText); | ||
| 149 | Utility.logInfo("---------- start evaluating upper bound for Query " + record.getQueryID() + " ----------", queryText); | ||
| 150 | if(!record.isProcessed()) | ||
| 151 | evaluateUpper(record); | ||
| 152 | // AnswerTuples tuples = record.getUpperBoundAnswers(); | 134 | // AnswerTuples tuples = record.getUpperBoundAnswers(); |
| 153 | // for (AnswerTuple tuple; tuples.isValid(); tuples.moveNext()) { | 135 | // for (AnswerTuple tuple; tuples.isValid(); tuples.moveNext()) { |
| 154 | // tuple = tuples.getTuple(); | 136 | // tuple = tuples.getTuple(); |
| 155 | // if (tuple.toString().contains("NC")) | 137 | // if (tuple.toString().contains("NC")) |
| 156 | // System.out.println(tuple.toString()); | 138 | // System.out.println(tuple.toString()); |
| 157 | // } | 139 | // } |
| 158 | return record.getUpperBoundAnswers(); | 140 | return record.getUpperBoundAnswers(); |
| 159 | } else | 141 | } |
| 160 | return evaluate(queryText); | 142 | else |
| 161 | } | 143 | return evaluate(queryText); |
| 162 | 144 | } | |
| 163 | // public void evaluate(Collection<QueryRecord> queryRecords) { | 145 | |
| 164 | // evaluate(queryRecords); | 146 | public AnswerTuples evaluate(String queryText) { |
| 165 | // } | 147 | if(isDisposed()) throw new DisposedException(); |
| 148 | QueryRecord record = m_queryManager.create(queryText); | ||
| 149 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); | ||
| 150 | if(!record.isProcessed()) | ||
| 151 | evaluate(record); | ||
| 152 | AnswerTuples answer = record.getAnswers(); | ||
| 153 | record.dispose(); | ||
| 154 | return answer; | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | public void evaluate_shell(String queryText) { | ||
| 159 | if(isDisposed()) throw new DisposedException(); | ||
| 160 | QueryRecord record = m_queryManager.create(queryText); | ||
| 161 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); | ||
| 162 | if(!record.isProcessed()) | ||
| 163 | evaluate(record); | ||
| 164 | Utility.logInfo("Answers to this query: ", record.outputSoundAnswerTuple()); | ||
| 165 | record.dispose(); | ||
| 166 | |||
| 167 | } | ||
| 166 | 168 | ||
| 167 | public AnswerTuples evaluate(String queryText) { | 169 | public void evaluate(Collection<QueryRecord> queryRecords) { |
| 168 | QueryRecord record = m_queryManager.create(queryText); | 170 | if(isDisposed()) throw new DisposedException(); |
| 169 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); | 171 | if(!isConsistent()) { |
| 170 | if(!record.isProcessed()) | 172 | Utility.logDebug("The ontology and dataset is inconsistent."); |
| 171 | evaluate(record); | 173 | return; |
| 172 | AnswerTuples answer = record.getAnswers(); | 174 | } |
| 173 | record.dispose(); | 175 | |
| 174 | return answer; | 176 | if(properties.getAnswerPath() != null && answerWriter == null) { |
| 175 | 177 | try { | |
| 176 | } | 178 | answerWriter = Files.newBufferedWriter(Paths.get(properties.getAnswerPath())); |
| 177 | 179 | } catch(IOException e) { | |
| 178 | public void evaluate_shell(String queryText) { | 180 | Utility.logError("The answer path is not valid!"); |
| 179 | QueryRecord record = m_queryManager.create(queryText); | 181 | e.printStackTrace(); |
| 180 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); | 182 | } |
| 181 | if(!record.isProcessed()) | 183 | } |
| 182 | evaluate(record); | 184 | |
| 183 | Utility.logInfo("Answers to this query: ", record.outputSoundAnswerTuple()); | 185 | Timer t = new Timer(); |
| 184 | record.dispose(); | 186 | Gson gson = QueryRecord.GsonCreator.getInstance(); |
| 185 | 187 | for(QueryRecord record : queryRecords) { | |
| 186 | } | ||
| 187 | |||
| 188 | public void evaluate(Collection<QueryRecord> queryRecords) { | ||
| 189 | if (!isConsistent()) { | ||
| 190 | Utility.logDebug("The ontology and dataset is inconsistent."); | ||
| 191 | return; | ||
| 192 | } | ||
| 193 | |||
| 194 | if(properties.getAnswerPath() != null && answerWriter == null) { | ||
| 195 | try { | ||
| 196 | answerWriter = Files.newBufferedWriter(Paths.get(properties.getAnswerPath())); | ||
| 197 | } catch (IOException e) { | ||
| 198 | Utility.logError("The answer path is not valid!"); | ||
| 199 | e.printStackTrace(); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | Timer t = new Timer(); | ||
| 204 | Gson gson = QueryRecord.GsonCreator.getInstance(); | ||
| 205 | for (QueryRecord record: queryRecords) { | ||
| 206 | // if (Integer.parseInt(record.getQueryID()) != 218) continue; | 188 | // if (Integer.parseInt(record.getQueryID()) != 218) continue; |
| 207 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", | 189 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", |
| 208 | record.getQueryText()); | 190 | record.getQueryText()); |
| 209 | if(!record.isProcessed()) { | 191 | if(!record.isProcessed()) { |
| 210 | t.reset(); | 192 | t.reset(); |
| 211 | if(!record.isProcessed()) | 193 | if(!record.isProcessed()) |
| 212 | evaluate(record); | 194 | evaluate(record); |
| 213 | Utility.logInfo("Total time to answer this query: " + t.duration()); | 195 | Utility.logInfo("Total time to answer this query: " + t.duration()); |
| 214 | if(!fullReasoner && !record.isProcessed()) { | 196 | if(!fullReasoner && !record.isProcessed()) { |
| 215 | Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds."); | 197 | Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds."); |
| 216 | continue; | 198 | continue; |
| 217 | } | 199 | } |
| 218 | } | 200 | } |
| 219 | record.outputAnswerStatistics(); | 201 | record.outputAnswerStatistics(); |
| 220 | record.outputTimes(); | 202 | record.outputTimes(); |
| 221 | } | 203 | } |
| 222 | /* TODO it can handle one call only | 204 | /* TODO it can handle one call only |
| 223 | if you call twice, you will end up with a json file with multiple roots */ | 205 | if you call twice, you will end up with a json file with multiple roots */ |
| 224 | if(answerWriter != null) gson.toJson(queryRecords, answerWriter); | 206 | if(answerWriter != null) gson.toJson(queryRecords, answerWriter); |
| 225 | // queryRecords.stream().forEach(record -> Utility.logDebug(gson.toJson(record))); | 207 | // queryRecords.stream().forEach(record -> Utility.logDebug(gson.toJson(record))); |
| 226 | queryRecords.stream().forEach(record -> record.dispose()); | 208 | queryRecords.stream().forEach(record -> record.dispose()); |
| 227 | } | 209 | } |
| 228 | 210 | ||
| 229 | public void dispose() { | 211 | // public void evaluate(Collection<QueryRecord> queryRecords) { |
| 230 | if (answerWriter != null) { | 212 | // evaluate(queryRecords); |
| 231 | try { | 213 | // } |
| 232 | answerWriter.close(); | 214 | |
| 233 | } catch (IOException e) { | 215 | @Override |
| 234 | e.printStackTrace(); | 216 | public void dispose() { |
| 235 | } | 217 | super.dispose(); |
| 236 | } | 218 | if(answerWriter != null) { |
| 219 | try { | ||
| 220 | answerWriter.close(); | ||
| 221 | } catch(IOException e) { | ||
| 222 | e.printStackTrace(); | ||
| 223 | } | ||
| 224 | } | ||
| 237 | // Utility.cleanup(); | 225 | // Utility.cleanup(); |
| 238 | } | 226 | } |
| 239 | 227 | ||
| 240 | public QueryManager getQueryManager() { | 228 | public QueryManager getQueryManager() { |
| 241 | return m_queryManager; | 229 | if(isDisposed()) throw new DisposedException(); |
| 242 | } | 230 | return m_queryManager; |
| 231 | } | ||
| 232 | |||
| 233 | private void importDataDirectory(File file) { | ||
| 234 | for(File child : file.listFiles()) | ||
| 235 | if(child.isFile()) importDataFile(child); | ||
| 236 | else importDataDirectory(child); | ||
| 237 | } | ||
| 238 | |||
| 239 | private void importDataFile(File file) { | ||
| 240 | String datafile; | ||
| 241 | try { | ||
| 242 | datafile = file.getCanonicalPath(); | ||
| 243 | } catch(IOException e) { | ||
| 244 | e.printStackTrace(); | ||
| 245 | return; | ||
| 246 | } | ||
| 247 | importDataFile(datafile); | ||
| 248 | } | ||
| 249 | |||
| 250 | protected final void importDataFile(String datafile) { | ||
| 251 | if(importedData.length() == 0) | ||
| 252 | importedData.append(datafile); | ||
| 253 | else | ||
| 254 | importedData.append(ImportDataFileSeparator).append(datafile); | ||
| 255 | |||
| 256 | } | ||
| 243 | 257 | ||
| 244 | 258 | ||
| 245 | public enum Type {Full, RLU, ELHOU} | 259 | public enum Type {Full, RLU, ELHOU} |
| 246 | 260 | ||
| 247 | } | 261 | } |
