diff --git a/api/src/main/java/org/semanticweb/owlapi/util/AutoIRIMapper.java b/api/src/main/java/org/semanticweb/owlapi/util/AutoIRIMapper.java index ac8c2c74ae..7102a52aca 100644 --- a/api/src/main/java/org/semanticweb/owlapi/util/AutoIRIMapper.java +++ b/api/src/main/java/org/semanticweb/owlapi/util/AutoIRIMapper.java @@ -48,8 +48,6 @@ import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; -import com.google.common.base.Splitter; - /** * A mapper which given a root folder attempts to automatically discover and map files to * ontologies. The mapper is capable of mapping ontologies in RDF/XML, OWL/XML, Manchester OWL @@ -64,6 +62,7 @@ public class AutoIRIMapper extends DefaultHandler implements OWLOntologyIRIMapper, Serializable { static final Pattern pattern = Pattern.compile("Ontology\\(<([^>]+)>"); + static final Pattern manPattern = Pattern.compile("Ontology:[\r\n ]*<([^>]+)>"); private static final Logger LOGGER = LoggerFactory.getLogger(AutoIRIMapper.class); private final Set fileExtensions = new HashSet<>(Arrays.asList(".owl", ".xml", ".rdf", ".omn", ".ofn")); @@ -96,7 +95,10 @@ public AutoIRIMapper(File rootDirectory, boolean recursive) { * URI of the ontology document being parsed. */ handlerMap.put(Namespaces.RDF + "RDF", this::baseIRI); - /** A handler that can handle OWL/XML files. */ + /** + * A handler that can handle OWL/XML files as well as RDF/XML with an owl:Ontology element + * is defined with a non empty rdf:about. + */ handlerMap.put(OWLXMLVocabulary.ONTOLOGY.toString(), this::ontologyIRI); } @@ -106,6 +108,9 @@ protected IRI ontologyIRI(Attributes attributes) { if (ontURI == null) { ontURI = attributes.getValue("ontologyIRI"); } + if (ontURI == null) { + ontURI = attributes.getValue(Namespaces.RDF.toString(), "about"); + } if (ontURI == null) { return null; } @@ -313,12 +318,11 @@ private void parseManchesterSyntaxFile(File file) { @Nullable private IRI parseManLine(File file, String line) { - for (String tok : Splitter.on(" ").split(line)) { - if (tok.startsWith("<") && tok.endsWith(">")) { - IRI iri = unquote(tok); - addMapping(iri, file); - return iri; - } + Matcher matcher = manPattern.matcher(line); + if (matcher.matches()) { + IRI iri = IRI.create(matcher.group(1)); + addMapping(iri, file); + return iri; } return null; } @@ -326,12 +330,15 @@ private IRI parseManLine(File file, String line) { @Override public void startElement(@Nullable String uri, @Nullable String localName, @Nullable String qName, @Nullable Attributes attributes) throws SAXException { - OntologyRootElementHandler handler = handlerMap.get(uri + localName); + String tag = uri + localName; + OntologyRootElementHandler handler = handlerMap.get(tag); if (handler != null) { IRI ontologyIRI = handler.handle(checkNotNull(attributes)); if (ontologyIRI != null && currentFile != null) { addMapping(ontologyIRI, verifyNotNull(currentFile)); } + } + if (tag.equals("http://www.w3.org/2002/07/owl#Ontology")) { throw new SAXException(); } } diff --git a/contract/src/test/java/org/semanticweb/owlapi/api/test/imports/ImportsTestCase.java b/contract/src/test/java/org/semanticweb/owlapi/api/test/imports/ImportsTestCase.java index 1be07e1783..5198eaa8ef 100644 --- a/contract/src/test/java/org/semanticweb/owlapi/api/test/imports/ImportsTestCase.java +++ b/contract/src/test/java/org/semanticweb/owlapi/api/test/imports/ImportsTestCase.java @@ -16,7 +16,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,7 +34,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; + import javax.annotation.Nonnull; + import org.junit.Test; import org.semanticweb.owlapi.api.test.baseclasses.TestBase; import org.semanticweb.owlapi.apibinding.OWLManager; @@ -78,14 +80,19 @@ public void testImportsClosureUpdate() throws OWLOntologyCreationException { @Test public void shouldLoad() throws Exception { - @Nonnull File importsBothNameAndVersion = folder.newFile("tempimportsNameAndVersion.owl"); - @Nonnull File importsBothNameAndOther = folder.newFile("tempimportsNameAndOther.owl"); - @Nonnull File ontologyByName = folder.newFile("tempmain.owl"); - @Nonnull File ontologyByVersion = folder.newFile("tempversion.owl"); - @Nonnull File ontologyByOtherPath = folder.newFile("tempother.owl"); - OWLOntology ontology = getOWLOntology( - new OWLOntologyID(optional(IRI.create(ontologyByName)), optional(IRI - .create(ontologyByVersion)))); + @Nonnull + File importsBothNameAndVersion = folder.newFile("tempimportsNameAndVersion.owl"); + @Nonnull + File importsBothNameAndOther = folder.newFile("tempimportsNameAndOther.owl"); + @Nonnull + File ontologyByName = folder.newFile("tempmain.owl"); + @Nonnull + File ontologyByVersion = folder.newFile("tempversion.owl"); + @Nonnull + File ontologyByOtherPath = folder.newFile("tempother.owl"); + OWLOntology ontology = + getOWLOntology(new OWLOntologyID(optional(IRI.create(ontologyByName)), + optional(IRI.create(ontologyByVersion)))); ontology.saveOntology(IRI.create(ontologyByName)); ontology.saveOntology(IRI.create(ontologyByVersion)); ontology.saveOntology(IRI.create(ontologyByOtherPath)); @@ -115,8 +122,8 @@ public void shouldLoad() throws Exception { @Test public void shouldNotLoadWrong() throws OWLOntologyCreationException { m.createOntology(IRI.create("urn:test#", "test")); - StringDocumentSource documentSource = new StringDocumentSource( - "\n" + StringDocumentSource documentSource = + new StringDocumentSource("\n" + "\n" + " "); OWLOntology o = m.loadOntologyFromOntologyDocument(documentSource); @@ -176,38 +183,33 @@ public void testTurtleGraphImport() throws OWLOntologyCreationException { File ontologyDirectory = new File(RESOURCES, "importNoOntology"); String ns = "http://www.w3.org/2013/12/FDA-TA/tests/RenalTransplantation/"; IRI bobsOntologyName = IRI.create(ns, "subject-bob"); - OWLNamedIndividual bobsIndividual = df.getOWLNamedIndividual(ns + "subject-bob#", - "subjectOnImmunosuppressantA2"); + OWLNamedIndividual bobsIndividual = + df.getOWLNamedIndividual(ns + "subject-bob#", "subjectOnImmunosuppressantA2"); m.getIRIMappers().add(new SimpleIRIMapper(IRI.create(ns, "subject-amy"), - IRI.create(new File(ontologyDirectory, - "subject-amy.ttl")))); - m.getIRIMappers() - .add(new SimpleIRIMapper(bobsOntologyName, IRI.create(new File(ontologyDirectory, - "subject-bob.ttl")))); + IRI.create(new File(ontologyDirectory, "subject-amy.ttl")))); + m.getIRIMappers().add(new SimpleIRIMapper(bobsOntologyName, + IRI.create(new File(ontologyDirectory, "subject-bob.ttl")))); m.getIRIMappers().add(new SimpleIRIMapper(IRI.create(ns, "subject-sue"), - IRI.create(new File(ontologyDirectory, - "subject-sue.ttl")))); - m.getIRIMappers().add( - new SimpleIRIMapper(IRI.create("http://www.w3.org/2013/12/FDA-TA/", "core"), IRI.create( - new File(ontologyDirectory, "core.ttl")))); - OWLOntology topLevelImport = m - .loadOntologyFromOntologyDocument(new File(ontologyDirectory, "subjects.ttl")); + IRI.create(new File(ontologyDirectory, "subject-sue.ttl")))); + m.getIRIMappers() + .add(new SimpleIRIMapper(IRI.create("http://www.w3.org/2013/12/FDA-TA/", "core"), + IRI.create(new File(ontologyDirectory, "core.ttl")))); + OWLOntology topLevelImport = + m.loadOntologyFromOntologyDocument(new File(ontologyDirectory, "subjects.ttl")); assertTrue("Individuals about Bob are missing...", - topLevelImport.containsEntityInSignature(bobsIndividual, - INCLUDED)); + topLevelImport.containsEntityInSignature(bobsIndividual, INCLUDED)); } /** - * Tests to see if the method which obtains the imports closure behaves - * correctly. + * Tests to see if the method which obtains the imports closure behaves correctly. */ @Test public void testImportsClosure() { OWLOntology ontA = getOWLOntology(); OWLOntology ontB = getOWLOntology(); assertTrue(contains(ontA.importsClosure(), ontA)); - OWLImportsDeclaration importsDeclaration = ImportsDeclaration( - get(ontB.getOntologyID().getOntologyIRI())); + OWLImportsDeclaration importsDeclaration = + ImportsDeclaration(get(ontB.getOntologyID().getOntologyIRI())); ontA.applyChange(new AddImport(ontA, importsDeclaration)); assertTrue(contains(ontA.importsClosure(), ontB)); ontA.applyChange(new RemoveImport(ontA, importsDeclaration)); @@ -238,13 +240,11 @@ public void shouldRemapImport() throws OWLOntologyCreationException { @Test public void shouldRemapImportRdfXML() throws OWLOntologyCreationException { String input = "\n" + "\n" + " \n" + " \n" - + " \n" - + ""; + + " \n" + ""; IRI testImport = IRI.create("http://test.org/", "TestPizzaImport.owl"); IRI remap = IRI.create("urn:test:", "mockImport"); StringDocumentSource source = new StringDocumentSource(input); @@ -259,7 +259,8 @@ public void shouldRemapImportRdfXML() throws OWLOntologyCreationException { @Test public void testImportOntologyByLocation() throws Exception { - @Nonnull File f = folder.newFile("a.owl"); + @Nonnull + File f = folder.newFile("a.owl"); createOntologyFile(IRI("http://a.com", ""), f); // have to load an ontology for it to get a document IRI OWLOntology a = m.loadOntologyFromOntologyDocument(f); @@ -276,8 +277,8 @@ public void testImportOntologyByLocation() throws Exception { @Test public void shouldNotCreateIllegalPunning() throws OWLOntologyCreationException { m.getIRIMappers().add(new AutoIRIMapper(new File(RESOURCES, "importscyclic"), true)); - OWLOntology o = m.loadOntologyFromOntologyDocument(IRI.create(new File(RESOURCES, - "importscyclic/relaMath.owl"))); + OWLOntology o = m.loadOntologyFromOntologyDocument( + IRI.create(new File(RESOURCES, "importscyclic/relaMath.owl"))); OWLProfileReport checkOntology = Profiles.OWL2_DL.checkOntology(o); assertTrue(checkOntology.toString(), checkOntology.isInProfile()); o.directImports() @@ -298,8 +299,8 @@ public void testImportsWhenRemovingAndReloading() throws Exception { AutoIRIMapper mapper = new AutoIRIMapper(new File(RESOURCES, "imports"), true); man.getIRIMappers().add(mapper); String name = "/imports/thesubont.omn"; - OWLOntology root = man - .loadOntologyFromOntologyDocument(getClass().getResourceAsStream(name)); + OWLOntology root = + man.loadOntologyFromOntologyDocument(getClass().getResourceAsStream(name)); assertEquals(1, root.imports().count()); for (OWLOntology ontology : asList(man.ontologies())) { man.removeOntology(ontology); @@ -308,4 +309,18 @@ public void testImportsWhenRemovingAndReloading() throws Exception { root = man.loadOntologyFromOntologyDocument(getClass().getResourceAsStream(name)); assertEquals(1, root.imports().count()); } + + @Test + public void testAutoIRIMapperShouldNotBeConfusedByPrefixes() { + AutoIRIMapper mapper = new AutoIRIMapper(new File(RESOURCES, "imports"), true); + assertTrue(mapper.getOntologyIRIs() + .contains(IRI.create("http://owlapitestontologies.com/thesubont"))); + } + + @Test + public void testAutoIRIMapperShouldRecogniseRdfAboutInOwlOntology() { + AutoIRIMapper mapper = new AutoIRIMapper(new File(RESOURCES, "imports"), true); + assertTrue( + mapper.getOntologyIRIs().contains(IRI.create("http://test.org/compleximports/A.owl"))); + } } diff --git a/contract/src/test/resources/imports/A.owl b/contract/src/test/resources/imports/A.owl index 68dd7aa094..6ef1b0de45 100644 --- a/contract/src/test/resources/imports/A.owl +++ b/contract/src/test/resources/imports/A.owl @@ -11,7 +11,6 @@