Search in sources :

Example 36 with ProcessException

use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.

the class ProcessJob method execute.

@Override
public void execute(final JobExecutionContext jec) throws JobExecutionException {
    final JobDataMap parameters = jec.getJobDetail().getJobDataMap();
    final Object objFactoryId = parameters.get(KEY_FACTORY_ID);
    final Object objProcessId = parameters.get(KEY_PROCESS_ID);
    final Object objProcessParams = parameters.get(KEY_PARAMETERS);
    final Object objProcess = parameters.get(KEY_PROCESS);
    if (!(objFactoryId instanceof String)) {
        throw new JobExecutionException("Factory id is not String, value found : " + objFactoryId);
    }
    if (!(objProcessId instanceof String)) {
        throw new JobExecutionException("Process id is not String, value found : " + objProcessId);
    }
    if (!(objProcessParams instanceof ParameterValueGroup)) {
        throw new JobExecutionException("Parameters is not an ISO parameter, value found : " + objProcessParams);
    }
    if (objProcess != null && !(objProcess instanceof Process)) {
        throw new JobExecutionException("Process object is invalid, value found : " + objProcess);
    }
    final String factoryId = (String) objFactoryId;
    final String processId = (String) objProcessId;
    final Parameters params = Parameters.castOrWrap((ParameterValueGroup) objProcessParams);
    process = (Process) objProcess;
    if (process == null) {
        final ProcessDescriptor desc = getProcessDescriptor(factoryId, processId);
        process = desc.createProcess(params);
    }
    final StoreExceptionMonitor monitor = new StoreExceptionMonitor();
    process.addListener(monitor);
    for (ProcessListener pl : listeners) {
        process.addListener(pl);
    }
    // set the result int he context, for listener that might want it.
    final ParameterValueGroup result;
    try {
        result = process.call();
    } catch (ProcessException ex) {
        if (monitor.failed != null) {
            throw monitor.failed;
        } else {
            throw new JobExecutionException(ex);
        }
    }
    jec.setResult(result);
}
Also used : JobDataMap(org.quartz.JobDataMap) ProcessException(org.geotoolkit.process.ProcessException) JobExecutionException(org.quartz.JobExecutionException) Parameters(org.apache.sis.parameter.Parameters) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ProcessListener(org.geotoolkit.process.ProcessListener) AbstractProcess(org.geotoolkit.processing.AbstractProcess) Process(org.geotoolkit.process.Process) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor)

Example 37 with ProcessException

use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.

the class ChainProcess method executeConditionalElement.

private boolean executeConditionalElement(final ElementCondition condition, final ParameterValueGroup inputs) throws ProcessException {
    final FilterFactory ff = FilterUtilities.FF;
    final String statement = condition.getExpression();
    final String syntax = condition.getSyntax();
    Filter filter = null;
    if ("CQL".equalsIgnoreCase(syntax)) {
        try {
            filter = CQL.parseFilter(statement);
        } catch (Exception ex) {
        // maybe it's just an expression
        }
        if (filter == null) {
            try {
                final Expression exp = CQL.parseExpression(statement);
                filter = ff.equal(exp, ff.literal(true));
            } catch (Exception ex) {
                throw new ProcessException("Unvalid CQL : " + statement, this, null);
            }
        }
    } else if ("JAVASCRIPT".equalsIgnoreCase(syntax)) {
        final Expression exp = ff.function(JavaScriptFunctionFactory.JAVASCRIPT, ff.literal(statement));
        filter = ff.equal(exp, ff.literal(true));
    } else if ("GROOVY".equalsIgnoreCase(syntax)) {
        final Expression exp = ff.function(GroovyFunctionFactory.GROOVY, ff.literal(statement));
        filter = ff.equal(exp, ff.literal(true));
    } else {
        throw new ProcessException("Unknwoned syntax : " + syntax + " supported sysntaxes are : CQL,Javascript,Groovy", this, null);
    }
    return filter.test(inputs);
}
Also used : ProcessException(org.geotoolkit.process.ProcessException) Filter(org.opengis.filter.Filter) Expression(org.opengis.filter.Expression) FilterFactory(org.opengis.filter.FilterFactory) ProcessException(org.geotoolkit.process.ProcessException) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException)

Example 38 with ProcessException

use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.

the class ChainProcess method execute.

/**
 * {@inheritDoc}
 */
@Override
protected void execute() throws ProcessException {
    final Chain model = getDescriptor().getModel();
    // processing progress
    final float workLoadPart = 100 / model.getElements().size();
    float currentProgress = 0;
    int i = 1;
    final Collection<FlowNode> nodes = Flow.createFlow(model);
    List<List<FlowNode>> ranked = Flow.sortByRank(nodes);
    // prepare all parameters for each process step
    final Map<Integer, ParameterValueGroup> configs = new HashMap<>();
    for (FlowNode node : nodes) {
        final Object obj = node.getObject();
        if (obj == ElementProcess.END) {
            configs.put(Integer.MAX_VALUE, outputParameters);
        } else if (obj == ElementProcess.BEGIN) {
        // do nothing
        } else if (obj instanceof ElementProcess) {
            final ElementProcess element = (ElementProcess) obj;
            final ProcessDescriptor desc;
            try {
                desc = getProcessDescriptor(element);
            } catch (NoSuchIdentifierException ex) {
                throw new ProcessException("Sub process " + element.getAuthority() + "." + element.getCode() + " not found.", this, ex);
            }
            configs.put(element.getId(), desc.getInputDescriptor().createValue());
        } else if (obj instanceof ElementCondition) {
            final ElementCondition element = (ElementCondition) obj;
            if (element.getFailed().isEmpty() || element.getSuccess().isEmpty()) {
                throw new ProcessException("A conditional element should have at least one success AND one failed execution link.", this, null);
            }
            configs.put(element.getId(), ChainProcessDescriptor.createParams(element.getInputs(), "conditionInput", true).createValue());
        }
    }
    // set all constant values in configurations
    for (Constant cst : model.getConstants()) {
        // copy constant in children nodes
        final Object value = ConstantUtilities.stringToValue(cst.getValue(), cst.getType());
        for (DataLink link : model.getInputLinks(cst.getId())) {
            setValue(value, configs.get(link.getTargetId()).parameter(link.getTargetCode()));
        }
    }
    // Will contain all the versions of processes used
    final StringBuilder processVersion = new StringBuilder();
    // run processes in order
    for (int j = 0; j < ranked.size(); j++) {
        final List<FlowNode> rank = ranked.get(j);
        currentProgress = (i - 1) * workLoadPart;
        for (FlowNode node : rank) {
            final Object obj = node.getObject();
            if (obj == ElementProcess.BEGIN) {
                // copy input params in children nodes
                for (DataLink link : model.getInputLinks(Integer.MIN_VALUE)) {
                    List<ParameterValue> values = getValues(inputParameters, link.getSourceCode());
                    boolean first = true;
                    for (ParameterValue paramValue : values) {
                        if (first) {
                            final Object value = paramValue.getValue();
                            setValue(value, configs.get(link.getTargetId()).parameter(link.getTargetCode()));
                            first = false;
                        } else {
                            final Object value = paramValue.getValue();
                            final ParameterDescriptor desc = (ParameterDescriptor) configs.get(link.getTargetId()).getDescriptor().descriptor(link.getTargetCode());
                            final ParameterValue newParam = new DefaultParameterValue(desc);
                            setValue(value, newParam);
                            configs.get(link.getTargetId()).values().add(newParam);
                        }
                    }
                }
            } else if (obj == ElementProcess.END) {
            // do nothing
            } else if (obj instanceof ElementProcess) {
                // handle process cancel
                stopIfDismissed();
                // handle process pause
                if (isPaused()) {
                    fireProcessPaused(descriptor.getIdentifier().getCode() + " paused", currentProgress);
                    while (isPaused()) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ex) {
                            LOGGER.log(Level.WARNING, "Interruption while process is in pause", ex);
                        }
                    }
                    fireProcessResumed(descriptor.getIdentifier().getCode() + " resumed", currentProgress);
                }
                // execute process
                final ElementProcess element = (ElementProcess) obj;
                final ParameterValueGroup config = configs.get(element.getId());
                final ProcessDescriptor pdesc;
                try {
                    pdesc = getProcessDescriptor(element);
                } catch (NoSuchIdentifierException ex) {
                    throw new ProcessException("Sub process not found", this, ex);
                }
                currentProcess = pdesc.createProcess(config);
                currentProcess.addListener(new ForwardProcessListener(this, currentProgress, workLoadPart));
                if (currentProcess instanceof AbstractProcess) {
                    ((AbstractProcess) currentProcess).setJobId(jobId);
                }
                final String processId = pdesc.getIdentifier().getCode();
                // Fill process version with values coming from the current process.
                if (processVersion.length() > 0) {
                    processVersion.append(", ");
                }
                processVersion.append(processId).append(" ");
                if (currentProcess.getDescriptor() instanceof AbstractProcessDescriptor) {
                    processVersion.append(((AbstractProcessDescriptor) currentProcess.getDescriptor()).getVersion());
                } else {
                    processVersion.append("1.0");
                }
                final ParameterValueGroup result = currentProcess.call();
                // fireProgressing(pdesc.getIdentifier().getCode() + " completed", i * part, false);
                i++;
                // set result in children
                for (DataLink link : model.getInputLinks(element.getId())) {
                    final List<ParameterValue> values = getValues(result, link.getSourceCode());
                    boolean first = true;
                    for (ParameterValue paramValue : values) {
                        if (first) {
                            final Object value = paramValue.getValue();
                            setValue(value, configs.get(link.getTargetId()).parameter(link.getTargetCode()));
                            first = false;
                        } else {
                            final Object value = paramValue.getValue();
                            final ParameterDescriptor desc = (ParameterDescriptor) configs.get(link.getTargetId()).getDescriptor().descriptor(link.getTargetCode());
                            final ParameterValue newParam = new DefaultParameterValue(desc);
                            setValue(value, newParam);
                            configs.get(link.getTargetId()).values().add(newParam);
                        }
                    }
                }
            } else if (obj instanceof ElementCondition) {
                final ElementCondition condition = (ElementCondition) obj;
                final Boolean result = executeConditionalElement(condition, configs.get(condition.getId()));
                final Chain updateModel = new Chain(model);
                if (result) {
                    updateModel.getFlowLinks().removeAll(condition.getFailed());
                } else {
                    updateModel.getFlowLinks().removeAll(condition.getSuccess());
                }
                ranked = Flow.sortByRank(Flow.createFlow(updateModel));
            }
        }
    }
}
Also used : ElementProcess(org.geotoolkit.processing.chain.model.ElementProcess) DefaultParameterValue(org.apache.sis.parameter.DefaultParameterValue) Chain(org.geotoolkit.processing.chain.model.Chain) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) HashMap(java.util.HashMap) Constant(org.geotoolkit.processing.chain.model.Constant) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) AbstractProcessDescriptor(org.geotoolkit.processing.AbstractProcessDescriptor) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) ArrayList(java.util.ArrayList) List(java.util.List) DataLink(org.geotoolkit.processing.chain.model.DataLink) ParameterValue(org.opengis.parameter.ParameterValue) DefaultParameterValue(org.apache.sis.parameter.DefaultParameterValue) GeneralParameterValue(org.opengis.parameter.GeneralParameterValue) AbstractProcess(org.geotoolkit.processing.AbstractProcess) ForwardProcessListener(org.geotoolkit.processing.ForwardProcessListener) ProcessException(org.geotoolkit.process.ProcessException) ElementCondition(org.geotoolkit.processing.chain.model.ElementCondition) AbstractProcessDescriptor(org.geotoolkit.processing.AbstractProcessDescriptor) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor)

Example 39 with ProcessException

use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.

the class Predictor method write.

private void write(final List<Output> outputs, final GridGeometry grid, final Instant startTime, final Path outputFile) throws IOException, InvalidRangeException, ProcessException {
    MathTransform grid2Crs = grid.getGridToCRS(PixelInCell.CELL_CENTER);
    if (!(grid2Crs instanceof AffineTransform)) {
        throw new ProcessException("Unsupported case: grid 2 crs is not affine", this);
    }
    final AffineTransform g2c = (AffineTransform) grid2Crs;
    Output.write(outputs, grid.getCoordinateReferenceSystem(), startTime.toEpochMilli(), g2c.getTranslateX(), g2c.getTranslateY(), g2c.getScaleX(), g2c.getScaleY(), outputFile.toString());
}
Also used : ProcessException(org.geotoolkit.process.ProcessException) MathTransform(org.opengis.referencing.operation.MathTransform) AffineTransform(java.awt.geom.AffineTransform)

Example 40 with ProcessException

use of org.geotoolkit.process.ProcessException in project geotoolkit by Geomatys.

the class Predictor method initData.

private MeteoDataset initData() throws ProcessException {
    final GridCoverageResource wind = inputParameters.getMandatoryValue(WIND_RESOURCE);
    final GridCoverageResource current = inputParameters.getMandatoryValue(CURRENT_RESOURCE);
    try {
        return new SimpleMeteoDataset(new SimpleUVSource(wind), new SimpleUVSource(current));
    } catch (DataStoreException e) {
        throw new ProcessException("Incompatible data source", this, e);
    }
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) ProcessException(org.geotoolkit.process.ProcessException) GridCoverageResource(org.apache.sis.storage.GridCoverageResource)

Aggregations

ProcessException (org.geotoolkit.process.ProcessException)70 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)27 Geometry (org.locationtech.jts.geom.Geometry)26 FactoryException (org.opengis.util.FactoryException)24 TransformException (org.opengis.referencing.operation.TransformException)21 RenderedImage (java.awt.image.RenderedImage)15 DataStoreException (org.apache.sis.storage.DataStoreException)13 IOException (java.io.IOException)12 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)12 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)11 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)11 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)11 BufferedImage (java.awt.image.BufferedImage)10 ArrayList (java.util.ArrayList)10 PixelIterator (org.apache.sis.image.PixelIterator)9 List (java.util.List)8 Parameters (org.apache.sis.parameter.Parameters)8 DismissProcessException (org.geotoolkit.process.DismissProcessException)8 WritablePixelIterator (org.apache.sis.image.WritablePixelIterator)7 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)7