TWiki> Public Web>DpHipe>DpHipeTools (revision 48)EditAttach

Adding Tools to HIPE

Task, tools and variables

Tools are processing units that operate on specific data elements.
From the Java point of view, a tool is an implementation of the Tool interface.
The well-known Tasks are examples of tools within HIPE. In this case, TaskTool is used under the hoods.

If a data element is selected, a list of tools that can operate on that data should appear. Double clicking on the tool will open an associated view (for non-task tools) or a dialog for settings parameters (for task tools).

This section explains:

  • how you can create a Tool and register it for being available for dedicated data
  • how you can make HIPE aware of an existing Task,
  • how your task can react better on an active data element,
  • the default task dialog and how you implement and contribute a dedicated input dialog for your task,
  • how you can implement and contribute a specific parameter editor

Adding a Tool as a Task

Task Registry

Up to now you have made you task globally available to the system by specifying an instance of that task within the file of your sub-system, e.g.:
    # file
    compute = ComputeTask()

To make your task appear in the "Tasks" view, you need to add the following lines:

    from herschel.ia.task.views import TaskToolRegistry
    toolRegistry = TaskToolRegistry.getInstance()

For PACS users, this file is located at $install_dir/data/toolbox/your_sub_system.

You can also specify that your task belongs to one or more Category :

    from herschel.ia.gui.kernel.Tool import Category
    toolRegistry.register(compute, [Category.IMAGE, Category.PACS]))
Your task will now be enabled whenever a session variable is selected which matches the type of the first input parameter within your task!

