Search in sources :

Example 1 with JenaReasoningService

use of org.apache.stanbol.reasoners.jena.JenaReasoningService 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 2 with JenaReasoningService

use of org.apache.stanbol.reasoners.jena.JenaReasoningService in project stanbol by apache.

the class ReasoningServiceTaskResource method prepare.

public void prepare(String serviceID, String taskIDstr, String jobFlag) {
    this.taskID = taskIDstr;
    log.debug("Called service {} to perform task {}", serviceID, taskID);
    // Parameters for customized reasoning services
    this.parameters = prepareParameters();
    // Retrieve the service
    try {
        service = getService(serviceID);
    } catch (UnboundReasoningServiceException e) {
        log.error("Service not found: {}", serviceID);
        throw new WebApplicationException(e, Response.Status.NOT_FOUND);
    }
    log.debug("Service retrieved");
    // Check if the task is allowed
    if (this.service.supportsTask(taskID) || taskID.equals(ReasoningServiceExecutor.TASK_CHECK)) {
    // Ok
    } else {
        log.error("Unsupported task (not found): {}", taskID);
        throw new WebApplicationException(new Exception("Unsupported task (not found): " + taskID), Response.Status.NOT_FOUND);
    }
    // Check for the job parameter
    if (!jobFlag.equals("")) {
        log.debug("Job param is {}", job);
        if (jobFlag.equals("/job")) {
            log.debug("Ask for background job");
            this.job = true;
        } else {
            log.error("Malformed request");
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }
    }
    // Now we check if the service implementation is supported
    if (getCurrentService() instanceof JenaReasoningService) {
    } else if (getCurrentService() instanceof OWLApiReasoningService) {
    } else {
        log.error("This implementation of ReasoningService is not supported: {}", getCurrentService().getClass());
        throw new WebApplicationException(new Exception("This implementation of ReasoningService is not supported: " + getCurrentService().getClass()), Response.Status.INTERNAL_SERVER_ERROR);
    }
    log.debug("Implementation is supported");
}
Also used : UnboundReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException) JenaReasoningService(org.apache.stanbol.reasoners.jena.JenaReasoningService) WebApplicationException(javax.ws.rs.WebApplicationException) OWLApiReasoningService(org.apache.stanbol.reasoners.owlapi.OWLApiReasoningService) WebApplicationException(javax.ws.rs.WebApplicationException) UnboundReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

JenaReasoningService (org.apache.stanbol.reasoners.jena.JenaReasoningService)2 OWLApiReasoningService (org.apache.stanbol.reasoners.owlapi.OWLApiReasoningService)2 Model (com.hp.hpl.jena.rdf.model.Model)1 Statement (com.hp.hpl.jena.rdf.model.Statement)1 Rule (com.hp.hpl.jena.reasoner.rulesys.Rule)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)1 ReasoningServiceInputProvider (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputProvider)1 UnboundReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException)1 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)1 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)1 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)1 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)1