Skip to content
Advertisement

Fix OWLOntologyStorageException

I’m developing a Java application using owl-api. For testing I want to use a TestDataProvider to provide test data for unit-testing.

I’m using version 5.0.0 because the tutorial I read is using this version too. I plan to update later.

<dependency>
            <groupId>net.sourceforge.owlapi</groupId>
            <artifactId>owlapi-distribution</artifactId>
            <version>5.0.0</version>
</dependency>

But I run in following exception

org.semanticweb.owlapi.model.OWLOntologyStorageException: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
    at org.semanticweb.owlapi.util.AbstractOWLStorer.storeOntology(AbstractOWLStorer.java:49)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1160)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1134)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.saveOntology(OWLOntologyManagerImpl.java:1122)
    at de.abk.wiwiservice.OntologyTestDataProvider.addOWLIndividualToOWLOntology(OntologyTestDataProvider.java:58)
    at de.abk.wiwiservice.OntologyTestDataProvider.<init>(OntologyTestDataProvider.java:32)
    at de.abk.wiwiservice.owlindividual.OWLIndividualRepositoryImplTest.setup(OWLIndividualRepositoryImplTest.java:40)
    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.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptLifecycleMethod(TimeoutExtension.java:126)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptBeforeAllMethod(TimeoutExtension.java:68)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllMethods$11(ClassBasedTestDescriptor.java:397)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllMethods(ClassBasedTestDescriptor.java:395)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:209)
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:80)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:148)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1349)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1342)
    at org.semanticweb.owlapi.util.AbstractOWLStorer.prepareActualOutput(AbstractOWLStorer.java:64)
    at org.semanticweb.owlapi.util.AbstractOWLStorer.storeOntology(AbstractOWLStorer.java:46)
    ... 65 more

at the last line of addOWLIndividualToOWLOntology()

public class OntologyTestDataProvider {

    private OWLOntology owlOntology;

    private IRI baseIRI;

    private List<OWLClass> owlClasses;

    private List<OWLIndividual> owlIndividuals;

    private List<OWLDeclarationAxiom> owlDeclarationAxioms;

    public OntologyTestDataProvider() throws OWLOntologyCreationException, OWLOntologyStorageException {
        owlClasses = new ArrayList<>();
        owlDeclarationAxioms = new ArrayList<>();
        owlIndividuals = new ArrayList<>();
        baseIRI = IRI.create("http://www.semanticweb.org/andreas.b/ontologies/2022/1/test-ontology");
        OWLOntologyManager owlOntologyManager = OWLManager.createOWLOntologyManager();
        owlOntology = owlOntologyManager.createOntology(baseIRI);
        addOWLClassesToOWLOntology();
        addOWLIndividualToOWLOntology();
    }

    private void addOWLClassesToOWLOntology() {
        final OWLClass person = owlOntology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(baseIRI + "#Person");
        final OWLClass role = owlOntology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(baseIRI + "#Role");

        owlClasses.add(person);
        owlClasses.add(role);

        OWLDeclarationAxiom owlDeclarationAxiomPerson = owlOntology.getOWLOntologyManager().getOWLDataFactory().getOWLDeclarationAxiom(person);
        OWLDeclarationAxiom owlDeclarationAxiomRolle = owlOntology.getOWLOntologyManager().getOWLDataFactory().getOWLDeclarationAxiom(role);

        owlDeclarationAxioms.add(owlDeclarationAxiomPerson);
        owlDeclarationAxioms.add(owlDeclarationAxiomRolle);

        owlOntology.add(owlDeclarationAxiomPerson);
        owlOntology.add(owlDeclarationAxiomRolle);
    }

    private void addOWLIndividualToOWLOntology() throws OWLOntologyStorageException {
        final OWLIndividual andy = owlOntology.getOWLOntologyManager().getOWLDataFactory().getOWLNamedIndividual( baseIRI + "#Andy");
        owlIndividuals.add(andy);
        OWLClassAssertionAxiom andyClassAssertion = owlOntology.getOWLOntologyManager().getOWLDataFactory().getOWLClassAssertionAxiom(owlClasses.get(0), andy);
        owlOntology.getOWLOntologyManager().addAxiom(owlOntology, andyClassAssertion);
        owlOntology.add(andyClassAssertion);
        owlOntology.getOWLOntologyManager().saveOntology(owlOntology);
    }

}

I already read that I have to retrieve an new OWLOntologyManager from the ontology everytime and changed my code accordingly but that didn’t do the trick. I hope you can give me a hint.

Advertisement

Answer

By default, an ontology is saved in the same place it was loaded from – in this case it was loaded from a remote IRI, and there’s currently no support for saving an ontology directly to a remote URL.

You can work around this by specifying a target for your saveOntology() call, e.g.,

StringDocumentTarget target = new StringDocumentTarget();
ontology.saveOntology(target); // calling the method on the ontology object redirects to the method on its manager, so you don't have to worry about having picked the right manager.

a StringDocumentTarget writes the data to memory and you can check its contents, e.g., for testing purposes (same thing happens in the OWLAPI unit tests, for example).

Note that the methods shown in the tutorial are unlikely to have changed in the whole 5.x.x series, so you can probably move to the latest OWLAPI version right away without this affecting what you’re doing.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement