From 308431bf5fa5d5b423e10510e5cb4ce5a0e6631d Mon Sep 17 00:00:00 2001 From: ignazio Date: Thu, 21 Jan 2016 21:18:24 +0000 Subject: [PATCH] Fix OWLOntologyManager.copyOntology does not copy imports #480 --- .../test/ontology/MoveOntologyTestCase.java | 73 +++++++++++-------- .../cs/owl/owlapi/OWLOntologyManagerImpl.java | 51 +++++++------ 2 files changed, 72 insertions(+), 52 deletions(-) diff --git a/contract/src/test/java/org/semanticweb/owlapi/api/test/ontology/MoveOntologyTestCase.java b/contract/src/test/java/org/semanticweb/owlapi/api/test/ontology/MoveOntologyTestCase.java index 0bb2e63819..e1481c8f86 100644 --- a/contract/src/test/java/org/semanticweb/owlapi/api/test/ontology/MoveOntologyTestCase.java +++ b/contract/src/test/java/org/semanticweb/owlapi/api/test/ontology/MoveOntologyTestCase.java @@ -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; /** @@ -32,54 +37,64 @@ public class MoveOntologyTestCase extends TestBase { private final static @Nonnull String s = "\n" + "\n" - + " \n" + " \n" - + " \n" - + " \n" - + " \n" + " \n" + " \n" - + " datatype definition\n" - + " \n" - + " \n" + " \n" - + " \n" - + " \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" + + " \n" + + " \n" + " \n" + + " \n" + + " \n" + " \n" + " \n" + + " datatype definition\n" + + " \n" + + " \n" + " \n" + + " \n" + + " \n" + " "; + + @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); } } diff --git a/impl/src/main/java/uk/ac/manchester/cs/owl/owlapi/OWLOntologyManagerImpl.java b/impl/src/main/java/uk/ac/manchester/cs/owl/owlapi/OWLOntologyManagerImpl.java index 377ba72ea3..572d531fed 100644 --- a/impl/src/main/java/uk/ac/manchester/cs/owl/owlapi/OWLOntologyManagerImpl.java +++ b/impl/src/main/java/uk/ac/manchester/cs/owl/owlapi/OWLOntologyManagerImpl.java @@ -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); @@ -279,8 +279,8 @@ public OWLOntology getOntology(IRI iri) { if (result != null) { return result; } - java.util.Optional> findAny = ontologiesByID.entrySet().stream() - .filter(o -> o.getKey().match(iri)).findAny(); + java.util.Optional> findAny = ontologiesByID.entrySet().stream().filter( + o -> o.getKey().match(iri)).findAny(); return findAny.isPresent() ? findAny.get().getValue() : null; } finally { readLock.unlock(); @@ -294,8 +294,8 @@ public OWLOntology getOntology(OWLOntologyID id) { try { OWLOntology result = ontologiesByID.get(id); if (result == null && !id.isAnonymous()) { - java.util.Optional findAny = ids() - .filter(o -> o.matchOntology(id.getOntologyIRI().get())).findAny(); + java.util.Optional findAny = ids().filter(o -> o.matchOntology(id.getOntologyIRI() + .get())).findAny(); if (findAny.isPresent()) { result = ontologiesByID.get(findAny.get()); } @@ -446,10 +446,10 @@ public List 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; @@ -535,8 +535,8 @@ protected void rollBack(List 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"); } } } @@ -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 @@ -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(); } @@ -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: @@ -803,8 +808,8 @@ protected OWLOntology loadOntology(IRI iri, boolean allowExists, OWLOntologyLoad private OWLOntology getOntologyByDocumentIRI(IRI iri) { readLock.lock(); try { - java.util.Optional> findAny = documentIRIsByID.entrySet().stream() - .filter(o -> iri.equals(o.getValue())).findAny(); + java.util.Optional> findAny = documentIRIsByID.entrySet().stream().filter(o -> iri + .equals(o.getValue())).findAny(); if (findAny.isPresent()) { return getOntology(findAny.get().getKey()); } @@ -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; @@ -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(); @@ -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();