Ich schrieb ein Ontologie-Importer in Java, um eine RDF-formatierte .owl-Datei in eine JSON-formatierte Zeichenfolge zu analysieren. Genauer gesagt analysiert die statische Methode parseOntologyObjectHierarchy
die in der Ontologie definierte Klassenhierarchie in JSON. Alles funktioniert gut, wenn ich die Methode aus einem JUnit Test oder der main
Methode einer Klasse (JUnit und die Klasse main werden von IntelliJ IDEA Professional 2017 aufgerufen) aufrufen. Wenn ich jedoch meine Klassen als ein Jar unter Verwendung von Gradle (einschließlich aller Abhängigkeiten) paketiere, erhalte ich eine org.semanticweb.owlapi.io.UnparsableOntologyException
. Das Glas enthält tatsächlich die erforderlichen RDFXMLParser
. Ist der Klassenpfad im Jar nicht richtig eingestellt?OWLAPI: Parser nicht gefunden, wenn von Jar ausgeführt
ist hier ein minimales Beispiel IntelliJ IDEA-Projekt: https://drive.google.com/open?id=0B10MbhsMWfrydVNKZVJ0QVg1NlE
Und hier ist der entsprechende minimale jar: https://drive.google.com/open?id=0B10MbhsMWfrybjJIcDNWd0JFMUk
Hier ist der Code:
public static String parseOntologyObjectHierarchy(String filename) throws OWLException {
System.out.println("OWL file: " + filename);
OWLOntology ontology = loadOntology(filename);
OWLDataFactory df = OWLManager.getOWLDataFactory();
return json = hierarchyToString(ontology, df.getOWLThing());
}
public static OWLOntology loadOntology(String filename) throws OWLOntologyCreationException {
File ontologyFile = new File(filename);
if (!ontologyFile.exists() || !ontologyFile.isFile()) {
throw new IllegalArgumentException("OWL file is not a file");
}
OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
OWLOntologyDocumentSource source = new FileDocumentSource(new File(filename), new RDFXMLDocumentFormat());
return ontologyManager.loadOntologyFromOntologyDocument(source);
}
Hier ist meine build.gradle:
group 'com.example'
version '0.1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'net.sourceforge.owlapi', name: 'owlapi-osgidistribution', version: '5.1.0'
compile group: 'net.sourceforge.owlapi', name: 'owlapi-apibinding', version: '5.1.0'
compile group: 'net.sourceforge.owlapi', name: 'owlapi-parsers', version: '5.1.0'
compile group: 'net.sourceforge.owlapi', name: 'owlapi-impl', version: '5.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'net.sourceforge.owlapi:org.semanticweb.hermit:1.3.8.510'
compile group: 'org.glassfish', name: 'javax.json', version: '1.0.4'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'ExampleCom Ontology Importer',
'Implementation-Version': version,
'Main-Class': 'com.example.ontology.OntologyImporter'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Hier ist die exc eption Text:
$ java -jar am-ontology_importer-all-0.1.0-SNAPSHOT.jar
OWL file: C:/Users/me/Desktop/Projects/example/example-0.1.0.owl
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further detail
s.
Exception in thread "main" org.semanticweb.owlapi.io.UnparsableOntologyException
: Problem parsing file:/C:/Users/me/Desktop/Projects/example/example-0.1.0.owl
Could not parse ontology. Either a suitable parser could not be found, or parsi
ng failed. See parser logs below for explanation.
The following parsers were tried:
1) [email protected]
Detailed logs:
--------------------------------------------------------------------------------
Parser: [email protected]
Stack trace:
Lexical error at line 1, column 22. Encountered: "\n" (10), after : "" o
rg.coode.owlapi.obo12.parser.OBOParserTokenManager.getNextToken(OBOParserTokenMa
nager.java:1059)
org.coode.owlapi.obo12.parser.OBOParser.jj_ntk_f(OBOParser.java:296)
org.coode.owlapi.obo12.parser.OBOParser.TagValuePair(OBOParser.java:147)
org.coode.owlapi.obo12.parser.OBOParser.Header(OBOParser.java:110)
org.coode.owlapi.obo12.parser.OBOParser.parse(OBOParser.java:80)
org.coode.owlapi.obo12.parser.OWLOBO12Parser.parse(OWLOBO12Parser.java:1
09)
uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.loadOWLOntology(OW
LOntologyFactoryImpl.java:188)
uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntologyMa
nagerImpl.java:1072)
uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOn
tologyManagerImpl.java:1033)
uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOn
tologyDocument(OWLOntologyManagerImpl.java:982)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.loadOWLOntology
(OWLOntologyFactoryImpl.java:229)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.load(OWLOntolog
yManagerImpl.java:1072)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OW
LOntologyManagerImpl.java:1033)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFro
mOntologyDocument(OWLOntologyManagerImpl.java:982)
at com.example.ontology.OntologyImporter.loadOntology(OntologyImpo
rter.java:52)
at com.example.ontology.OntologyImporter.parseOntologyObjectHierar
chy(OntologyImporter.java:64)
at com.example.ontology.OntologyImporter.main(OntologyImporter.jav
a:142)
Wie werden Parser geladen/entdeckt? Es scheint, dass es einen Parser (OWLOBO12) gefunden hat, aber das ist nicht der Parser, den Sie wollten. Ist das korrekt? Könnte auch helfen zu sehen, wie Sie Unit-Test und Main ausführen. Das heißt, verwenden Sie eine IDE oder Befehlszeile. Dies kann beim Debuggen von Klassenpfadproblemen helfen. – cmonkey
@cmonkey, Ich habe die Beschreibung bearbeitet: Ich starte die Klassenhaupt- und JUnit-Tests von IntelliJ IDEA. Für das Jar wird die gleiche Ausnahme ausgelöst, unabhängig davon, ob ich es über die Befehlszeile oder über IntelliJ ausführe. Es ist mir egal, welcher Parser verwendet wird, ich brauche nur einen, der RDF-XML parsen kann. – balt
@cmonkey, ich habe auch Links zu einem minimalen IntelliJ IDEA-Projekt und dem Krug, den ich mit diesem Projekt von IDEA erstellt habe, hinzugefügt. – balt