Defining your own type converter
You define a custom type converter in a Java™ project.
About this task
You create a Java project to contain the custom classes and then define all the type converters that you want to use in your rule project.
Procedure
To define your own type converter:
- In Rule Designer, change to the Java perspective: click Window > Open perspective > Other > Java (default) and then click OK.
- Create a Java project:
- On the File menu click New > Java project.
- On the Create a Java Project page, type a name for the custom type project and then click Finish.
- Right-click the src folder in the new project and then click New > Package.
- On the New Java Package page, type a name for the package and then click Finish.
- Right-click the new package and then click New > Class.
- On the New Java Class page, type a
name for the custom type class and then click Finish.
By convention, the name starts with an uppercase letter.
- In the Java Class
Editor, define the custom type converter by using a constructor.
The following sample code shows how to define a boolean type converter by implementing a converter class. The comments describe what each method does.
package sample.converter; import java.util.Map; import ilog.rules.res.session.rawdata.*; @TypeConvertor public class SimpleBooleanConverter { protected short trueValue; //Initialize the converter for configuration information. public void init(Map<String, String> props) { this.trueValue = Short.parseShort(props.get("TRUE-VALUE")); } //Convert COBOL source data type to target Java data type. //Default Java mapping for COBOL type PIC 9(2) is short. Target type is= boolean. public synchronized boolean convertToTarget(short source) { return (trueValue == source); } //Convert target type (boolean) back to the COBOL source type (short). public synchronized short convertToSource(boolean target) { return target ? trueValue : 0; } }zRule Execution Server for z/OS® uses the @Convertor annotation at run time to identify all user-defined type converters. With this annotation, you can define your own keys and values when you use the custom type converter.
If instead you want to define a fixed set of configuration keys, add the @ConvertorProperties annotation to the code. With this annotation, when you use the type converter, you can modify the values but not the keys. The following code shows how to use the @TypeConvertorProperties annotation:
package sample.converter; import java.util.Map; import ilog.rules.res.session.rawdata.*; @TypeConvertor @TypeConvertorProperties ({"TRUE-VALUE"}) public class SimpleBooleanConverter {Important:You must add the @TypeConvertor annotation, and the @TypeConvertorProperties annotation if you are using it, for each custom type converter class you create.
The converterToTarget() and convertToSource() methods must be thread safe.
- Import the import ilog.rules.res.session.rawdata.*; package
into the project.
These classes are delivered with res-rawdata-execution.jar, which can be found in a location similar to C:\Program Files (x86)\IBM\IMShared\plugins\com.ibm.rules.zrules.zxom_<version>-<date>_<time>\lib.
- Save your work.
- Reference the type converter project from your rule project:
- Right-click your rule project and then click Properties.
- Select Java Execution Object Model, and on the Projects tab of the Java Execution Object Model page, select the type converter project.
- Click OK.
Results
The new custom type converter is now available for selection when you use the Import COBOL XOM wizard.