Skip to content

Commit

Permalink
Fix OWLOntologyManager.copyOntology does not copy imports #480
Browse files Browse the repository at this point in the history
  • Loading branch information
ignazio1977 committed Jan 21, 2016
1 parent 89b6685 commit 308431b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
package org.semanticweb.owlapi.api.test.ontology;

import static org.junit.Assert.*;
import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.asSet;

import javax.annotation.Nonnull;

import org.junit.Before;
import org.junit.Test;
import org.semanticweb.owlapi.api.test.baseclasses.TestBase;
import org.semanticweb.owlapi.io.StringDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.UnknownOWLOntologyException;
import org.semanticweb.owlapi.model.parameters.OntologyCopy;

/**
Expand All @@ -32,54 +37,64 @@
public class MoveOntologyTestCase extends TestBase {

private final static @Nonnull String s = "<?xml version=\"1.0\"?>\n" + "<rdf:RDF xmlns=\"urn:test#\"\n"
+ " xml:base=\"urn:test\"\n" + " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n"
+ " xmlns:swrl=\"http://www.w3.org/2003/11/swrl#\"\n"
+ " xmlns:swrlb=\"http://www.w3.org/2003/11/swrlb#\"\n"
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\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" + " <rdfs:Datatype rdf:about=\"urn:mydatatype\">\n"
+ " <owl:equivalentClass>\n"
+ " <rdfs:Datatype rdf:about=\"http://www.w3.org/2001/XMLSchema#double\"/>\n"
+ " </owl:equivalentClass>\n" + " </rdfs:Datatype>\n" + " <owl:Axiom>\n"
+ " <rdfs:label >datatype definition</rdfs:label>\n"
+ " <owl:annotatedProperty rdf:resource=\"http://www.w3.org/2002/07/owl#equivalentClass\"/>\n"
+ " <owl:annotatedSource rdf:resource=\"urn:mydatatype\"/>\n" + " <owl:annotatedTarget>\n"
+ " <rdfs:Datatype rdf:about=\"http://www.w3.org/2001/XMLSchema#double\"/>\n"
+ " </owl:annotatedTarget>\n" + " </owl:Axiom></rdf:RDF>";
+ " xml:base=\"urn:test\"\n" + " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n"
+ " xmlns:swrl=\"http://www.w3.org/2003/11/swrl#\"\n"
+ " xmlns:swrlb=\"http://www.w3.org/2003/11/swrlb#\"\n"
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\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:testcopy\"><owl:imports rdf:resource=\"urn:test\"/></owl:Ontology>\n"
+ " <rdfs:Datatype rdf:about=\"urn:mydatatype\">\n" + " <owl:equivalentClass>\n"
+ " <rdfs:Datatype rdf:about=\"http://www.w3.org/2001/XMLSchema#double\"/>\n"
+ " </owl:equivalentClass>\n" + " </rdfs:Datatype>\n" + " <owl:Axiom>\n"
+ " <rdfs:label >datatype definition</rdfs:label>\n"
+ " <owl:annotatedProperty rdf:resource=\"http://www.w3.org/2002/07/owl#equivalentClass\"/>\n"
+ " <owl:annotatedSource rdf:resource=\"urn:mydatatype\"/>\n" + " <owl:annotatedTarget>\n"
+ " <rdfs:Datatype rdf:about=\"http://www.w3.org/2001/XMLSchema#double\"/>\n"
+ " </owl:annotatedTarget>\n" + " </owl:Axiom></rdf:RDF>";

@Before
public void setUp() throws OWLOntologyCreationException {
m.createOntology(IRI.create("urn:test"));
}

@Test
public void testMove() throws OWLOntologyCreationException {
OWLOntology o = loadOntologyFromString(s);
OWLOntologyManager m2 = o.getOWLOntologyManager();
OWLOntology o = m.loadOntologyFromOntologyDocument(new StringDocumentSource(s));
OWLOntology copy = m1.copyOntology(o, OntologyCopy.MOVE);
assertSame(o, copy);
assertEquals(m1, copy.getOWLOntologyManager());
assertFalse(m2.contains(o));
assertFalse(m.contains(o));
assertTrue(m1.contains(copy));
assertNull(m2.getOntologyFormat(o));
assertEquals(asSet(o.annotations()), asSet(copy.annotations()));
expectedException.expect(UnknownOWLOntologyException.class);
m.getOntologyFormat(o);
}

@Test
public void testShallow() throws OWLOntologyCreationException {
OWLOntology o = loadOntologyFromString(s);
OWLOntologyManager m2 = o.getOWLOntologyManager();
OWLOntology o = m.loadOntologyFromOntologyDocument(new StringDocumentSource(s));
OWLOntology copy = m1.copyOntology(o, OntologyCopy.SHALLOW);
assertEquals(m1, copy.getOWLOntologyManager());
assertTrue(m2.contains(o));
assertTrue(m.contains(o));
assertTrue(m1.contains(copy));
assertNotNull(m2.getOntologyFormat(o));
assertNotNull(m.getOntologyFormat(o));
assertEquals(asSet(o.annotations()), asSet(copy.annotations()));
assertEquals(asSet(o.importsDeclarations()), asSet(copy.importsDeclarations()));
}

@Test
public void testDeep() throws OWLOntologyCreationException {
OWLOntology o = loadOntologyFromString(s);
OWLOntologyManager m2 = o.getOWLOntologyManager();
public void testDeep() throws OWLOntologyCreationException, OWLOntologyStorageException {
OWLOntology o = m.loadOntologyFromOntologyDocument(new StringDocumentSource(s));
OWLOntology copy = m1.copyOntology(o, OntologyCopy.DEEP);
assertEquals(m1, copy.getOWLOntologyManager());
assertTrue(m2.contains(o));
assertTrue(m.contains(o));
assertTrue(m1.contains(copy));
assertNotNull(m2.getOntologyFormat(o));
assertNotNull(m.getOntologyFormat(o));
assertNotNull(m1.getOntologyFormat(o));
assertEquals(asSet(o.annotations()), asSet(copy.annotations()));
assertEquals(asSet(o.importsDeclarations()), asSet(copy.importsDeclarations()));
copy.annotations().forEach(System.out::println);
copy.saveOntology(System.out);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
* Informatics Group
* @since 2.0.0
*/
public class OWLOntologyManagerImpl
implements OWLOntologyManager, OWLOntologyFactory.OWLOntologyCreationHandler, Serializable {
public class OWLOntologyManagerImpl implements OWLOntologyManager, OWLOntologyFactory.OWLOntologyCreationHandler,
Serializable {

private static final String BADLISTENER = "BADLY BEHAVING LISTENER: {} has been removed";
private static final Logger LOGGER = LoggerFactory.getLogger(OWLOntologyManagerImpl.class);
Expand Down Expand Up @@ -279,8 +279,8 @@ public OWLOntology getOntology(IRI iri) {
if (result != null) {
return result;
}
java.util.Optional<Entry<OWLOntologyID, OWLOntology>> findAny = ontologiesByID.entrySet().stream()
.filter(o -> o.getKey().match(iri)).findAny();
java.util.Optional<Entry<OWLOntologyID, OWLOntology>> findAny = ontologiesByID.entrySet().stream().filter(
o -> o.getKey().match(iri)).findAny();
return findAny.isPresent() ? findAny.get().getValue() : null;
} finally {
readLock.unlock();
Expand All @@ -294,8 +294,8 @@ public OWLOntology getOntology(OWLOntologyID id) {
try {
OWLOntology result = ontologiesByID.get(id);
if (result == null && !id.isAnonymous()) {
java.util.Optional<OWLOntologyID> findAny = ids()
.filter(o -> o.matchOntology(id.getOntologyIRI().get())).findAny();
java.util.Optional<OWLOntologyID> findAny = ids().filter(o -> o.matchOntology(id.getOntologyIRI()
.get())).findAny();
if (findAny.isPresent()) {
result = ontologiesByID.get(findAny.get());
}
Expand Down Expand Up @@ -446,10 +446,10 @@ public List<OWLOntology> getSortedImportsClosure(OWLOntology ontology) {
* {@code false}.
*/
private boolean isChangeApplicable(OWLOntologyChange change) {
OWLOntologyLoaderConfiguration ontologyConfig = ontologyConfigurationsByOntologyID
.get(change.getOntology().getOntologyID());
if (ontologyConfig != null && !ontologyConfig.isLoadAnnotationAxioms() && change.isAddAxiom()
&& change.getAxiom() instanceof OWLAnnotationAxiom) {
OWLOntologyLoaderConfiguration ontologyConfig = ontologyConfigurationsByOntologyID.get(change.getOntology()
.getOntologyID());
if (ontologyConfig != null && !ontologyConfig.isLoadAnnotationAxioms() && change.isAddAxiom() && change
.getAxiom() instanceof OWLAnnotationAxiom) {
return false;
}
return true;
Expand Down Expand Up @@ -535,8 +535,8 @@ protected void rollBack(List<OWLOntologyChange> appliedChanges) {
for (OWLOntologyChange c : appliedChanges) {
if (enactChangeApplication(c.reverseChange()) == ChangeApplied.UNSUCCESSFULLY) {
// rollback could not complete, throw an exception
throw new OWLRuntimeException(
"Rollback of changes unsuccessful: Change " + c + " could not be rolled back");
throw new OWLRuntimeException("Rollback of changes unsuccessful: Change " + c
+ " could not be rolled back");
}
}
}
Expand Down Expand Up @@ -583,8 +583,8 @@ private void checkForImportsChange(OWLOntologyChange change) {
// then the import does not refer to a known IRI for
// ontologies; check for a document IRI to find the ontology
// id corresponding to the file location
documentIRIsByID.entrySet().stream().filter(o -> o.getValue().equals(iri)).findAny()
.ifPresent(o -> ontologyIDsByImportsDeclaration.put(addImportDeclaration, o.getKey()));
documentIRIsByID.entrySet().stream().filter(o -> o.getValue().equals(iri)).findAny().ifPresent(
o -> ontologyIDsByImportsDeclaration.put(addImportDeclaration, o.getKey()));
}
} else {
// Remove the mapping from declaration to ontology
Expand Down Expand Up @@ -636,7 +636,11 @@ public OWLDocumentFormat getOntologyFormat(OWLOntology ontology) {
readLock.lock();
try {
OWLOntologyID ontologyID = ontology.getOntologyID();
return ontologyFormatsByOntology.get(ontologyID);
OWLDocumentFormat format = ontologyFormatsByOntology.get(ontologyID);
if (format == null) {
throw new UnknownOWLOntologyException(ontologyID);
}
return format;
} finally {
readLock.unlock();
}
Expand Down Expand Up @@ -726,6 +730,7 @@ public OWLOntology copyOntology(OWLOntology toCopy, OntologyCopy settings) throw
OWLOntology o = createOntology(toCopy.getOntologyID());
AxiomType.AXIOM_TYPES.forEach(t -> addAxioms(o, toCopy.axioms(t)));
toCopy.annotations().forEach(a -> applyChange(new AddOntologyAnnotation(o, a)));
toCopy.importsDeclarations().forEach(a -> applyChange(new AddImport(o, a)));
toReturn = o;
break;
default:
Expand Down Expand Up @@ -803,8 +808,8 @@ protected OWLOntology loadOntology(IRI iri, boolean allowExists, OWLOntologyLoad
private OWLOntology getOntologyByDocumentIRI(IRI iri) {
readLock.lock();
try {
java.util.Optional<Entry<OWLOntologyID, IRI>> findAny = documentIRIsByID.entrySet().stream()
.filter(o -> iri.equals(o.getValue())).findAny();
java.util.Optional<Entry<OWLOntologyID, IRI>> findAny = documentIRIsByID.entrySet().stream().filter(o -> iri
.equals(o.getValue())).findAny();
if (findAny.isPresent()) {
return getOntology(findAny.get().getKey());
}
Expand Down Expand Up @@ -870,8 +875,8 @@ protected OWLOntology loadOntology(@Nullable IRI ontologyIRI, OWLOntologyDocumen
LOGGER.error(
"Runtime Warning: Parsers should load imported ontologies using the makeImportLoadRequest method.");
}
fireStartedLoadingEvent(new OWLOntologyID(optional(ontologyIRI), emptyOptional()),
documentSource.getDocumentIRI(), loadCount.get() > 0);
fireStartedLoadingEvent(new OWLOntologyID(optional(ontologyIRI), emptyOptional()), documentSource
.getDocumentIRI(), loadCount.get() > 0);
loadCount.incrementAndGet();
broadcastChanges.set(false);
Exception ex = null;
Expand Down Expand Up @@ -1520,8 +1525,8 @@ protected void fireStartedLoadingEvent(OWLOntologyID ontologyID, IRI documentIRI
writeLock.lock();
try {
for (OWLOntologyLoaderListener listener : new ArrayList<>(loaderListeners)) {
listener.startedLoadingOntology(
new OWLOntologyLoaderListener.LoadingStartedEvent(ontologyID, documentIRI, imported));
listener.startedLoadingOntology(new OWLOntologyLoaderListener.LoadingStartedEvent(ontologyID,
documentIRI, imported));
}
} finally {
writeLock.unlock();
Expand All @@ -1533,8 +1538,8 @@ protected void fireFinishedLoadingEvent(OWLOntologyID ontologyID, IRI documentIR
writeLock.lock();
try {
for (OWLOntologyLoaderListener listener : new ArrayList<>(loaderListeners)) {
listener.finishedLoadingOntology(
new OWLOntologyLoaderListener.LoadingFinishedEvent(ontologyID, documentIRI, imported, ex));
listener.finishedLoadingOntology(new OWLOntologyLoaderListener.LoadingFinishedEvent(ontologyID,
documentIRI, imported, ex));
}
} finally {
writeLock.unlock();
Expand Down

0 comments on commit 308431b

Please sign in to comment.