Skip to content

Commit

Permalink
ont-api: OntGraphDocumentSource - use RDF/XML instead of turtle, as a…
Browse files Browse the repository at this point in the history
… workaround to owlcs/owlapi#1149; + minor changes
  • Loading branch information
sszuev committed Aug 7, 2024
1 parent be58554 commit f1ecf02
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 40 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/github/owlcs/ontapi/OntGraphDocumentSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.InputStream;
import java.io.Reader;
import java.util.Objects;
import java.util.Optional;

/**
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -96,14 +97,16 @@ public Optional<InputStream> 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<Exception> error) {
public static InputStream toInputStream(Graph graph, Lang lang, AtomicReference<Exception> error) {
Objects.requireNonNull(graph);
Objects.requireNonNull(lang);
Objects.requireNonNull(error);
Expand All @@ -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);
Expand All @@ -158,7 +161,7 @@ public void close() throws IOException {
}
}
}
}).start();
});
try {
complete.await();
} catch (InterruptedException e) {
Expand Down Expand Up @@ -203,7 +206,8 @@ public Optional<OWLDocumentFormat> 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<OntFormat> format() {
Expand All @@ -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;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentSourceMapping> store = getDocumentSourceMappers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -79,16 +79,16 @@ 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
public void testOntGraphDocumentSourceInOWL() throws OWLOntologyCreationException {
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);
Expand All @@ -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());
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -68,7 +66,6 @@ private static Graph makeGraphWithRecursion() {

@Test
public void testImportsOnConcurrentManager() {
Class<? extends OWLOntology> expected = OntologyModelImpl.Concurrent.class;
OntologyManager m = OntManagers.createConcurrentManager();
DataFactory df = m.getOWLDataFactory();
Ontology a = m.createOntology(IRI.create("A"));
Expand Down Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

Expand Down

0 comments on commit f1ecf02

Please sign in to comment.