|
1
|
|
|
/* |
|
2
|
|
|
* |
|
3
|
|
|
* Copyright (C) 2009-2017 Julian Mendez |
|
4
|
|
|
* |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of jcel. |
|
7
|
|
|
* |
|
8
|
|
|
* |
|
9
|
|
|
* The contents of this file are subject to the GNU Lesser General Public License |
|
10
|
|
|
* version 3 |
|
11
|
|
|
* |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
15
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
16
|
|
|
* (at your option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Lesser General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25
|
|
|
* |
|
26
|
|
|
* |
|
27
|
|
|
* Alternatively, the contents of this file may be used under the terms |
|
28
|
|
|
* of the Apache License, Version 2.0, in which case the |
|
29
|
|
|
* provisions of the Apache License, Version 2.0 are applicable instead of those |
|
30
|
|
|
* above. |
|
31
|
|
|
* |
|
32
|
|
|
* |
|
33
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
34
|
|
|
* you may not use this file except in compliance with the License. |
|
35
|
|
|
* You may obtain a copy of the License at |
|
36
|
|
|
* |
|
37
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
38
|
|
|
* |
|
39
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
40
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
41
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
42
|
|
|
* See the License for the specific language governing permissions and |
|
43
|
|
|
* limitations under the License. |
|
44
|
|
|
* |
|
45
|
|
|
*/ |
|
46
|
|
|
|
|
47
|
|
|
package de.tudresden.inf.lat.jcel.owlapi.console; |
|
48
|
|
|
|
|
49
|
|
|
import java.io.BufferedWriter; |
|
50
|
|
|
import java.io.File; |
|
51
|
|
|
import java.io.FileNotFoundException; |
|
52
|
|
|
import java.io.FileOutputStream; |
|
53
|
|
|
import java.io.FileWriter; |
|
54
|
|
|
import java.io.IOException; |
|
55
|
|
|
import java.io.PrintStream; |
|
56
|
|
|
import java.util.Arrays; |
|
57
|
|
|
import java.util.Date; |
|
58
|
|
|
import java.util.List; |
|
59
|
|
|
import java.util.Objects; |
|
60
|
|
|
import java.util.logging.Level; |
|
61
|
|
|
import java.util.logging.Logger; |
|
62
|
|
|
|
|
63
|
|
|
import org.semanticweb.owlapi.apibinding.OWLManager; |
|
64
|
|
|
import org.semanticweb.owlapi.functional.renderer.OWLFunctionalSyntaxRenderer; |
|
65
|
|
|
import org.semanticweb.owlapi.io.AbstractOWLRenderer; |
|
66
|
|
|
import org.semanticweb.owlapi.io.OWLRendererException; |
|
67
|
|
|
import org.semanticweb.owlapi.krss2.renderer.KRSS2OWLSyntaxRenderer; |
|
68
|
|
|
import org.semanticweb.owlapi.krss2.renderer.KRSS2SyntaxRenderer; |
|
69
|
|
|
import org.semanticweb.owlapi.krss2.renderer.KRSSSyntaxRenderer; |
|
70
|
|
|
import org.semanticweb.owlapi.latex.renderer.LatexRenderer; |
|
71
|
|
|
import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxRenderer; |
|
72
|
|
|
import org.semanticweb.owlapi.model.IRI; |
|
73
|
|
|
import org.semanticweb.owlapi.model.OWLDataFactory; |
|
74
|
|
|
import org.semanticweb.owlapi.model.OWLOntology; |
|
75
|
|
|
import org.semanticweb.owlapi.model.OWLOntologyCreationException; |
|
76
|
|
|
import org.semanticweb.owlapi.model.OWLOntologyManager; |
|
77
|
|
|
import org.semanticweb.owlapi.owlxml.renderer.OWLXMLRenderer; |
|
78
|
|
|
import org.semanticweb.owlapi.reasoner.InferenceType; |
|
79
|
|
|
|
|
80
|
|
|
import de.tudresden.inf.lat.jcel.owlapi.main.JcelReasoner; |
|
81
|
|
|
import de.tudresden.inf.lat.jcel.reasoner.main.VersionInfo; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* This class makes possible to start a classifier instance from the command |
|
85
|
|
|
* line. |
|
86
|
|
|
* |
|
87
|
|
|
* @author Julian Mendez |
|
88
|
|
|
*/ |
|
89
|
|
|
public class ConsoleStarter { |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Mode of execution. |
|
93
|
|
|
*/ |
|
94
|
|
|
public enum Mode { |
|
95
|
|
|
CLASSIFICATION, CONSISTENCY, ENTAILMENT, NOTHING, QUERY, SATISFIABILITY |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static final String cmdClassification = "classification"; |
|
99
|
|
|
public static final String cmdConsistency = "consistency"; |
|
100
|
|
|
public static final String cmdEntailment = "entailment"; |
|
101
|
|
|
public static final String cmdQuery = "query"; |
|
102
|
|
|
public static final String cmdSat = "sat"; |
|
103
|
|
|
public static final String cmdSatisfiability = "satisfiability "; |
|
104
|
|
|
|
|
105
|
|
|
private static final String errorSuffix = "_err"; |
|
106
|
|
|
|
|
107
|
|
|
public static final String licenseInfo = "" + "Copyright (C) 2009-2014 Julian Mendez" + "\nLicenses:" |
|
108
|
|
|
+ "\n GNU Lesser General Public License version 3 <http://www.gnu.org/licenses/lgpl.txt>" |
|
109
|
|
|
+ "\n Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0.txt>" |
|
110
|
|
|
+ "\nThis is free software: you are free to change and redistribute it." |
|
111
|
|
|
+ "\njcel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY." + "\n"; |
|
112
|
|
|
|
|
113
|
|
|
private static final Logger logger = Logger.getLogger("de.tudresden.inf.lat.jcel"); |
|
114
|
|
|
|
|
115
|
|
|
private static final String msgPartCompleted = "Completed "; |
|
116
|
|
|
private static final String msgPartOn = " on "; |
|
117
|
|
|
private static final String msgPartOperationTime = "Operation time: "; |
|
118
|
|
|
private static final String msgPartStarted = "Started "; |
|
119
|
|
|
|
|
120
|
|
|
public static final String optClassURI = "--classuri="; |
|
121
|
|
|
public static final String optConclusion = "--conclusion="; |
|
122
|
|
|
public static final String optHelp = "--help"; |
|
123
|
|
|
public static final String optLogLevel = "--loglevel="; |
|
124
|
|
|
public static final String optOntology = "--ontology="; |
|
125
|
|
|
public static final String optOperation = "--operation="; |
|
126
|
|
|
public static final String optOutput = "--output="; |
|
127
|
|
|
public static final String optRenderer = "--renderer="; |
|
128
|
|
|
public static final String optTimeOut = "--timeout="; |
|
129
|
|
|
public static final String optVerbose = "--verbose"; |
|
130
|
|
|
public static final String optVersion = "--version"; |
|
131
|
|
|
public static final String rendererFunctional = "functional"; |
|
132
|
|
|
public static final String rendererKRSS = "krss"; |
|
133
|
|
|
public static final String rendererKRSS2 = "krss2"; |
|
134
|
|
|
public static final String rendererKRSS2OWL = "krss2owl"; |
|
135
|
|
|
public static final String rendererLatex = "latex"; |
|
136
|
|
|
public static final String rendererManchester = "manchester"; |
|
137
|
|
|
public static final String rendererXML = "xml"; |
|
138
|
|
|
public static final String versionInfo = VersionInfo.reasonerName + " " + VersionInfo.reasonerVersion; |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Starts a classifier instance from the command line. |
|
142
|
|
|
* |
|
143
|
|
|
* @param args |
|
144
|
|
|
* a list containing the command line parameters, they are first |
|
145
|
|
|
* parameter: input file (required), second parameter: output |
|
146
|
|
|
* file (required), third parameter: log level (optional) |
|
147
|
|
|
* @throws IOException |
|
148
|
|
|
* if an I/O error occurs |
|
149
|
|
|
* @throws SecurityException |
|
150
|
|
|
* if a security error occurs |
|
151
|
|
|
* @throws OWLOntologyCreationException |
|
152
|
|
|
* if the ontology could not be created |
|
153
|
|
|
* @throws OWLRendererException |
|
154
|
|
|
* if a renderer error occurs |
|
155
|
|
|
*/ |
|
156
|
|
|
public static void main(String[] args) |
|
157
|
|
|
throws OWLRendererException, OWLOntologyCreationException, SecurityException, IOException { |
|
158
|
|
|
Objects.requireNonNull(args); |
|
159
|
|
|
(new ConsoleStarter()).start(args); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** A very small help about how to start a new instance. */ |
|
163
|
|
|
private final String minihelp = "\nusage: java -jar jcel.jar <operation> <ontologyFile> <output> [<classURI>] [options]..." |
|
164
|
|
|
+ "\n\n\n<operation>:" + "\n " + cmdConsistency |
|
165
|
|
|
+ " determine whether the given ontology is consistent" + "\n " + cmdSat |
|
166
|
|
|
+ " determine whether the given class (<classURI>) is satisfiable with respect to the given ontology" |
|
167
|
|
|
+ "\n " + cmdClassification |
|
168
|
|
|
+ " compute the class hierarchy and the object property hierarchy of the given ontology" |
|
169
|
|
|
+ "\n " + cmdEntailment |
|
170
|
|
|
+ " determine whether the given ontology entails the given conclusion" + "\n\n" |
|
171
|
|
|
+ "<ontologyFile> ontology to be classified (or premise ontology)" + "\n\n" |
|
172
|
|
|
+ "<output> output with the inferred data" + "\n\n" |
|
173
|
|
|
+ "<classURI> (only for " + cmdSat + ") URI of the class to check satisfiability" |
|
174
|
|
|
+ "\n\n\nthe available options are:" + "\n " + optConclusion + "FILE conclusion ontology" |
|
175
|
|
|
+ "\n " + optRenderer |
|
176
|
|
|
+ "RENDERER renderer for the class hierarchy computed by the classification operation" + "\n " |
|
177
|
|
|
+ optTimeOut + "MILLISECONDS force a time-out after a given number of milliseconds" + "\n " |
|
178
|
|
|
+ optLogLevel + "LEVEL log level" + "\n " + optVerbose + " run verbose mode" |
|
179
|
|
|
+ "\n " + optHelp + " display this help" + "\n " + optVersion |
|
180
|
|
|
+ " output version" + "\n\n" + "these options override the main arguments:" + "\n " |
|
181
|
|
|
+ optOperation + "OPERATION select an operation to execute" + "\n " + optOntology |
|
182
|
|
|
+ "FILE ontology to be classified (or premise ontology)" + "\n " + optOutput |
|
183
|
|
|
+ "FILE output with the inferred data" + "\n " + optClassURI + "CLASS_URI (only for " |
|
184
|
|
|
+ cmdSat + ") URI of the class to check satisfiability" + "\n\nthe types are:" |
|
185
|
|
|
+ "\n CLASS_URI a class URI, e.g.: https://www.w3.org/2002/07/owl#Thing" |
|
186
|
|
|
+ "\n FILE a file name, e.g.: /tmp/inputOntology.owl" |
|
187
|
|
|
+ "\n LEVEL a " + (Level.class).getName() + " number or string, e.g. " |
|
188
|
|
|
+ Level.INFO.intValue() + " or " + " \n " + Level.OFF.getName() + " | " |
|
189
|
|
|
+ Level.SEVERE.getName() + " | " + Level.WARNING.getName() + " | " + Level.INFO.getName() + " | " |
|
190
|
|
|
+ Level.CONFIG.getName() + " | " + Level.FINE.getName() + " | " + Level.FINER.getName() + " | " |
|
191
|
|
|
+ Level.FINEST.getName() + " | " + Level.ALL.getName() |
|
192
|
|
|
+ "\n MILLISECONDS a natural number, e.g.: 300000" + "\n OPERATION " |
|
193
|
|
|
+ cmdConsistency + " | " + cmdSat + " | " + cmdClassification + " | " + cmdEntailment |
|
194
|
|
|
+ "\n RENDERER " + rendererFunctional + " | " + rendererKRSS + " | " + rendererKRSS2 |
|
195
|
|
|
+ " | " + rendererKRSS2OWL + " | " + rendererLatex + " | " + rendererManchester + " | " + rendererXML |
|
196
|
|
|
+ "\n\n\n\n"; |
|
197
|
|
|
private long timeOut = 0; |
|
198
|
|
|
private boolean timeOutMode = false; |
|
199
|
|
|
private boolean verboseMode = false; |
|
200
|
|
|
|
|
201
|
|
|
private final PrintStream verboseModeOutput = System.out; |
|
202
|
|
|
|
|
203
|
|
|
public ConsoleStarter() { |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Checks the consistency of a given ontology. |
|
208
|
|
|
* |
|
209
|
|
|
* @param ontologyFile |
|
210
|
|
|
* ontology file to be checked |
|
211
|
|
|
* @throws OWLOntologyCreationException |
|
212
|
|
|
* if the ontology could not be created |
|
213
|
|
|
* @return <code>true</code> if and only if the given ontology is consistent |
|
214
|
|
|
*/ |
|
215
|
|
|
public boolean checkConsistency(File ontologyFile) throws OWLOntologyCreationException { |
|
216
|
|
|
Objects.requireNonNull(ontologyFile); |
|
217
|
|
|
JcelReasoner reasoner = createReasoner(ontologyFile); |
|
218
|
|
|
OWLDataFactory dataFactory = reasoner.getRootOntology().getOWLOntologyManager().getOWLDataFactory(); |
|
219
|
|
|
boolean ret = !reasoner.getEquivalentClasses(dataFactory.getOWLThing()).contains(dataFactory.getOWLNothing()); |
|
220
|
|
|
return ret; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Classifies a given ontology and checks whether another ontology is |
|
225
|
|
|
* entailed by the former. |
|
226
|
|
|
* |
|
227
|
|
|
* @param premiseFile |
|
228
|
|
|
* ontology file to be classified and used as premise |
|
229
|
|
|
* @param conclusionFile |
|
230
|
|
|
* file with the conclusion |
|
231
|
|
|
* @throws FileNotFoundException |
|
232
|
|
|
* if the file was not found |
|
233
|
|
|
* @throws OWLOntologyCreationException |
|
234
|
|
|
* if the ontology could not be created |
|
235
|
|
|
* @throws OWLRendererException |
|
236
|
|
|
* if a renderer error occurs |
|
237
|
|
|
* @return <code>true</code> if and only if the premise ontology entails the |
|
238
|
|
|
* conclusion ontology |
|
239
|
|
|
*/ |
|
240
|
|
|
public boolean checkEntailment(File premiseFile, File conclusionFile) |
|
241
|
|
|
throws OWLOntologyCreationException, OWLRendererException, FileNotFoundException { |
|
242
|
|
|
Objects.requireNonNull(premiseFile); |
|
243
|
|
|
Objects.requireNonNull(conclusionFile); |
|
244
|
|
|
logger.fine("starting jcel console ..."); |
|
245
|
|
|
|
|
246
|
|
|
OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); |
|
247
|
|
|
|
|
248
|
|
|
logger.fine("loading premise ontology using the OWL API ..."); |
|
249
|
|
|
OWLOntology premiseOntology = manager.loadOntologyFromOntologyDocument(premiseFile); |
|
250
|
|
|
|
|
251
|
|
|
logger.fine("loading conclusion ontology using the OWL API ..."); |
|
252
|
|
|
OWLOntology conclusionOntology = manager.loadOntologyFromOntologyDocument(conclusionFile); |
|
253
|
|
|
|
|
254
|
|
|
logger.fine("starting reasoner ..."); |
|
255
|
|
|
JcelReasoner reasoner = new JcelReasoner(premiseOntology, false); |
|
256
|
|
|
|
|
257
|
|
|
logger.fine("precomputing inferences ..."); |
|
258
|
|
|
reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); |
|
259
|
|
|
|
|
260
|
|
|
boolean ret = conclusionOntology.getAxioms().stream().allMatch(axiom -> reasoner.isEntailed(axiom)); |
|
261
|
|
|
|
|
262
|
|
|
logger.fine("jcel console finished."); |
|
263
|
|
|
return ret; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Checks satisfiability of a given concept with respect to an ontology. |
|
268
|
|
|
* |
|
269
|
|
|
* @param ontologyFile |
|
270
|
|
|
* ontology file to be checked |
|
271
|
|
|
* @param conceptIRI |
|
272
|
|
|
* concept IRI |
|
273
|
|
|
* @throws OWLOntologyCreationException |
|
274
|
|
|
* if the ontology could not be created |
|
275
|
|
|
* @return <code>true</code> if and only if the given concept is satisfiable |
|
276
|
|
|
* with respect to the given ontology |
|
277
|
|
|
*/ |
|
278
|
|
|
public boolean checkSatisfiability(File ontologyFile, IRI conceptIRI) throws OWLOntologyCreationException { |
|
279
|
|
|
Objects.requireNonNull(ontologyFile); |
|
280
|
|
|
Objects.requireNonNull(conceptIRI); |
|
281
|
|
|
JcelReasoner reasoner = createReasoner(ontologyFile); |
|
282
|
|
|
OWLDataFactory dataFactory = reasoner.getRootOntology().getOWLOntologyManager().getOWLDataFactory(); |
|
283
|
|
|
boolean ret = !reasoner.getEquivalentClasses(dataFactory.getOWLClass(conceptIRI)) |
|
284
|
|
|
.contains(dataFactory.getOWLNothing()); |
|
285
|
|
|
return ret; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Classifies a given ontology and computes the class hierarchy and the |
|
290
|
|
|
* object property hierarchy. |
|
291
|
|
|
* |
|
292
|
|
|
* @param ontologyFile |
|
293
|
|
|
* ontology file to be classified |
|
294
|
|
|
* @param inferredFile |
|
295
|
|
|
* file to write the inferred data |
|
296
|
|
|
* @param renderer |
|
297
|
|
|
* renderer |
|
298
|
|
|
* @throws FileNotFoundException |
|
299
|
|
|
* if the file was not found |
|
300
|
|
|
* @throws OWLOntologyCreationException |
|
301
|
|
|
* if the ontology could not be created |
|
302
|
|
|
* @throws OWLRendererException |
|
303
|
|
|
* if a renderer error occurs |
|
304
|
|
|
*/ |
|
305
|
|
|
public void computeClassification(File ontologyFile, File inferredFile, AbstractOWLRenderer renderer) |
|
306
|
|
|
throws OWLOntologyCreationException, OWLRendererException, FileNotFoundException { |
|
307
|
|
|
Objects.requireNonNull(ontologyFile); |
|
308
|
|
|
Objects.requireNonNull(inferredFile); |
|
309
|
|
|
JcelReasoner reasoner = createReasoner(ontologyFile); |
|
310
|
|
|
|
|
311
|
|
|
logger.fine("precomputing inferences ..."); |
|
312
|
|
|
reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); |
|
313
|
|
|
|
|
314
|
|
|
logger.fine("generating output ..."); |
|
315
|
|
|
OWLInferredOntologyWrapper inferredOntology = new OWLInferredOntologyWrapper(reasoner); |
|
316
|
|
|
|
|
317
|
|
|
renderer.render(inferredOntology.getOWLOntology(), new FileOutputStream(inferredFile)); |
|
318
|
|
|
|
|
319
|
|
|
logger.fine("jcel console finished."); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Creates an instance of jcel reasoner using the given ontology file. |
|
324
|
|
|
* |
|
325
|
|
|
* @param ontologyFile |
|
326
|
|
|
* ontology file |
|
327
|
|
|
* @return an instance of jcel reasoner |
|
328
|
|
|
* @throws OWLOntologyCreationException |
|
329
|
|
|
* if the ontology could not be created |
|
330
|
|
|
*/ |
|
331
|
|
|
public JcelReasoner createReasoner(File ontologyFile) throws OWLOntologyCreationException { |
|
332
|
|
|
Objects.requireNonNull(ontologyFile); |
|
333
|
|
|
logger.fine("starting jcel console ..."); |
|
334
|
|
|
|
|
335
|
|
|
OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); |
|
336
|
|
|
|
|
337
|
|
|
logger.fine("loading ontology using the OWL API ..."); |
|
338
|
|
|
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(ontologyFile); |
|
339
|
|
|
|
|
340
|
|
|
logger.fine("starting reasoner ..."); |
|
341
|
|
|
|
|
342
|
|
|
long wallClockTimeBeginning = (new Date()).getTime(); |
|
343
|
|
|
|
|
344
|
|
|
JcelReasoner ret = new JcelReasoner(ontology, false); |
|
345
|
|
|
|
|
346
|
|
|
long wallClockTimeMidPoint = (new Date()).getTime(); |
|
347
|
|
|
|
|
348
|
|
|
TimeOutMonitor monitor = new TimeOutMonitor(ret, |
|
349
|
|
|
this.timeOut - (wallClockTimeMidPoint - wallClockTimeBeginning)); |
|
350
|
|
|
|
|
351
|
|
|
if (this.timeOutMode) { |
|
352
|
|
|
monitor.start(); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
logger.fine("precomputing inferences ..."); |
|
356
|
|
|
|
|
357
|
|
|
ret.precomputeInferences(InferenceType.CLASS_HIERARCHY); |
|
358
|
|
|
|
|
359
|
|
|
long wallClockTimeEnd = (new Date()).getTime(); |
|
360
|
|
|
|
|
361
|
|
|
if (this.timeOutMode) { |
|
362
|
|
|
monitor.interrupt(); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
if (this.verboseMode) { |
|
366
|
|
|
this.verboseModeOutput.println(msgPartOperationTime + (wallClockTimeEnd - wallClockTimeBeginning)); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
return ret; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Returns the mode of execution that corresponds to the given identifier. |
|
374
|
|
|
* |
|
375
|
|
|
* @param argument |
|
376
|
|
|
* mode identifier |
|
377
|
|
|
* @return the mode of execution that corresponds to the given identifier |
|
378
|
|
|
*/ |
|
379
|
|
|
public Mode parseMode(String argument) { |
|
380
|
|
|
Objects.requireNonNull(argument); |
|
381
|
|
|
Mode mode = Mode.NOTHING; |
|
382
|
|
|
if (argument.equals(cmdClassification)) { |
|
383
|
|
|
mode = Mode.CLASSIFICATION; |
|
384
|
|
|
} else if (argument.equals(cmdConsistency)) { |
|
385
|
|
|
mode = Mode.CONSISTENCY; |
|
386
|
|
|
} else if (argument.equals(cmdSatisfiability) || argument.equals(cmdSat)) { |
|
387
|
|
|
mode = Mode.SATISFIABILITY; |
|
388
|
|
|
} else if (argument.equals(cmdQuery)) { |
|
389
|
|
|
mode = Mode.QUERY; |
|
390
|
|
|
} else if (argument.equals(cmdEntailment)) { |
|
391
|
|
|
mode = Mode.ENTAILMENT; |
|
392
|
|
|
} else { |
|
393
|
|
|
throw new IllegalArgumentException("Unrecognized mode: '" + argument + "'"); |
|
394
|
|
|
} |
|
395
|
|
|
return mode; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Creates a new abstract OWL renderer. |
|
400
|
|
|
* |
|
401
|
|
|
* @param argument |
|
402
|
|
|
* renderer identifier |
|
403
|
|
|
* @return a new abstract OWL renderer |
|
404
|
|
|
*/ |
|
405
|
|
|
public AbstractOWLRenderer parseRenderer(String argument) { |
|
406
|
|
|
Objects.requireNonNull(argument); |
|
407
|
|
|
AbstractOWLRenderer ret = null; |
|
408
|
|
|
if (argument.equals(rendererKRSS2OWL)) { |
|
409
|
|
|
ret = new KRSS2OWLSyntaxRenderer(); |
|
410
|
|
|
} else if (argument.equals(rendererKRSS2)) { |
|
411
|
|
|
ret = new KRSS2SyntaxRenderer(); |
|
412
|
|
|
} else if (argument.equals(rendererKRSS)) { |
|
413
|
|
|
ret = new KRSSSyntaxRenderer(); |
|
414
|
|
|
} else if (argument.equals(rendererLatex)) { |
|
415
|
|
|
ret = new LatexRenderer(); |
|
416
|
|
|
} else if (argument.equals(rendererManchester)) { |
|
417
|
|
|
ret = new ManchesterOWLSyntaxRenderer(); |
|
418
|
|
|
} else if (argument.equals(rendererFunctional)) { |
|
419
|
|
|
ret = new OWLFunctionalSyntaxRenderer(); |
|
420
|
|
|
} else if (argument.equals(rendererXML)) { |
|
421
|
|
|
ret = new OWLXMLRenderer(); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
return ret; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
/** |
|
428
|
|
|
* Returns a string that identifies the mode of execution (e.g. |
|
429
|
|
|
* classification, consistency, ...). |
|
430
|
|
|
* |
|
431
|
|
|
* @param mode |
|
432
|
|
|
* mode |
|
433
|
|
|
* @return a string that identifies the mode of execution |
|
434
|
|
|
*/ |
|
435
|
|
|
public String renderMode(Mode mode) { |
|
436
|
|
|
Objects.requireNonNull(mode); |
|
437
|
|
|
String ret = ""; |
|
438
|
|
|
if (mode.equals(Mode.CLASSIFICATION)) { |
|
439
|
|
|
ret = cmdClassification; |
|
440
|
|
|
} else if (mode.equals(Mode.CONSISTENCY)) { |
|
441
|
|
|
ret = cmdConsistency; |
|
442
|
|
|
} else if (mode.equals(Mode.SATISFIABILITY)) { |
|
443
|
|
|
ret = cmdSat; |
|
444
|
|
|
} else if (mode.equals(Mode.QUERY)) { |
|
445
|
|
|
ret = cmdQuery; |
|
446
|
|
|
} else if (mode.equals(Mode.ENTAILMENT)) { |
|
447
|
|
|
ret = cmdEntailment; |
|
448
|
|
|
} else if (mode.equals(Mode.NOTHING)) { |
|
449
|
|
|
ret = ""; |
|
450
|
|
|
} else { |
|
451
|
|
|
throw new IllegalStateException("Unrecognized mode: '" + mode + "'"); |
|
452
|
|
|
} |
|
453
|
|
|
return ret; |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
/** |
|
457
|
|
|
* Starts the application. |
|
458
|
|
|
* |
|
459
|
|
|
* @param args |
|
460
|
|
|
* console parameter |
|
461
|
|
|
* @throws OWLRendererException |
|
462
|
|
|
* if a renderer error occurs |
|
463
|
|
|
* @throws OWLOntologyCreationException |
|
464
|
|
|
* if the ontology could not be created |
|
465
|
|
|
* @throws SecurityException |
|
466
|
|
|
* if a security error occurs |
|
467
|
|
|
* @throws IOException |
|
468
|
|
|
* if an I/O error occurs |
|
469
|
|
|
*/ |
|
470
|
|
|
public void start(String[] args) |
|
471
|
|
|
throws OWLRendererException, OWLOntologyCreationException, SecurityException, IOException { |
|
472
|
|
|
|
|
473
|
|
|
if (args.length == 0) { |
|
474
|
|
|
System.out.println(this.minihelp); |
|
475
|
|
|
} else { |
|
476
|
|
|
List<String> arguments = Arrays.asList(args); |
|
477
|
|
|
if (arguments.contains(optHelp)) { |
|
478
|
|
|
System.out.println(this.minihelp); |
|
479
|
|
|
} else if (arguments.contains(optVersion)) { |
|
480
|
|
|
System.out.println(versionInfo); |
|
481
|
|
|
System.out.println(licenseInfo); |
|
482
|
|
|
} else if (arguments.size() >= 3) { |
|
483
|
|
|
|
|
484
|
|
|
Mode operation = null; |
|
485
|
|
|
File ontologyFile = null; |
|
486
|
|
|
File outputFile = null; |
|
487
|
|
|
File conclusionFile = null; |
|
488
|
|
|
Level logLevel = Level.OFF; |
|
489
|
|
|
IRI classIRI = null; |
|
490
|
|
|
AbstractOWLRenderer renderer = null; |
|
491
|
|
|
|
|
492
|
|
|
for (String argument : arguments) { |
|
493
|
|
|
|
|
494
|
|
|
if (argument.startsWith(optOperation)) { |
|
495
|
|
|
operation = parseMode(argument.substring(optOperation.length())); |
|
496
|
|
|
|
|
497
|
|
|
} else if (argument.startsWith(optOntology)) { |
|
498
|
|
|
ontologyFile = new File(argument.substring(optOntology.length())); |
|
499
|
|
|
|
|
500
|
|
|
} else if (argument.startsWith(optOutput)) { |
|
501
|
|
|
outputFile = new File(argument.substring(optOutput.length())); |
|
502
|
|
|
|
|
503
|
|
|
} else if (argument.startsWith(optClassURI)) { |
|
504
|
|
|
classIRI = IRI.create(argument.substring(optClassURI.length())); |
|
505
|
|
|
|
|
506
|
|
|
} else if (argument.startsWith(optConclusion)) { |
|
507
|
|
|
conclusionFile = new File(argument.substring(optConclusion.length())); |
|
508
|
|
|
|
|
509
|
|
|
} else if (argument.startsWith(optRenderer)) { |
|
510
|
|
|
renderer = parseRenderer(argument.substring(optRenderer.length())); |
|
511
|
|
|
|
|
512
|
|
|
} else if (argument.startsWith(optLogLevel)) { |
|
513
|
|
|
logLevel = Level.parse(argument.substring(optLogLevel.length())); |
|
514
|
|
|
|
|
515
|
|
|
} else if (argument.startsWith(optTimeOut)) { |
|
516
|
|
|
this.timeOutMode = true; |
|
517
|
|
|
this.timeOut = Long.parseLong(argument.substring(optTimeOut.length())); |
|
518
|
|
|
|
|
519
|
|
|
} else if (argument.startsWith(optVerbose)) { |
|
520
|
|
|
this.verboseMode = true; |
|
521
|
|
|
|
|
522
|
|
|
} |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
if (Objects.isNull(operation)) { |
|
526
|
|
|
operation = parseMode(arguments.get(0)); |
|
527
|
|
|
} |
|
528
|
|
|
if (Objects.isNull(ontologyFile)) { |
|
529
|
|
|
ontologyFile = new File(arguments.get(1)); |
|
530
|
|
|
} |
|
531
|
|
|
if (Objects.isNull(outputFile)) { |
|
532
|
|
|
outputFile = new File(arguments.get(2)); |
|
533
|
|
|
} |
|
534
|
|
|
if ((Objects.isNull(classIRI)) && operation.equals(Mode.SATISFIABILITY)) { |
|
535
|
|
|
classIRI = IRI.create(arguments.get(3)); |
|
536
|
|
|
} |
|
537
|
|
|
if (Objects.isNull(renderer)) { |
|
538
|
|
|
renderer = new OWLFunctionalSyntaxRenderer(); |
|
539
|
|
|
} |
|
540
|
|
|
|
|
541
|
|
|
if (Objects.nonNull(outputFile.getParentFile())) { |
|
542
|
|
|
outputFile.getParentFile().mkdirs(); |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
logger.setLevel(logLevel); |
|
546
|
|
|
logger.addHandler(new OutputStreamHandler(System.out)); |
|
547
|
|
|
|
|
548
|
|
|
try { |
|
549
|
|
|
|
|
550
|
|
|
if (this.verboseMode) { |
|
551
|
|
|
this.verboseModeOutput.println( |
|
552
|
|
|
msgPartStarted + renderMode(operation) + msgPartOn + ontologyFile.getAbsolutePath()); |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
if (operation == Mode.CLASSIFICATION) { |
|
556
|
|
|
|
|
557
|
|
|
computeClassification(ontologyFile, outputFile, renderer); |
|
558
|
|
|
|
|
559
|
|
|
} else if (operation == Mode.SATISFIABILITY) { |
|
560
|
|
|
|
|
561
|
|
|
storeInFile("" + classIRI.toString() + ", " + checkSatisfiability(ontologyFile, classIRI), |
|
562
|
|
|
outputFile); |
|
563
|
|
|
|
|
564
|
|
|
} else if (operation == Mode.CONSISTENCY) { |
|
565
|
|
|
|
|
566
|
|
|
storeInFile("" + checkConsistency(ontologyFile), outputFile); |
|
567
|
|
|
|
|
568
|
|
|
} else if (operation == Mode.QUERY) { |
|
569
|
|
|
throw new UnsupportedOperationException("Operation is not supported yet."); |
|
570
|
|
|
|
|
571
|
|
|
} else if (operation == Mode.ENTAILMENT) { |
|
572
|
|
|
if (Objects.isNull(conclusionFile)) { |
|
573
|
|
|
throw new IllegalArgumentException("No conclusion file has been defined."); |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
storeInFile("" + checkEntailment(ontologyFile, conclusionFile), outputFile); |
|
577
|
|
|
|
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
if (this.verboseMode) { |
|
581
|
|
|
this.verboseModeOutput.println( |
|
582
|
|
|
msgPartCompleted + renderMode(operation) + msgPartOn + ontologyFile.getAbsolutePath()); |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
} catch (RuntimeException e) { |
|
586
|
|
|
|
|
587
|
|
|
// TODO change the way the exception is displayed |
|
588
|
|
|
BufferedWriter output = new BufferedWriter( |
|
589
|
|
|
new FileWriter(outputFile.getAbsoluteFile() + errorSuffix, true)); |
|
590
|
|
|
output.write(e.toString()); |
|
591
|
|
|
output.newLine(); |
|
592
|
|
|
output.flush(); |
|
593
|
|
|
output.close(); |
|
594
|
|
|
|
|
595
|
|
|
} |
|
596
|
|
|
|
|
597
|
|
|
} else { |
|
598
|
|
|
System.out.println(this.minihelp); |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
} |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
private void storeInFile(String output, File outputFile) throws IOException { |
|
605
|
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile, true)); |
|
606
|
|
|
writer.write(output); |
|
607
|
|
|
writer.newLine(); |
|
608
|
|
|
writer.flush(); |
|
609
|
|
|
writer.close(); |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
} |
|
613
|
|
|
|