Skip to content

Commit

Permalink
Fix AutoIriMapper chooses wrong IRIs. #755
Browse files Browse the repository at this point in the history
  • Loading branch information
ignazio1977 committed May 7, 2018
1 parent a59799a commit c38ff43
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
27 changes: 17 additions & 10 deletions api/src/main/java/org/semanticweb/owlapi/util/AutoIRIMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> fileExtensions =
new HashSet<>(Arrays.asList(".owl", ".xml", ".rdf", ".omn", ".ofn"));
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -313,25 +318,27 @@ 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;
}

@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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
StringDocumentSource documentSource =
new StringDocumentSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:owl =\"http://www.w3.org/2002/07/owl#\">\n"
+ " <owl:Ontology><owl:imports rdf:resource=\"urn:test#test\"/></owl:Ontology></rdf:RDF>");
OWLOntology o = m.loadOntologyFromOntologyDocument(documentSource);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -238,13 +240,11 @@ public void shouldRemapImport() throws OWLOntologyCreationException {
@Test
public void shouldRemapImportRdfXML() throws OWLOntologyCreationException {
String input = "<?xml version=\"1.0\"?>\n" + "<rdf:RDF xmlns=\"urn:test#\"\n"
+ " xml:base=\"urn:test\"\n"
+ " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"
+ " xml:base=\"urn:test\"\n" + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n"
+ " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
+ " <owl:Ontology rdf:about=\"urn:test\">\n"
+ " <owl:imports rdf:resource=\"http://test.org/TestPizzaImport.owl\"/>\n"
+ " </owl:Ontology>\n"
+ "</rdf:RDF>";
+ " </owl:Ontology>\n" + "</rdf:RDF>";
IRI testImport = IRI.create("http://test.org/", "TestPizzaImport.owl");
IRI remap = IRI.create("urn:test:", "mockImport");
StringDocumentSource source = new StringDocumentSource(input);
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -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);
Expand All @@ -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")));
}
}
1 change: 0 additions & 1 deletion contract/src/test/resources/imports/A.owl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


<rdf:RDF xmlns="http://test.org/compleximports/A.owl#"
xml:base="http://test.org/compleximports/A.owl"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:A="http://test.org/compleximports/A.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
Expand Down

0 comments on commit c38ff43

Please sign in to comment.