diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | 67 |
1 files changed, 36 insertions, 31 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java index f2b7251..1f08fdf 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | |||
| @@ -1,16 +1,10 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner; | 1 | package uk.ac.ox.cs.pagoda.reasoner; |
| 2 | 2 | ||
| 3 | import java.io.BufferedWriter; | 3 | import com.google.gson.Gson; |
| 4 | import java.io.File; | 4 | import com.google.gson.GsonBuilder; |
| 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; | 5 | import org.semanticweb.owlapi.model.OWLOntology; |
| 12 | |||
| 13 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | 6 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; |
| 7 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 14 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | 8 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; |
| 15 | import uk.ac.ox.cs.pagoda.query.QueryManager; | 9 | import uk.ac.ox.cs.pagoda.query.QueryManager; |
| 16 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | 10 | import uk.ac.ox.cs.pagoda.query.QueryRecord; |
| @@ -18,6 +12,13 @@ import uk.ac.ox.cs.pagoda.util.Properties; | |||
| 18 | import uk.ac.ox.cs.pagoda.util.Timer; | 12 | import uk.ac.ox.cs.pagoda.util.Timer; |
| 19 | import uk.ac.ox.cs.pagoda.util.Utility; | 13 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 20 | 14 | ||
| 15 | import java.io.BufferedWriter; | ||
| 16 | import java.io.File; | ||
| 17 | import java.io.IOException; | ||
| 18 | import java.nio.file.Files; | ||
| 19 | import java.nio.file.Paths; | ||
| 20 | import java.util.Collection; | ||
| 21 | |||
| 21 | public abstract class QueryReasoner { | 22 | public abstract class QueryReasoner { |
| 22 | 23 | ||
| 23 | // protected boolean forSemFacet = false; | 24 | // protected boolean forSemFacet = false; |
| @@ -179,28 +180,33 @@ public abstract class QueryReasoner { | |||
| 179 | 180 | ||
| 180 | } | 181 | } |
| 181 | 182 | ||
| 183 | // public void evaluate(Collection<QueryRecord> queryRecords) { | ||
| 184 | // evaluate(queryRecords); | ||
| 185 | // } | ||
| 186 | |||
| 187 | BufferedWriter answerWriter = null; | ||
| 188 | |||
| 182 | public void evaluate(Collection<QueryRecord> queryRecords) { | 189 | public void evaluate(Collection<QueryRecord> queryRecords) { |
| 183 | evaluate(queryRecords, null); | ||
| 184 | } | ||
| 185 | |||
| 186 | BufferedWriter answerWriter = null; | ||
| 187 | |||
| 188 | public void evaluate(Collection<QueryRecord> queryRecords, String answerFile) { | ||
| 189 | if (!isConsistent()) { | 190 | if (!isConsistent()) { |
| 190 | Utility.logDebug("The ontology and dataset is inconsistent."); | 191 | Utility.logDebug("The ontology and dataset is inconsistent."); |
| 191 | return ; | 192 | return ; |
| 192 | } | 193 | } |
| 193 | 194 | ||
| 194 | if (answerWriter == null && answerFile != null) { | 195 | if(properties.getAnswerPath() != null && answerWriter == null) { |
| 195 | try { | 196 | try { |
| 196 | answerWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(answerFile))); | 197 | answerWriter = Files.newBufferedWriter(Paths.get(properties.getAnswerPath())); |
| 197 | } catch (FileNotFoundException e) { | 198 | } catch (IOException e) { |
| 198 | Utility.logInfo("The answer file not found! " + answerFile); | 199 | Utility.logError("The answer path is not valid!"); |
| 199 | return ; | 200 | e.printStackTrace(); |
| 200 | } | 201 | } |
| 201 | } | 202 | } |
| 202 | 203 | ||
| 203 | Timer t = new Timer(); | 204 | Timer t = new Timer(); |
| 205 | Gson gson = new GsonBuilder() | ||
| 206 | .registerTypeAdapter(AnswerTuple.class, new AnswerTuple.AnswerTupleSerializer()) | ||
| 207 | .registerTypeAdapter(QueryRecord.class, new QueryRecord.QueryRecordSerializer()) | ||
| 208 | .setPrettyPrinting() | ||
| 209 | .create(); | ||
| 204 | for (QueryRecord record: queryRecords) { | 210 | for (QueryRecord record: queryRecords) { |
| 205 | // if (Integer.parseInt(record.getQueryID()) != 218) continue; | 211 | // if (Integer.parseInt(record.getQueryID()) != 218) continue; |
| 206 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", | 212 | Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", |
| @@ -215,24 +221,23 @@ public abstract class QueryReasoner { | |||
| 215 | continue; | 221 | continue; |
| 216 | } | 222 | } |
| 217 | } | 223 | } |
| 218 | // FIXME: change the argument below | 224 | record.outputAnswerStatistics(); |
| 219 | try { | ||
| 220 | record.outputAnswers(answerWriter); | ||
| 221 | } catch (IOException e) { | ||
| 222 | Utility.logInfo("Error in outputing answers " + answerFile); | ||
| 223 | } | ||
| 224 | record.outputTimes(); | 225 | record.outputTimes(); |
| 225 | record.dispose(); | ||
| 226 | } | 226 | } |
| 227 | // TODO it can handle one call only | ||
| 228 | // if you call twice, you will end up with a json file with multiple roots | ||
| 229 | gson.toJson(queryRecords, answerWriter); | ||
| 230 | queryRecords.stream().forEach(record -> record.dispose()); | ||
| 227 | } | 231 | } |
| 228 | 232 | ||
| 229 | public void dispose() { | 233 | public void dispose() { |
| 230 | if (answerWriter != null) | 234 | if (answerWriter != null) { |
| 231 | try { | 235 | try { |
| 232 | answerWriter.close(); | 236 | answerWriter.close(); |
| 233 | } catch (IOException e) { | 237 | } catch (IOException e) { |
| 234 | e.printStackTrace(); | 238 | e.printStackTrace(); |
| 235 | } | 239 | } |
| 240 | } | ||
| 236 | Utility.cleanup(); | 241 | Utility.cleanup(); |
| 237 | } | 242 | } |
| 238 | 243 | ||
