Search in sources :

Example 1 with DataLink

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

the class EventChain method isValidLink.

/**
 * Check if the given link is valid.
 * @return true only if the link is valid
 */
public boolean isValidLink(final DataLink link) {
    if (link == null) {
        return false;
    }
    final Object source = link.getSource(this);
    final Object target = link.getTarget(this);
    // source and target must exist
    if (source == null || target == null) {
        return false;
    }
    // input can not go on output of same process
    if (source == target) {
        return false;
    }
    // we can not have more then one link pointing to the same target
    final List<DataLink> otherLinks = new ArrayList<>();
    for (DataLink lk : getDataLinks()) {
        if (lk.getTargetId() == link.getTargetId() && lk.getTargetCode().equals(link.getTargetCode())) {
            otherLinks.add(lk);
        }
    }
    if (!otherLinks.isEmpty()) {
        final Integer newSourceId = link.getSourceId();
        final List<FlowLink> execLinks = getFlowLinkFromId(link.getTargetId(), true);
        clearFlowLinkList(execLinks, otherLinks);
        if (execLinks.isEmpty()) {
            return false;
        } else {
            final Set<Integer> newSourceConnditional = new HashSet<>();
            searchForConditionalParent(newSourceId, newSourceConnditional);
            if (newSourceConnditional.isEmpty()) {
                return false;
            } else {
                final Set<Integer> otherSourceConditonal = new HashSet<>();
                for (FlowLink exec : execLinks) {
                    if (exec.getSourceId() != newSourceId) {
                        searchForConditionalParent(exec.getSourceId(), otherSourceConditonal);
                    }
                }
                boolean valid = false;
                for (Integer id : newSourceConnditional) {
                    if (otherSourceConditonal.contains(id)) {
                        valid = true;
                        break;
                    }
                }
                if (!valid) {
                    return false;
                }
            }
        }
    }
    // check types
    final Object sourceDataType = getDataType(source, false, link.getSourceCode());
    final Object targetDataType = getDataType(target, true, link.getTargetCode());
    return matcher.canBeConverted(sourceDataType, targetDataType);
}
Also used : FlowLink(org.geotoolkit.processing.chain.model.FlowLink) ArrayList(java.util.ArrayList) DataLink(org.geotoolkit.processing.chain.model.DataLink) HashSet(java.util.HashSet)

Example 2 with DataLink

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

the class EventChain method clearFlowLinkList.

private void clearFlowLinkList(final List<FlowLink> inputExecLinks, final List<DataLink> inputLinks) {
    final List<FlowLink> clearedExecLinkList = new ArrayList<>();
    for (DataLink lk : inputLinks) {
        for (FlowLink executionLink : inputExecLinks) {
            if (executionLink.getSourceId() == lk.getSourceId()) {
                clearedExecLinkList.add(executionLink);
                break;
            }
        }
    }
    inputExecLinks.removeAll(clearedExecLinkList);
}
Also used : FlowLink(org.geotoolkit.processing.chain.model.FlowLink) ArrayList(java.util.ArrayList) DataLink(org.geotoolkit.processing.chain.model.DataLink)

Example 3 with DataLink

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

the class EventChain method clearObsoleteLinks.

private void clearObsoleteLinks() {
    final List<DataLink> lst = new ArrayList<DataLink>(getDataLinks());
    for (final DataLink link : lst) {
        // remove link if source or target doesn't exist anymore
        if (link.getSource(this) == null) {
            getDataLinks().remove(link);
        } else if (link.getTarget(this) == null) {
            getDataLinks().remove(link);
        }
    }
    final List<FlowLink> lstEx = new ArrayList<FlowLink>(getFlowLinks());
    for (final FlowLink link : lstEx) {
        // remove link if source or target doesn't exist anymore
        if (link.getSource(this) == null) {
            getFlowLinks().remove(link);
        } else if (link.getTarget(this) == null) {
            getFlowLinks().remove(link);
        }
    }
}
Also used : FlowLink(org.geotoolkit.processing.chain.model.FlowLink) ArrayList(java.util.ArrayList) DataLink(org.geotoolkit.processing.chain.model.DataLink)

Example 4 with DataLink

use of org.geotoolkit.processing.chain.model.DataLink 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)

Aggregations

ArrayList (java.util.ArrayList)4 DataLink (org.geotoolkit.processing.chain.model.DataLink)4 FlowLink (org.geotoolkit.processing.chain.model.FlowLink)3 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 DefaultParameterValue (org.apache.sis.parameter.DefaultParameterValue)1 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)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 Chain (org.geotoolkit.processing.chain.model.Chain)1 Constant (org.geotoolkit.processing.chain.model.Constant)1 ElementCondition (org.geotoolkit.processing.chain.model.ElementCondition)1 ElementProcess (org.geotoolkit.processing.chain.model.ElementProcess)1 GeneralParameterValue (org.opengis.parameter.GeneralParameterValue)1 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)1 ParameterValue (org.opengis.parameter.ParameterValue)1 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)1