Search in sources :

Example 1 with ElementProcess

use of org.geotoolkit.processing.chain.model.ElementProcess in project geotoolkit by Geomatys.

the class ChainProcessDemo method main.

public static void main(String[] args) throws ProcessException {
    // produce a chain equivalent to :  ($a + 10) / $b
    final Chain chain = new Chain("myChain");
    // input/out/constants parameters
    final Parameter a = chain.addInputParameter("a", Double.class, "title", "desc", 1, 1, null);
    final Parameter b = chain.addInputParameter("b", Double.class, "title", "desc", 1, 1, null);
    final Parameter r = chain.addOutputParameter("r", Double.class, "title", "desc", 1, 1, null);
    final Constant c = chain.addConstant(1, Double.class, 10d);
    // chain blocks
    final ElementProcess add = chain.addProcessElement(2, "math", "add");
    final ElementProcess divide = chain.addProcessElement(3, "math", "divide");
    // execution flow links
    chain.addFlowLink(Element.BEGIN.getId(), add.getId());
    chain.addFlowLink(add.getId(), divide.getId());
    chain.addFlowLink(divide.getId(), Element.END.getId());
    // data flow links
    chain.addDataLink(Element.BEGIN.getId(), a.getCode(), add.getId(), "first");
    chain.addDataLink(c.getId(), "", add.getId(), "second");
    chain.addDataLink(add.getId(), "result", divide.getId(), "first");
    chain.addDataLink(Element.BEGIN.getId(), b.getCode(), divide.getId(), "second");
    chain.addDataLink(divide.getId(), "result", Element.END.getId(), r.getCode());
    // ////////////////// execute the chain /////////////////////////////////
    // create a process descriptor to use it like any process.
    final ProcessDescriptor desc = new ChainProcessDescriptor(chain, new DefaultDataIdentification());
    // input params
    final ParameterValueGroup input = desc.getInputDescriptor().createValue();
    input.parameter("a").setValue(15d);
    input.parameter("b").setValue(2d);
    final org.geotoolkit.process.Process process = desc.createProcess(input);
    final ParameterValueGroup result = process.call();
    System.out.println(result.parameter("r").getValue());
}
Also used : ElementProcess(org.geotoolkit.processing.chain.model.ElementProcess) Chain(org.geotoolkit.processing.chain.model.Chain) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) Constant(org.geotoolkit.processing.chain.model.Constant) ChainProcessDescriptor(org.geotoolkit.processing.chain.ChainProcessDescriptor) Parameter(org.geotoolkit.processing.chain.model.Parameter) DefaultDataIdentification(org.apache.sis.metadata.iso.identification.DefaultDataIdentification) ChainProcessDescriptor(org.geotoolkit.processing.chain.ChainProcessDescriptor) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor)

Example 2 with ElementProcess

use of org.geotoolkit.processing.chain.model.ElementProcess in project geotoolkit by Geomatys.

the class EventChain method getDataType.

/**
 * Return the Java class of a parameter or constant.
 *
 * @param obj Could be a Parameter, Constant or ChainElement
 * @param in in case of ChainElement obj, search code in input or output parameters. True to search in inputs, false for outputs.
 * @param code of the parameter in case of ChainElement obj.
 * @return Class or ClassFull for Parameter.
 */
private Object getDataType(final Object obj, final boolean in, final String code) {
    if (obj instanceof Parameter) {
        final Parameter dto = (Parameter) obj;
        return dto.getType();
    } else if (obj instanceof Constant) {
        final Constant dto = (Constant) obj;
        return dto.getType();
    } else if (obj instanceof ElementProcess) {
        final ElementProcess dto = (ElementProcess) obj;
        try {
            final ProcessDescriptor pd = ProcessFinder.getProcessDescriptor(getFactories().iterator(), dto.getAuthority(), dto.getCode());
            final GeneralParameterDescriptor gd = (in) ? pd.getInputDescriptor().descriptor(code) : pd.getOutputDescriptor().descriptor(code);
            if (gd instanceof ExtendedParameterDescriptor) {
                Object type = ((ExtendedParameterDescriptor) gd).getUserObject().get(ChainProcessDescriptor.KEY_DISTANT_CLASS);
                if (type == null) {
                    // rely on base type
                    type = ((ExtendedParameterDescriptor) gd).getValueClass();
                }
                return type;
            } else if (gd instanceof ParameterDescriptor) {
                final Object type = ((ParameterDescriptor) gd).getValueClass();
                return type;
            }
        } catch (NoSuchIdentifierException ex) {
            return null;
        }
    }
    return null;
}
Also used : ElementProcess(org.geotoolkit.processing.chain.model.ElementProcess) Constant(org.geotoolkit.processing.chain.model.Constant) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) Parameter(org.geotoolkit.processing.chain.model.Parameter) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) ChainProcessDescriptor(org.geotoolkit.processing.chain.ChainProcessDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor)

Example 3 with ElementProcess

use of org.geotoolkit.processing.chain.model.ElementProcess 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 4 with ElementProcess

use of org.geotoolkit.processing.chain.model.ElementProcess in project geotoolkit by Geomatys.

the class ChainProcessTest method createBranchChain.

