Debian Codegraph @

FaCT++.Java/src/test/java/conformancetests/JUnitRunner.java

raw symbols SCIP
1 package conformancetests;
2
3 /* This file is part of the JFact DL reasoner
4 Copyright 2011-2013 by Ignazio Palmisano, Dmitry Tsarkov, University of Manchester
5 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
6 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7 You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA*/
8 import static org.junit.Assert.*;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14
15 import org.semanticweb.owlapi.apibinding.OWLManager;
16 import org.semanticweb.owlapi.formats.FunctionalSyntaxDocumentFormat;
17 import org.semanticweb.owlapi.io.StringDocumentSource;
18 import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
19 import org.semanticweb.owlapi.model.OWLAxiom;
20 import org.semanticweb.owlapi.model.OWLException;
21 import org.semanticweb.owlapi.model.OWLOntology;
22 import org.semanticweb.owlapi.model.OWLOntologyChangeListener;
23 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
24 import org.semanticweb.owlapi.model.OWLOntologyStorageException;
25 import org.semanticweb.owlapi.profiles.OWL2DLProfile;
26 import org.semanticweb.owlapi.profiles.OWLProfileReport;
27 import org.semanticweb.owlapi.reasoner.OWLReasoner;
28 import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;
29
30 @SuppressWarnings({ "javadoc", "null" })
31 public class JUnitRunner {
32
33 private static final int _10000 = 1000000;
34
35 public static void print(String premise) throws OWLException {
36 OWLOntology o = OWLManager.createOWLOntologyManager()
37 .loadOntologyFromOntologyDocument(
38 new StringDocumentSource(premise));
39 o.getOWLOntologyManager().saveOntology(o,
40 new FunctionalSyntaxDocumentFormat(),
41 new SystemOutDocumentTarget());
42 }
43
44 private final TestClasses t;
45 private OWLReasonerFactory f;
46 private final String testId;
47 private final String premise;
48 private final String consequence;
49 private final String description;
50
51 public JUnitRunner(String premise, String consequence, String testId,
52 TestClasses t, String description) {
53 this.testId = testId;
54 this.premise = premise;
55 this.consequence = consequence;
56 this.description = description;
57 this.t = t;
58 }
59
60 public void setReasonerFactory(OWLReasonerFactory f) {
61 this.f = f;
62 }
63
64 private static boolean isConsistent(OWLReasoner reasoner) {
65 return reasoner.isConsistent();
66 }
67
68 private static boolean
69 isEntailed(OWLReasoner reasoner, OWLAxiom conclusion) {
70 return reasoner.isEntailed(conclusion);
71 }
72
73 public OWLOntology getPremise() throws OWLOntologyCreationException {
74 if (premise != null) {
75 StringDocumentSource documentSource = new StringDocumentSource(
76 premise);
77 return OWLManager.createOWLOntologyManager()
78 .loadOntologyFromOntologyDocument(documentSource);
79 }
80 return null;
81 }
82
83 public OWLOntology getConsequence() throws OWLOntologyCreationException {
84 if (consequence != null) {
85 StringDocumentSource documentSource = new StringDocumentSource(
86 consequence);
87 return OWLManager.createOWLOntologyManager()
88 .loadOntologyFromOntologyDocument(documentSource);
89 }
90 return null;
91 }
92
93 public void run() {
94 OWLOntology premiseOntology = null;
95 // OWLOntologyManager m = OWLManager.createOWLOntologyManager();
96 // m.setSilentMissingImportsHandling(true);
97 try {
98 if (premise != null) {
99 premiseOntology = getPremise();
100 OWL2DLProfile profile = new OWL2DLProfile();
101 OWLProfileReport report = profile
102 .checkOntology(premiseOntology);
103 if (report.getViolations().size() > 0) {
104 System.out.println("JUnitRunner.run() " + testId);
105 System.out
106 .println("JUnitRunner.run() premise violations:\n"
107 + report.toString());
108 throw new RuntimeException("errors!\n" + report.toString());
109 }
110 }
111 } catch (Exception e) {
112 e.printStackTrace(System.out);
113 // System.out.println("JUnitRunner.run() premise:\n" + premise);
114 throw new RuntimeException(e);
115 }
116 OWLOntology conclusionOntology = null;
117 try {
118 if (consequence != null) {
119 conclusionOntology = getConsequence();
120 OWL2DLProfile profile = new OWL2DLProfile();
121 OWLProfileReport report = profile
122 .checkOntology(conclusionOntology);
123 if (report.getViolations().size() > 0) {
124 System.out.println("JUnitRunner.run() " + testId
125 + report.getViolations().size());
126 System.out
127 .println("JUnitRunner.run() conclusion violations:\n"
128 + report.toString());
129 throw new RuntimeException("errors!");
130 }
131 }
132 } catch (Exception e) {
133 e.printStackTrace(System.out);
134 throw new RuntimeException(e);
135 }
136 this.run(premiseOntology, conclusionOntology);
137 }
138
139 protected void run(OWLOntology premiseOntology,
140 OWLOntology conclusionOntology) {
141 StringBuilder b = new StringBuilder();
142 b.append("JUnitRunner.logTroubles() premise");
143 b.append('\n');
144 for (OWLAxiom ax1 : premiseOntology.getAxioms()) {
145 b.append(ax1);
146 b.append('\n');
147 }
148 OWLReasoner reasoner = f.createReasoner(premiseOntology);
149 actual(conclusionOntology, b, reasoner);
150 // reasoner = roundtrip(reasoner);
151 // actual(conclusionOntology, b, reasoner);
152 premiseOntology.getOWLOntologyManager().removeOntologyChangeListener(
153 (OWLOntologyChangeListener) reasoner);
154 }
155
156 private static OWLReasoner roundtrip(OWLReasoner r) {
157 try {
158 ByteArrayOutputStream out = new ByteArrayOutputStream();
159 ObjectOutputStream stream = new ObjectOutputStream(out);
160 stream.writeObject(r);
161 stream.flush();
162 ByteArrayInputStream in = new ByteArrayInputStream(
163 out.toByteArray());
164 ObjectInputStream inStream = new ObjectInputStream(in);
165 return (OWLReasoner) inStream.readObject();
166 } catch (Exception e) {
167 e.printStackTrace();
168 return null;
169 }
170 }
171
172 private void actual(OWLOntology conclusionOntology, StringBuilder b,
173 OWLReasoner reasoner) {
174 switch (t) {
175 case CONSISTENCY: {
176 boolean consistent = isConsistent(reasoner);
177 if (!consistent) {
178 String message = b.toString()
179 + logTroubles(true, consistent, t, null);
180 assertTrue(message, consistent);
181 }
182 }
183 break;
184 case INCONSISTENCY: {
185 boolean consistent = isConsistent(reasoner);
186 if (consistent) {
187 String message = b.toString()
188 + logTroubles(false, consistent, t, null);
189 assertFalse(message, consistent);
190 }
191 }
192 break;
193 case NEGATIVE_IMPL: {
194 boolean consistent = isConsistent(reasoner);
195 if (!consistent) {
196 String message = b.toString()
197 + logTroubles(true, consistent, t, null);
198 assertTrue(message, consistent);
199 }
200 boolean entailed = false;
201 for (OWLAxiom ax : conclusionOntology.getLogicalAxioms()) {
202 boolean temp = isEntailed(reasoner, ax);
203 entailed |= temp;
204 if (temp) {
205 b.append(logTroubles(false, entailed, t, ax));
206 }
207 }
208 assertFalse(b.toString(), entailed);
209 }
210 break;
211 case POSITIVE_IMPL: {
212 boolean consistent = isConsistent(reasoner);
213 if (!consistent) {
214 String message = b.toString()
215 + logTroubles(true, consistent, t, null);
216 assertTrue(message, consistent);
217 }
218 boolean entailed = true;
219 for (OWLAxiom ax : conclusionOntology.getLogicalAxioms()) {
220 boolean temp = isEntailed(reasoner, ax);
221 entailed &= temp;
222 if (!temp) {
223 b.append(logTroubles(true, entailed, t, ax));
224 }
225 }
226 assertTrue(b.toString(), entailed);
227 }
228 break;
229 default:
230 break;
231 }
232 }
233
234 public String logTroubles(boolean expected, boolean actual,
235 TestClasses testclass, OWLAxiom axiom) {
236 StringBuilder b = new StringBuilder();
237 b.append("JUnitRunner.logTroubles() \t");
238 b.append(testclass);
239 b.append('\t');
240 b.append(testId);
241 b.append("\n ======================================\n");
242 b.append(description);
243 b.append("\nexpected: ");
244 b.append(expected);
245 b.append("\t actual: ");
246 b.append(actual);
247 if (axiom != null) {
248 b.append("\n for axiom: ");
249 b.append(axiom.toString());
250 }
251 String string = b.toString();
252 System.out.println(string);
253 return string;
254 }
255
256 public void printPremise() throws OWLOntologyStorageException,
257 OWLOntologyCreationException {
258 OWLOntology premise2 = getPremise();
259 premise2.getOWLOntologyManager().saveOntology(premise2,
260 new FunctionalSyntaxDocumentFormat(),
261 new SystemOutDocumentTarget());
262 }
263
264 public void printConsequence() throws OWLOntologyStorageException,
265 OWLOntologyCreationException {
266 OWLOntology consequence2 = getConsequence();
267 consequence2.getOWLOntologyManager().saveOntology(consequence2,
268 new FunctionalSyntaxDocumentFormat(),
269 new SystemOutDocumentTarget());
270 }
271 }