Search in sources :

Example 6 with OWLOntologyCreationException

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

the class ReasoningServiceExecutor method executeOWLApiReasoningService.

/**
     * Executes the OWLApiReasoingService
     * 
     * @param task
     * @param s
     * @param input
     * @param rules
     * @param targetGraphID
     * @param parameters
     * @return
     * @throws InconsistentInputException
     * @throws ReasoningServiceException
     * @throws UnsupportedTaskException
     */
private ReasoningServiceResult<OWLOntology> executeOWLApiReasoningService(String task, OWLApiReasoningService s, OWLOntology input, List<SWRLRule> rules, String targetGraphID, boolean filtered, Map<String, List<String>> parameters) throws InconsistentInputException, ReasoningServiceException, UnsupportedTaskException {
    // Check task: this is managed directly by the endpoint
    if (task.equals(ReasoningServiceExecutor.TASK_CHECK)) {
        log.debug("Task is '{}'", ReasoningServiceExecutor.TASK_CHECK);
        try {
            boolean is = s.isConsistent(input);
            return new ReasoningServiceResult<OWLOntology>(ReasoningServiceExecutor.TASK_CHECK, is);
        } catch (ReasoningServiceException e) {
            throw e;
        }
    }
    // We get the manager from the input ontology
    // XXX We must be aware of this.
    OWLOntologyManager manager = input.getOWLOntologyManager();
    try {
        OWLOntology output = manager.createOntology();
        Set<OWLAxiom> axioms = s.runTask(task, input, rules, filtered, parameters);
        log.debug("Prepare output: {} axioms", axioms.size());
        manager.addAxioms(output, axioms);
        if (targetGraphID == null) {
            return new ReasoningServiceResult<OWLOntology>(task, true, manager.getOntology(output.getOntologyID()));
        } else {
            save(output, targetGraphID);
            return new ReasoningServiceResult<OWLOntology>(task, true);
        }
    } catch (InconsistentInputException e) {
        log.warn("The input is not consistent");
        return new ReasoningServiceResult<OWLOntology>(ReasoningServiceExecutor.TASK_CHECK, false);
    } catch (ReasoningServiceException e) {
        throw e;
    } catch (OWLOntologyCreationException e) {
        log.error("Error! \n", e);
        throw new ReasoningServiceException(new IOException(e));
    } catch (UnsupportedTaskException e) {
        log.error("Error! \n", e);
        throw e;
    } catch (Throwable t) {
        log.error("Error! \n", t);
        throw new ReasoningServiceException(t);
    }
}
Also used : InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) IOException(java.io.IOException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 7 with OWLOntologyCreationException

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

the class ReasoningServiceExecutor method execute.

/**
     * General method for execution, delegates to specific implementations.
     * 
     * @param task
     * @param service
     * @param targetGraphID
     * @param parameters
     * @return
     * @throws ReasoningServiceException
     * @throws UnsupportedTaskException
     * @throws InconsistentInputException
     */
private ReasoningServiceResult<?> execute(String task, ReasoningService<?, ?, ?> service, String targetGraphID, Map<String, List<String>> parameters) throws ReasoningServiceException, UnsupportedTaskException, InconsistentInputException {
    long start = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.debug("[start] Execution: {}", service.getClass().getCanonicalName());
        log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        log.debug("-----------------------------------------------------");
        log.debug("execute()");
        log.debug(" > task: {}", task);
        log.debug(" > service: {}", service.getClass().getCanonicalName());
        log.debug(" > target: {}", targetGraphID);
        log.debug(" > parameters:");
        for (Entry<String, List<String>> e : parameters.entrySet()) {
            log.debug(" >> {}: {}", e.getKey());
            for (String v : e.getValue()) {
                log.debug(" >>> value: {}", v);
            }
        }
        log.debug(" > input providers:");
        for (ReasoningServiceInputProvider p : inmgr.getProviders()) {
            log.debug(" >> {}", p.getClass().getCanonicalName());
        }
        log.debug("-----------------------------------------------------");
    }
    ReasoningServiceResult<?> result = null;
    /**
         * TODO Switch this into the ReasoningService implementation
         */
    if (service instanceof JenaReasoningService) {
        Model input = ModelFactory.createDefaultModel();
        synchronized (inmgr) {
            Iterator<Statement> statements = inmgr.getInputData(Statement.class);
            while (statements.hasNext()) {
                input.add(statements.next());
            }
        }
        List<Rule> rules = null;
        synchronized (inmgr) {
            Iterator<Rule> rulesI = inmgr.getInputData(Rule.class);
            while (rulesI.hasNext()) {
                Rule o = rulesI.next();
                log.debug("Rule: {}", o);
                if (rules == null) {
                    rules = new ArrayList<Rule>();
                }
                rules.add(o);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Input size is {} statements", input.listStatements().toSet().size());
        }
        result = executeJenaReasoningService(task, (JenaReasoningService) service, input, rules, targetGraphID, true, parameters);
    } else if (service instanceof OWLApiReasoningService) {
        OWLOntology input;
        try {
            input = OWLManager.createOWLOntologyManager().createOntology();
        } catch (OWLOntologyCreationException e) {
            throw new ReasoningServiceException(e);
        }
        synchronized (inmgr) {
            Iterator<OWLAxiom> statements = inmgr.getInputData(OWLAxiom.class);
            while (statements.hasNext()) {
                input.getOWLOntologyManager().addAxiom(input, statements.next());
            }
        }
        // FIXME Please check if this is really necessary!!!
        input = input.getOWLOntologyManager().getOntology(input.getOntologyID());
        List<SWRLRule> rules = null;
        synchronized (inmgr) {
            Iterator<SWRLRule> rulesI = inmgr.getInputData(SWRLRule.class);
            while (rulesI.hasNext()) {
                if (rules == null) {
                    rules = new ArrayList<SWRLRule>();
                }
                rules.add(rulesI.next());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("Input size is {} statements", input.getAxiomCount());
        }
        result = executeOWLApiReasoningService(task, (OWLApiReasoningService) service, input, rules, targetGraphID, true, parameters);
    } else
        throw new UnsupportedOperationException("Service implementation not supported!");
    if (log.isDebugEnabled()) {
        log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        long end = System.currentTimeMillis();
        log.debug("[end] In time: {}ms", (end - start));
        log.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }
    return result;
}
Also used : JenaReasoningService(org.apache.stanbol.reasoners.jena.JenaReasoningService) Statement(com.hp.hpl.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) OWLApiReasoningService(org.apache.stanbol.reasoners.owlapi.OWLApiReasoningService) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) ReasoningServiceInputProvider(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Model(com.hp.hpl.jena.rdf.model.Model) Iterator(java.util.Iterator) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) ArrayList(java.util.ArrayList) List(java.util.List) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) Rule(com.hp.hpl.jena.reasoner.rulesys.Rule) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 8 with OWLOntologyCreationException

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

