创建推理器不适用于 Tomcat 部署

Create reasoner does not work on Tomcat deployment

上下文: 我正在开发一个 Java Spring 启动 Web 应用程序,该应用程序为各种任务操作 OWL 和 RDF 数据(ontology 加载、一致性检查、不一致解释生成和通过 SPARQL 查询)。我正在使用 OWLAPI、HermiT、OWLExplanation 和 ONTAPI 库(下面的依赖项)。当我 运行 使用嵌入在 Intellij 中的 tomcat 的应用程序时,一切正常。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-apibinding</artifactId>
        <version>5.1.20</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>org.semanticweb.hermit</artifactId>
        <version>1.3.8.510</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlexplanation</artifactId>
        <version>5.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.owlcs</groupId>
        <artifactId>ontapi</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>

问题: 当我在 Tomcat9 实例中部署 webapp 时,它在尝试从推理器工厂创建推理器时抛出异常(下面代码片段的最后一行) .可能,这是一个依赖性问题。但我不知道从哪里开始寻找,因为上面引用的所有库都与 webapp 一起部署。随后是代码片段、错误消息和堆栈跟踪。

代码片段:

import com.github.owlcs.ontapi.OntManagers;
import com.github.owlcs.ontapi.Ontology;
import com.github.owlcs.ontapi.OntologyManager;
import org.semanticweb.HermiT.Configuration;
import org.semanticweb.HermiT.ReasonerFactory;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.rdf.turtle.parser.TurtleOntologyParser;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;


Ontology ontology;
// load string source into ontology manager
StringDocumentSource docSource = new StringDocumentSource(source);
OntologyManager manager = OntManagers.createManager();
try {
    ontology = manager.loadOntologyFromOntologyDocument(docSource);
} catch (OWLOntologyCreationException e) {
    return null;
} catch (Throwable t) {
    throw new Throwable("OWL/XML syntax parsing problem.");
}

// start reasoner with configuration for explanations
Configuration configuration = new Configuration();
configuration.throwInconsistentOntologyException = false;
OWLReasonerFactory reasonerFactory = new ReasonerFactory();
OWLReasoner reasoner;
reasoner = reasonerFactory.createReasoner(ontology, configuration);

错误信息:

Unable to create injector, see the following errors:

1) No implementation for java.util.Set<org.semanticweb.owlapi.io.OWLParserFactory> was bound.
  while locating java.util.Set<org.semanticweb.owlapi.io.OWLParserFactory>
    for the 1st parameter of uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.setOntologyParsers(OWLOntologyManagerImpl.java:1456)
  at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.setOntologyParsers(OWLOntologyManagerImpl.java:1456)
  at uk.ac.manchester.cs.owl.owlapi.OWLAPIImplModule.configure(Unknown Source)

2) No implementation for java.util.Set<org.semanticweb.owlapi.model.OWLOntologyIRIMapper> was bound.
  while locating java.util.Set<org.semanticweb.owlapi.model.OWLOntologyIRIMapper>
    for the 1st parameter of uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.setIRIMappers(OWLOntologyManagerImpl.java:1407)
  at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.setIRIMappers(OWLOntologyManagerImpl.java:1407)
  at uk.ac.manchester.cs.owl.owlapi.OWLAPIImplModule.configure(Unknown Source)

3) No implementation for java.util.Set<org.semanticweb.owlapi.model.OWLStorerFactory> was bound.
  while locating java.util.Set<org.semanticweb.owlapi.model.OWLStorerFactory>
    for the 1st parameter of uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.setOntologyStorers(OWLOntologyManagerImpl.java:1394)
  at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.setOntologyStorers(OWLOntologyManagerImpl.java:1394)
  at uk.ac.manchester.cs.owl.owlapi.OWLAPIImplModule.configure(Unknown Source)

4) An exception was caught and reported. Message: Injection failed for interface org.semanticweb.owlapi.model.OWLOntologyFactory
  at org.semanticweb.owlapi.OWLAPIServiceLoaderModule.configure(Unknown Source)

堆栈跟踪:

com.google.inject.internal.MessageProcessor.visit An exception was caught and reported. Message: java.lang.NoSuchMethodException: uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.<init>()
        org.semanticweb.owlapi.model.OWLRuntimeException: Injection failed for interface org.semanticweb.owlapi.model.OWLOntologyFactory
                at org.semanticweb.owlapi.OWLAPIServiceLoaderModule.loadInstancesFromServiceLoader(OWLAPIServiceLoaderModule.java:62)
                at org.semanticweb.owlapi.OWLAPIServiceLoaderModule.configure(OWLAPIServiceLoaderModule.java:49)
                at com.google.inject.AbstractModule.configure(AbstractModule.java:62)
                at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340)
                at com.google.inject.spi.Elements.getElements(Elements.java:110)
                at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:138)
                at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:104)
                at com.google.inject.Guice.createInjector(Guice.java:99)
                at com.google.inject.Guice.createInjector(Guice.java:73)
                at com.google.inject.Guice.createInjector(Guice.java:62)
                at org.semanticweb.owlapi.apibinding.OWLManager.createInjector(OWLManager.java:104)
                at org.semanticweb.owlapi.apibinding.OWLManager.createOWLOntologyManager(OWLManager.java:44)
                at org.semanticweb.HermiT.structural.ObjectPropertyInclusionManager.buildIndividualAutomata(ObjectPropertyInclusionManager.java:721)
                at org.semanticweb.HermiT.structural.ObjectPropertyInclusionManager.createAutomata(ObjectPropertyInclusionManager.java:192)
                at org.semanticweb.HermiT.structural.ObjectPropertyInclusionManager.<init>(ObjectPropertyInclusionManager.java:52)
                at org.semanticweb.HermiT.structural.OWLClausification.preprocessAndClausify(OWLClausification.java:83)
                at org.semanticweb.HermiT.Reasoner.loadOntology(Reasoner.java:210)
                at org.semanticweb.HermiT.Reasoner.<init>(Reasoner.java:201)
                at org.semanticweb.HermiT.Reasoner.<init>(Reasoner.java:175)
                at org.semanticweb.HermiT.ReasonerFactory.createHermiTOWLReasoner(ReasonerFactory.java:51)
                at org.semanticweb.HermiT.ReasonerFactory.createReasoner(ReasonerFactory.java:19)
                at ufpe.dare.arcade.arcadewebontapi.OntologyController.loadOntology(OntologyController.java:130)
                at ufpe.dare.arcade.arcadewebontapi.ArcadeWebOntAPIApplication.uploadOntology(ArcadeWebOntAPIApplication.java:78)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.base/java.lang.reflect.Method.invoke(Method.java:566)
                at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
                at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
                at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
                at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
                at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
                at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
                at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
                at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
                at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
                at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
                at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:126)
                at org.springframework.boot.web.servlet.support.ErrorPageFilter.access[=14=]0(ErrorPageFilter.java:64)
                at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilterInternal(ErrorPageFilter.java:101)
                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
                at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:119)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
                at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
                at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
                at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:375)
                at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
                at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
                at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
                at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
                at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
                at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
                at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
                at java.base/java.lang.Thread.run(Thread.java:829)
        Caused by: java.util.ServiceConfigurationError: org.semanticweb.owlapi.model.OWLOntologyFactory: uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl Unable to get public no-arg constructor
                at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:582)
                at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:673)
                at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1233)
                at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1265)
                at java.base/java.util.ServiceLoader.hasNext(ServiceLoader.java:1300)
                at java.base/java.util.ServiceLoader.hasNext(ServiceLoader.java:1385)
                at java.base/java.lang.Iterable.forEach(Iterable.java:74)
                at org.semanticweb.owlapi.OWLAPIServiceLoaderModule.load(OWLAPIServiceLoaderModule.java:86)
                at org.semanticweb.owlapi.OWLAPIServiceLoaderModule.loadInstancesFromServiceLoader(OWLAPIServiceLoaderModule.java:60)
                ... 80 more
        Caused by: java.lang.NoSuchMethodException: uk.ac.manchester.cs.owl.owlapi.OWLOntologyFactoryImpl.<init>()
                at java.base/java.lang.Class.getConstructor0(Class.java:3349)
                at java.base/java.lang.Class.getConstructor(Class.java:2151)
                at java.base/java.util.ServiceLoader.run(ServiceLoader.java:660)
                at java.base/java.util.ServiceLoader.run(ServiceLoader.java:657)
                at java.base/java.security.AccessController.doPrivileged(Native Method)
                at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:668)
                ... 87 more

如有任何帮助,我将不胜感激。

错误堆栈跟踪指的是 Guice 问题,但 Guice 已从 OWLAPI 5.1.6 和更新版本中删除。

您的 pom 显示 OWLAPI 5.1.20,因此这意味着您部署的应用程序的 Tomcat 类路径中有多个 OWLAPI 版本。

根据您的 IDE,您可能能够看到引用了哪些版本的 OWLAPI 并将它们排除在外。另一种方法是检查您为 Tomcat 构建的 war 文件并查看其中出现了哪些 jars。我希望标记为 5.1.20 的 OWLAPI jar 将与具有相同名称和较早版本号的 OWLAPI jar 一起出现。

这可能在您的嵌入式 Tomcat 上按预期工作,只是因为类路径顺序 - 如果在不正确的文件之前找到正确的 OWLAPI 文件,一切都会正常工作。