Search in sources :

Example 1 with ChainException

use of org.apache.stanbol.enhancer.servicesapi.ChainException in project stanbol by apache.

the class GenericEnhancerUiResource method getExecutionNodes.

/**
 * Getter for the executionNodes
 *
 * @return
 */
public Set<ExecutionNode> getExecutionNodes() {
    if (_executionNodes == null) {
        ImmutableGraph ep;
        try {
            ep = chain.getExecutionPlan();
        } catch (ChainException e) {
            ep = null;
        }
        if (ep != null) {
            _executionNodes = new LinkedHashSet<ExecutionNode>();
            Set<BlankNodeOrIRI> processed = new HashSet<BlankNodeOrIRI>();
            Set<BlankNodeOrIRI> next;
            do {
                next = ExecutionPlanHelper.getExecutable(ep, processed);
                for (BlankNodeOrIRI node : next) {
                    _executionNodes.add(new ExecutionNode(ep, node));
                }
                processed.addAll(next);
            } while (!next.isEmpty());
        }
    }
    return _executionNodes;
}
Also used : BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with ChainException

use of org.apache.stanbol.enhancer.servicesapi.ChainException in project stanbol by apache.

the class EventJobManagerImpl method enhanceContent.

@Override
public void enhanceContent(ContentItem ci, Chain chain) throws EnhancementException {
    if (ci == null) {
        throw new IllegalArgumentException("The parsed contentItem MUST NOT be NULL!");
    }
    if (chain == null) {
        throw new IllegalArgumentException("Unable to enhance ContentItem '" + ci.getUri() + "' because NULL was passed as enhancement chain");
    }
    long start = System.currentTimeMillis();
    enhancementJobManagerLog.debug(">> enhance {} with chain {}", ci.getUri(), chain.getName());
    boolean isDefaultChain = chain.equals(chainManager.getDefault());
    EnhancementJob job = new EnhancementJob(ci, chain.getName(), chain.getExecutionPlan(), isDefaultChain);
    // start the execution
    // wait for the results
    EnhancementJobObserver observer = jobHandler.register(job);
    // now wait for the execution to finish for the configured maximum time
    boolean completed = observer.waitForCompletion(maxEnhancementJobWaitTime);
    if (!completed) {
        // throw timeout exception
        StringBuilder sb = new StringBuilder("Status:\n");
        ExecutionMetadata em = ExecutionMetadata.parseFrom(job.getExecutionMetadata(), ci.getUri());
        for (Entry<String, Execution> ex : em.getEngineExecutions().entrySet()) {
            sb.append("  -").append(ex.getKey()).append(": ").append(ex.getValue().getStatus()).append('\n');
        }
        throw new ChainException("Execution timeout after " + ((System.currentTimeMillis() - start) / 1000f) + "sec (timeout:" + (maxEnhancementJobWaitTime / 1000) + "sec) for ContentItem " + ci.getUri() + "\n" + sb.toString() + " \n To change the timeout change value of property '" + MAX_ENHANCEMENT_JOB_WAIT_TIME + "' for the service " + getClass());
    }
    log.info("Execution of Chain {} {} after {}ms for ContentItem {}", new Object[] { chain.getName(), job.isFailed() ? "failed" : "finished", System.currentTimeMillis() - start, job.getContentItem().getUri() });
    // ci.getMetadata().addAll(job.getExecutionMetadata());
    if (job.isFailed()) {
        Exception e = job.getError();
        EnhancementJobHandler.logJobInfo(log, job, null, true);
        logExecutionMetadata(enhancementJobManagerLog, job, true);
        log.warn("ExecutionMetadata: ");
        for (Iterator<Triple> it = job.getExecutionMetadata().iterator(); it.hasNext(); log.warn(it.next().toString())) ;
        if (e instanceof SecurityException) {
            throw (SecurityException) e;
        } else if (e instanceof EnhancementException) {
            throw (EnhancementException) e;
        } else {
            throw new ChainException(job.getErrorMessage(), e);
        }
    }
    if (!job.isFinished()) {
        log.warn("Execution finished, but Job is not finished!");
        EnhancementJobHandler.logJobInfo(log, job, null, true);
        logExecutionMetadata(log, job, true);
        throw new ChainException("EnhancementJobManager was deactivated while" + " enhancing the passed ContentItem " + job.getContentItem() + " (EnhancementJobManager type: " + getClass() + ")");
    } else {
        // log infos about the execution times to the enhancementJobManager
        EnhancementJobHandler.logExecutionTimes(enhancementJobManagerLog, job);
        logExecutionMetadata(enhancementJobManagerLog, job, false);
    }
}
Also used : EnhancementException(org.apache.stanbol.enhancer.servicesapi.EnhancementException) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) EnhancementException(org.apache.stanbol.enhancer.servicesapi.EnhancementException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Triple(org.apache.clerezza.commons.rdf.Triple) ExecutionMetadata(org.apache.stanbol.enhancer.servicesapi.helper.execution.ExecutionMetadata) Execution(org.apache.stanbol.enhancer.servicesapi.helper.execution.Execution) EnhancementJobObserver(org.apache.stanbol.enhancer.jobmanager.event.impl.EnhancementJobHandler.EnhancementJobObserver) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException)

