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