diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/summary')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/summary/HermitSummaryFilter.java | 226 |
1 files changed, 108 insertions, 118 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/summary/HermitSummaryFilter.java b/src/uk/ac/ox/cs/pagoda/summary/HermitSummaryFilter.java index a0ecce8..663c716 100644 --- a/src/uk/ac/ox/cs/pagoda/summary/HermitSummaryFilter.java +++ b/src/uk/ac/ox/cs/pagoda/summary/HermitSummaryFilter.java | |||
| @@ -1,16 +1,6 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.summary; | 1 | package uk.ac.ox.cs.pagoda.summary; |
| 2 | 2 | ||
| 3 | import java.util.HashSet; | 3 | import org.semanticweb.owlapi.model.*; |
| 4 | import java.util.Set; | ||
| 5 | |||
| 6 | import org.semanticweb.owlapi.model.IRI; | ||
| 7 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 8 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 9 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 10 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 11 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 12 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 13 | |||
| 14 | import uk.ac.ox.cs.JRDFox.model.Individual; | 4 | import uk.ac.ox.cs.JRDFox.model.Individual; |
| 15 | import uk.ac.ox.cs.pagoda.endomorph.Endomorph; | 5 | import uk.ac.ox.cs.pagoda.endomorph.Endomorph; |
| 16 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | 6 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; |
| @@ -25,166 +15,166 @@ import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoder; | |||
| 25 | import uk.ac.ox.cs.pagoda.util.Timer; | 15 | import uk.ac.ox.cs.pagoda.util.Timer; |
| 26 | import uk.ac.ox.cs.pagoda.util.Utility; | 16 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 27 | 17 | ||
| 18 | import java.util.HashSet; | ||
| 19 | import java.util.Set; | ||
| 20 | |||
| 28 | public class HermitSummaryFilter implements Checker { | 21 | public class HermitSummaryFilter implements Checker { |
| 29 | 22 | ||
| 30 | QueryRecord m_record; | 23 | public static final String QueryAnswerTermPrefix = TrackingRuleEncoder.QueryPredicate + "_term"; |
| 24 | QueryRecord m_record; | ||
| 31 | Summary summary = null; | 25 | Summary summary = null; |
| 32 | HermitChecker summarisedHermiT = null; | 26 | HermitChecker summarisedHermiT = null; |
| 33 | boolean summarisedConsistency; | 27 | boolean summarisedConsistency; |
| 34 | |||
| 35 | Endomorph endomorphismChecker = null; | 28 | Endomorph endomorphismChecker = null; |
| 36 | 29 | ||
| 37 | public HermitSummaryFilter(QueryRecord record, boolean toCallHermiT) { | 30 | public HermitSummaryFilter(QueryRecord record, boolean toCallHermiT) { |
| 38 | m_record = record; | 31 | m_record = record; |
| 39 | HermitChecker hermitChecker = new HermitChecker(record.getRelevantOntology(), record, toCallHermiT); | 32 | HermitChecker hermitChecker = new HermitChecker(record.getRelevantOntology(), record, toCallHermiT); |
| 40 | endomorphismChecker = new Endomorph(record, hermitChecker); | 33 | endomorphismChecker = new Endomorph(record, hermitChecker); |
| 41 | hermitChecker.setDependencyGraph(endomorphismChecker.getDependencyGraph()); | 34 | hermitChecker.setDependencyGraph(endomorphismChecker.getDependencyGraph()); |
| 35 | } | ||
| 36 | |||
| 37 | public static OWLOntology addOntologyWithQueryPreciate(OWLOntology ontology, QueryRecord record, AnswerTuples answers) { | ||
| 38 | OWLOntology newOntology = null; | ||
| 39 | OWLOntologyManager manager = ontology.getOWLOntologyManager(); | ||
| 40 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 41 | try { | ||
| 42 | newOntology = manager.createOntology(); | ||
| 43 | manager.addAxioms(newOntology, ontology.getAxioms()); | ||
| 44 | |||
| 45 | OWLClass[] queryClass = new OWLClass[answers.getArity()]; | ||
| 46 | int arity = answers.getArity(); | ||
| 47 | for(int i = 0; i < arity; ++i) | ||
| 48 | queryClass[i] = factory.getOWLClass(IRI.create(QueryAnswerTermPrefix + i)); | ||
| 49 | AnswerTuple answer; | ||
| 50 | for(; answers.isValid(); answers.moveNext()) { | ||
| 51 | answer = answers.getTuple(); | ||
| 52 | for(int i = 0; i < arity; ++i) | ||
| 53 | if(answer.getGroundTerm(i) instanceof Individual) { | ||
| 54 | String iri = ((Individual) answer.getGroundTerm(i)).getIRI(); | ||
| 55 | if(!record.isPredicate(answer, i)) { | ||
| 56 | manager.addAxiom(newOntology, | ||
| 57 | factory.getOWLClassAssertionAxiom( | ||
| 58 | queryClass[i], | ||
| 59 | factory.getOWLNamedIndividual(IRI.create(iri)))); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | answers.reset(); | ||
| 64 | } catch(OWLOntologyCreationException e) { | ||
| 65 | e.printStackTrace(); | ||
| 66 | } | ||
| 67 | |||
| 68 | return newOntology; | ||
| 69 | } | ||
| 70 | |||
| 71 | public static void printRelatedABoxAxioms(OWLOntology onto, String str) { | ||
| 72 | if(!str.startsWith("<")) str = OWLHelper.addAngles(str); | ||
| 73 | |||
| 74 | System.out.println("Axioms in " + onto.getOntologyID().getOntologyIRI() + " related to " + str); | ||
| 75 | |||
| 76 | for(OWLAxiom axiom : onto.getABoxAxioms(true)) | ||
| 77 | if(axiom.toString().contains(str)) | ||
| 78 | System.out.println(axiom); | ||
| 79 | |||
| 80 | System.out.println("-----------------------------"); | ||
| 81 | } | ||
| 82 | |||
| 83 | public static void printRelatedTBoxAxioms(OWLOntology onto, String str) { | ||
| 84 | |||
| 85 | System.out.println("Axioms in " + onto.getOntologyID().getOntologyIRI() + " related to " + str); | ||
| 86 | |||
| 87 | for(OWLAxiom axiom : onto.getTBoxAxioms(true)) | ||
| 88 | if(axiom.toString().contains(str)) | ||
| 89 | System.out.println(axiom); | ||
| 90 | |||
| 91 | for(OWLAxiom axiom : onto.getRBoxAxioms(true)) | ||
| 92 | if(axiom.toString().contains(str)) | ||
| 93 | System.out.println(axiom); | ||
| 94 | |||
| 95 | System.out.println("-----------------------------"); | ||
| 42 | } | 96 | } |
| 43 | 97 | ||
| 44 | @Override | 98 | @Override |
| 45 | public boolean isConsistent() { | 99 | public boolean isConsistent() { |
| 46 | if (summary == null) | 100 | if (summary == null) |
| 47 | summary = new Summary(endomorphismChecker.getOntology(), endomorphismChecker.getGraph()); | 101 | summary = new Summary(endomorphismChecker.getOntology(), endomorphismChecker.getGraph()); |
| 48 | 102 | ||
| 49 | if (summarisedHermiT == null) | 103 | if(summarisedHermiT == null) |
| 50 | initialiseSummarisedReasoner(); | 104 | initialiseSummarisedReasoner(); |
| 51 | 105 | ||
| 52 | if (summarisedConsistency) return true; | 106 | if(summarisedConsistency) return true; |
| 53 | return endomorphismChecker.isConsistent(); | 107 | return endomorphismChecker.isConsistent(); |
| 54 | } | 108 | } |
| 55 | 109 | ||
| 56 | private void initialiseSummarisedReasoner() { | 110 | private void initialiseSummarisedReasoner() { |
| 57 | Timer t = new Timer(); | 111 | Timer t = new Timer(); |
| 58 | summarisedHermiT = new HermitChecker(summary.getSummary(), summary.getSummary(m_record)); | 112 | summarisedHermiT = new HermitChecker(summary.getSummary(), summary.getSummary(m_record)); |
| 59 | // summary.save("summarised_query" + m_record.getQueryID() + ".owl"); | 113 | // summary.save("summarised_query" + m_record.getQueryID() + ".owl"); |
| 60 | if (summarisedConsistency = summarisedHermiT.isConsistent()) | 114 | if(summarisedConsistency = summarisedHermiT.isConsistent()) |
| 61 | Utility.logDebug("The summary of ABox is consistent with the TBox."); | 115 | Utility.logDebug("The summary of ABox is consistent with the TBox."); |
| 62 | else | 116 | else |
| 63 | Utility.logDebug("The summary of ABox is NOT consistent with the TBox."); | 117 | Utility.logDebug("The summary of ABox is NOT consistent with the TBox."); |
| 64 | m_record.addProcessingTime(Step.Summarisation, t.duration()); | 118 | m_record.addProcessingTime(Step.SUMMARISATION, t.duration()); |
| 65 | } | 119 | } |
| 66 | 120 | ||
| 67 | @Override | 121 | @Override |
| 68 | public int check(AnswerTuples answers) { | 122 | public int check(AnswerTuples answers) { |
| 69 | Timer t = new Timer(); | 123 | Timer t = new Timer(); |
| 70 | OWLOntology newOntology = addOntologyWithQueryPreciate(endomorphismChecker.getOntology(), m_record, answers); | 124 | OWLOntology newOntology = addOntologyWithQueryPreciate(endomorphismChecker.getOntology(), m_record, answers); |
| 71 | summary = new Summary(newOntology); | 125 | summary = new Summary(newOntology); |
| 72 | initialiseSummarisedReasoner(); | 126 | initialiseSummarisedReasoner(); |
| 73 | 127 | ||
| 74 | if (summarisedConsistency) { | 128 | if (summarisedConsistency) { |
| 75 | Set<AnswerTuple> passed = new HashSet<AnswerTuple>(), succ = new HashSet<AnswerTuple>(); | 129 | Set<AnswerTuple> passed = new HashSet<AnswerTuple>(), succ = new HashSet<AnswerTuple>(); |
| 76 | Set<AnswerTuple> falsified = new HashSet<AnswerTuple>(), fail = new HashSet<AnswerTuple>(); | 130 | Set<AnswerTuple> falsified = new HashSet<AnswerTuple>(), fail = new HashSet<AnswerTuple>(); |
| 77 | 131 | ||
| 78 | int counter = 0; | 132 | int counter = 0; |
| 79 | AnswerTuple representative; | 133 | AnswerTuple representative; |
| 80 | for (AnswerTuple answer; answers.isValid(); answers.moveNext()) { | 134 | for (AnswerTuple answer; answers.isValid(); answers.moveNext()) { |
| 81 | ++counter; | 135 | ++counter; |
| 82 | answer = answers.getTuple(); | 136 | answer = answers.getTuple(); |
| 83 | representative = summary.getSummary(answer); | 137 | representative = summary.getSummary(answer); |
| 84 | if (fail.contains(representative)) | 138 | if(fail.contains(representative)) |
| 85 | falsified.add(answer); | 139 | falsified.add(answer); |
| 86 | else if (succ.contains(representative)) | 140 | else if(succ.contains(representative)) |
| 87 | passed.add(answer); | 141 | passed.add(answer); |
| 88 | else | 142 | else if(summarisedHermiT.check(representative)) { |
| 89 | if (summarisedHermiT.check(representative)) { | ||
| 90 | succ.add(representative); | 143 | succ.add(representative); |
| 91 | passed.add(answer); | 144 | passed.add(answer); |
| 92 | } | 145 | } |
| 93 | else { | 146 | else { |
| 94 | fail.add(representative); | 147 | fail.add(representative); |
| 95 | falsified.add(answer); | 148 | falsified.add(answer); |
| 96 | } | 149 | } |
| 97 | } | 150 | } |
| 98 | answers.dispose(); | 151 | answers.dispose(); |
| 99 | 152 | ||
| 100 | Utility.logDebug("@TIME to filter out non-answers by summarisation: " + t.duration()); | 153 | Utility.logDebug("@TIME to filter out non-answers by summarisation: " + t.duration()); |
| 101 | 154 | ||
| 102 | m_record.removeUpperBoundAnswers(falsified); | 155 | m_record.removeUpperBoundAnswers(falsified); |
| 103 | 156 | ||
| 104 | if (m_record.processed()) { | 157 | if (m_record.processed()) { |
| 105 | m_record.setDifficulty(Step.Summarisation); | 158 | m_record.setDifficulty(Step.SUMMARISATION); |
| 106 | m_record.addProcessingTime(Step.Summarisation, t.duration()); | 159 | m_record.addProcessingTime(Step.SUMMARISATION, t.duration()); |
| 107 | return 0; | 160 | return 0; |
| 108 | } | 161 | } |
| 109 | 162 | ||
| 110 | Utility.logDebug("The number of answers to be checked with HermiT: " + passed.size() + "/" + counter); | 163 | Utility.logDebug("The number of answers to be checked with HermiT: " + passed.size() + "/" + counter); |
| 111 | 164 | ||
| 112 | m_record.setDifficulty(Step.FullReasoning); | 165 | m_record.setDifficulty(Step.FULL_REASONING); |
| 113 | m_record.addProcessingTime(Step.Summarisation, t.duration()); | 166 | m_record.addProcessingTime(Step.SUMMARISATION, t.duration()); |
| 114 | 167 | ||
| 115 | return endomorphismChecker.check(new AnswerTuplesImp(m_record.getAnswerVariables(), passed)); | 168 | return endomorphismChecker.check(new AnswerTuplesImp(m_record.getAnswerVariables(), passed)); |
| 116 | } | 169 | } |
| 117 | else { | 170 | else { |
| 118 | m_record.addProcessingTime(Step.Summarisation, t.duration()); | 171 | m_record.addProcessingTime(Step.SUMMARISATION, t.duration()); |
| 119 | // m_record.saveRelevantOntology("fragment.owl"); | 172 | // m_record.saveRelevantOntology("fragment.owl"); |
| 120 | m_record.setDifficulty(Step.FullReasoning); | 173 | m_record.setDifficulty(Step.FULL_REASONING); |
| 121 | return endomorphismChecker.check(answers); | 174 | return endomorphismChecker.check(answers); |
| 122 | } | 175 | } |
| 123 | } | 176 | } |
| 124 | 177 | ||
| 125 | public static final String QueryAnswerTermPrefix = TrackingRuleEncoder.QueryPredicate + "_term"; | ||
| 126 | |||
| 127 | public static OWLOntology addOntologyWithQueryPreciate(OWLOntology ontology, QueryRecord record, AnswerTuples answers) { | ||
| 128 | OWLOntology newOntology = null; | ||
| 129 | OWLOntologyManager manager = ontology.getOWLOntologyManager(); | ||
| 130 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 131 | try { | ||
| 132 | newOntology = manager.createOntology(); | ||
| 133 | manager.addAxioms(newOntology, ontology.getAxioms()); | ||
| 134 | |||
| 135 | OWLClass[] queryClass = new OWLClass[answers.getArity()]; | ||
| 136 | int arity = answers.getArity(); | ||
| 137 | for (int i = 0; i < arity; ++i) | ||
| 138 | queryClass[i] = factory.getOWLClass(IRI.create(QueryAnswerTermPrefix + i)); | ||
| 139 | AnswerTuple answer; | ||
| 140 | for (; answers.isValid(); answers.moveNext()) { | ||
| 141 | answer = answers.getTuple(); | ||
| 142 | for (int i = 0; i < arity; ++i) | ||
| 143 | if (answer.getGroundTerm(i) instanceof Individual) { | ||
| 144 | String iri = ((Individual) answer.getGroundTerm(i)).getIRI(); | ||
| 145 | if (!record.isPredicate(answer, i)) { | ||
| 146 | manager.addAxiom(newOntology, | ||
| 147 | factory.getOWLClassAssertionAxiom( | ||
| 148 | queryClass[i], | ||
| 149 | factory.getOWLNamedIndividual(IRI.create(iri)))); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | answers.reset(); | ||
| 154 | } catch (OWLOntologyCreationException e) { | ||
| 155 | e.printStackTrace(); | ||
| 156 | } | ||
| 157 | |||
| 158 | return newOntology; | ||
| 159 | } | ||
| 160 | |||
| 161 | public static void printRelatedABoxAxioms(OWLOntology onto, String str) { | ||
| 162 | if (!str.startsWith("<")) str = OWLHelper.addAngles(str); | ||
| 163 | |||
| 164 | System.out.println("Axioms in " + onto.getOntologyID().getOntologyIRI() + " related to " + str); | ||
| 165 | |||
| 166 | for (OWLAxiom axiom: onto.getABoxAxioms(true)) | ||
| 167 | if (axiom.toString().contains(str)) | ||
| 168 | System.out.println(axiom); | ||
| 169 | |||
| 170 | System.out.println("-----------------------------"); | ||
| 171 | } | ||
| 172 | |||
| 173 | public static void printRelatedTBoxAxioms(OWLOntology onto, String str) { | ||
| 174 | |||
| 175 | System.out.println("Axioms in " + onto.getOntologyID().getOntologyIRI() + " related to " + str); | ||
| 176 | |||
| 177 | for (OWLAxiom axiom: onto.getTBoxAxioms(true)) | ||
| 178 | if (axiom.toString().contains(str)) | ||
| 179 | System.out.println(axiom); | ||
| 180 | |||
| 181 | for (OWLAxiom axiom: onto.getRBoxAxioms(true)) | ||
| 182 | if (axiom.toString().contains(str)) | ||
| 183 | System.out.println(axiom); | ||
| 184 | |||
| 185 | System.out.println("-----------------------------"); | ||
| 186 | } | ||
| 187 | |||
| 188 | @Override | 178 | @Override |
| 189 | public boolean check(AnswerTuple answer) { | 179 | public boolean check(AnswerTuple answer) { |
| 190 | AnswerTuple representative = summary.getSummary(answer); | 180 | AnswerTuple representative = summary.getSummary(answer); |
