Search in sources :

Example 1 with Parameter

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

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

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

the class ChainProcessDescriptor method createParams.

public static ParameterDescriptorGroup createParams(final List<Parameter> inputs, final String name, final boolean realType) {
    int parameterSize = inputs.size();
    final GeneralParameterDescriptor[] paramDescs = new GeneralParameterDescriptor[parameterSize];
    int index = 0;
    for (Parameter param : inputs) {
        final ParameterDescriptor desc = convertParameterDtoToParameterDescriptor(param, realType);
        paramDescs[index] = desc;
        index++;
    }
    return new ParameterBuilder().addName(name).createGroup(paramDescs);
}
Also used : GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) ExtendedParameterDescriptor(org.geotoolkit.utility.parameter.ExtendedParameterDescriptor) ParameterDescriptor(org.opengis.parameter.ParameterDescriptor) GeneralParameterDescriptor(org.opengis.parameter.GeneralParameterDescriptor) Parameter(org.geotoolkit.processing.chain.model.Parameter) ParameterBuilder(org.apache.sis.parameter.ParameterBuilder)

Example 4 with Parameter

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

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

the class ChainProcessTest method testXmlRW.

@Test
public void testXmlRW() throws ProcessException, JAXBException, IOException {
    // test various problematic case
    final Chain chain = new Chain("myChain");
    // sting/string in user map
    Parameter param = new Parameter("in1", String.class, "enumerated string", "none", 0, 12, "def", new String[] { "def", "undef" });
    Map<String, Object> userMap = new HashMap<>();
    userMap.put("test", "test");
    param.setUserMap(userMap);
    chain.getInputs().add(param);
    Parameter param2 = new Parameter("in2", Character.class, "char", "none", 0, 12, 'c');
    chain.getInputs().add(param2);
    File f = File.createTempFile("chain", ".xml");
    chain.write(f);
    final Chain chainr = Chain.read(f);
    Parameter p = chainr.getInput("in2");
    // assertEquals('c', p.getDefaultValue()); problem here char are rw to integer...
    // sting/double in user map
    userMap = new HashMap<>();
    userMap.put("test-double", 125d);
    param.setUserMap(userMap);
    f = File.createTempFile("chain", ".xml");
    chain.write(f);
    // sting/map in user map
    userMap = new HashMap<>();
    HashMap<String, String> typeMap = new HashMap<>();
    typeMap.put("String", "String");
    userMap.put("test-map", new StringMap(typeMap));
    param.setUserMap(userMap);
    f = File.createTempFile("chain", ".xml");
    chain.write(f);
    // sting/map/list in user map
    userMap = new HashMap<>();
    HashMap<String, StringList> typeMapList = new HashMap<>();
    ArrayList a = new ArrayList<>();
    a.add("String");
    a.add("String2");
    typeMapList.put("String", new StringList(a));
    userMap.put("test-map", new StringMapList(typeMapList));
    param.setUserMap(userMap);
    f = File.createTempFile("chain", ".xml");
    chain.write(f);
}
Also used : Chain(org.geotoolkit.processing.chain.model.Chain) StringMap(org.geotoolkit.processing.chain.model.StringMap) HashMap(java.util.HashMap) StringList(org.geotoolkit.processing.chain.model.StringList) ArrayList(java.util.ArrayList) StringMapList(org.geotoolkit.processing.chain.model.StringMapList) Parameter(org.geotoolkit.processing.chain.model.Parameter) File(java.io.File) Test(org.junit.Test)

Aggregations

Parameter (org.geotoolkit.processing.chain.model.Parameter)6 Chain (org.geotoolkit.processing.chain.model.Chain)4 Constant (org.geotoolkit.processing.chain.model.Constant)4 ElementProcess (org.geotoolkit.processing.chain.model.ElementProcess)4 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)2 ChainProcessDescriptor (org.geotoolkit.processing.chain.ChainProcessDescriptor)2 ExtendedParameterDescriptor (org.geotoolkit.utility.parameter.ExtendedParameterDescriptor)2 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)2 ParameterDescriptor (org.opengis.parameter.ParameterDescriptor)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DefaultDataIdentification (org.apache.sis.metadata.iso.identification.DefaultDataIdentification)1 ParameterBuilder (org.apache.sis.parameter.ParameterBuilder)1 ElementCondition (org.geotoolkit.processing.chain.model.ElementCondition)1 FlowLink (org.geotoolkit.processing.chain.model.FlowLink)1 StringList (org.geotoolkit.processing.chain.model.StringList)1 StringMap (org.geotoolkit.processing.chain.model.StringMap)1 StringMapList (org.geotoolkit.processing.chain.model.StringMapList)1 Test (org.junit.Test)1