diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-07 19:26:24 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-07 19:26:24 +0100 |
| commit | 5be5fd3daa0d50980fb3791e904e035cdbca254f (patch) | |
| tree | 8109fdd92d6a8f3b59b47f147d21e23b84301494 /src/uk/ac/ox/cs | |
| parent | 11a432bfc3cb11e07c68c4298fcec060ff1e25fa (diff) | |
| download | ACQuA-5be5fd3daa0d50980fb3791e904e035cdbca254f.tar.gz ACQuA-5be5fd3daa0d50980fb3791e904e035cdbca254f.zip | |
Making the output machine-readable (JSON).
Diffstat (limited to 'src/uk/ac/ox/cs')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java | 41 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/query/QueryRecord.java | 191 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | 67 | ||||
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/Utility.java | 39 |
4 files changed, 213 insertions, 125 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java index 8d7e0b1..183cbe1 100644 --- a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java | |||
| @@ -1,13 +1,10 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | 1 | package uk.ac.ox.cs.pagoda.query; |
| 2 | 2 | ||
| 3 | import java.util.HashMap; | 3 | import com.google.gson.*; |
| 4 | import java.util.Map; | ||
| 5 | |||
| 6 | import org.semanticweb.HermiT.model.Constant; | 4 | import org.semanticweb.HermiT.model.Constant; |
| 7 | import org.semanticweb.HermiT.model.Individual; | 5 | import org.semanticweb.HermiT.model.Individual; |
| 8 | import org.semanticweb.HermiT.model.Term; | 6 | import org.semanticweb.HermiT.model.Term; |
| 9 | import org.semanticweb.HermiT.model.Variable; | 7 | import org.semanticweb.HermiT.model.Variable; |
| 10 | |||
| 11 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | 8 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; |
| 12 | import uk.ac.ox.cs.JRDFox.model.Datatype; | 9 | import uk.ac.ox.cs.JRDFox.model.Datatype; |
| 13 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | 10 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; |
| @@ -15,6 +12,10 @@ import uk.ac.ox.cs.JRDFox.model.Literal; | |||
| 15 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | 12 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; |
| 16 | import uk.ac.ox.cs.pagoda.util.Namespace; | 13 | import uk.ac.ox.cs.pagoda.util.Namespace; |
| 17 | 14 | ||
| 15 | import java.lang.reflect.Type; | ||
| 16 | import java.util.HashMap; | ||
| 17 | import java.util.Map; | ||
| 18 | |||
| 18 | public class AnswerTuple { | 19 | public class AnswerTuple { |
| 19 | 20 | ||
| 20 | public static final String SEPARATOR = "\t"; | 21 | public static final String SEPARATOR = "\t"; |
| @@ -40,17 +41,21 @@ public class AnswerTuple { | |||
| 40 | m_tuple = new GroundTerm[arity]; | 41 | m_tuple = new GroundTerm[arity]; |
| 41 | for (int i = 0; i < arity; ++i) m_tuple[i] = sup.m_tuple[i]; | 42 | for (int i = 0; i < arity; ++i) m_tuple[i] = sup.m_tuple[i]; |
| 42 | } | 43 | } |
| 44 | |||
| 45 | private AnswerTuple(String m_str) { | ||
| 46 | this.m_str = m_str; | ||
| 47 | } | ||
| 43 | 48 | ||
| 44 | public int getArity() { | 49 | public int getArity() { |
| 45 | return m_tuple.length; | 50 | return m_tuple.length; |
| 46 | } | 51 | } |
| 47 | 52 | ||
| 48 | public int hashCode() { | 53 | public int hashCode() { |
| 49 | // return toString().hashCode(); | 54 | return toString().hashCode(); |
| 50 | int code = 0; | 55 | // int code = 0; |
| 51 | for (int i = 0; i < m_tuple.length; ++i) | 56 | // for (int i = 0; i < m_tuple.length; ++i) |
| 52 | code = code * 1997 + m_tuple[i].hashCode(); | 57 | // code = code * 1997 + m_tuple[i].hashCode(); |
| 53 | return code; | 58 | // return code; |
| 54 | } | 59 | } |
| 55 | 60 | ||
| 56 | public boolean equals(Object obj) { | 61 | public boolean equals(Object obj) { |
| @@ -131,5 +136,21 @@ public class AnswerTuple { | |||
| 131 | if (length == extendedTuple.getArity()) return extendedTuple; | 136 | if (length == extendedTuple.getArity()) return extendedTuple; |
| 132 | else return new AnswerTuple(extendedTuple, length); | 137 | else return new AnswerTuple(extendedTuple, length); |
| 133 | } | 138 | } |
| 134 | 139 | ||
| 140 | |||
| 141 | public static class AnswerTupleSerializer implements JsonSerializer<AnswerTuple> { | ||
| 142 | |||
| 143 | public JsonElement serialize(AnswerTuple src, Type typeOfSrc, JsonSerializationContext context) { | ||
| 144 | return new JsonPrimitive(src.toString()); | ||
| 145 | } | ||
| 146 | |||
| 147 | } | ||
| 148 | |||
| 149 | public class AnswerTupleDeserializer implements JsonDeserializer<AnswerTuple> { | ||
| 150 | public AnswerTuple deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
| 151 | throws JsonParseException { | ||
| 152 | return new AnswerTuple(json.getAsJsonPrimitive().getAsString()); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 135 | } | 156 | } |
diff --git a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java index ce92a67..4b55046 100644 --- a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java +++ b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java | |||
| @@ -1,37 +1,9 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | 1 | package uk.ac.ox.cs.pagoda.query; |
| 2 | 2 | ||
| 3 | import java.io.BufferedWriter; | 3 | import com.google.gson.*; |
| 4 | import java.io.FileNotFoundException; | 4 | import com.google.gson.reflect.TypeToken; |
| 5 | import java.io.FileOutputStream; | 5 | import org.semanticweb.HermiT.model.*; |
| 6 | import java.io.IOException; | 6 | import org.semanticweb.owlapi.model.*; |
| 7 | import java.io.OutputStreamWriter; | ||
| 8 | import java.util.Collection; | ||
| 9 | import java.util.HashMap; | ||
| 10 | import java.util.HashSet; | ||
| 11 | import java.util.Iterator; | ||
| 12 | import java.util.LinkedList; | ||
| 13 | import java.util.Map; | ||
| 14 | import java.util.Set; | ||
| 15 | |||
| 16 | import org.semanticweb.HermiT.model.Atom; | ||
| 17 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 18 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 19 | import org.semanticweb.HermiT.model.DLClause; | ||
| 20 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 21 | import org.semanticweb.HermiT.model.Variable; | ||
| 22 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 23 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 24 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 25 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 26 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 27 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 28 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 29 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 30 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 31 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 32 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 33 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; | ||
| 34 | |||
| 35 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | 7 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; |
| 36 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxAnswerTuples; | 8 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxAnswerTuples; |
| 37 | import uk.ac.ox.cs.pagoda.rules.GeneralProgram; | 9 | import uk.ac.ox.cs.pagoda.rules.GeneralProgram; |
| @@ -39,6 +11,10 @@ import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; | |||
| 39 | import uk.ac.ox.cs.pagoda.util.Namespace; | 11 | import uk.ac.ox.cs.pagoda.util.Namespace; |
| 40 | import uk.ac.ox.cs.pagoda.util.Utility; | 12 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 41 | 13 | ||
| 14 | import java.io.*; | ||
| 15 | import java.lang.reflect.Type; | ||
| 16 | import java.util.*; | ||
| 17 | |||
| 42 | public class QueryRecord { | 18 | public class QueryRecord { |
| 43 | 19 | ||
| 44 | public static final String botQueryText = "SELECT ?X WHERE { ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Nothing> }"; | 20 | public static final String botQueryText = "SELECT ?X WHERE { ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Nothing> }"; |
| @@ -47,11 +23,12 @@ public class QueryRecord { | |||
| 47 | 23 | ||
| 48 | private String queryText; | 24 | private String queryText; |
| 49 | private int queryID = -1; | 25 | private int queryID = -1; |
| 50 | private String[][] answerVariables = null; | 26 | |
| 51 | private Set<AnswerTuple> soundAnswerTuples = new HashSet<AnswerTuple>(); | 27 | private String[][] answerVariables = null; |
| 28 | private Set<AnswerTuple> soundAnswerTuples = new HashSet<AnswerTuple>(); | ||
| 52 | private Set<AnswerTuple> gapAnswerTuples = null; | 29 | private Set<AnswerTuple> gapAnswerTuples = null; |
| 53 | 30 | ||
| 54 | private QueryManager m_manager; | 31 | private QueryManager m_manager; |
| 55 | 32 | ||
| 56 | public QueryRecord(QueryManager manager, String text, int id, int subID) { | 33 | public QueryRecord(QueryManager manager, String text, int id, int subID) { |
| 57 | m_manager =manager; | 34 | m_manager =manager; |
| @@ -178,7 +155,7 @@ public class QueryRecord { | |||
| 178 | // str.contains("internal:def"); | 155 | // str.contains("internal:def"); |
| 179 | // } | 156 | // } |
| 180 | 157 | ||
| 181 | boolean processed = false; | 158 | boolean processed = false; |
| 182 | 159 | ||
| 183 | public void markAsProcessed() { | 160 | public void markAsProcessed() { |
| 184 | processed = true; | 161 | processed = true; |
| @@ -205,7 +182,7 @@ public class QueryRecord { | |||
| 205 | return queryText; | 182 | return queryText; |
| 206 | } | 183 | } |
| 207 | 184 | ||
| 208 | String stringQueryID = null; | 185 | String stringQueryID = null; |
| 209 | 186 | ||
| 210 | public String getQueryID() { | 187 | public String getQueryID() { |
| 211 | return stringQueryID; | 188 | return stringQueryID; |
| @@ -233,27 +210,27 @@ public class QueryRecord { | |||
| 233 | writer.newLine(); | 210 | writer.newLine(); |
| 234 | writer.write(queryText); | 211 | writer.write(queryText); |
| 235 | writer.newLine(); | 212 | writer.newLine(); |
| 236 | StringBuilder space = new StringBuilder(); | 213 | StringBuilder space = new StringBuilder(); |
| 237 | int arity = getArity(), varSpace = 0; | 214 | int arity = getArity(), varSpace = 0; |
| 238 | for (int i = 0; i < arity; ++i) | 215 | for (int i = 0; i < arity; ++i) |
| 239 | varSpace += answerVariables[0][i].length(); | 216 | varSpace += answerVariables[0][i].length(); |
| 240 | for (int i = 0; i < (SEPARATOR.length() - varSpace) / (arity + 1); ++i) | 217 | for (int i = 0; i < (SEPARATOR.length() - varSpace) / (arity + 1); ++i) |
| 241 | space.append(" "); | 218 | space.append(" "); |
| 242 | for (int i = 0; i < getArity(); ++i) { | 219 | for (int i = 0; i < getArity(); ++i) { |
| 243 | writer.write(space.toString()); | 220 | writer.write(space.toString()); |
| 244 | writer.write(answerVariables[0][i]); | 221 | writer.write(answerVariables[0][i]); |
| 245 | } | 222 | } |
| 246 | writer.newLine(); | 223 | writer.newLine(); |
| 247 | writer.write(SEPARATOR); | 224 | writer.write(SEPARATOR); |
| 248 | writer.newLine(); | 225 | writer.newLine(); |
| 249 | for (AnswerTuple tuple: soundAnswerTuples) { | 226 | for (AnswerTuple tuple: soundAnswerTuples) { |
| 250 | writer.write(tuple.toString()); | 227 | writer.write(tuple.toString()); |
| 251 | writer.newLine(); | 228 | writer.newLine(); |
| 252 | } | 229 | } |
| 253 | if (!processed()) | 230 | if (!processed()) |
| 254 | for (AnswerTuple tuple: gapAnswerTuples) { | 231 | for (AnswerTuple tuple: gapAnswerTuples) { |
| 255 | writer.write("*"); | 232 | writer.write("*"); |
| 256 | writer.write(tuple.toString()); | 233 | writer.write(tuple.toString()); |
| 257 | writer.newLine(); | 234 | writer.newLine(); |
| 258 | } | 235 | } |
| 259 | // writer.write(SEPARATOR); | 236 | // writer.write(SEPARATOR); |
| @@ -261,6 +238,38 @@ public class QueryRecord { | |||
| 261 | } | 238 | } |
| 262 | 239 | ||
| 263 | } | 240 | } |
| 241 | |||
| 242 | public void outputAnswerStatistics() { | ||
| 243 | |||
| 244 | int answerCounter = soundAnswerTuples.size(); | ||
| 245 | if (!processed()) answerCounter += gapAnswerTuples.size(); | ||
| 246 | |||
| 247 | Utility.logInfo("The number of answer tuples: " + answerCounter); | ||
| 248 | // if (jsonAnswers != null) { | ||
| 249 | // JSONObject jsonAnswer = new JSONObject(); | ||
| 250 | // | ||
| 251 | // jsonAnswer.put("queryID", queryID); | ||
| 252 | // jsonAnswer.put("queryText", queryText); | ||
| 253 | // | ||
| 254 | // JSONArray answerVars = new JSONArray(); | ||
| 255 | // int arity = getArity(), varSpace = 0; | ||
| 256 | // for (int i = 0; i < getArity(); i++) | ||
| 257 | // answerVars.add(answerVariables[0][i]); | ||
| 258 | // jsonAnswer.put("answerVars", answerVars); | ||
| 259 | // | ||
| 260 | // JSONArray answerTuples = new JSONArray(); | ||
| 261 | // soundAnswerTuples.stream().forEach(t -> answerTuples.add(t)); | ||
| 262 | // jsonAnswer.put("answerTuples", answerTuples); | ||
| 263 | // | ||
| 264 | // if (!processed) { | ||
| 265 | // JSONArray gapAnswerTuples = new JSONArray(); | ||
| 266 | // gapAnswerTuples.stream().forEach(t -> gapAnswerTuples.add(t)); | ||
| 267 | // } | ||
| 268 | // jsonAnswer.put("gapAnswerTuples", gapAnswerTuples); | ||
| 269 | // | ||
| 270 | // jsonAnswers.put(Integer.toString(queryID), jsonAnswer); | ||
| 271 | // } | ||
| 272 | } | ||
| 264 | 273 | ||
| 265 | public void outputTimes() { | 274 | public void outputTimes() { |
| 266 | for (Step step: Step.values()) { | 275 | for (Step step: Step.values()) { |
| @@ -290,8 +299,8 @@ public class QueryRecord { | |||
| 290 | return diffculty; | 299 | return diffculty; |
| 291 | } | 300 | } |
| 292 | 301 | ||
| 293 | OWLOntology relevantOntology = null; | 302 | OWLOntology relevantOntology = null; |
| 294 | Set<DLClause> relevantClauses = new HashSet<DLClause>(); | 303 | Set<DLClause> relevantClauses = new HashSet<DLClause>(); |
| 295 | 304 | ||
| 296 | public void setRelevantOntology(OWLOntology knowledgebase) { | 305 | public void setRelevantOntology(OWLOntology knowledgebase) { |
| 297 | relevantOntology = knowledgebase; | 306 | relevantOntology = knowledgebase; |
| @@ -355,7 +364,7 @@ public class QueryRecord { | |||
| 355 | public enum Step {LowerBound, UpperBound, ELLowerBound, | 364 | public enum Step {LowerBound, UpperBound, ELLowerBound, |
| 356 | Fragment, FragmentRefinement, Summarisation, Dependency, FullReasoning}; | 365 | Fragment, FragmentRefinement, Summarisation, Dependency, FullReasoning}; |
| 357 | 366 | ||
| 358 | double[] timer; | 367 | double[] timer; |
| 359 | 368 | ||
| 360 | public void addProcessingTime(Step step, double time) { | 369 | public void addProcessingTime(Step step, double time) { |
| 361 | timer[step.ordinal()] += time; | 370 | timer[step.ordinal()] += time; |
| @@ -431,14 +440,14 @@ public class QueryRecord { | |||
| 431 | } | 440 | } |
| 432 | } | 441 | } |
| 433 | 442 | ||
| 434 | int subID; | 443 | int subID; |
| 435 | 444 | ||
| 436 | public void updateSubID() { | 445 | public void updateSubID() { |
| 437 | ++subID; | 446 | ++subID; |
| 438 | stringQueryID = String.valueOf(queryID) + "_" + subID; | 447 | stringQueryID = String.valueOf(queryID) + "_" + subID; |
| 439 | } | 448 | } |
| 440 | 449 | ||
| 441 | DLClause queryClause = null; | 450 | DLClause queryClause = null; |
| 442 | 451 | ||
| 443 | public DLClause getClause() { | 452 | public DLClause getClause() { |
| 444 | if (queryClause != null) | 453 | if (queryClause != null) |
| @@ -557,6 +566,88 @@ public class QueryRecord { | |||
| 557 | public boolean hasNonAnsDistinguishedVariables() { | 566 | public boolean hasNonAnsDistinguishedVariables() { |
| 558 | return answerVariables[1].length > answerVariables[0].length; | 567 | return answerVariables[1].length > answerVariables[0].length; |
| 559 | } | 568 | } |
| 560 | |||
| 561 | 569 | ||
| 570 | public static class QueryRecordSerializer implements JsonSerializer<QueryRecord> { | ||
| 571 | |||
| 572 | public JsonElement serialize(QueryRecord src, Type typeOfSrc, JsonSerializationContext context) { | ||
| 573 | Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| 574 | JsonObject object = new JsonObject(); | ||
| 575 | object.addProperty("queryID", src.queryID); | ||
| 576 | object.addProperty("queryText", src.queryText); | ||
| 577 | object.addProperty("difficulty", src.diffculty.toString()); | ||
| 578 | |||
| 579 | object.add("answerVariables", context.serialize(src.getAnswerVariables())); | ||
| 580 | object.add("answers", context.serialize(src.soundAnswerTuples)); | ||
| 581 | object.add("gapAnswers", context.serialize(src.gapAnswerTuples)); | ||
| 582 | |||
| 583 | return object; | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | private QueryRecord() { } | ||
| 588 | |||
| 589 | public class QueryRecordDeserializer implements JsonDeserializer<QueryRecord> { | ||
| 590 | |||
| 591 | public QueryRecord deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
| 592 | throws JsonParseException { | ||
| 593 | |||
| 594 | QueryRecord record = new QueryRecord(); | ||
| 595 | JsonObject object = json.getAsJsonObject(); | ||
| 596 | record.queryID = object.getAsJsonPrimitive("queryID").getAsInt(); | ||
| 597 | record.queryText = object.getAsJsonPrimitive("queryText").getAsString(); | ||
| 598 | |||
| 599 | JsonArray answerVariablesJson = object.getAsJsonArray("answerVariables"); | ||
| 600 | record.answerVariables = new String[2][]; | ||
| 601 | record.answerVariables[0] = new String[answerVariablesJson.size()]; | ||
| 602 | for(int i = 0; i < answerVariablesJson.size(); i++) | ||
| 603 | record.answerVariables[0][i] = answerVariablesJson.get(i).getAsString(); | ||
| 604 | |||
| 605 | record.soundAnswerTuples = context.deserialize(object.getAsJsonObject("answers"), | ||
| 606 | new TypeToken<Set<AnswerTuple>>() {}.getType()); | ||
| 607 | |||
| 608 | record.gapAnswerTuples = context.deserialize(object.getAsJsonObject("gapAnswers"), | ||
| 609 | new TypeToken<Set<AnswerTuple>>() {}.getType()); | ||
| 610 | return null; | ||
| 611 | } | ||
| 612 | } | ||
| 613 | |||
| 614 | /* | ||
| 615 | * Two QueryRecords are equal iff | ||
| 616 | * they have the same queryText and | ||
| 617 | * their AnswerTuples have the same string representation | ||
| 618 | * */ | ||
| 619 | @Override | ||
| 620 | public boolean equals(Object o) { | ||
| 621 | if(!o.getClass().equals(getClass())) return false; | ||
| 622 | QueryRecord that = (QueryRecord) o; | ||
| 623 | |||
| 624 | if(!this.queryText.equals(that.queryText)) return false; | ||
| 625 | |||
| 626 | if(soundAnswerTuples.size() != that.soundAnswerTuples.size()) return false; | ||
| 627 | if(gapAnswerTuples.size() != that.gapAnswerTuples.size()) return false; | ||
| 628 | |||
| 629 | ArrayList<AnswerTuple> thisSoundAnswers = new ArrayList<>(soundAnswerTuples); | ||
| 630 | Collections.sort(thisSoundAnswers, (AnswerTuple t1, AnswerTuple t2) -> t1.m_str.compareTo(t2.m_str)); | ||
| 631 | |||
| 632 | ArrayList<AnswerTuple> thatSoundAnswers = new ArrayList<>(that.soundAnswerTuples); | ||
| 633 | Collections.sort(thatSoundAnswers, (AnswerTuple t1, AnswerTuple t2) -> t1.m_str.compareTo(t2.m_str)); | ||
| 634 | |||
| 635 | Iterator<AnswerTuple> soundIt1 = this.soundAnswerTuples.iterator(); | ||
| 636 | Iterator<AnswerTuple> soundIt2 = that.soundAnswerTuples.iterator(); | ||
| 637 | while(soundIt1.hasNext() && soundIt2.hasNext()) | ||
| 638 | if(!soundIt1.next().m_str.equals(soundIt2.next().m_str)) return false; | ||
| 639 | |||
| 640 | ArrayList<AnswerTuple> thisGapAnswers = new ArrayList<>(gapAnswerTuples); | ||
| 641 | Collections.sort(thisGapAnswers, (AnswerTuple t1, AnswerTuple t2) -> t1.m_str.compareTo(t2.m_str)); | ||
| 642 | |||
| 643 | ArrayList<AnswerTuple> thatGapAnswers = new ArrayList<>(that.gapAnswerTuples); | ||
| 644 | Collections.sort(thatGapAnswers, (AnswerTuple t1, AnswerTuple t2) -> t1.m_str.compareTo(t2.m_str)); | ||
| 645 | |||
| 646 | Iterator<AnswerTuple> gapIt1 = this.gapAnswerTuples.iterator(); | ||
| 647 | Iterator<AnswerTuple> gapIt2 = that.gapAnswerTuples.iterator(); | ||
| 648 | while(gapIt1.hasNext() && gapIt2.hasNext()) | ||
| 649 | if(!gapIt1.next().m_str.equals(gapIt2.next().m_str)) return false; | ||
| 650 | |||
| 651 | return true; | ||
| 652 | } | ||
| 562 | } | 653 | } |
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 | ||
diff --git a/src/uk/ac/ox/cs/pagoda/util/Utility.java b/src/uk/ac/ox/cs/pagoda/util/Utility.java index 7138ca9..6d50ee0 100644 --- a/src/uk/ac/ox/cs/pagoda/util/Utility.java +++ b/src/uk/ac/ox/cs/pagoda/util/Utility.java | |||
| @@ -1,32 +1,13 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.util; | 1 | package uk.ac.ox.cs.pagoda.util; |
| 2 | 2 | ||
| 3 | import java.io.BufferedReader; | ||
| 4 | import java.io.BufferedWriter; | ||
| 5 | import java.io.File; | ||
| 6 | import java.io.FileInputStream; | ||
| 7 | import java.io.FileNotFoundException; | ||
| 8 | import java.io.FileOutputStream; | ||
| 9 | import java.io.IOException; | ||
| 10 | import java.io.InputStreamReader; | ||
| 11 | import java.io.OutputStreamWriter; | ||
| 12 | import java.io.PrintStream; | ||
| 13 | import java.nio.file.Files; | ||
| 14 | import java.nio.file.Paths; | ||
| 15 | import java.text.SimpleDateFormat; | ||
| 16 | import java.time.LocalDateTime; | ||
| 17 | import java.time.format.DateTimeFormatter; | ||
| 18 | import java.util.Collection; | ||
| 19 | import java.util.Date; | ||
| 20 | import java.util.HashSet; | ||
| 21 | import java.util.LinkedList; | ||
| 22 | import java.util.Scanner; | ||
| 23 | import java.util.Set; | ||
| 24 | import java.util.Stack; | ||
| 25 | |||
| 26 | import org.apache.log4j.Logger; | 3 | import org.apache.log4j.Logger; |
| 27 | import org.semanticweb.HermiT.model.Atom; | 4 | import org.semanticweb.HermiT.model.Atom; |
| 28 | 5 | ||
| 29 | import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | 6 | import java.io.*; |
| 7 | import java.text.SimpleDateFormat; | ||
| 8 | import java.time.LocalDateTime; | ||
| 9 | import java.time.format.DateTimeFormatter; | ||
| 10 | import java.util.*; | ||
| 30 | 11 | ||
| 31 | public class Utility { | 12 | public class Utility { |
| 32 | 13 | ||
| @@ -259,14 +240,4 @@ public class Utility { | |||
| 259 | return iri.replace(FILE_SEPARATOR, JAVA_FILE_SEPARATOR).replace(" ", "%20"); | 240 | return iri.replace(FILE_SEPARATOR, JAVA_FILE_SEPARATOR).replace(" ", "%20"); |
| 260 | } | 241 | } |
| 261 | 242 | ||
| 262 | public static String combinePaths(String path1, String path2) { | ||
| 263 | File file1 = new File(path1); | ||
| 264 | File file2 = new File(file1, path2); | ||
| 265 | return file2.getPath(); | ||
| 266 | } | ||
| 267 | |||
| 268 | public static void copyFile(String src, String dst) throws IOException { | ||
| 269 | Files.copy(Paths.get(src), Paths.get(dst), REPLACE_EXISTING); | ||
| 270 | } | ||
| 271 | |||
| 272 | } | 243 | } |