private Chain createBranchChain() {
    // produce a chain equivalent to :  (($a+10) > 20) ? *10 : /10
    final Chain chain = new Chain("branchChain");
    int id = 1;
    // input/out/constants parameters
    final Parameter a = chain.addInputParameter("a", Double.class, "title", "desc", 1, 1, null);
    final Parameter r = chain.addOutputParameter("r", Double.class, "title", "desc", 1, 1, null);
    final Constant c10 = chain.addConstant(id++, Double.class, 10d);
    // chain blocks
    final ElementProcess add = chain.addProcessElement(id++, "demo", "add");
    final ElementProcess multi = chain.addProcessElement(id++, "demo", "multiply");
    final ElementProcess divide = chain.addProcessElement(id++, "demo", "divide");
    final ElementCondition condition = chain.addConditionElement(id++);
    condition.getInputs().add(new Parameter("value", Double.class, "", "", 1, 1));
    condition.setSyntax("CQL");
    condition.setExpression("value > 20");
    // execution flow links
    chain.addFlowLink(BEGIN.getId(), add.getId());
    chain.addFlowLink(add.getId(), condition.getId());
    final FlowLink success = chain.addFlowLink(condition.getId(), multi.getId());
    condition.getSuccess().add(success);
    final FlowLink fail = chain.addFlowLink(condition.getId(), divide.getId());
    condition.getFailed().add(fail);
    chain.addFlowLink(divide.getId(), END.getId());
    chain.addFlowLink(multi.getId(), END.getId());
    // data flow links
    chain.addDataLink(c10.getId(), "", add.getId(), "second");
    chain.addDataLink(c10.getId(), "", multi.getId(), "second");
    chain.addDataLink(c10.getId(), "", divide.getId(), "second");
    chain.addDataLink(BEGIN.getId(), a.getCode(), add.getId(), "first");
    chain.addDataLink(add.getId(), "result", condition.getId(), "value");
    chain.addDataLink(add.getId(), "result", multi.getId(), "first");
    chain.addDataLink(add.getId(), "result", divide.getId(), "first");
    chain.addDataLink(divide.getId(), "result", END.getId(), r.getCode());
    chain.addDataLink(multi.getId(), "result", END.getId(), r.getCode());
    return chain;
}
Also used : ElementProcess(org.geotoolkit.processing.chain.model.ElementProcess) Chain(org.geotoolkit.processing.chain.model.Chain) Constant(org.geotoolkit.processing.chain.model.Constant) ElementCondition(org.geotoolkit.processing.chain.model.ElementCondition) FlowLink(org.geotoolkit.processing.chain.model.FlowLink) Parameter(org.geotoolkit.processing.chain.model.Parameter)

Example 5 with ElementProcess

use of org.geotoolkit.processing.chain.model.ElementProcess in project geotoolkit by Geomatys.

the class ChainProcessTest method createSimpleChain.

private Chain createSimpleChain() {
    // produce a chain equivalent to :  ($1 + 10) / $2
    final Chain chain = new Chain("myChain");
    int id = 1;
    // input/out/constants parameters
    final Parameter a = chain.addInputParameter("a", Double.class, "title", "desc", 1, 1, 2.0);
    final Parameter b = chain.addInputParameter("b", Double.class, "title", "desc", 1, 1, 3.0);
    final Parameter r = chain.addOutputParameter("r", Double.class, "title", "desc", 1, 1, null);
    final Constant c = chain.addConstant(id++, Double.class, 10d);
    // chain blocks
    final ElementProcess add = chain.addProcessElement(id++, "demo", "add");
    final ElementProcess divide = chain.addProcessElement(id++, "demo", "divide");
    // execution flow links
    chain.addFlowLink(BEGIN.getId(), add.getId());
    chain.addFlowLink(add.getId(), divide.getId());
    chain.addFlowLink(divide.getId(), END.getId());
    // data flow links
    chain.addDataLink(BEGIN.getId(), a.getCode(), add.getId(), "first");
    chain.addDataLink(c.getId(), "", add.getId(), "second");
    chain.addDataLink(add.getId(), "result", divide.getId(), "first");
    chain.addDataLink(BEGIN.getId(), b.getCode(), divide.getId(), "second");
    chain.addDataLink(divide.getId(), "result", END.getId(), r.getCode());
    return chain;
}
Also used : ElementProcess(org.geotoolkit.processing.chain.model.ElementProcess) Chain(org.geotoolkit.processing.chain.model.Chain) Constant(org.geotoolkit.processing.chain.model.Constant) Parameter(org.geotoolkit.processing.chain.model.Parameter)

Aggregations

Constant (org.geotoolkit.processing.chain.model.Constant)5 ElementProcess (org.geotoolkit.processing.chain.model.ElementProcess)5 Chain (org.geotoolkit.processing.chain.model.Chain)4 Parameter (org.geotoolkit.processing.chain.model.Parameter)4 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)3 ChainProcessDescriptor (org.geotoolkit.processing.chain.ChainProcessDescriptor)2 ElementCondition (org.geotoolkit.processing.chain.model.ElementCondition)2 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)2 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)2 NoSuchIdentifierException (org.opengis.util.NoSuchIdentifierException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DefaultDataIdentification (org.apache.sis.metadata.iso.identification.DefaultDataIdentification)1 DefaultParameterValue (org.apache.sis.parameter.DefaultParameterValue)1 ProcessException (org.geotoolkit.process.ProcessException)1 AbstractProcess (org.geotoolkit.processing.AbstractProcess)1 AbstractProcessDescriptor (org.geotoolkit.processing.AbstractProcessDescriptor)1 ForwardProcessListener (org.geotoolkit.processing.ForwardProcessListener)1 DataLink (org.geotoolkit.processing.chain.model.DataLink)1