Search in sources :

Example 6 with LifeCycleException

use of org.dkpro.lab.engine.LifeCycleException in project dkpro-lab by dkpro.

the class BatchTaskEngine method run.

@Override
public String run(Task aConfiguration) throws ExecutionException, LifeCycleException {
    if (!(aConfiguration instanceof BatchTask)) {
        throw new ExecutionException("This engine can only execute [" + BatchTask.class.getName() + "]");
    }
    // Create persistence service for injection into analysis components
    TaskContext ctx = null;
    try {
        ctx = contextFactory.createContext(aConfiguration);
        // Now the setup is complete
        ctx.getLifeCycleManager().initialize(ctx, aConfiguration);
        // Start recording
        ctx.getLifeCycleManager().begin(ctx, aConfiguration);
        try {
            BatchTask cfg = (BatchTask) aConfiguration;
            ParameterSpace parameterSpace = cfg.getParameterSpace();
            // Try to calculate the parameter space size.
            int estimatedSize = 1;
            for (Dimension<?> d : parameterSpace.getDimensions()) {
                if (d instanceof FixedSizeDimension) {
                    FixedSizeDimension fsd = (FixedSizeDimension) d;
                    if (fsd.size() > 0) {
                        estimatedSize *= fsd.size();
                    }
                }
            }
            // A subtask execution may apply to multiple parameter space coordinates!
            Set<String> executedSubtasks = new LinkedHashSet<String>();
            ProgressMeter progress = new ProgressMeter(estimatedSize);
            for (Map<String, Object> config : parameterSpace) {
                if (cfg.getConfiguration() != null) {
                    for (Entry<String, Object> e : cfg.getConfiguration().entrySet()) {
                        if (!config.containsKey(e.getKey())) {
                            config.put(e.getKey(), e.getValue());
                        }
                    }
                }
                log.info("== Running new configuration [" + ctx.getId() + "] ==");
                List<String> keys = new ArrayList<String>(config.keySet());
                for (String key : keys) {
                    log.info("[" + key + "]: [" + StringUtils.abbreviateMiddle(Util.toString(config.get(key)), "…", 150) + "]");
                }
                executeConfiguration(cfg, ctx, config, executedSubtasks);
                progress.next();
                log.info("Completed configuration " + progress);
            }
            // Set the subtask property and persist again, so the property is available to
            // reports
            cfg.setAttribute(SUBTASKS_KEY, executedSubtasks.toString());
            cfg.persist(ctx);
        } catch (LifeCycleException e) {
            ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
            throw e;
        } catch (UnresolvedImportException e) {
            // HACK - pass unresolved import exceptions up to the outer batch task
            ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
            throw e;
        } catch (Throwable e) {
            ctx.getLifeCycleManager().fail(ctx, aConfiguration, e);
            throw new ExecutionException(e);
        }
        // End recording (here the reports will nbe done)
        ctx.getLifeCycleManager().complete(ctx, aConfiguration);
        return ctx.getId();
    } finally {
        if (ctx != null) {
            ctx.getLifeCycleManager().destroy(ctx, aConfiguration);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TaskContext(org.dkpro.lab.engine.TaskContext) BatchTask(org.dkpro.lab.task.BatchTask) ArrayList(java.util.ArrayList) LifeCycleException(org.dkpro.lab.engine.LifeCycleException) FixedSizeDimension(org.dkpro.lab.task.FixedSizeDimension) UnresolvedImportException(org.dkpro.lab.storage.UnresolvedImportException) ParameterSpace(org.dkpro.lab.task.ParameterSpace) ProgressMeter(org.dkpro.lab.ProgressMeter) ExecutionException(org.dkpro.lab.engine.ExecutionException)

Example 7 with LifeCycleException

use of org.dkpro.lab.engine.LifeCycleException in project dkpro-lab by dkpro.

the class DefaultLifeCycleManager method complete.

@Override
public void complete(TaskContext aContext, Task aConfiguration) throws LifeCycleException {
    aContext.getMetadata().setEnd(System.currentTimeMillis());
    aContext.message("Completing task [" + aConfiguration.getType() + "]");
    aContext.message("Running reports for task [" + aConfiguration.getType() + "]");
    List<Report> reports = new ArrayList<Report>(aConfiguration.getReports());
    int i = 1;
    for (Report report : reports) {
        for (int g = 0; g < 3; g++) {
            System.gc();
        }
        try {
            aContext.message("Starting report [" + report.getClass().getName() + "] (" + i + "/" + reports.size() + ")");
            report.setContext(aContext);
            report.execute();
            aContext.message("Report complete [" + report.getClass().getName() + "] (" + i + "/" + reports.size() + ")");
        } catch (Exception e) {
            aContext.error("Report failed [" + report.getClass().getName() + "] (" + i + "/" + reports.size() + ")", e);
            throw new LifeCycleException(e);
        } finally {
            i++;
        }
    }
    // potentially incomplete version of this file has to be deleted.
    try {
        aContext.storeBinary(TaskContextMetadata.METADATA_KEY, aContext.getMetadata());
    } catch (Throwable e) {
        aContext.getStorageService().delete(aContext.getId(), TaskContextMetadata.METADATA_KEY);
        throw new LifeCycleException("Unable to write [" + TaskContextMetadata.METADATA_KEY + "] to mark context as complete.", e);
    }
    aContext.message("Completed task [" + aConfiguration.getType() + "]");
}
Also used : Report(org.dkpro.lab.reporting.Report) ArrayList(java.util.ArrayList) LifeCycleException(org.dkpro.lab.engine.LifeCycleException) IOException(java.io.IOException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) LifeCycleException(org.dkpro.lab.engine.LifeCycleException)

Aggregations

LifeCycleException (org.dkpro.lab.engine.LifeCycleException)7 ExecutionException (org.dkpro.lab.engine.ExecutionException)5 TaskContext (org.dkpro.lab.engine.TaskContext)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 UIMAFramework.newDefaultResourceManager (org.apache.uima.UIMAFramework.newDefaultResourceManager)3 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)3 ResourceManager (org.apache.uima.resource.ResourceManager)3 UimaTask (org.dkpro.lab.uima.task.UimaTask)3 UnresolvedImportException (org.dkpro.lab.storage.UnresolvedImportException)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 UIMAFramework.produceCollectionReader (org.apache.uima.UIMAFramework.produceCollectionReader)1 UimaContextAdmin (org.apache.uima.UimaContextAdmin)1 AnalysisEngine (org.apache.uima.analysis_engine.AnalysisEngine)1 AggregateAnalysisEngine_impl (org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl)1 PrimitiveAnalysisEngine_impl (org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl)1 CAS (org.apache.uima.cas.CAS)1 Feature (org.apache.uima.cas.Feature)1 CollectionProcessingEngine (org.apache.uima.collection.CollectionProcessingEngine)1