diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java new file mode 100644 index 0000000..0c009a2 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | |||
| @@ -0,0 +1,221 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner; | ||
| 2 | |||
| 3 | import java.io.BufferedWriter; | ||
| 4 | import java.io.File; | ||
| 5 | import java.io.FileNotFoundException; | ||
| 6 | import java.io.FileOutputStream; | ||
| 7 | import java.io.IOException; | ||
| 8 | import java.io.OutputStreamWriter; | ||
| 9 | import java.util.Collection; | ||
| 10 | |||
| 11 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 12 | |||
| 13 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 14 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 15 | import uk.ac.ox.cs.pagoda.query.QueryManager; | ||
| 16 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 17 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 18 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 19 | |||
| 20 | public abstract class QueryReasoner { | ||
| 21 | |||
| 22 | protected boolean forSemFacet = false; | ||
| 23 | |||
| 24 | public static enum Type { Full, RLU, ELHOU }; | ||
| 25 | |||
| 26 | public static QueryReasoner getInstanceForSemFacet(OWLOntology o) { | ||
| 27 | QueryReasoner reasoner = getInstance(Type.Full, o, true, true); | ||
| 28 | reasoner.forSemFacet = true; | ||
| 29 | return reasoner; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | public static QueryReasoner getInstance(OWLOntology o) { | ||
| 34 | return getInstance(Type.Full, o, true, true); | ||
| 35 | } | ||
| 36 | |||
| 37 | public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) { | ||
| 38 | Utility.initialise(); | ||
| 39 | QueryReasoner reasoner; | ||
| 40 | if (OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner(); | ||
| 41 | else if (OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner(); | ||
| 42 | else | ||
| 43 | switch (type) { | ||
| 44 | case RLU: | ||
| 45 | reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities); break; | ||
| 46 | case ELHOU: | ||
| 47 | reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities); break; | ||
| 48 | default: | ||
| 49 | reasoner = new MyQueryReasoner(performMultiStages, considerEqualities); | ||
| 50 | } | ||
| 51 | return reasoner; | ||
| 52 | } | ||
| 53 | |||
| 54 | public static final String ImportDataFileSeparator = ";"; | ||
| 55 | protected StringBuilder importedData = new StringBuilder(); | ||
| 56 | |||
| 57 | public void importData(String datafile) { | ||
| 58 | if (datafile != null && !datafile.equalsIgnoreCase("null")) | ||
| 59 | importData(datafile.split(ImportDataFileSeparator)); | ||
| 60 | } | ||
| 61 | |||
| 62 | public void importData(String[] datafiles) { | ||
| 63 | if (datafiles != null) { | ||
| 64 | for (String datafile: datafiles) { | ||
| 65 | File file = new File(datafile); | ||
| 66 | if (file.exists()) { | ||
| 67 | if (file.isFile()) importDataFile(file); | ||
| 68 | else importDataDirectory(file); | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | Utility.logError("warning: file " + datafile + " doesn't exists."); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | private void importDataDirectory(File file) { | ||
| 78 | for (File child: file.listFiles()) | ||
| 79 | if (child.isFile()) importDataFile(child); | ||
| 80 | else importDataDirectory(child); | ||
| 81 | } | ||
| 82 | |||
| 83 | private void importDataFile(File file) { | ||
| 84 | String datafile; | ||
| 85 | try { | ||
| 86 | datafile = file.getCanonicalPath(); | ||
| 87 | } catch (IOException e) { | ||
| 88 | e.printStackTrace(); | ||
| 89 | return ; | ||
| 90 | } | ||
| 91 | importDataFile(datafile); | ||
| 92 | } | ||
| 93 | |||
| 94 | protected final void importDataFile(String datafile) { | ||
| 95 | if (importedData.length() == 0) | ||
| 96 | importedData.append(datafile); | ||
| 97 | else | ||
| 98 | importedData.append(ImportDataFileSeparator).append(datafile); | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | public abstract void loadOntology(OWLOntology ontology); | ||
| 103 | |||
| 104 | public abstract boolean preprocess(); | ||
| 105 | |||
| 106 | public abstract boolean isConsistent(); | ||
| 107 | |||
| 108 | public boolean fullReasoner = this instanceof MyQueryReasoner; | ||
| 109 | |||
| 110 | public abstract void evaluate(QueryRecord record); | ||
| 111 | |||
| 112 | public abstract void evaluateUpper(QueryRecord record); | ||
| 113 | |||
| 114 | public AnswerTuples evaluate(String queryText, boolean forFacetGeneration) { | ||
| 115 | if (forFacetGeneration) { | ||
| 116 | QueryRecord record = m_queryManager.create(queryText); | ||
| 117 | Utility.logInfo("---------- start evaluating upper bound for Query " + record.getQueryID() + " ----------", queryText); | ||
| 118 | if (!record.processed()) | ||
| 119 | evaluateUpper(record); | ||
| 120 | // AnswerTuples tuples = record.getUpperBoundAnswers(); | ||
| 121 | // for (AnswerTuple tuple; tuples.isValid(); tuples.moveNext()) { | ||
| 122 | // tuple = tuples.getTuple(); | ||
| 123 | // if (tuple.toString().contains("NC")) | ||
| 124 | // System.out.println(tuple.toString()); | ||
| 125 | // } | ||
| 126 | return record.getUpperBoundAnswers(); | ||
| 127 | } | ||
| 128 | else | ||
| 129 | return evaluate(queryText); | ||
| 130 | } | ||
| 131 | |||
| 132 | public AnswerTuples evaluate(String queryText) { | ||
| 133 | QueryRecord record = m_queryManager.create(queryText); | ||
| 134 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); | ||
| 135 | if (!record.processed()) | ||
| 136 | evaluate(record); | ||
| 137 | AnswerTuples answer = record.getAnswers(); | ||
| 138 | record.dispose(); | ||
| 139 | return answer; | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 | public void evaluate_shell(String queryText) { | ||
| 144 | QueryRecord record = m_queryManager.create(queryText); | ||
| 145 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); | ||
| 146 | if (!record.processed()) | ||
| 147 | evaluate(record); | ||
| 148 | Utility.logInfo("Answers to this query: ", record.outputSoundAnswerTuple()); | ||
| 149 | record.dispose(); | ||
| 150 | |||
| 151 | } | ||
| 152 | |||
| 153 | public void evaluate(Collection<QueryRecord> queryRecords) { | ||
| 154 | evaluate(queryRecords, null); | ||
| 155 | } | ||
| 156 | |||
| 157 | BufferedWriter answerWriter = null; | ||
| 158 | |||
| 159 | public void evaluate(Collection<QueryRecord> queryRecords, String answerFile) { | ||
| 160 | if (!isConsistent()) { | ||
| 161 | Utility.logDebug("The ontology and dataset is inconsistent."); | ||
| 162 | return ; | ||
| 163 | } | ||
| 164 | |||
| 165 | if (answerWriter == null && answerFile != null) { | ||
| 166 | try { | ||
| 167 | answerWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(answerFile))); | ||
| 168 | } catch (FileNotFoundException e) { | ||
| 169 | Utility.logInfo("The answer file not found! " + answerFile); | ||
| 170 | return ; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | Timer t = new Timer(); | ||
| 175 | for (QueryRecord record: queryRecords) { | ||
| 176 | // if (Integer.parseInt(record.getQueryID()) != 218) continue; | ||
| 177 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", | ||
| 178 | record.getQueryText()); | ||
| 179 | if (!record.processed()) { | ||
| 180 | t.reset(); | ||
| 181 | if (!record.processed()) | ||
| 182 | evaluate(record); | ||
| 183 | Utility.logInfo("Total time to answer this query: " + t.duration()); | ||
| 184 | if (!fullReasoner && !record.processed()) { | ||
| 185 | Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds."); | ||
| 186 | continue; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | // FIXME: change the argument below | ||
| 190 | try { | ||
| 191 | record.outputAnswers(answerWriter); | ||
| 192 | } catch (IOException e) { | ||
| 193 | Utility.logInfo("Error in outputing answers " + answerFile); | ||
| 194 | } | ||
| 195 | record.outputTimes(); | ||
| 196 | record.dispose(); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | public void dispose() { | ||
| 201 | if (answerWriter != null) | ||
| 202 | try { | ||
| 203 | answerWriter.close(); | ||
| 204 | } catch (IOException e) { | ||
| 205 | e.printStackTrace(); | ||
| 206 | } | ||
| 207 | Utility.cleanup(); | ||
| 208 | } | ||
| 209 | |||
| 210 | private QueryManager m_queryManager = new QueryManager(); | ||
| 211 | |||
| 212 | public QueryManager getQueryManager() { | ||
| 213 | return m_queryManager; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) { | ||
| 218 | return new HermiTReasoner(toCheckSatisfiability); | ||
| 219 | } | ||
| 220 | |||
| 221 | } | ||
