From f1ecf02088f51987f055bdbabe5161ad5ac58839 Mon Sep 17 00:00:00 2001 From: sszuev Date: Wed, 7 Aug 2024 21:40:34 +0300 Subject: [PATCH] ont-api: OntGraphDocumentSource - use RDF/XML instead of turtle, as a workaround to https://github.com/owlcs/owlapi/issues/1149; + minor changes --- .../owlcs/ontapi/OntGraphDocumentSource.java | 17 +++++++++ .../ontapi/OntGraphDocumentSourceImpl.java | 35 +++++++------------ .../owlcs/ontapi/OntologyManagerImpl.java | 2 +- .../tests/managers/CopyManagerTest.java | 6 ++-- .../managers/GraphDocumentSourceTest.java | 14 ++++---- .../tests/managers/MiscOntologyTest.java | 5 +-- .../tests/model/ImportsOntModelTest.java | 4 +-- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSource.java b/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSource.java index 432abd6c..92e4c933 100644 --- a/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSource.java +++ b/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSource.java @@ -19,6 +19,7 @@ import java.io.InputStream; import java.io.Reader; +import java.util.Objects; import java.util.Optional; /** @@ -31,6 +32,22 @@ */ public interface OntGraphDocumentSource extends OWLOntologyDocumentSource { + /** + * Creates an {@link OWLOntologyDocumentSource} that wraps the given graph. + * + * @param graph {@link Graph}, not {@code null} + * @return {@link OntGraphDocumentSource} + */ + static OntGraphDocumentSource of(Graph graph) { + Objects.requireNonNull(graph, "Null graph"); + return new OntGraphDocumentSourceImpl() { + @Override + public Graph getGraph() { + return graph; + } + }; + } + /** * Returns the {@link Graph Jena RDF Graph} to be wrapped as an ontology into the manager. * diff --git a/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSourceImpl.java b/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSourceImpl.java index 5650e200..1003baa7 100644 --- a/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSourceImpl.java +++ b/src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSourceImpl.java @@ -35,6 +35,7 @@ import java.util.Objects; import java.util.Optional; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicReference; @@ -96,14 +97,16 @@ public Optional getInputStream() { /** * Creates a new {@code InputStream} for the given {@code Graph} and {@code lang}. - * Please don't forget to call {@link AutoCloseable#close()} - all exceptions are handled there. + * Please remember to call {@link AutoCloseable#close()} - all exceptions are handled there. + * Note: this method is for compatibility, + * it can be used with OWL-API-impl internals, ONT-API does not use it, but uses the graph directly. * * @param graph {@link Graph} a graph to read from * @param lang {@link Lang} format syntax * @param error {@link AtomicReference}, a container that will contain an {@code Exception} if it occurs * @return {@code InputStream} */ - protected static InputStream toInputStream(Graph graph, Lang lang, AtomicReference error) { + public static InputStream toInputStream(Graph graph, Lang lang, AtomicReference error) { Objects.requireNonNull(graph); Objects.requireNonNull(lang); Objects.requireNonNull(error); @@ -121,18 +124,18 @@ public void close() throws IOException { super.close(); } catch (IOException e) { IOException x = findIOException(error.get(), graph, lang); - if (x == null) { - error.set(e); - } else { + if (x != null) { x.addSuppressed(e); } + error.set(e); + } IOException ex = findIOException(error.get(), graph, lang); if (ex != null) throw ex; } }; - new Thread(() -> { + Executors.newSingleThreadExecutor().execute(() -> { PipedOutputStream out; try { out = new PipedOutputStream(in); @@ -158,7 +161,7 @@ public void close() throws IOException { } } } - }).start(); + }); try { complete.await(); } catch (InterruptedException e) { @@ -203,7 +206,8 @@ public Optional getFormat() { * @return {@link OntFormat} */ public OntFormat getOntFormat() { - return OntFormat.TURTLE; + // there is an issue with TURTLE: https://github.com/owlcs/owlapi/issues/1149 + return OntFormat.RDF_XML; } private Optional format() { @@ -230,19 +234,4 @@ public void setIRIResolutionFailed(boolean value) { throw new OntApiException.Unsupported("#setIRIResolutionFailed is not supported."); } - /** - * Creates an {@link OWLOntologyDocumentSource} that wraps the given graph. - * - * @param graph {@link Graph}, not {@code null} - * @return {@link OntGraphDocumentSourceImpl} - */ - public static OntGraphDocumentSourceImpl of(Graph graph) { - Objects.requireNonNull(graph, "Null graph"); - return new OntGraphDocumentSourceImpl() { - @Override - public Graph getGraph() { - return graph; - } - }; - } } diff --git a/src/main/java/com/github/owlcs/ontapi/OntologyManagerImpl.java b/src/main/java/com/github/owlcs/ontapi/OntologyManagerImpl.java index 1a7a048e..1c88aadc 100644 --- a/src/main/java/com/github/owlcs/ontapi/OntologyManagerImpl.java +++ b/src/main/java/com/github/owlcs/ontapi/OntologyManagerImpl.java @@ -703,7 +703,7 @@ public Ontology addOntology(@Nonnull Graph graph, @Nonnull OntLoaderConfiguratio DocumentSourceMapping mapping = i -> graphs.entrySet() .stream() .filter(e -> matchIDs(e.getKey(), i)) - .map(e -> OntGraphDocumentSourceImpl.of(e.getValue())) + .map(e -> OntGraphDocumentSource.of(e.getValue())) .findFirst() .orElse(null); RWLockedCollection store = getDocumentSourceMappers(); diff --git a/src/test/java/com/github/owlcs/ontapi/tests/managers/CopyManagerTest.java b/src/test/java/com/github/owlcs/ontapi/tests/managers/CopyManagerTest.java index 7bd252c4..b23ebebb 100644 --- a/src/test/java/com/github/owlcs/ontapi/tests/managers/CopyManagerTest.java +++ b/src/test/java/com/github/owlcs/ontapi/tests/managers/CopyManagerTest.java @@ -16,7 +16,7 @@ import com.github.owlcs.ontapi.OntApiException; import com.github.owlcs.ontapi.OntFormat; -import com.github.owlcs.ontapi.OntGraphDocumentSourceImpl; +import com.github.owlcs.ontapi.OntGraphDocumentSource; import com.github.owlcs.ontapi.OntManagers; import com.github.owlcs.ontapi.Ontology; import com.github.owlcs.ontapi.OntologyManager; @@ -305,8 +305,8 @@ public void testShallowCopingIfSourceIsOntologyModel() { LOGGER.debug("New doc IRI: {}", di); Assertions.assertNotNull(f); Assertions.assertNotNull(di); - Assertions.assertEquals(OntFormat.TURTLE.createOwlFormat().getKey(), f.getKey()); - Assertions.assertEquals(OntGraphDocumentSourceImpl.of(a.getBaseGraph()).getDocumentIRI(), di); + Assertions.assertEquals(OntFormat.RDF_XML.createOwlFormat().getKey(), f.getKey()); + Assertions.assertEquals(OntGraphDocumentSource.of(a.getBaseGraph()).getDocumentIRI(), di); } @Test diff --git a/src/test/java/com/github/owlcs/ontapi/tests/managers/GraphDocumentSourceTest.java b/src/test/java/com/github/owlcs/ontapi/tests/managers/GraphDocumentSourceTest.java index 00013153..68e4557d 100644 --- a/src/test/java/com/github/owlcs/ontapi/tests/managers/GraphDocumentSourceTest.java +++ b/src/test/java/com/github/owlcs/ontapi/tests/managers/GraphDocumentSourceTest.java @@ -39,7 +39,7 @@ import org.apache.jena.vocabulary.RDFS; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.semanticweb.owlapi.formats.TurtleDocumentFormat; +import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat; import org.semanticweb.owlapi.io.OWLOntologyDocumentSource; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLAxiom; @@ -69,7 +69,7 @@ public class GraphDocumentSourceTest { @Test public void testCommonValidateOGDS() { OntModel m = OntModelFactory.createModel(OWLIOUtils.loadResourceAsModel("/ontapi/pizza.ttl", Lang.TURTLE).getGraph()); - OntGraphDocumentSourceImpl s = OntGraphDocumentSourceImpl.of(m.getGraph()); + OntGraphDocumentSourceImpl s = (OntGraphDocumentSourceImpl) OntGraphDocumentSource.of(m.getGraph()); URI u1 = s.getDocumentIRI().toURI(); Assertions.assertFalse(s.hasAlredyFailedOnStreams()); Assertions.assertFalse(s.hasAlredyFailedOnIRIResolution()); @@ -79,8 +79,8 @@ public void testCommonValidateOGDS() { Assertions.assertFalse(s.hasAlredyFailedOnStreams()); Assertions.assertFalse(s.hasAlredyFailedOnIRIResolution()); Assertions.assertEquals(u1, s.getDocumentIRI().toURI()); - Assertions.assertEquals(OntFormat.TURTLE, s.getOntFormat()); - Assertions.assertInstanceOf(TurtleDocumentFormat.class, s.getFormat().orElseThrow(AssertionError::new)); + Assertions.assertEquals(OntFormat.RDF_XML, s.getOntFormat()); + Assertions.assertInstanceOf(RDFXMLDocumentFormat.class, s.getFormat().orElseThrow(AssertionError::new)); } @Test @@ -88,7 +88,7 @@ public void testOntGraphDocumentSourceInOWL() throws OWLOntologyCreationExceptio IRI pizza = IRI.create(Objects.requireNonNull(MiscOntologyTest.class.getResource("/ontapi/pizza.ttl"))); LOGGER.debug("File: {}", pizza); Ontology ont = OntManagers.createManager().loadOntology(pizza); - OWLOntologyDocumentSource src = OntGraphDocumentSourceImpl.of(ont.asGraphModel().getBaseGraph()); + OWLOntologyDocumentSource src = OntGraphDocumentSource.of(ont.asGraphModel().getBaseGraph()); URI uri = src.getDocumentIRI().toURI(); LOGGER.debug("Load using pipes from: {}", uri); OWLOntology owl = OntManagers.createOWLAPIImplManager().loadOntologyFromOntologyDocument(src); @@ -107,7 +107,7 @@ public void testOntGraphDocumentSourceInONT() throws OWLOntologyCreationExceptio OntModel c = OntModelFactory.createModel(); c.setID(iris.get(2)).addImport(iris.get(0)).addImport(iris.get(1)); OWLIOUtils.print(c); - OntGraphDocumentSource src = OntGraphDocumentSourceImpl.of(c.getGraph()); + OntGraphDocumentSource src = OntGraphDocumentSource.of(c.getGraph()); LOGGER.debug("Load graph from: {}", src.getDocumentIRI().toURI()); m.loadOntologyFromOntologyDocument(src); Assertions.assertEquals(3, m.ontologies().count()); @@ -220,7 +220,7 @@ public void testDisableTransforms() throws OWLOntologyCreationException { })); OntModel g = OntModelFactory.createModel(OWLIOUtils.loadResourceAsModel("/ontapi/pizza.ttl", Lang.TURTLE).getGraph()); - OntGraphDocumentSourceImpl s1 = OntGraphDocumentSourceImpl.of(g.getBaseGraph()); + OntGraphDocumentSourceImpl s1 = (OntGraphDocumentSourceImpl) OntGraphDocumentSource.of(g.getBaseGraph()); try { m.loadOntologyFromOntologyDocument(s1); Assertions.fail("No transforms are running"); diff --git a/src/test/java/com/github/owlcs/ontapi/tests/managers/MiscOntologyTest.java b/src/test/java/com/github/owlcs/ontapi/tests/managers/MiscOntologyTest.java index c7a828f2..3773bf3d 100644 --- a/src/test/java/com/github/owlcs/ontapi/tests/managers/MiscOntologyTest.java +++ b/src/test/java/com/github/owlcs/ontapi/tests/managers/MiscOntologyTest.java @@ -21,7 +21,6 @@ import com.github.owlcs.ontapi.OntManagers; import com.github.owlcs.ontapi.Ontology; import com.github.owlcs.ontapi.OntologyManager; -import com.github.owlcs.ontapi.OntologyModelImpl; import com.github.owlcs.ontapi.testutils.OWLIOUtils; import org.apache.jena.graph.Graph; import org.apache.jena.ontapi.OntJenaException; @@ -39,7 +38,6 @@ import org.semanticweb.owlapi.model.AddImport; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLDocumentFormat; -import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyStorageException; import org.slf4j.Logger; @@ -68,7 +66,6 @@ private static Graph makeGraphWithRecursion() { @Test public void testImportsOnConcurrentManager() { - Class expected = OntologyModelImpl.Concurrent.class; OntologyManager m = OntManagers.createConcurrentManager(); DataFactory df = m.getOWLDataFactory(); Ontology a = m.createOntology(IRI.create("A")); @@ -141,7 +138,7 @@ public void testPrefixesOnReload() throws IOException, OWLOntologyStorageExcepti Ontology ontology1 = manager1.addOntology(model.getGraph()); Assertions.assertEquals(1, ontology1.getAxiomCount()); OWLDocumentFormat f = manager1.getOntologyFormat(ontology1); - Assertions.assertEquals(OntFormat.TURTLE.createOwlFormat(), f); + Assertions.assertEquals(OntFormat.RDF_XML.createOwlFormat(), f); Assertions.assertEquals(model.getNsPrefixURI(prefName), f != null ? f.asPrefixOWLDocumentFormat().getPrefix(prefName + ":") : null, "Wrong prefix"); diff --git a/src/test/java/com/github/owlcs/ontapi/tests/model/ImportsOntModelTest.java b/src/test/java/com/github/owlcs/ontapi/tests/model/ImportsOntModelTest.java index d8215243..2c71e81f 100644 --- a/src/test/java/com/github/owlcs/ontapi/tests/model/ImportsOntModelTest.java +++ b/src/test/java/com/github/owlcs/ontapi/tests/model/ImportsOntModelTest.java @@ -17,7 +17,7 @@ import com.github.owlcs.ontapi.DataFactory; import com.github.owlcs.ontapi.OWLAdapter; import com.github.owlcs.ontapi.OntBaseModel; -import com.github.owlcs.ontapi.OntGraphDocumentSourceImpl; +import com.github.owlcs.ontapi.OntGraphDocumentSource; import com.github.owlcs.ontapi.OntManagers; import com.github.owlcs.ontapi.Ontology; import com.github.owlcs.ontapi.OntologyManager; @@ -144,7 +144,7 @@ private static void testMutualImportsWhileLoading(OntologyManager m) throws Exce } private static OWLOntologyDocumentSource createSource(IRI ont, IRI imports) { - return OntGraphDocumentSourceImpl.of(createGraph(ont.getIRIString(), + return OntGraphDocumentSource.of(createGraph(ont.getIRIString(), imports.getIRIString(), ont.getIRIString() + "#C")); }