the class SessionManagerResource method listSessions.

@GET
@Produces(value = { RDF_XML, OWL_XML, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON, N3, N_TRIPLE, TEXT_PLAIN })
public Response listSessions(@Context UriInfo uriInfo, @Context HttpHeaders headers) {
    OWLOntologyManager ontMgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = ontMgr.getOWLDataFactory();
    OWLClass cSession = df.getOWLClass(IRI.create("http://stanbol.apache.org/ontologies/meta/Session"));
    OWLOntology o;
    try {
        o = ontMgr.createOntology(IRI.create(uriInfo.getRequestUri()));
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        for (String id : sessionManager.getRegisteredSessionIDs()) {
            IRI sessionid = IRI.create(sessionManager.getDefaultNamespace() + sessionManager.getID() + "/" + id);
            OWLNamedIndividual ind = df.getOWLNamedIndividual(sessionid);
            changes.add(new AddAxiom(o, df.getOWLClassAssertionAxiom(cSession, ind)));
        }
        ontMgr.applyChanges(changes);
    } catch (OWLOntologyCreationException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.ok(o);
    MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
    if (mediaType != null)
        rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) MediaType(javax.ws.rs.core.MediaType) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with OWLOntologyCreationException

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

the class SessionResource method manageOntology.

/**
     * Tells the session that it should manage the ontology obtained by parsing the supplied content.<br>
     * <br>
     * Note that the PUT method cannot be used, as it is not possible to predict what ID the ontology will
     * have until it is parsed.
     * 
     * @param content
     *            the ontology content
     * @return {@link Status#OK} if the addition was successful, {@link Status#NOT_FOUND} if there is no such
     *         session at all, {@link Status#FORBIDDEN} if the session is locked or cannot modified for some
     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some other error occurs.
     */
@POST
@Consumes(value = { RDF_XML, OWL_XML, N_TRIPLE, N3, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON })
public Response manageOntology(InputStream content, @PathParam("id") String sessionId, @Context HttpHeaders headers) {
    long before = System.currentTimeMillis();
    ResponseBuilder rb;
    session = sesMgr.getSession(sessionId);
    String mt = headers.getMediaType().toString();
    if (// Always check session first
    session == null)
        // Always check session first
        rb = Response.status(NOT_FOUND);
    else
        try {
            log.debug("POST content claimed to be of type {}.", mt);
            OntologyInputSource<?> src;
            if (OWL_XML.equals(mt) || FUNCTIONAL_OWL.equals(mt) || MANCHESTER_OWL.equals(mt))
                src = new OntologyContentInputSource(content);
            else
                // content = new BufferedInputStream(content);
                src = new GraphContentInputSource(content, mt, ontologyProvider.getStore());
            log.debug("SUCCESS parse with media type {}.", mt);
            OWLOntologyID key = session.addOntology(src);
            if (key == null || key.isAnonymous()) {
                log.error("FAILED parse with media type {}.", mt);
                throw new WebApplicationException(INTERNAL_SERVER_ERROR);
            }
            // FIXME ugly but will have to do for the time being
            log.debug("SUCCESS add ontology to session {}.", session.getID());
            log.debug("Storage key : {}", key);
            // key.split("::")[1];
            String uri = OntologyUtils.encode(key);
            // uri = uri.substring((ontologyProvider.getGraphPrefix() + "::").length());
            URI created = null;
            if (uri != null && !uri.isEmpty()) {
                created = getCreatedResource(uri);
                rb = Response.created(created);
            } else
                rb = Response.ok();
            log.info("POST request for ontology addition completed in {} ms.", (System.currentTimeMillis() - before));
            log.info("New resource URL is {}", created);
        } catch (UnmodifiableOntologyCollectorException e) {
            throw new WebApplicationException(e, FORBIDDEN);
        } catch (OWLOntologyCreationException e) {
            log.error("FAILED parse with media type {}.", mt);
            throw new WebApplicationException(e, BAD_REQUEST);
        }
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) OntologyContentInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.OntologyContentInputSource) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OntologyInputSource(org.apache.stanbol.ontologymanager.servicesapi.io.OntologyInputSource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 10 with OWLOntologyCreationException

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

the class SessionResource method manageOntology.

/**
     * Tells the session that it should manage the ontology obtained by dereferencing the supplied IRI.<br>
     * <br>
     * Note that the PUT method cannot be used, as it is not possible to predict what ID the ontology will
     * have until it is parsed.
     * 
     * @param content
     *            the ontology physical IRI
     * @return {@link Status#OK} if the addition was successful, {@link Status#NOT_FOUND} if there is no such
     *         session at all, {@link Status#FORBIDDEN} if the session is locked or cannot modified for some
     *         other reason, {@link Status#INTERNAL_SERVER_ERROR} if some other error occurs.
     */
@POST
@Consumes(value = MediaType.TEXT_PLAIN)
public Response manageOntology(String iri, @PathParam("id") String sessionId, @Context HttpHeaders headers) {
    session = sesMgr.getSession(sessionId);
    if (session == null)
        return Response.status(NOT_FOUND).build();
    try {
        session.addOntology(new RootOntologySource(IRI.create(iri)));
    } catch (UnmodifiableOntologyCollectorException e) {
        throw new WebApplicationException(e, FORBIDDEN);
    } catch (OWLOntologyCreationException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.ok();
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) UnmodifiableOntologyCollectorException(org.apache.stanbol.ontologymanager.servicesapi.collector.UnmodifiableOntologyCollectorException) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)54 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)45 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)33 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)18 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)15 HashSet (java.util.HashSet)13 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)13 AddImport (org.semanticweb.owlapi.model.AddImport)12 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)11 ArrayList (java.util.ArrayList)10 OntModel (com.hp.hpl.jena.ontology.OntModel)9 WebApplicationException (javax.ws.rs.WebApplicationException)9 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)9 IRI (org.semanticweb.owlapi.model.IRI)9 OWLClass (org.semanticweb.owlapi.model.OWLClass)9 IOException (java.io.IOException)8 Consumes (javax.ws.rs.Consumes)8 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 POST (javax.ws.rs.POST)7