Difference: WritingTasks (46 vs. 47)

Revision 472013-02-22 - JavierDiaz

Line: 1 to 1
 
META TOPICPARENT name="DpHipe"
<-- ANALYTICS CODE - DO NOT EDIT -->
<-- Google Analytics script BEGIN -->
<-- Google Analytics script END -->
Line: 182 to 182
 You cannot set multiple data types for a parameter, but you can achieve the same effect by using one of the following workarounds:

  • Use a more general data type, then define a validator doing additional checks.
Changed:
<
<
  • Register your task several times with different primary inputs. CURRENTLY BROKEN
  • Use multiple optional parameters.
>
>
For example, this is a task whose primary input accepts String, Integer and Product:
.public class CombinedTask extends Task {
        private static final String TASKNAME = "combined";
        private static final String PRIME = "input";
        /** Jython doc string */
        public static PyString __doc__;

        /**Constructor*/
        public CombinedTask() {
            super(TASKNAME);
            addTaskParameter(TaskUtil.buildParameter(PRIME, Object.class, INPUT, MANDATORY, null, NULL_NOT_ALLOWED,
                    "String, Integer or Product"));

            getParameter(PRIME).setParameterValidator(new ParameterValidatorAdapter() {
                @Override
                public void validate(Object value) throws ParameterValidationException {
                    if (value instanceof String || value instanceof Integer || value instanceof Product) {
                        return;
                    }
                    throw new ParameterValidationException("Not String, Integer or Product but " + value.getClass());
                }
            });
            setDescription("task to test object parameters");
            __doc__ = TaskUtil.makeDoc(this);
        }


        @Override
        public void execute() {
            Object prime = getValue(PRIME);
            if (prime instanceof String) {
                System.out.println("A String " + prime);
            } else if (prime instanceof Integer) {
                System.out.println("An Integer " + prime);
            } else {
                System.out.println("A Product " + prime);
            }
        }
}

  • Use multiple optional parameters (not recommended, too many parameters)
 

Cleaning of parameters after execution

 
This site is powered by the TWiki collaboration platform Powered by Perl