aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
diff options
context:
space:
mode:
authorRncLsn <rnc.lsn@gmail.com>2015-06-03 15:21:30 +0100
committerRncLsn <rnc.lsn@gmail.com>2015-06-03 15:21:30 +0100
commitb3b822d187a6402a39d30e471fe90a5dfad64312 (patch)
tree912e1914610c8cf6103b7b7aef07087ddd7eb62f /src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
parent691964863246bbf6ef9f72cc5e82c83df34f135a (diff)
downloadACQuA-b3b822d187a6402a39d30e471fe90a5dfad64312.tar.gz
ACQuA-b3b822d187a6402a39d30e471fe90a5dfad64312.zip
Before reintroducing extended queries.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java330
1 files changed, 168 insertions, 162 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java b/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
index 5dcf0f8..35db0f2 100644
--- a/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
+++ b/src/uk/ac/ox/cs/pagoda/reasoner/full/HermitChecker.java
@@ -1,24 +1,10 @@
1package uk.ac.ox.cs.pagoda.reasoner.full; 1package uk.ac.ox.cs.pagoda.reasoner.full;
2 2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Set;
7
8import org.semanticweb.HermiT.Reasoner; 3import org.semanticweb.HermiT.Reasoner;
9import org.semanticweb.HermiT.model.DLClause; 4import org.semanticweb.HermiT.model.DLClause;
10import org.semanticweb.HermiT.model.Term; 5import org.semanticweb.HermiT.model.Term;
11import org.semanticweb.HermiT.model.Variable; 6import org.semanticweb.HermiT.model.Variable;
12import org.semanticweb.owlapi.model.IRI; 7import org.semanticweb.owlapi.model.*;
13import org.semanticweb.owlapi.model.OWLAxiom;
14import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
15import org.semanticweb.owlapi.model.OWLDataFactory;
16import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
17import org.semanticweb.owlapi.model.OWLIndividual;
18import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
19import org.semanticweb.owlapi.model.OWLOntology;
20import org.semanticweb.owlapi.model.OWLOntologyManager;
21
22import uk.ac.ox.cs.pagoda.endomorph.Clique; 8import uk.ac.ox.cs.pagoda.endomorph.Clique;
23import uk.ac.ox.cs.pagoda.endomorph.DependencyGraph; 9import uk.ac.ox.cs.pagoda.endomorph.DependencyGraph;
24import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; 10import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
@@ -30,216 +16,236 @@ import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper;
30import uk.ac.ox.cs.pagoda.util.Namespace; 16import uk.ac.ox.cs.pagoda.util.Namespace;
31import uk.ac.ox.cs.pagoda.util.Timer; 17import uk.ac.ox.cs.pagoda.util.Timer;
32import uk.ac.ox.cs.pagoda.util.Utility; 18import uk.ac.ox.cs.pagoda.util.Utility;
19import uk.ac.ox.cs.pagoda.util.disposable.DisposedException;
33 20
34public class HermitChecker implements Checker { 21import java.util.HashMap;
35 22import java.util.HashSet;
36 protected OWLDataFactory factory; 23import java.util.Map;
24import java.util.Set;
37 25
38 private String queryText; 26public class HermitChecker extends Checker {
39 private DLClause queryClause;
40 27
41 private Reasoner hermit; 28 protected OWLDataFactory factory;
42 protected String[][] answerVariable; 29 protected String[][] answerVariable;
43 protected OWLOntology ontology; 30 protected OWLOntology ontology;
44 protected QueryRecord record; 31 protected QueryRecord record;
45 protected QueryGraph qGraph = null; 32 protected QueryGraph qGraph = null;
46 boolean toCheck = true; 33 boolean toCheck = true;
47 34 AnswerTuple topAnswerTuple = null, botAnswerTuple = null;
35 private String queryText;
36 private DLClause queryClause;
37 private Reasoner hermit;
38 private int tag = 0;
39 private int counter = 0;
40 private DependencyGraph dGraph = null;
48 public HermitChecker(Checker checker) { 41 public HermitChecker(Checker checker) {
49 if (checker instanceof HermitChecker) { 42 if (checker instanceof HermitChecker) {
50 HermitChecker other = (HermitChecker) checker; 43 HermitChecker other = (HermitChecker) checker;
51 factory = other.factory; 44 factory = other.factory;
52 queryText = other.queryText; 45 queryText = other.queryText;
53 queryClause = other.queryClause; 46 queryClause = other.queryClause;
54 answerVariable = other.answerVariable; 47 answerVariable = other.answerVariable;
55 ontology = other.ontology; 48 ontology = other.ontology;
56// record = other.record; 49// record = other.record;
57 } 50 }
58 51
59 hermit = new Reasoner(ontology); 52 hermit = new Reasoner(ontology);
60 } 53 }
61 54
62 public HermitChecker(OWLOntology ontology, QueryRecord record, boolean toCheck) { 55 public HermitChecker(OWLOntology ontology, QueryRecord record, boolean toCheck) {
63 this.ontology = ontology; 56 this.ontology = ontology;
64 queryText = record.getQueryText(); 57 queryText = record.getQueryText();
65 answerVariable = record.getVariables(); 58 answerVariable = record.getVariables();
66 queryClause = record.getClause(); 59 queryClause = record.getClause();
67// this.record = record; 60// this.record = record;
68 this.toCheck = toCheck; 61 this.toCheck = toCheck;
69 } 62 }
70 63
71 public HermitChecker(OWLOntology ontology, String queryText) { 64 public HermitChecker(OWLOntology ontology, String queryText) {
72 this.ontology = ontology; 65 this.ontology = ontology;
73 this.queryText = queryText; 66 this.queryText = queryText;
74 answerVariable = queryText == null ? null : ConjunctiveQueryHelper.getAnswerVariables(queryText); 67 answerVariable = queryText == null ? null : ConjunctiveQueryHelper.getAnswerVariables(queryText);
75 queryClause = DLClauseHelper.getQuery(queryText, null); 68 queryClause = DLClauseHelper.getQuery(queryText, null);
76// this.record = null; 69// this.record = null;
70 }
71
72 @Override
73 public int check(AnswerTuples answers) {
74 if(isDisposed()) throw new DisposedException();
75
76 if(hermit == null) initialiseReasoner();
77 int answerCounter = 0, counter = 0;
78 for(; answers.isValid(); answers.moveNext()) {
79 ++counter;
80 if(check(answers.getTuple())) ++answerCounter;
81 }
82 answers.dispose();
83
84 Utility.logDebug("The number of individuals to be checked by HermiT: " + counter,
85 "The number of correct answers: " + answerCounter);
86 return answerCounter;
87 }
88
89 @Override
90 public boolean check(AnswerTuple answerTuple) {
91 if(isDisposed()) throw new DisposedException();
92
93 if(!toCheck) return false;
94
95 if(hermit == null) initialiseReasoner();
96 if(tag != 0) return tag == 1;
97 ++counter;
98 Timer t = new Timer();
99 Map<Variable, Term> sub = answerTuple.getAssignment(answerVariable[1]);
100 Set<OWLAxiom> toCheckAxioms = qGraph.getAssertions(sub);
101
102// for (OWLAxiom axiom: toCheckAxioms) System.out.println(axiom.toString());
103
104 if(hermit.isEntailed(toCheckAxioms)) {
105 Utility.logDebug("@TIME to check one tuple: " + t.duration());
106 return true;
107 }
108 Utility.logDebug("@TIME to check one tuple: " + t.duration());
109 return false;
77 } 110 }
78 111
79 private int tag = 0; 112 @Override
80 AnswerTuple topAnswerTuple = null, botAnswerTuple = null; 113 public boolean isConsistent() {
81 114 if(isDisposed()) throw new DisposedException();
115
116 if(hermit == null) initialiseReasoner();
117 return hermit.isConsistent();
118 }
119
120 public void dispose() {
121 super.dispose();
122
123 Utility.logInfo("Hermit was called " + counter + " times.");
124 if(hermit != null) hermit.dispose();
125 hermit = null;
126 }
127
128 public void setDependencyGraph(DependencyGraph dGraph) {
129 if(isDisposed()) throw new DisposedException();
130
131 this.dGraph = dGraph;
132 }
133
82 private void initialiseReasoner() { 134 private void initialiseReasoner() {
83 qGraph = new QueryGraph(queryClause.getBodyAtoms(), answerVariable[1], ontology); 135 qGraph = new QueryGraph(queryClause.getBodyAtoms(), answerVariable[1], ontology);
84 OWLOntologyManager manager = ontology.getOWLOntologyManager(); 136 OWLOntologyManager manager = ontology.getOWLOntologyManager();
85 factory = manager.getOWLDataFactory(); 137 factory = manager.getOWLDataFactory();
86 138
87 if (hermit != null) hermit.dispose(); 139 if(hermit != null) hermit.dispose();
88 140
89 if (dGraph != null && answerVariable[1].length == 1 && (dGraph.getExits().size() > 1 || dGraph.getEntrances().size() > 1)) { 141 if(dGraph != null && answerVariable[1].length == 1 && (dGraph.getExits().size() > 1 || dGraph.getEntrances()
90 Set<OWLAxiom> topAxioms = new HashSet<OWLAxiom>(); 142 .size() > 1)) {
91 Set<OWLAxiom> botAxioms = new HashSet<OWLAxiom>(); 143 Set<OWLAxiom> topAxioms = new HashSet<OWLAxiom>();
144 Set<OWLAxiom> botAxioms = new HashSet<OWLAxiom>();
92 addTopAndBotTuple(topAxioms, botAxioms); 145 addTopAndBotTuple(topAxioms, botAxioms);
93 manager.addAxioms(ontology, topAxioms); 146 manager.addAxioms(ontology, topAxioms);
94 manager.addAxioms(ontology, botAxioms); 147 manager.addAxioms(ontology, botAxioms);
95 hermit = new Reasoner(ontology); 148 hermit = new Reasoner(ontology);
96 boolean topValid = true; 149 boolean topValid = true;
97 if (!hermit.isConsistent() || topAnswerTuple != null && (topValid = check(topAnswerTuple))) { 150 if(!hermit.isConsistent() || topAnswerTuple != null && (topValid = check(topAnswerTuple))) {
98 hermit.dispose(); 151 hermit.dispose();
99 manager.removeAxioms(ontology, topAxioms); 152 manager.removeAxioms(ontology, topAxioms);
100 hermit = new Reasoner(ontology); 153 hermit = new Reasoner(ontology);
101 } else { 154 }
102 if (!topValid) tag = -1; 155 else {
103 else 156 if(!topValid) tag = -1;
104 if (botAnswerTuple != null && check(botAnswerTuple)) tag = 1; 157 else if(botAnswerTuple != null && check(botAnswerTuple)) tag = 1;
105 } 158 }
106 } 159 }
107 else 160 else
108 hermit = new Reasoner(ontology); 161 hermit = new Reasoner(ontology);
109 } 162 }
110 163
111 private void addTopAndBotTuple(Set<OWLAxiom> topAxioms, Set<OWLAxiom> botAxioms) { 164 private void addTopAndBotTuple(Set<OWLAxiom> topAxioms, Set<OWLAxiom> botAxioms) {
112 String top_str = Namespace.PAGODA_ANONY + "top", bot_str = Namespace.PAGODA_ANONY + "bot"; 165 String top_str = Namespace.PAGODA_ANONY + "top", bot_str = Namespace.PAGODA_ANONY + "bot";
113 topAnswerTuple = new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[] { uk.ac.ox.cs.JRDFox.model.Individual.create(top_str) } ); 166 topAnswerTuple =
114 botAnswerTuple = new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[] { uk.ac.ox.cs.JRDFox.model.Individual.create(bot_str) } ); 167 new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[]{uk.ac.ox.cs.JRDFox.model.Individual.create(top_str)});
115 OWLIndividual top_ind = factory.getOWLNamedIndividual(IRI.create(top_str)), bot_ind = factory.getOWLNamedIndividual(IRI.create(bot_str)); 168 botAnswerTuple =
116 Map<OWLAxiom, Integer> counter = new HashMap<OWLAxiom, Integer>(); 169 new AnswerTuple(new uk.ac.ox.cs.JRDFox.model.Individual[]{uk.ac.ox.cs.JRDFox.model.Individual.create(bot_str)});
117 170 OWLIndividual top_ind = factory.getOWLNamedIndividual(IRI.create(top_str)), bot_ind =
171 factory.getOWLNamedIndividual(IRI.create(bot_str));
172 Map<OWLAxiom, Integer> counter = new HashMap<OWLAxiom, Integer>();
173
118 Set<String> topAnswers = new HashSet<String>(), botAnswers = new HashSet<String>(); 174 Set<String> topAnswers = new HashSet<String>(), botAnswers = new HashSet<String>();
119 OWLIndividual sub, obj; 175 OWLIndividual sub, obj;
120 if (dGraph.getExits().size() > 1) { 176 if(dGraph.getExits().size() > 1) {
121 for (Clique answerClique: dGraph.getExits()) 177 for(Clique answerClique : dGraph.getExits())
122 topAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative().getAnswerTuple().getGroundTerm(0)).getIRI()); 178 topAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative()
179 .getAnswerTuple()
180 .getGroundTerm(0)).getIRI());
123 } 181 }
124 else topAnswerTuple = null; 182 else topAnswerTuple = null;
125 183
126 if (dGraph.getEntrances().size() > 1) { 184 if(dGraph.getEntrances().size() > 1) {
127 for (Clique answerClique: dGraph.getEntrances()) 185 for(Clique answerClique : dGraph.getEntrances())
128 botAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative().getAnswerTuple().getGroundTerm(0)).getIRI()); 186 botAnswers.add(((uk.ac.ox.cs.JRDFox.model.Individual) answerClique.getRepresentative()
187 .getAnswerTuple()
188 .getGroundTerm(0)).getIRI());
129 } 189 }
130 else botAnswerTuple = null; 190 else botAnswerTuple = null;
131 191
132 for (OWLAxiom axiom: ontology.getABoxAxioms(true)) 192 for(OWLAxiom axiom : ontology.getABoxAxioms(true))
133 if (axiom instanceof OWLClassAssertionAxiom) { 193 if(axiom instanceof OWLClassAssertionAxiom) {
134 OWLClassAssertionAxiom ca = (OWLClassAssertionAxiom) axiom; 194 OWLClassAssertionAxiom ca = (OWLClassAssertionAxiom) axiom;
135 sub = ca.getIndividual(); 195 sub = ca.getIndividual();
136 if (topAnswers.contains(sub.toStringID())) 196 if(topAnswers.contains(sub.toStringID()))
137 topAxioms.add(factory.getOWLClassAssertionAxiom(ca.getClassExpression(), top_ind)); 197 topAxioms.add(factory.getOWLClassAssertionAxiom(ca.getClassExpression(), top_ind));
138 if (botAnswers.contains(sub.toStringID())) 198 if(botAnswers.contains(sub.toStringID()))
139 inc(counter, factory.getOWLClassAssertionAxiom(ca.getClassExpression(), bot_ind)); 199 inc(counter, factory.getOWLClassAssertionAxiom(ca.getClassExpression(), bot_ind));
140 } 200 }
141 else if (axiom instanceof OWLObjectPropertyAssertionAxiom) { 201 else if(axiom instanceof OWLObjectPropertyAssertionAxiom) {
142 OWLObjectPropertyAssertionAxiom oa = (OWLObjectPropertyAssertionAxiom) axiom; 202 OWLObjectPropertyAssertionAxiom oa = (OWLObjectPropertyAssertionAxiom) axiom;
143 sub = oa.getSubject(); obj = oa.getObject(); 203 sub = oa.getSubject();
144 if (topAnswers.contains(sub.toStringID())) 204 obj = oa.getObject();
145 if (topAnswers.contains(obj.toStringID())) 205 if(topAnswers.contains(sub.toStringID()))
206 if(topAnswers.contains(obj.toStringID()))
146 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, top_ind)); 207 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, top_ind));
147 else 208 else
148 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, obj)); 209 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), top_ind, obj));
149 else { 210 else {
150 if (topAnswers.contains(obj.toStringID())) 211 if(topAnswers.contains(obj.toStringID()))
151 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, top_ind)); 212 topAxioms.add(factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, top_ind));
152 } 213 }
153 214
154 if (botAnswers.contains(sub.toStringID())) 215 if(botAnswers.contains(sub.toStringID()))
155 if (botAnswers.contains(obj.toStringID())) 216 if(botAnswers.contains(obj.toStringID()))
156 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, bot_ind)); 217 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, bot_ind));
157 else 218 else
158 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, obj)); 219 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), bot_ind, obj));
159 else { 220 else {
160 if (botAnswers.contains(obj.toStringID())) 221 if(botAnswers.contains(obj.toStringID()))
161 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, bot_ind)); 222 inc(counter, factory.getOWLObjectPropertyAssertionAxiom(oa.getProperty(), sub, bot_ind));
162 } 223 }
163 224
164 } 225 }
165 else if (axiom instanceof OWLDataPropertyAssertionAxiom) { 226 else if(axiom instanceof OWLDataPropertyAssertionAxiom) {
166 OWLDataPropertyAssertionAxiom da = (OWLDataPropertyAssertionAxiom) axiom; 227 OWLDataPropertyAssertionAxiom da = (OWLDataPropertyAssertionAxiom) axiom;
167 sub = da.getSubject(); 228 sub = da.getSubject();
168 if (topAnswers.contains(sub.toStringID())) 229 if(topAnswers.contains(sub.toStringID()))
169 topAxioms.add(factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), top_ind, da.getObject())); 230 topAxioms.add(factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), top_ind, da.getObject()));
170 231
171 if (botAnswers.contains(sub.toStringID())) 232 if(botAnswers.contains(sub.toStringID()))
172 inc(counter, factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), bot_ind, da.getObject())); 233 inc(counter, factory.getOWLDataPropertyAssertionAxiom(da.getProperty(), bot_ind, da.getObject()));
173 } 234 }
174 235
175 int number = botAnswers.size(); 236 int number = botAnswers.size();
176 for (Map.Entry<OWLAxiom, Integer> entry: counter.entrySet()) { 237 for(Map.Entry<OWLAxiom, Integer> entry : counter.entrySet()) {
177 if (entry.getValue() == number) 238 if(entry.getValue() == number)
178 botAxioms.add(entry.getKey()); 239 botAxioms.add(entry.getKey());
179 } 240 }
180 } 241 }
181 242
182 private void inc(Map<OWLAxiom, Integer> counter, OWLAxiom newAxiom) { 243 private void inc(Map<OWLAxiom, Integer> counter, OWLAxiom newAxiom) {
183 Integer number = counter.get(newAxiom); 244 if(isDisposed()) throw new DisposedException();
184 if (number == null) counter.put(newAxiom, 1);
185 else counter.put(newAxiom, number + 1);
186 }
187 245
188 @Override 246 Integer number = counter.get(newAxiom);
189 public int check(AnswerTuples answers) { 247 if(number == null) counter.put(newAxiom, 1);
190 if (hermit == null) initialiseReasoner(); 248 else counter.put(newAxiom, number + 1);
191 int answerCounter = 0, counter = 0;
192 for (; answers.isValid(); answers.moveNext()) {
193 ++counter;
194 if (check(answers.getTuple())) ++answerCounter;
195 }
196 answers.dispose();
197
198 Utility.logDebug("The number of individuals to be checked by HermiT: " + counter,
199 "The number of correct answers: " + answerCounter);
200 return answerCounter;
201 }
202
203 private int counter = 0;
204
205 @Override
206 public boolean check(AnswerTuple answerTuple) {
207 if (!toCheck) return false;
208
209 if (hermit == null) initialiseReasoner();
210 if (tag != 0) return tag == 1;
211 ++counter;
212 Timer t = new Timer();
213 Map<Variable, Term> sub = answerTuple.getAssignment(answerVariable[1]);
214 Set<OWLAxiom> toCheckAxioms = qGraph.getAssertions(sub);
215
216// for (OWLAxiom axiom: toCheckAxioms) System.out.println(axiom.toString());
217
218 if (hermit.isEntailed(toCheckAxioms)) {
219 Utility.logDebug("@TIME to check one tuple: " + t.duration());
220 return true;
221 }
222 Utility.logDebug("@TIME to check one tuple: " + t.duration());
223 return false;
224 }
225
226 @Override
227 public boolean isConsistent() {
228 if (hermit == null) initialiseReasoner();
229 return hermit.isConsistent();
230 }
231
232
233 public void dispose() {
234 Utility.logInfo("Hermit was called " + counter + " times.");
235 if (hermit != null) hermit.dispose();
236 hermit = null;
237 }
238
239 private DependencyGraph dGraph = null;
240
241 public void setDependencyGraph(DependencyGraph dGraph) {
242 this.dGraph = dGraph;
243 } 249 }
244 250
245} 251}