use of org.semanticweb.owlapi.model.OWLOntologyID 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.OWLOntologyID in project stanbol by apache.
the class RootResource method performShowOntology.
/**
* Helper method to make sure a ResponseBuilder is created on every conditions, so that it is then
* possible to enable CORS on it afterwards.
*
* @param ontologyId
* @return
*/
protected ResponseBuilder performShowOntology(String ontologyId) {
if (ontologyId == null || ontologyId.isEmpty()) {
return Response.status(BAD_REQUEST);
}
OWLOntologyID key = OntologyUtils.decode(ontologyId);
if (ontologyProvider.listOrphans().contains(key)) {
return Response.status(NO_CONTENT);
}
OWLOntology o = getOWLOntology(ontologyId, false, uriInfo.getRequestUri());
if (o == null) {
return Response.status(NOT_FOUND);
}
// Assemble dependency list
Map<OWLOntologyID, OntologyProvider.Status> deps = new HashMap<OWLOntologyID, OntologyProvider.Status>();
for (OWLOntologyID dep : getDescriptor().getDependencies(key)) {
deps.put(dep, ontologyProvider.getStatus(dep));
}
Set<OntologyCollector> handles = new HashSet<OntologyCollector>();
if (onManager != null) {
for (Scope scope : onManager.getRegisteredScopes()) {
if (scope.getCoreSpace().hasOntology(key)) {
handles.add(scope.getCoreSpace());
}
if (scope.getCustomSpace().hasOntology(key)) {
handles.add(scope.getCustomSpace());
}
}
}
if (sessionManager != null) {
for (String sesId : sessionManager.getRegisteredSessionIDs()) {
if (sessionManager.getSession(sesId).hasOntology(key)) {
handles.add(sessionManager.getSession(sesId));
}
}
}
return Response.ok(new Viewable("ontology", new OntologyStats(uriInfo, key, o, ontologyProvider.listAliases(key), deps, handles)));
}
use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.
the class RootResource method getStandaloneOntology.
/**
* Gets the ontology with the given identifier in its version managed by the session.
*
* @param sessionId
* the session identifier.
* @param ontologyId
* the ontology identifier.
* @param uriInfo
* @param headers
* @return the requested managed ontology, or {@link Status#NOT_FOUND} if either the sessionn does not
* exist, or the if the ontology either does not exist or is not managed.
*/
@GET
@Path("/{ontologyId:.+}")
@Produces(value = { RDF_XML, TURTLE, X_TURTLE, MANCHESTER_OWL, FUNCTIONAL_OWL, OWL_XML, TEXT_PLAIN })
public Response getStandaloneOntology(@PathParam("ontologyId") String ontologyId, @DefaultValue("false") @QueryParam("merge") boolean merged, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
ResponseBuilder rb;
if (ontologyId == null || ontologyId.isEmpty()) {
rb = Response.status(BAD_REQUEST);
}
OWLOntologyID key = OntologyUtils.decode(ontologyId);
if (ontologyProvider.listOrphans().contains(key)) {
rb = Response.status(NO_CONTENT);
} else {
OWLOntology o = getOWLOntology(ontologyId, merged, uriInfo.getRequestUri());
rb = o == null ? Response.status(NOT_FOUND) : Response.ok(o);
}
// addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.
the class RootResource method getGraph.
private Graph getGraph(String ontologyId, boolean merged, URI requestUri) {
long before = System.currentTimeMillis();
OWLOntologyID key = OntologyUtils.decode(ontologyId);
log.debug("Will try to retrieve ontology {} from provider.", key);
/*
* Export directly to Graph since the OWLOntologyWriter uses (de-)serializing converters for the
* other formats.
*
* Use oTemp for the "real" graph and o for the graph that will be exported. This is due to the fact
* that in o we want to change import statements, but we do not want these changes to be stored
* permanently.
*/
Graph o = null, oTemp = null;
try {
oTemp = ontologyProvider.getStoredOntology(key, Graph.class, merged);
} catch (Exception ex) {
log.warn("Retrieval of ontology with ID " + key + " failed.", ex);
}
if (oTemp == null) {
log.debug("Ontology {} missing from provider. Trying libraries...", key);
// TODO remove once registry supports OWLOntologyID as public key.
IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
// 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 {
oTemp = registryManager.getLibrary(smallest).getOntology(iri, Graph.class);
} catch (RegistryContentException e) {
log.warn("The content of library " + smallest + " could not be accessed.", e);
}
}
}
// resource-intensive IndexedGraph, since both o and oTemp will be GC'ed after serialization.
if (oTemp != null) {
o = new SimpleGraph(oTemp);
}
if (o == null) {
log.debug("Ontology {} not found in any ontology provider or library.", ontologyId);
return null;
}
log.debug("Retrieved ontology {} .", ontologyId);
// Rewrite imports
String uri = uriInfo.getRequestUri().toString();
URI base = URI.create(uri.substring(0, uri.lastIndexOf(ontologyId) - 1));
// Rewrite import statements
/*
* TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
* going upOne() add "session" or "ontology" if needed).
*/
Iterator<Triple> imports = o.filter(null, OWL.imports, null);
Set<Triple> oldImports = new HashSet<Triple>();
while (imports.hasNext()) {
oldImports.add(imports.next());
}
for (Triple t : oldImports) {
// construct new statement
String s = ((org.apache.clerezza.commons.rdf.IRI) t.getObject()).getUnicodeString();
if (s.contains("::")) {
s = s.substring(s.indexOf("::") + 2, s.length());
}
org.apache.clerezza.commons.rdf.IRI target = new org.apache.clerezza.commons.rdf.IRI(base + "/" + s);
o.add(new TripleImpl(t.getSubject(), OWL.imports, target));
// remove old statement
o.remove(t);
}
// Versioning.
OWLOntologyID id = OWLUtils.extractOntologyID(o);
if (id != null && !id.isAnonymous() && id.getVersionIRI() == null) {
org.apache.clerezza.commons.rdf.IRI viri = new org.apache.clerezza.commons.rdf.IRI(requestUri.toString());
log.debug("Setting version IRI for export : {}", viri);
o.add(new TripleImpl(new org.apache.clerezza.commons.rdf.IRI(id.getOntologyIRI().toString()), new org.apache.clerezza.commons.rdf.IRI(OWL2Constants.OWL_VERSION_IRI), viri));
}
log.debug("Exported as Clerezza ImmutableGraph in {} ms. Handing over to writer.", System.currentTimeMillis() - before);
return o;
}
use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.
the class RootResource method getStandaloneGraph.
@GET
@Path("/{ontologyId:.+}")
@Produces(value = { APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON })
public Response getStandaloneGraph(@PathParam("ontologyId") String ontologyId, @DefaultValue("false") @QueryParam("meta") boolean meta, @DefaultValue("false") @QueryParam("merge") boolean merged, @Context UriInfo uriInfo, @Context HttpHeaders headers) {
if (meta) {
return getMetadata(ontologyId, uriInfo, headers);
}
ResponseBuilder rb;
if (ontologyId == null || ontologyId.isEmpty()) {
rb = Response.status(BAD_REQUEST);
}
OWLOntologyID key = OntologyUtils.decode(ontologyId);
if (ontologyProvider.listOrphans().contains(key)) {
rb = Response.status(NO_CONTENT);
} else {
Graph o = getGraph(ontologyId, merged, uriInfo.getRequestUri());
rb = o == null ? Response.status(NOT_FOUND) : Response.ok(o);
}
// addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
Aggregations