Within your task, you can control which parameter signs-up to be the prime parameter (the one which reacts on a selected data variable) by the Task API:

    class ComputeTask extends Task {
       ComputeTask() {
          prime = new TaskParameter("spectrum", SpecificProduct.class)

Naming conventions for task when to be registered in Hipe should follow this example assuming that the task will perform the functionality named "reduce" :

     Name of the Class              : ReduceTask
     Name of the Task (getName())   : reduce
     Name of the variable in Jython : reduce

Warning, important For naming tasks we follow the Java conventions, we use camel-case , not underscores : "reduceLight" is valid, "reduce_light" is not. Info log messages are produced for invalid task names.

Prime input validation

The mechanism above makes you task to become a tool within the system and it appears whenever a variable of type SpecificProduct (i.e. the type of the value of the Parameter) is selected.

Sometimes this may not be enough, e.g. in certain situations your task will only run on a SpecificProduct if it contains certain contents. A typical situation would be when a SPIRE reduction operates on a ObservationContext: such a task should not be listed whenever a HIFI observation is selected...

You can write a ParameterValidator to do just that:

    prime = new TaskParameter("spectrum", SpecificProduct.class)
    prime.setParameterValidator(new ParameterValidatorAdapter() {
        public void validate(Object value) throws ParameterValidationException {
            SpecificProduct s = (SpecificProduct)value;
            if (! (logic that would validate the contents of the value...)) {
                throw new ParameterValidationException(reason);

In other words, rather than writing this logic within the pre-amble or execution block of your task, we recommend you to move that logic into the parameter validation. This way we achieve two things:

  • make the logic appear where it should be and therefore keeping the execution block of your task concentrated to the algorithm, and
  • make your task appear as a tool within HIPE that can be ran against specific data.

Task Dialogs

Default Task Dialog

The system generates a default input dialog for all registered tasks within the software. As the system does not know the intent of your task, it can only provide a dry-listing of all requested parameters; such a dialog may not be suitable for your purposes.

The default dialog for the crop task:

As for instance you may want to have more control over how the input parameters are presented to the user:

  • you may only want to provide a sub-set of parameters (and leave the full-set to expert users on the command-line)
  • you may want to organize your parameters by grouping them in tabs, or putting a border around a group of parameters
  • you may want to have more informative tooltips, labels or even input fields that are more suitable for your task.

To adapt to these scenarios and more, the system provides three ways for customizing you Task dialogs:

  • Parameter Modifiers
  • Signature Components
  • Task Panels.

Parameter Modifiers

The system provides a default dialog displaying an input area for setting the values of the parameter based on a composition of Modifiers.

The input area for the crop task:

The composition of Modifiers is created based on the types of the values of the Task Parameters of the Task Signature.

The Modifier for the row1 Parameter of the crop task:

Currently the system contains basic implementation for the simple types Boolean, Integer, Float, Long, Double, String and few more, so there's still a lot of space for improvements and contribution. You can find the general available modifiers in package herschel.ia.gui.apps.modifier; please consult the Javadoc of your HIPE installation.

If the default parameter doesn't fit for your Task Parameter, you can:

  • Implement a Modifier,
  • Register it to the system.

Alternatively, you could want to write your specific Modifier for one of the already available types. In that case, you could create your modifier in a custom Signature Component.

Warning, important NOTE:
The following behaviours and limitations are present in the provided modifiers:

  • While you can always write SomeTask(param = null) in Console, using a task dialog you will get SomeTask(): for GUIs "null is not allowed". The task machinery will take nulls as if the parameter has been ignored by the user.
  • Modifiers have no notion of the optionality of parameters: if they have a valid value, they will return it. The task machinery will not generate a parameter assignment if the value equals the default.
  • Specialized modifiers:
    • Will mark as errorenous variables incompatible with the type (dynamically), but will accept any variable.

Implement a Modifier
The Modifier interface consists of two explicit contracts:
  • Support the drag and drop features (the set/getVariableSelection)
  • Support the inspection for Object (the set/getObject)
and two implicit contracts:

Register a Modifier
The registration of the Modifier is done again in the via the Extension Registry with the usual syntax (please note the name of the factory: factory.modifier).

Be aware that the registration is system wise so the registration overrides any other registered modifier for that type.


In case the Modifier you have created is only applicable to a specific task or even to a specific parameter of a specific task, you can simply assign it to the applicable Task Parameter:

    // YourTask constructor
    public YourTask() {
        addTaskParameter(new TaskParameter("someInput", MyClass.class));

    // Customize your modifiers
    public Map<String, Modifier> getCustomModifiers() {
	Map<String, Modifier> map = new LinkedHashMap<String, Modifier>();
	map.put("someInput", new MyModifier());
	return map;

Warning, important NOTE:
Using TaskParameter.setModifier() for customizing modifiers is deprecated.
This mechanism is unsafe, even dangerous, because it implies executing GUI code at task creation, which is done in a non-GUI thread.
In consequence, this method is planned to be removed.

Signature Components

In case the default input area based on Modifiers doesn't fit your needs you can just replace it by your own implementation.

Rotate Alternative Signature:

If this is the case you need to:

  • Implement a Task Signature Component
  • Register it to the system.

Implement a Task Signature Component

The TaskSignatureComponent interface consists of four explicit contracts
  • Support the setVariableSelection for initial assignment from the Tool Window
  • Assign the Signature to display (setSignature)
  • Return a map of parameters and assigned values (in Map<TaskParameter, VariableSelection> getParameters)
  • Clear and check user inputs (used by the default buttons)
and the two implicit contracts inherited by the Extension Registry
  • Be JComponent
  • Have an empty constructor

An easy way of implementing TaskSignatureComponent is by extending JTaskSignatureComponent and providing your own implementation for the makeModifierMap() method.

For example, if you want to use a custom Signature Component that just wants to use JFilePathModifier for a parameter aimed for a file name, you could do it like this:

public class MySignatureComponent extends JTaskSignatureComponent {

    private static final long serialVersionUID = 1L;

    protected Map<TaskParameter, Modifier> makeModifierMap() {

	SignatureApi signature = getSignature();
	Map<TaskParameter, Modifier> m = new LinkedHashMap<TaskParameter, Modifier>();

	m.put(signature.getTaskParameter("file"), new JFilePathModifier(SAVE));
	m.put(signature.getTaskParameter("number"), new JIntegerModifier());

	return m;

Register a Task Signature Component

The registration of the Task Signature Component is done again in the via the Extension Registry with the usual syntax (please note the name of the factory: factory.editor.tool.task.signature).

        "Rotate Signature",

See also the Extension Registry documentation for more details.

Warning, important NOTE: Conventions for labels for input parameters: (see DM). The static function

 public static JLabel getDecoratedLabel(TaskParameter tp, boolean isPrimeInput, String altName) 
provides a decorated label (including tooltip) that follows the standard style.

If some other function needs to be made public so that tasks can follow the default style, they will be added above.

Custom Task Dialogs

Eventually, if the above options still do not accommodate you needs you can replace the the default Task Panel with your own implementation

If this is the case you need to:

  • Implement a Task Panel
  • Register it to the system.

Implement a Task Panel

The TaskPanel interface consists of three explicit contracts
  • Support the setVariableSelection for initial assignment from the Tool Window
  • Assign the Task to display ( the setTask)
  • Notify request of executions to the framework by
and the two implicit contracts inherited by the Extension Registry
  • Be JComponent
  • Have an empty constructor

The Rotate Panel example (herschel.ia.task.example.RotatePanel):

Register a Task Panel

The registration of the Task Panel Component is done again in the via the Extension Registry with the usual syntax (please note the name of the factory: factory.editor.tool.task.signature).

        "Rotate Task Panel",

See also the Extension Registry documentation for more details.

Task compliance

  • Write user documentation (jtags)! That will be automatically picked up whenever a user asks the system for help on your task.
  • The name of the task should be a legal variable name in the global name-space. For example your instance of MyTask should report itself as e.g.: "myTask" and not "This is my task".
  • If your prime parameter is not the first parameter in your task, specify the prime parameter using the setPrimeInput method in the signature
  • Write a parameter validator for your prime parameter if your task should be listed not only on prime data type but on prime data contents as well.

Recommendations for simple tasks and limitations

  • Use a function that fully initializes TaskParameters: you avoid working with partially initialized data or data with non obvious defaults.
  • Please take care to register your task properly: otherwise it may half-work, which is much worse than not working at all!
  • Capture exceptions and check your params before trying to properly execute, if you want to provide better error reporting than the one provided by default.
  • You should not do user interaction after you have started execute() (not on EDT).
  • Validators: you cannot compare multiple parameters, order of execution is not specified, will be executed several times. So, if you need to validate dependent parameters, it should be done later.
  • Once you are in execute() console has already been updated with the command : Although you can change the value of TaskParameters, this will not be properly reflected on the UI, so don't do it. So once you are on execute() you can check , possibly abort, and properly execute (nothing else).

Adding a Tool that is not a Task

If you have an existing task and want to make it available in HIPE, you just need to follow the steps described in the above section.

Now, a task has its limitations. It is somewhat an atomic operation for which you provide some inputs and expect some result.
Therefore, it is not expected for acting interactively with a user, and it is not meant for holding internal status either, that a user can modify during its execution.

If you need more flexibility, you can write your own implementation of the Tool interface.
Besides, you would most probably need a viewer associated to your tool, for letting the user interact with it.

This follows in some way the MVC pattern: your target data is the Model, your associated viewer is the View, and your tool is the Controller.

Tool Implementation

In order to write a tool, Tool interface needs to be implemented. Instead of doing it directly, it is encouraged to extend AbstractTool.

The information to be provided is passed to one of its constructors in a super call from the derived class:

    /** Constructor for a tool with a single parameter and general category. */
    protected AbstractTool(String name, Parameter primeInput)

    /** Constructor for a tool with a single input parameter. */
    protected AbstractTool(String name, Parameter primeInput, Category... categories)

    /** Constructor for a tool with multiple input parameters and general category. */
    protected AbstractTool(String name, Parameter primeInput, List<? extends Parameter> inputs)

    /** Constructor with all arguments. */
    protected AbstractTool(String name,
                           Parameter primeInput,
                           List<? extends Parameter> inputs,
                           Category... categories)
You provide the variable types you are interested in within the prime input: just return a ToolParameter initiated with the proper class of data you want to handle:
new ToolParameter("data", MyTargetDataType.class)

More conditions for checking whether the tool can react on a particular variable can be added by providing a ParameterValidator to the prime input.

The actual job to be done can be delegated to a third object (the "tool object"), or just be executed by the tool class itself.
This latter case is the default, otherwise, you need to call setToolObject(Object toolObject) in your constructor.

Moreover, you may return the categories you think the tool is meaningful for, by providing the proper ones in the super call.

Naming conventions

Conventions for names of a tool class and its variable in the Tasks view are similar than those for tasks. For example, a tool for spectrum filtering could be called:

     Name of the Class              : SpectrumFilterTool
     Name of the Tool (getName())   : spectrumFilter
     Name of the variable in Jython : spectrumFilter

Tool Viewer

Every tool has an associated viewer, which must implement EditorComponent (by extending AbstractEditorComponent or one of its subclasses).

Tool Registry

Once you have your tool and the corresponding viewer, you need to register them like this:
# Associate the tool with the viewer
                 "Spectrum Filter Tool",

# Register the tool so it is automatically available for the proper variables in HIPE
from herschel.ia.gui.kernel import ToolRegistry
from import SpectrumFilterTool
spectrumFilter = SpectrumFilterTool()

__all__ = [ "spectrumFilter", "SpectrumFilterTool", ... ]

Communicating Tool & Viewer

In the viewer, you can access the tool and the selected data within the makeEditorContent method provided by AbstractEditorComponent.
At this point, you can let the tool know about the viewer as well, if you want:
protected boolean makeEditorContent() {

    // Get the tool and the selected data
    ToolSelection selection = getSelection();
    Tool   tool = selection.getTool();
    Object data = selection.getSelection().getValue();

    // Optional - you would need to provide a setViewer method

    // Build the editor contents ...

Simple sample

This simple reproducible example wraps up the just explained steps altogether.
It is just a button whose label is changed by the tool when the user clicks on it:

    1. The tool class

public class SimpleButtonTool extends AbstractTool {

    private ArrayData _data;
    private boolean _flag = true;

    public SimpleButtonTool() {
	super("simpleButton", new ToolParameter("data", ArrayData.class));

    void setData(ArrayData data) {
	_data = data;

    void updateLabel(JButton button) {
	int size = _data.getSize();
	int rank = _data.getRank();
	button.setText("Data has " + (_flag? "size " + size : "rank " + rank));
	_flag = !_flag;

    2. The viewer class

public class SimpleButtonToolComponent extends AbstractEditorComponent<ToolSelection> {

    private static final long serialVersionUID = 1L;
    private static int _counter = 1;
    private SimpleButtonTool _tool;

    public SimpleButtonToolComponent() {
	super(new BorderLayout());

    protected Class getSelectionType() {
	return ToolSelection.class;

    protected boolean makeEditorContent() {
	final JButton button = new JButton();
	setName("Button Tool " + _counter++);
	_tool = (SimpleButtonTool)getSelection().getTool();
	button.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
	return true;

    public Icon getComponentIcon() {
	return IconLibrary.VARIABLE;

    3. The registration

REGISTRY  = ExtensionRegistry.getInstance()

                 "Button Tool",

from herschel.ia.gui.kernel import ToolRegistry
from import SimpleButtonTool
simpleButton = SimpleButtonTool()

# cleanup
del(ExtensionRegistry, Extension, REGISTRY, COMPONENT)

__all__ = [ "simpleButton", "SimpleButtonTool" ]

    4. Executing the example

For executing this simple tool, just include it in a package owned by you, open the workbench in HIPE, and execute the following in the console:
x = Int1d.range(12)
y = Double2d([[1,2,3],[4,5,6]])
Then open the x and y variables with the Button Tool and click the button: its label is updated by the tool.

Triggering Events

For a full detailed section about triggering events have a look at DpHipeCommonUtilities.

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng crop.png r1 manage 13.7 K 2009-09-29 - 15:55 JaimeSaiz  
PNGpng crop_input.png r1 manage 14.5 K 2009-09-29 - 16:09 JaimeSaiz crop task with input highlighted
PNGpng crop_modifier.png r1 manage 14.4 K 2009-09-29 - 16:09 JaimeSaiz crop task with modifier highlighted
PNGpng tasks.png r1 manage 13.0 K 2009-09-29 - 16:11 JaimeSaiz Tasks and variables
Edit | Attach | Watch | Print version | History: r113 | r50 < r49 < r48 < r47 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r48 - 2010-02-11 - JavierDiaz
This site is powered by the TWiki collaboration platform Powered by Perl