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