Search in sources :

Example 66 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class OWLUtils method guessOntologyID.

public static OWLOntologyID guessOntologyID(InputStream content, Parser parser, String format, int limit, int versionIriOffset) throws IOException {
    long before = System.currentTimeMillis();
    log.info("Guessing ontology ID. Read limit = {} triples; offset = {} triples.", limit, versionIriOffset);
    BufferedInputStream bIn = new BufferedInputStream(content);
    // set an appropriate limit
    bIn.mark(limit * 512);
    OntologyLookaheadGraph graph = new OntologyLookaheadGraph(limit, versionIriOffset);
    try {
        parser.parse(graph, bIn, format);
    } catch (RuntimeException e) {
        log.error("Parsing failed for format {}. Returning null.", format);
    }
    OWLOntologyID result;
    if (graph.getOntologyIRI() == null) {
        // No Ontology ID found
        log.warn(" *** No ontology ID found, ontology has a chance of being anonymous.");
        result = new OWLOntologyID();
    } else {
        // bIn.reset(); // reset set the stream to the start
        IRI oiri = IRI.create(graph.getOntologyIRI().getUnicodeString());
        result = graph.getVersionIRI() == null ? new OWLOntologyID(oiri) : new OWLOntologyID(oiri, IRI.create(graph.getVersionIRI().getUnicodeString()));
        log.info(" *** Guessed ID : {}", result);
    }
    log.info(" ... Triples scanned : {}; filtered in : {}", graph.getScannedTripleCount(), graph.size());
    log.info(" ... Time : {} ms", System.currentTimeMillis() - before);
    return result;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) BufferedInputStream(java.io.BufferedInputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologyLookaheadGraph(org.apache.stanbol.commons.owl.OntologyLookaheadGraph)

Example 67 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class TestOWLUtils method lookaheadVersionedDistance100.

/*
     * Guessing the ID of a versioned ontology whose ontology IRI is declared >100 triples before its version
     * IRI.
     */
@Test
public void lookaheadVersionedDistance100() throws Exception {
    // Actual distance is 102
    String location = "/owl/versioned_distance-100.owl";
    log.info("Testing lookahead for location {}", location);
    org.semanticweb.owlapi.model.IRI incubatedVersion = org.semanticweb.owlapi.model.IRI.create("http://svn.apache.org/repos/asf/incubator/stanbol/trunk/commons/owl/src/test/resources/owl/versioned_distance-100.owl");
    OWLOntologyID expectedOntId = new OWLOntologyID(ontologyIri, incubatedVersion);
    // No triple limit but offset < 102 : guessing should return the unversioned ID.
    InputStream content = getClass().getResourceAsStream(location);
    OWLOntologyID id = OWLUtils.guessOntologyID(content, parser, TURTLE, -1, 99);
    assertNotNull(id);
    assertEquals(new OWLOntologyID(ontologyIri), id);
    // Try again, setting limit = 1024 (offset = 102) should work.
    content = getClass().getResourceAsStream(location);
    id = OWLUtils.guessOntologyID(content, parser, TURTLE, 1024);
    assertNotNull(id);
    assertEquals(expectedOntId, id);
}
Also used : InputStream(java.io.InputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Test(org.junit.Test)

Example 68 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class TestOWLUtils method lookaheadNamed.

/*
     * Guessing the ID of a named ontology whose IRI is near the end of the graph.
     */
@Test
public void lookaheadNamed() throws Exception {
    String location = "/owl/named.owl";
    log.info("Testing lookahead for location {}", location);
    // Try a low triple limit (the ontology IRI triple is much further).
    InputStream content = getClass().getResourceAsStream(location);
    OWLOntologyID id = OWLUtils.guessOntologyID(content, parser, RDF_XML, 10);
    assertTrue(id.isAnonymous());
    // Try again with no limit
    content = getClass().getResourceAsStream(location);
    id = OWLUtils.guessOntologyID(content, parser, RDF_XML);
    assertNotNull(id);
    assertFalse(id.isAnonymous());
    assertEquals(new OWLOntologyID(ontologyIri), id);
}
Also used : InputStream(java.io.InputStream) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) Test(org.junit.Test)

Example 69 with OWLOntologyID

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;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ArrayList(java.util.ArrayList) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) URI(java.net.URI) AddImport(org.semanticweb.owlapi.model.AddImport) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) RemoveImport(org.semanticweb.owlapi.model.RemoveImport) SetOntologyID(org.semanticweb.owlapi.model.SetOntologyID) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 70 with OWLOntologyID

use of org.semanticweb.owlapi.model.OWLOntologyID in project stanbol by apache.

the class RootResource method createOntologyEntry.

@PUT
@Path("/{ontologyId:.+}")
public Response createOntologyEntry(@PathParam("ontologyId") String ontologyId, @Context HttpHeaders headers, @Context UriInfo uriInfo) {
    OWLOntologyID key = OntologyUtils.decode(ontologyId);
    ResponseBuilder rb;
    if (ontologyProvider.listAllRegisteredEntries().contains(key)) {
        rb = Response.status(CONFLICT);
    } else {
        ontologyProvider.createBlankOntologyEntry(key);
        rb = Response.created(uriInfo.getRequestUri());
    }
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Aggregations

OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)86 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)31 Test (org.junit.Test)27 HashSet (java.util.HashSet)22 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)22 InputStream (java.io.InputStream)20 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)19 Triple (org.apache.clerezza.commons.rdf.Triple)19 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)19 IRI (org.apache.clerezza.commons.rdf.IRI)17 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)16 IRI (org.semanticweb.owlapi.model.IRI)14 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)13 IOException (java.io.IOException)12 UnmodifiableOntologyCollectorException (org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException)12 WebApplicationException (javax.ws.rs.WebApplicationException)11 Graph (org.apache.clerezza.commons.rdf.Graph)10 AddImport (org.semanticweb.owlapi.model.AddImport)10 Path (javax.ws.rs.Path)9 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)8