Categories

Versions

You are viewing the RapidMiner Developers documentation for version 9.7 - Check here for latest version

Adding Preconditions to Input Ports

As you saw after starting RapidMiner Studio, the operator works. However, it does not alert the user if nothing is connected or an object of wrong type is connected to the input port. To change this behavior and improve operator usability, you can add preconditions to the ports. These preconditions will register errors, if they are not fulfilled and are registered at the time of operator construction. To do so, add a few code fragments to the constructor. For example, this precondition checks whether a compatible IOObject is delivered:

public MyOwnOperator( OperatorDescription description ){
    super(description);
    exampleSetInput.addPrecondition(
        new SimplePrecondition( exampleSetInput, new MetaData(ExampleSet.class) ));
}

Since this is one of the most common cases, you can use a shortcut to achieve it. Specify the target IOObject class when constructing the input port:

private InputPort exampleSetInput = 
    getInputPorts().createPort("example set", ExampleSet.class);

There are many more special preconditions. Some test whether an example set satisfies specific conditions, whether it contains a special attribute of a specific role, whether an attribute with a specific name is inserted, and others. For example, you could add a precondition that tests if the attribute test is part of the input example set. The attribute can have any type.

exampleSetInput.addPrecondition(
    new ExampleSetPrecondition( exampleSetInput, new String[]{"test"}, 
        Ontology.ATTRIBUTE_VALUE) );

The ExampleSetPrecondition can also check whether the regular attributes are of a certain type, which special attributes have to be contained, and of which type they must be. If the user inserts the operator into a process without connecting an example set output port with the input port, an error is shown. If the user attaches an example set without the test attribute, a warning is shown.

The next step is to define generation rules for output ports.