public void mergeImportClosure(boolean isRemovedImportsDeclarations) throws OWLOntologyCreationException { OWLOntologyManager manager = getManager(); Set<OWLOntology> imports = sourceOntology.getImportsClosure(); for (OWLOntology o : imports) { if (o.equals(sourceOntology)) continue; String comment = "Includes "+summarizeOntology(o); LOG.info(comment); addCommentToOntology(sourceOntology, comment); manager.addAxioms(sourceOntology, o.getAxioms()); } Set<OWLImportsDeclaration> oids = sourceOntology.getImportsDeclarations(); for (OWLImportsDeclaration oid : oids) { RemoveImport ri = new RemoveImport(sourceOntology, oid); getManager().applyChange(ri); } }
/** * Merge a specific ontology from the import closure into the main ontology. * Removes the import statement. * * @param ontologyIRI id of the ontology to merge * @throws OWLOntologyCreationException */ public void mergeSpecificImport(IRI ontologyIRI) throws OWLOntologyCreationException { OWLOntologyManager manager = getManager(); Set<OWLOntology> imports = sourceOntology.getImportsClosure(); for (OWLOntology o : imports) { if (o.equals(sourceOntology)) continue; Optional<IRI> currentIRI = o.getOntologyID().getOntologyIRI(); if (currentIRI.isPresent() && currentIRI.get().equals(ontologyIRI)) { String comment = "Includes "+summarizeOntology(o); LOG.info(comment); addCommentToOntology(sourceOntology, comment); manager.addAxioms(sourceOntology, o.getAxioms()); } } Set<OWLImportsDeclaration> oids = sourceOntology.getImportsDeclarations(); for (OWLImportsDeclaration oid : oids) { if (ontologyIRI.equals(oid.getIRI())) { RemoveImport ri = new RemoveImport(sourceOntology, oid); getManager().applyChange(ri); } } }
private static List<OWLOntologyChange> handleSupportOntologies(OWLGraphWrapper graph) { OWLOntology ontology = graph.getSourceOntology(); OWLOntologyManager manager = ontology.getOWLOntologyManager(); OWLDataFactory factory = manager.getOWLDataFactory(); List<OWLOntologyChange> removeImportChanges = new ArrayList<OWLOntologyChange>(); Set<OWLOntology> supportOntologySet = graph.getSupportOntologySet(); for (OWLOntology support : supportOntologySet) { Optional<IRI> supportIRI = support.getOntologyID().getOntologyIRI(); if(supportIRI.isPresent()) { IRI ontologyIRI = supportIRI.get(); OWLImportsDeclaration importDeclaration = factory.getOWLImportsDeclaration(ontologyIRI); ChangeApplied status = manager.applyChange(new AddImport(ontology, importDeclaration)); if (ChangeApplied.SUCCESSFULLY == status) { // the change was successful, create remove import for later removeImportChanges.add(new RemoveImport(ontology, importDeclaration)); } } } return removeImportChanges; }
@Override public void updateOntology( OWLOntology generatedOntology, IRI generatedOntologyIRI, IRI previousOntologyIRI, URI physicalURI) { final OWLModelManager mm = getOWLModelManager(); IRI iri = generatedOntologyIRI; OWLOntology generatedOntologyToDelete = null; final List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); final Collection<OWLOntology> ontologies = mm.getOntologies(); for (OWLOntology oo : ontologies) { if (iri.equals(oo.getOntologyID().getOntologyIRI())) { log.info("Removing ontology " + iri); generatedOntologyToDelete = oo; } changes.add(new RemoveImport(oo, getOWLOntologyManager().getOWLDataFactory() .getOWLImportsDeclaration(iri))); } mm.applyChanges(changes); changes.clear(); if (generatedOntologyToDelete != null) { if ( !mm.removeOntology(generatedOntologyToDelete) ) { log.info("Removing ontology " + generatedOntologyToDelete.getOntologyID() + " NOT succesful."); } } changes.add(new SetOntologyID(generatedOntology, iri)); changes.add(new AddImport(generatedOntology, mm.getOWLDataFactory() .getOWLImportsDeclaration(previousOntologyIRI))); mm.applyChanges(changes); mm.setPhysicalURI(generatedOntology, physicalURI); }
@Override public void updateOntology( OWLOntology generatedOntology, IRI generatedOntologyIRI, IRI previousOntologyIRI, URI physicalURI) { IRI iri = generatedOntologyIRI; OWLOntology generatedOntologyToDelete = null; final List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); final Collection<OWLOntology> ontologies = m.getOntologies(); for (OWLOntology oo : ontologies) { if (iri.equals(oo.getOntologyID().getOntologyIRI())) { log.info("Removing ontology " + iri); generatedOntologyToDelete = oo; } changes.add(new RemoveImport(oo, getOWLOntologyManager() .getOWLDataFactory().getOWLImportsDeclaration(iri))); } m.applyChanges(changes); changes.clear(); if (generatedOntologyToDelete != null) { m.removeOntology(generatedOntologyToDelete); } changes.add(new SetOntologyID(generatedOntology, iri)); changes.add(new AddImport(generatedOntology, m.getOWLDataFactory() .getOWLImportsDeclaration(previousOntologyIRI))); m.applyChanges(changes); m.setOntologyDocumentIRI(generatedOntology, IRI.create(physicalURI)); try { m.saveOntology(generatedOntology); } catch (OWLOntologyStorageException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public OWLOntologyChange visit(AddImport addImport) { return new RemoveImport(addImport.getOntology(), addImport.getImportDeclaration()); }
public OWLOntologyChange visit(RemoveImport removeImport) { return new AddImport(removeImport.getOntology(), removeImport.getImportDeclaration()); }
@Override public void visit(RemoveImport change) { defaultVisit(change); }
/** * Testing correctness of the reasoner with respect to ontology changes * <p> * removing the import declaration for </impA> */ @Test public void testRemovingImpA() throws Exception { OWLOntologyManager man = TestOWLManager.createOWLOntologyManager(); OWLDataFactory dataFactory = man.getOWLDataFactory(); // set up resolution of prefixes PrefixManager pm = new DefaultPrefixManager(); pm.setDefaultPrefix("http://www.example.com/main#"); pm.setPrefix("A:", "http://www.example.com/A#"); pm.setPrefix("B:", "http://www.example.com/B#"); // define query classes OWLClass mainX = dataFactory.getOWLClass(":X", pm); OWLClass mainY = dataFactory.getOWLClass(":Y", pm); OWLClass extA = dataFactory.getOWLClass("A:A", pm); OWLClass extB = dataFactory.getOWLClass("B:B", pm); OWLClass extC = dataFactory.getOWLClass("B:C", pm); // loading the root ontology OWLOntology root = loadOntology(man, "root.owl"); // Create an ELK reasoner. OWLReasonerFactory reasonerFactory = new ElkReasonerFactory(); OWLReasoner reasoner = reasonerFactory.createReasoner(root); try { // ************************************ // ** removing the import declaration for </impA> // ************************************ OWLImportsDeclaration importA = new OWLImportsDeclarationImpl( IRI.create("http://www.example.com#impA")); OWLOntologyChange change = new RemoveImport(root, importA); man.applyChange(change); reasoner.flush(); // Now the root ontology should not import anything assertEquals(root.getAxiomCount(), 3); assertEquals(root.getImportsClosure().size(), 1); assertEquals(getAxioms(root).size(), 3); // reasoner queries -- only subsumptions of the root ontology are // there assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( mainY)); assertTrue(reasoner.getSuperClasses(mainX, true).containsEntity( extA)); assertTrue(reasoner.getSuperClasses(mainY, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extA, true).containsEntity( extB)); assertFalse(reasoner.getSuperClasses(extB, true).containsEntity( extC)); } finally { reasoner.dispose(); } }
public void createSubSet(OWLGraphWrapper targetGraph, Set<OWLClass> subset, Set<OWLOntology> toMerge, boolean isExcludeClosure, boolean isRemoveDangling) throws OWLOntologyCreationException { OWLOntology targetOntology = targetGraph.getSourceOntology(); // import axioms set Set<OWLAxiom> importAxioms = new HashSet<OWLAxiom>(); for (OWLOntology mergeOntology : toMerge) { for (OWLClass cls : subset) { importAxioms.addAll(mergeOntology.getAxioms(cls, Imports.EXCLUDED)); } } // remove merge imports OWLOntologyManager targetManager = targetOntology.getOWLOntologyManager(); List<OWLOntologyChange> removeImports = new ArrayList<OWLOntologyChange>(); for(OWLOntology m : toMerge) { removeImports.add(new RemoveImport(targetOntology, new OWLImportsDeclarationImpl(m.getOntologyID().getOntologyIRI().get()))); } targetManager.applyChanges(removeImports); // add axiom set to target ontology. targetManager.addAxioms(targetOntology, importAxioms); LOG.info("Start Mooncat for subset."); Mooncat mooncat = new Mooncat(targetGraph); for (OWLOntology ont : toMerge) { mooncat.addReferencedOntology(ont); } if (!isExcludeClosure) { // create Closure Set<OWLAxiom> axioms = mooncat.getClosureAxiomsOfExternalReferencedEntities(); mooncat.addSubAnnotationProperties(axioms); // add missing axioms targetManager.addAxioms(targetOntology, axioms); LOG.info("Added "+axioms.size()+" axioms to the query ontology"); } if (isRemoveDangling) { mooncat.removeDanglingAxioms(); } return; }
@Override public Boolean visit(RemoveImport change) { Objects.requireNonNull(change); return false; }
/** * Remove all imports from the given ontology * @param ont OWL ontology */ private void removeImports(OWLOntology ont) { for(OWLImportsDeclaration importDecl : ont.getImportsDeclarations()) ont.getOWLOntologyManager().applyChange(new RemoveImport(ont.getOWLOntologyManager().getImportedOntology(importDecl), importDecl)); }