aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java
diff options
context:
space:
mode:
authorRncLsn <rnc.lsn@gmail.com>2015-05-19 13:35:52 +0100
committerRncLsn <rnc.lsn@gmail.com>2015-05-19 13:35:52 +0100
commit5d54af2638a53721b414a41356a93686a9616272 (patch)
treee28c64b1887e7e964661d12d96df5b09abd4d9ee /src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java
parentc7dbc7c61c7094ea4ec49bd630023f23b92fd9d1 (diff)
downloadACQuA-5d54af2638a53721b414a41356a93686a9616272.tar.gz
ACQuA-5d54af2638a53721b414a41356a93686a9616272.zip
Backup before changes in MyQueryReasoner.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java174
1 files changed, 85 insertions, 89 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java
index dfe0c5f..d4f4596 100644
--- a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java
+++ b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java
@@ -19,24 +19,26 @@ import java.util.Collection;
19 19
20// TODO clean APIs 20// TODO clean APIs
21public abstract class QueryReasoner { 21public abstract class QueryReasoner {
22 22
23 public static final String ImportDataFileSeparator = ";";
24 private static final boolean DEFAULT_MULTI_STAGES = true;
25 private static final boolean DEFAULT_EQUALITIES = true;
26 public boolean fullReasoner = this instanceof MyQueryReasoner;
27 protected StringBuilder importedData = new StringBuilder();
23// protected boolean forSemFacet = false; 28// protected boolean forSemFacet = false;
24 Properties properties; 29 Properties properties;
30 BufferedWriter answerWriter = null;
31 private QueryManager m_queryManager = new QueryManager();
25 32
26 private static boolean defaultMultiStages = true;
27 private static boolean defaultEqualities = true;
28
29 public enum Type { Full, RLU, ELHOU }
30
31 public static QueryReasoner getInstance(Properties p) { 33 public static QueryReasoner getInstance(Properties p) {
32 OWLOntology ontology = OWLHelper.loadOntology(p.getOntologyPath()); 34 OWLOntology ontology = OWLHelper.loadOntology(p.getOntologyPath());
33 QueryReasoner pagoda = getInstance(ontology, p); 35 QueryReasoner pagoda = getInstance(ontology, p);
34 pagoda.properties = p; 36 pagoda.properties = p;
35 pagoda.loadOntology(ontology); 37 pagoda.loadOntology(ontology);
36 pagoda.importData(p.getDataPath()); 38 pagoda.importData(p.getDataPath());
37 if (pagoda.preprocess()) { 39 if (pagoda.preprocess()) {
38 Utility.logInfo("The ontology is consistent!"); 40 Utility.logInfo("The ontology is consistent!");
39 return pagoda; 41 return pagoda;
40 } 42 }
41 else { 43 else {
42 System.out.println("The ontology is inconsistent!"); 44 System.out.println("The ontology is inconsistent!");
@@ -44,60 +46,63 @@ public abstract class QueryReasoner {
44 return null; 46 return null;
45 } 47 }
46 } 48 }
47 49
48 public static QueryReasoner getInstance(OWLOntology o) { 50 public static QueryReasoner getInstance(OWLOntology o) {
49 QueryReasoner pagoda = getInstance(Type.Full, o, defaultMultiStages, defaultEqualities); 51 QueryReasoner pagoda = getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES);
50 pagoda.properties = new Properties(); 52 pagoda.properties = new Properties();
51 return pagoda; 53 return pagoda;
52 } 54 }
53 55
54 public void setToClassify(boolean flag) {
55 properties.setToClassify(flag);
56 }
57
58 public void setToCallHermiT(boolean flag) {
59 properties.setToCallHermiT(flag);
60 }
61
62 private static QueryReasoner getInstance(OWLOntology o, Properties p) { 56 private static QueryReasoner getInstance(OWLOntology o, Properties p) {
63 return getInstance(Type.Full, o, defaultMultiStages, defaultEqualities); 57 return getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES);
64 } 58 }
65 59
66 public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) { 60 public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) {
67// Utility.initialise(); 61// Utility.initialise();
68 QueryReasoner reasoner; 62 QueryReasoner reasoner;
69 if (OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner(); 63 if (OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner();
70 else if (OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner(); 64 else if (OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner();
71 else 65 else
72 switch (type) { 66 switch (type) {
73 case RLU: 67 case RLU:
74 reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities); break; 68 reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities);
75 case ELHOU: 69 break;
76 reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities); break; 70 case ELHOU:
77 default: 71 reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities);
78 reasoner = new MyQueryReasoner(performMultiStages, considerEqualities); 72 break;
73 default:
74 reasoner = new MyQueryReasoner(performMultiStages, considerEqualities);
79 } 75 }
80 return reasoner; 76 return reasoner;
77 }
78
79 public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) {
80 return new HermiTReasoner(toCheckSatisfiability);
81 }
82
83 public void setToClassify(boolean flag) {
84 properties.setToClassify(flag);
85 }
86
87 public void setToCallHermiT(boolean flag) {
88 properties.setToCallHermiT(flag);
81 } 89 }
82
83 public static final String ImportDataFileSeparator = ";";
84 protected StringBuilder importedData = new StringBuilder();
85 90
86 public void importData(String datafile) { 91 public void importData(String datafile) {
87 if (datafile != null && !datafile.equalsIgnoreCase("null")) 92 if (datafile != null && !datafile.equalsIgnoreCase("null"))
88 importData(datafile.split(ImportDataFileSeparator)); 93 importData(datafile.split(ImportDataFileSeparator));
89 } 94 }
90 95
91 public void importData(String[] datafiles) { 96 public void importData(String[] datafiles) {
92 if (datafiles != null) { 97 if (datafiles != null) {
93 for (String datafile: datafiles) { 98 for (String datafile: datafiles) {
94 File file = new File(datafile); 99 File file = new File(datafile);
95 if (file.exists()) { 100 if (file.exists()) {
96 if (file.isFile()) importDataFile(file); 101 if (file.isFile()) importDataFile(file);
97 else importDataDirectory(file); 102 else importDataDirectory(file);
98 } 103 }
99 else { 104 else {
100 Utility.logError("warning: file " + datafile + " doesn't exists."); 105 Utility.logError("warning: file " + datafile + " doesn't exists.");
101 } 106 }
102 } 107 }
103 } 108 }
@@ -115,80 +120,75 @@ public abstract class QueryReasoner {
115 datafile = file.getCanonicalPath(); 120 datafile = file.getCanonicalPath();
116 } catch (IOException e) { 121 } catch (IOException e) {
117 e.printStackTrace(); 122 e.printStackTrace();
118 return ; 123 return;
119 } 124 }
120 importDataFile(datafile); 125 importDataFile(datafile);
121 } 126 }
122 127
123 protected final void importDataFile(String datafile) { 128 protected final void importDataFile(String datafile) {
124 if (importedData.length() == 0) 129 if (importedData.length() == 0)
125 importedData.append(datafile); 130 importedData.append(datafile);
126 else 131 else
127 importedData.append(ImportDataFileSeparator).append(datafile); 132 importedData.append(ImportDataFileSeparator).append(datafile);
128 133
129 } 134 }
130
131 public abstract void loadOntology(OWLOntology ontology);
132
133 public abstract boolean preprocess();
134 135
135 public abstract boolean isConsistent(); 136 public abstract void loadOntology(OWLOntology ontology);
136 137
137 public boolean fullReasoner = this instanceof MyQueryReasoner; 138 public abstract boolean preprocess();
138 139
139 public abstract void evaluate(QueryRecord record); 140 public abstract boolean isConsistent();
140 141
141 public abstract void evaluateUpper(QueryRecord record); 142 public abstract void evaluate(QueryRecord record);
143
144 public abstract void evaluateUpper(QueryRecord record);
142 145
143 public AnswerTuples evaluate(String queryText, boolean forFacetGeneration) { 146 public AnswerTuples evaluate(String queryText, boolean forFacetGeneration) {
144 if (forFacetGeneration) { 147 if (forFacetGeneration) {
145 QueryRecord record = m_queryManager.create(queryText); 148 QueryRecord record = m_queryManager.create(queryText);
146 Utility.logInfo("---------- start evaluating upper bound for Query " + record.getQueryID() + " ----------", queryText); 149 Utility.logInfo("---------- start evaluating upper bound for Query " + record.getQueryID() + " ----------", queryText);
147 if (!record.processed()) 150 if(!record.processed())
148 evaluateUpper(record); 151 evaluateUpper(record);
149// AnswerTuples tuples = record.getUpperBoundAnswers(); 152// AnswerTuples tuples = record.getUpperBoundAnswers();
150// for (AnswerTuple tuple; tuples.isValid(); tuples.moveNext()) { 153// for (AnswerTuple tuple; tuples.isValid(); tuples.moveNext()) {
151// tuple = tuples.getTuple(); 154// tuple = tuples.getTuple();
152// if (tuple.toString().contains("NC")) 155// if (tuple.toString().contains("NC"))
153// System.out.println(tuple.toString()); 156// System.out.println(tuple.toString());
154// } 157// }
155 return record.getUpperBoundAnswers(); 158 return record.getUpperBoundAnswers();
156 } 159 } else
157 else 160 return evaluate(queryText);
158 return evaluate(queryText);
159 } 161 }
160 162
163// public void evaluate(Collection<QueryRecord> queryRecords) {
164// evaluate(queryRecords);
165// }
166
161 public AnswerTuples evaluate(String queryText) { 167 public AnswerTuples evaluate(String queryText) {
162 QueryRecord record = m_queryManager.create(queryText); 168 QueryRecord record = m_queryManager.create(queryText);
163 Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); 169 Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText);
164 if (!record.processed()) 170 if(!record.processed())
165 evaluate(record); 171 evaluate(record);
166 AnswerTuples answer = record.getAnswers(); 172 AnswerTuples answer = record.getAnswers();
167 record.dispose(); 173 record.dispose();
168 return answer; 174 return answer;
169 175
170 } 176 }
171 177
172 public void evaluate_shell(String queryText) { 178 public void evaluate_shell(String queryText) {
173 QueryRecord record = m_queryManager.create(queryText); 179 QueryRecord record = m_queryManager.create(queryText);
174 Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); 180 Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText);
175 if (!record.processed()) 181 if(!record.processed())
176 evaluate(record); 182 evaluate(record);
177 Utility.logInfo("Answers to this query: ", record.outputSoundAnswerTuple()); 183 Utility.logInfo("Answers to this query: ", record.outputSoundAnswerTuple());
178 record.dispose(); 184 record.dispose();
179 185
180 } 186 }
181 187
182// public void evaluate(Collection<QueryRecord> queryRecords) {
183// evaluate(queryRecords);
184// }
185
186 BufferedWriter answerWriter = null;
187
188 public void evaluate(Collection<QueryRecord> queryRecords) { 188 public void evaluate(Collection<QueryRecord> queryRecords) {
189 if (!isConsistent()) { 189 if (!isConsistent()) {
190 Utility.logDebug("The ontology and dataset is inconsistent."); 190 Utility.logDebug("The ontology and dataset is inconsistent.");
191 return ; 191 return;
192 } 192 }
193 193
194 if(properties.getAnswerPath() != null && answerWriter == null) { 194 if(properties.getAnswerPath() != null && answerWriter == null) {
@@ -199,21 +199,21 @@ public abstract class QueryReasoner {
199 e.printStackTrace(); 199 e.printStackTrace();
200 } 200 }
201 } 201 }
202 202
203 Timer t = new Timer(); 203 Timer t = new Timer();
204 Gson gson = QueryRecord.GsonCreator.getInstance(); 204 Gson gson = QueryRecord.GsonCreator.getInstance();
205 for (QueryRecord record: queryRecords) { 205 for (QueryRecord record: queryRecords) {
206// if (Integer.parseInt(record.getQueryID()) != 218) continue; 206// if (Integer.parseInt(record.getQueryID()) != 218) continue;
207 Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", 207 Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------",
208 record.getQueryText()); 208 record.getQueryText());
209 if (!record.processed()) { 209 if (!record.processed()) {
210 t.reset(); 210 t.reset();
211 if (!record.processed()) 211 if (!record.processed())
212 evaluate(record); 212 evaluate(record);
213 Utility.logInfo("Total time to answer this query: " + t.duration()); 213 Utility.logInfo("Total time to answer this query: " + t.duration());
214 if (!fullReasoner && !record.processed()) { 214 if (!fullReasoner && !record.processed()) {
215 Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds."); 215 Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds.");
216 continue; 216 continue;
217 } 217 }
218 } 218 }
219 record.outputAnswerStatistics(); 219 record.outputAnswerStatistics();
@@ -225,7 +225,7 @@ public abstract class QueryReasoner {
225// queryRecords.stream().forEach(record -> Utility.logDebug(gson.toJson(record))); 225// queryRecords.stream().forEach(record -> Utility.logDebug(gson.toJson(record)));
226 queryRecords.stream().forEach(record -> record.dispose()); 226 queryRecords.stream().forEach(record -> record.dispose());
227 } 227 }
228 228
229 public void dispose() { 229 public void dispose() {
230 if (answerWriter != null) { 230 if (answerWriter != null) {
231 try { 231 try {
@@ -235,17 +235,13 @@ public abstract class QueryReasoner {
235 } 235 }
236 } 236 }
237// Utility.cleanup(); 237// Utility.cleanup();
238 } 238 }
239
240 private QueryManager m_queryManager = new QueryManager();
241 239
242 public QueryManager getQueryManager() { 240 public QueryManager getQueryManager() {
243 return m_queryManager; 241 return m_queryManager;
244 } 242 }
245 243
246 244
247 public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) { 245 public enum Type {Full, RLU, ELHOU}
248 return new HermiTReasoner(toCheckSatisfiability);
249 }
250 246
251} 247}