MDM Rulebase Numeric Variable type handling in RulebaseCustomFunction.

MDM Rulebase Numeric Variable type handling in RulebaseCustomFunction.

book

Article ID: KB0085533

calendar_today

Updated On:

Products Versions
TIBCO MDM -
Not Applicable -

Description

Resolution:
When passing a numeric rulebase variable as a parameter to a RulebaseCustomFunction, the exact type of that parameter may not be quite as expected. This is because rulebase variables may initially be seen as strings that MDM then attempts to convert to a "number", as MDM rulebases are not strongly typed, MDM must take a guess at what it thinks the type of the value represented by the string is. It starts with java.lang.Integer and moves through java.math.BigInteger, java.math.BigDecimal to java.lang.Float and java.lang.Double. All of these being classes derived from java.lang.Number, which is in turn, derived from java.lang.Object. So, in RulebaseCustomFunction, the safest way to process such values is to handle each value as an Object and then convert to the relevant class using the instanceof construct. For example, to convert to a BigDecimal value, use the following construct for a RulebaseCustomFunction function <<functionName>>:

import java.lang.Double;
import java.lang.Float;
import java.lang.Long;
import java.lang.Object;
import java.lang.String;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;

...

@RulebaseFunction(name = "<<functionName>>")
public ArrayList<BigDecimal>
<<functionName>>(ArrayList<Object> worldObject) {
ArrayList<BigDecimal> result = new ArrayList<BigDecimal>();
ArrayList<BigDecimal> world = new ArrayList<BigDecimal>();
for (Object candidate : worldObject) {
if (candidate instanceof BigDecimal) {
world.add((BigDecimal)candidate);
} else if (candidate instanceof Long) {
world.add(new BigDecimal(((Long)candidate).longValue()));
} else if (candidate instanceof Integer) {
world.add(new BigDecimal(((Integer)candidate).intValue()));
} else if (candidate instanceof Float) {
world.add(new BigDecimal(((Float)candidate).doubleValue()));
} else if (candidate instanceof Double) {
world.add(new BigDecimal(((Double)candidate).doubleValue()));
} else if (candidate instanceof BigInteger) {
world.add(new BigDecimal((BigInteger)candidate));
} else if (candidate instanceof String) {
world.add(new BigDecimal((String)candidate));
} else {
System.out.println("Object type not recognized for one of the parameters in
<<functionName>> List");
}
}


Issue/Introduction

MDM Rulebase Numeric Variable type handling in RulebaseCustomFunction.

Additional Information

http://stackoverflow.com/questions/921621/cast-long-to-bigdecimal