use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.
the class RootResource method getOWLOntology.
private OWLOntology getOWLOntology(String ontologyId, boolean merge, URI requestUri) {
long before = System.currentTimeMillis();
IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
log.debug("Will try to retrieve ontology {} from provider.", iri);
// TODO be selective: if the ontology is small enough, use OWLOntology otherwise export to ImmutableGraph.
OWLOntology o = null;
try {
// XXX Guarantee that there MUST always be an entry for any decoded ontology ID submitted.
OWLOntologyID id = OntologyUtils.decode(ontologyId);
o = ontologyProvider.getStoredOntology(id, OWLOntology.class, merge);
} catch (Exception ex) {
log.warn("Retrieval of ontology with ID " + iri + " failed.", ex);
}
if (o == null) {
log.debug("Ontology {} missing from provider. Trying libraries...", iri);
// See if we can touch a library. TODO: replace with event model on the ontology provider.
int minSize = -1;
IRI smallest = null;
for (Library lib : registryManager.getLibraries(iri)) {
int size = lib.getChildren().length;
if (minSize < 1 || size < minSize) {
smallest = lib.getIRI();
minSize = size;
}
}
if (smallest != null) {
log.debug("Selected library for ontology {} is {} .", iri, smallest);
try {
o = registryManager.getLibrary(smallest).getOntology(iri, OWLOntology.class);
} catch (RegistryContentException e) {
log.warn("The content of library " + smallest + " could not be accessed.", e);
}
}
}
if (o == null) {
log.debug("Ontology {} not found in any ontology provider or library.", iri);
return null;
}
log.debug("Retrieved ontology {} .", iri);
// Rewrite import statements - no ontology collector to do it for us here.
URI base = URI.create(getPublicBaseUri() + "ontonet/");
List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
// TODO manage import rewrites better once the container ID is fully configurable.
for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
changes.add(new RemoveImport(o, oldImp));
String s = oldImp.getIRI().toString();
if (s.contains("::")) {
s = s.substring(s.indexOf("::") + 2, s.length());
}
IRI target = IRI.create(base + s);
changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
}
// Versioning.
OWLOntologyID id = o.getOntologyID();
if (!id.isAnonymous() && id.getVersionIRI() == null) {
IRI viri = IRI.create(requestUri);
log.debug("Setting version IRI for export : {}", viri);
changes.add(new SetOntologyID(o, new OWLOntologyID(id.getOntologyIRI(), viri)));
}
o.getOWLOntologyManager().applyChanges(changes);
log.debug("Exported as Clerezza ImmutableGraph in {} ms. Handing over to writer.", System.currentTimeMillis() - before);
return o;
}
use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.
the class OntologyImportUtils method buildImportTree.
/**
* Non-recursively adds import statements to the root ontology so that it is directly linked to all the
* ontologies in the subtrees set.
*
* @param parent
* the ontology to which import subtrees should be appended. If null, a runtime exception will
* be thrown.
* @param subtrees
* the set of target ontologies for import statements. These can in turn be importing other
* ontologies, hence the "subtree" notation. A single statement will be added for
* each member of this set.
* @param mgr
* the OWL ontology manager to use for constructing the import tree. If null, an internal one
* will be used instead, otherwise an existing ontology manager can be used e.g. for extracting
* import statements from its IRI mappers or known ontologies. Note that the supplied manager
* will <i>never</i> try to load any ontologies, even when they are unknown.
* @return the same input ontology as defined in <code>root</code>, but with the added import statements.
*/
public static OWLOntology buildImportTree(OWLOntology parent, Set<OWLOntology> subtrees, OWLOntologyManager mgr) {
if (parent == null)
throw new NullPointerException("Cannot append import trees to a nonexistent ontology.");
// If no manager was supplied, use a temporary one.
if (mgr == null)
mgr = OWLManager.createOWLOntologyManager();
OWLDataFactory owlFactory = mgr.getOWLDataFactory();
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
for (OWLOntology o : subtrees) {
IRI importIri = null;
try {
/*
* First query the manager, as it could know the physical location of anonymous ontologies, if
* previously loaded or IRI-mapped.
*/
importIri = mgr.getOntologyDocumentIRI(o);
} catch (UnknownOWLOntologyException ex) {
/*
* Otherwise, ask the ontology itself (the location of an anonymous ontology may have been
* known at creation/loading time, even if another manager built it.)
*/
importIri = o.getOntologyID().getDefaultDocumentIRI();
} catch (Exception ex) {
logger.error("Exception caught during tree building. Skipping import of ontology " + o.getOntologyID(), ex);
} finally {
/*
* It is still possible that an imported ontology is anonymous but has no physical document
* IRI (for example, because it was only generated in-memory but not stored). In this case it
* is necessary (and generally safe) to copy all its axioms and import statements to the
* parent ontology, or else it is lost.
*/
if (o.isAnonymous() && importIri == null) {
logger.warn("Anonymous import target " + o.getOntologyID() + " not mapped to physical IRI. Will add extracted axioms to parent ontology.");
for (OWLImportsDeclaration im : o.getImportsDeclarations()) changes.add(new AddImport(parent, im));
for (OWLAxiom im : o.getAxioms()) changes.add(new AddAxiom(parent, im));
} else if (importIri != null) {
// An anonymous ontology can still be imported if it has a
// valid document IRI.
changes.add(new AddImport(parent, owlFactory.getOWLImportsDeclaration(importIri)));
}
}
}
// apply the changes one by one, just in case.
for (OWLOntologyChange im : changes) try {
mgr.applyChange(im);
} catch (Exception ex) {
logger.error("KReS :: Exception caught during tree building. Skipping import", ex);
continue;
}
return parent;
}
use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.
the class ScopeSetRenderer method getScopes.
public static OWLOntology getScopes(Set<Scope> scopes) {
OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
OWLOntology ont = null;
try {
ont = mgr.createOntology();
} catch (OWLOntologyCreationException e) {
LoggerFactory.getLogger(ScopeSetRenderer.class).error("KReS :: could not create empty ontology for rendering scopes.", e);
return null;
}
List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
// The ODP metadata vocabulary is always imported.
// TODO : also import the ONM meta when it goes online.
additions.add(new AddImport(ont, __factory.getOWLImportsDeclaration(IRI.create("http://www.ontologydesignpatterns.org/schemas/meta.owl"))));
for (Scope scope : scopes) {
OWLNamedIndividual iScope = __factory.getOWLNamedIndividual(IRI.create(scope.getDefaultNamespace() + scope.getID()));
OWLAxiom ax = __factory.getOWLClassAssertionAxiom(cScope, iScope);
additions.add(new AddAxiom(ont, ax));
}
mgr.applyChanges(additions);
return ont;
}
use of org.semanticweb.owlapi.model.OWLOntologyChange in project stanbol by apache.
the class SessionImpl method attachScopeImportsOwlApi.
private void attachScopeImportsOwlApi(OWLOntology target, org.semanticweb.owlapi.model.IRI prefix) {
if (!attachedScopes.isEmpty()) {
String scopePrefix = prefix.toString();
scopePrefix = scopePrefix.substring(0, scopePrefix.lastIndexOf("/" + shortName + "/")) + "/ontology/";
List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
OWLOntologyManager ontologyManager = target.getOWLOntologyManager();
OWLDataFactory df = ontologyManager.getOWLDataFactory();
// Add import declarations for attached scopes.
for (String scopeID : attachedScopes) {
org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(scopePrefix + scopeID);
changes.add(new AddImport(target, df.getOWLImportsDeclaration(physIRI)));
}
// Commit
ontologyManager.applyChanges(changes);
}
}
Aggregations