Example 3 with ChainException

use of org.apache.stanbol.enhancer.servicesapi.ChainException in project stanbol by apache.

the class AbstractEnhancerResource method getExecutionPlan.

/*@OPTIONS
    public Response handleCorsPreflight(@Context HttpHeaders headers) {
        ResponseBuilder res = Response.ok();
        enableCORS(servletContext, res, headers);
        return res.build();
    }

    @OPTIONS
    @Path("/ep")
    public Response handleEpCorsPreflight(@Context HttpHeaders headers) {
        ResponseBuilder res = Response.ok();
        enableCORS(servletContext, res, headers,HttpMethod.OPTIONS,HttpMethod.GET);
        return res.build();
    }*/
@GET
@Path("/ep")
@Produces(value = { JSON_LD, APPLICATION_JSON, N3, N_TRIPLE, RDF_JSON, RDF_XML, TURTLE, X_TURTLE })
public Response getExecutionPlan(@Context HttpHeaders headers) {
    ResponseBuilder res;
    Chain chain = null;
    try {
        chain = getChain();
        res = Response.ok(chain.getExecutionPlan());
    } catch (ChainException e) {
        String chainName = chain == null ? "" : ("'" + chain.getName() + "' ");
        res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("The Enhancement Chain " + chainName + "is currently" + "not executeable (message: " + e.getMessage() + ")!");
    }
    // addCORSOrigin(servletContext, res, headers);
    return res.build();
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with ChainException

use of org.apache.stanbol.enhancer.servicesapi.ChainException in project stanbol by apache.

the class EventJobManagerImpl method getActiveEngines.

@Override
public List<EnhancementEngine> getActiveEngines() {
    // This implementation return the list of active engined for the default
    // Chain in the order they would be executed
    Chain defaultChain = chainManager.getDefault();
    if (defaultChain == null) {
        throw new IllegalStateException("Currently no enhancement chain is " + "active. Please configure a Chain or enable the default chain");
    }
    ImmutableGraph ep;
    try {
        ep = defaultChain.getExecutionPlan();
    } catch (ChainException e) {
        throw new IllegalStateException("Unable to get Execution Plan for " + "default enhancement chain (name: '" + defaultChain.getName() + "'| class: '" + defaultChain.getClass() + "')!", e);
    }
    return ExecutionPlanHelper.getActiveEngines(engineManager, ep);
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 5 with ChainException

use of org.apache.stanbol.enhancer.servicesapi.ChainException in project stanbol by apache.

the class EventJobManagerImpl method enhanceContent.

@Override
public void enhanceContent(ContentItem ci) throws EnhancementException {
    Chain defaultChain = chainManager.getDefault();
    if (defaultChain == null) {
        throw new ChainException("Unable to enhance ContentItem '" + ci.getUri() + "' because currently no enhancement chain is active. Please" + "configure a Chain or enable the default chain");
    }
    enhanceContent(ci, defaultChain);
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException)

Aggregations

ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)8 Chain (org.apache.stanbol.enhancer.servicesapi.Chain)4 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)2 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)2 Triple (org.apache.clerezza.commons.rdf.Triple)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)1 EnhancementJobObserver (org.apache.stanbol.enhancer.jobmanager.event.impl.EnhancementJobHandler.EnhancementJobObserver)1 EnhancementEngine (org.apache.stanbol.enhancer.servicesapi.EnhancementEngine)1 EnhancementException (org.apache.stanbol.enhancer.servicesapi.EnhancementException)1 EnhancementEngineHelper.getString (org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString)1