public OWLOntology getMergedOntology() { final IRI mergedOntologyIRI = IRI.create(queryOntology.getOntologyID().getDefaultDocumentIRI() + "-merged"); final OWLOntologyManager mm = controller.getOWLOntologyManager(); if (mm.contains(mergedOntologyIRI)) { return mm.getOntology(mergedOntologyIRI); } else { try { final OWLOntology mergedOntology = mm.createOntology(mergedOntologyIRI); mm.setOntologyFormat(mergedOntology, new RDFXMLOntologyFormat()); final String mergedOntologyFileName = mergedOntologyIRI .toURI() .toString() .substring(mergedOntologyIRI.toURI().toString().lastIndexOf("/") + 1) + ".owl"; mm.setOntologyDocumentIRI( mergedOntology, IRI.create( controller.getRuleSpec().getOutputDir().toURI() + "/" + mergedOntologyFileName)); mm.applyChange( new AddImport( mergedOntology, mm.getOWLDataFactory() .getOWLImportsDeclaration( queryOntology.getOntologyID().getDefaultDocumentIRI()))); return mergedOntology; } catch (OWLOntologyCreationException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
/** This method gets an OWL ontology used as an input for the supplied rule. */ private OWLOntology getInputOntologyForRule(IRI iri) throws OWLOntologyCreationException { if (iri.getScheme().equals(PREFIX)) { iri = getOntologyIRIForRuleName(iri.toString().substring(PREFIX.length())); } final OWLOntologyManager m = controller.getOWLOntologyManager(); OWLOntology o; if (m.contains(iri)) { o = m.getOntology(iri); } else { o = m.loadOntology(iri); } return o; }
/** @param args */ public static void main(String[] args) { String external = "http://purl.obolibrary.org/obo/iao/dev/external.owl"; String externalDerived = "http://purl.obolibrary.org/obo/iao/dev/externalDerived.owl"; String externalByHand = "http://purl.obolibrary.org/obo/iao/dev/externalByHand.owl"; String metaData = "http://purl.obolibrary.org/obo/iao/dev/ontology-metadata.owl"; String reasonerName = "hermit"; String path = "C:/Documents and Settings/Jie/My Documents/Ontology/iao/releases/2012-01-05/"; String saveIaoFilename = path + "merged/iao-main-infer.owl"; String inferOntURIStr = "http://purl.obolibrary.org/obo/iao/dev/iao_inferredSuperClasses.owl"; String saveInferFilename = path + "merged/iao-inferredSuperClasses.owl"; // Get hold of an ontology manager OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); AutoIRIMapper mapper = new AutoIRIMapper(new File(path + "ontology/"), false); manager.addIRIMapper(mapper); OWLOntology iaoOnt = OntologyManipulator.load(path + "ontology/iao-main.owl", manager); // OntologyManipulator.printPrefixNSs(manager, iaoOnt); OWLDataFactory df = manager.getOWLDataFactory(); // get all imported ontologies Set<OWLOntology> importOnts = iaoOnt.getImports(); // remove imports statements from the loaded ontology for (OWLOntology importOnt : importOnts) { IRI importOntIRI = importOnt.getOntologyID().getOntologyIRI(); if (importOntIRI.equals(IRI.create(metaData)) || importOntIRI.equals(IRI.create(external)) || importOntIRI.equals(IRI.create(externalByHand)) || importOntIRI.equals(IRI.create(externalDerived))) { RemoveImport ri = new RemoveImport(iaoOnt, df.getOWLImportsDeclaration(importOntIRI)); manager.applyChange(ri); } } // merge the removed imported ontologies to the loaded one for (OWLOntology importOnt : importOnts) { IRI importOntIRI = importOnt.getOntologyID().getOntologyIRI(); if (importOntIRI.equals(IRI.create(metaData)) || importOntIRI.equals(IRI.create(external)) || importOntIRI.equals(IRI.create(externalByHand)) || importOntIRI.equals(IRI.create(externalDerived))) { // OntologyManipulator.printPrefixNSs(manager, importOnt); iaoOnt = OntologyManipulator.mergeToTargetOnt(manager, iaoOnt, importOnt); } } // generate the inferred hierarchy and clean the super classes OWLReasoner reasoner = OWLReasonerRunner.runReasoner(manager, iaoOnt, reasonerName); iaoOnt = OWLReasonerRunner.getCleanedOntologyWithInferredSuperClasses( manager, iaoOnt, inferOntURIStr, reasoner); OntologyManipulator.saveToFile(manager, iaoOnt, saveIaoFilename); if (manager.contains(IRI.create(inferOntURIStr))) { OWLOntology inferOnt = manager.getOntology(IRI.create(inferOntURIStr)); OntologyManipulator.saveToFile(manager, inferOnt, saveInferFilename); } }
/** * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it * afterwards. * * <p>FIXME not merging yet FIXME not including imported ontologies unless they are merged * *before* storage. * * @param merge * @return */ protected OWLOntology exportToOWLOntology(boolean merge, IRI prefix) { long before = System.currentTimeMillis(); // Create a new ontology OWLOntology root; OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager(); IRI iri = IRI.create(prefix + _id); try { root = ontologyManager.createOntology(iri); } catch (OWLOntologyAlreadyExistsException e) { // It should be impossible, but just in case. ontologyManager.removeOntology(ontologyManager.getOntology(iri)); try { root = ontologyManager.createOntology(iri); } catch (OWLOntologyAlreadyExistsException e1) { root = ontologyManager.getOntology(iri); } catch (OWLOntologyCreationException e1) { log.error("Failed to assemble root ontology for scope " + iri, e); root = null; } } catch (OWLOntologyCreationException e) { log.error("Failed to assemble root ontology for scope " + _id, e); root = null; } // Add the import declarations for directly managed ontologies. if (root != null) { if (merge) { final Set<OWLOntology> set = new HashSet<OWLOntology>(); log.debug("Merging {} with its imports.", root); set.add(root); for (OWLOntologyID ontologyId : managedOntologies) { log.debug("Merging {} with {}.", ontologyId, root); set.add(getOntology(ontologyId, OWLOntology.class, true)); } OWLOntologySetProvider provider = new OWLOntologySetProvider() { @Override public Set<OWLOntology> getOntologies() { return set; } }; OWLOntologyMerger merger = new OWLOntologyMerger(provider); try { root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri); } catch (OWLOntologyCreationException e) { log.error("Failed to merge imports for ontology " + iri, e); root = null; } } else { // Add the import declarations for directly managed ontologies. List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>(); OWLDataFactory df = ontologyManager.getOWLDataFactory(); String base = prefix + getID(); for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString(); base += "/"; // The key set of managedOntologies contains the ontology IRIs, not their storage keys. for (OWLOntologyID ontologyId : managedOntologies) { // XXX some day the versionIRI will be the only physical reference for the ontology IRI physIRI = IRI.create(base + OntologyUtils.encode(ontologyId)); changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI))); } ontologyManager.applyChanges(changes); } } log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before); return root; }
public OWLOntology getKuabaOntology() { return manager.getOntology(IRI.create(ONTOLOGY_URL)); }