Hi Edson,
I think I got it working :-)
Edson Tirelli wrote:
For an example, look at the file SetEvaluatorsDefinition and look at
the BaseMemberOf inner class. So, all the logic is in there, but we
still used subclasses just to set the constructor attributes and
override the toString() method, but you could still implement
everything in a single class I guess.
Yes, this approach makes sense as long as you don't need to use the
different VariableContextEntry subclasses for handling double, char etc.
Yet, this can be handled, too.
toString():
You can avoid subclassing from your BaseMyEvaluatorClass to override
toString() by using a string method like:
public String toString() {
return getValueType().getName() + " " +
getOperator().getOperatorString();
}
Regarding the evaluator API:
If you only need one evaluator class (subclassed from BaseEvaluator),
personally I don't like the > 10 static calls to
addEvaluator(type, operator, myEvaluatorInstance)
in the EvaluatorDefinition class.
So, how about adding the method addDefaultEvaluator(operator,
evaluatorClass) to EvaluatorCache, see attachment?
This would allow you to register a evaluator as default and only overide
the special cases...
PS: If you are interested I will write some small tutorial about custom
evaluators / operators in drools trunk. This might take a week or two,
though. Any preferences regarding the format?
Thanks for the help....
cu, Michael
@SuppressWarnings("unchecked")
public void addDefaultEvaluator(final Operator operator, Class evaluatorClass) {
// define the constructor
Class[] evaluatorParameters = new Class[2];
evaluatorParameters[0] = ValueType.class;
evaluatorParameters[1] = Operator.class;
// get the constructor
try {
Constructor<BaseEvaluator> constructor;
constructor = evaluatorClass.getConstructor( evaluatorParameters );
// add feault operators
addEvaluator(ValueType.ARRAY_TYPE, operator,
constructor.newInstance(ValueType.ARRAY_TYPE, operator));
addEvaluator(ValueType.BIG_DECIMAL_TYPE, operator,
constructor.newInstance(ValueType.BIG_DECIMAL_TYPE, operator));
addEvaluator(ValueType.BIG_INTEGER_TYPE, operator,
constructor.newInstance(ValueType.BIG_INTEGER_TYPE, operator));
addEvaluator(ValueType.BOOLEAN_TYPE, operator,
constructor.newInstance(ValueType.BOOLEAN_TYPE, operator));
addEvaluator(ValueType.PBOOLEAN_TYPE, operator,
constructor.newInstance(ValueType.PBOOLEAN_TYPE, operator));
addEvaluator(ValueType.BYTE_TYPE, operator, constructor.newInstance(ValueType.BYTE_TYPE,
operator));
addEvaluator(ValueType.PBYTE_TYPE, operator,
constructor.newInstance(ValueType.PBYTE_TYPE, operator));
addEvaluator(ValueType.CHAR_TYPE, operator, constructor.newInstance(ValueType.CHAR_TYPE,
operator));
addEvaluator(ValueType.PCHAR_TYPE, operator,
constructor.newInstance(ValueType.PCHAR_TYPE, operator));
addEvaluator(ValueType.DATE_TYPE, operator, constructor.newInstance(ValueType.DATE_TYPE,
operator));
addEvaluator(ValueType.DOUBLE_TYPE, operator,
constructor.newInstance(ValueType.DOUBLE_TYPE, operator));
addEvaluator(ValueType.PDOUBLE_TYPE, operator,
constructor.newInstance(ValueType.PDOUBLE_TYPE, operator));
addEvaluator(ValueType.FLOAT_TYPE, operator, constructor.newInstance(ValueType.FLOAT_TYPE
, operator));
addEvaluator(ValueType.PFLOAT_TYPE, operator,
constructor.newInstance(ValueType.PFLOAT_TYPE, operator));
addEvaluator(ValueType.INTEGER_TYPE, operator,
constructor.newInstance(ValueType.INTEGER_TYPE, operator));
addEvaluator(ValueType.PINTEGER_TYPE, operator,
constructor.newInstance(ValueType.PINTEGER_TYPE, operator));
addEvaluator(ValueType.LONG_TYPE, operator, constructor.newInstance(ValueType.LONG_TYPE,
operator));
addEvaluator(ValueType.PLONG_TYPE, operator,
constructor.newInstance(ValueType.PLONG_TYPE, operator));
addEvaluator(ValueType.OBJECT_TYPE, operator,
constructor.newInstance(ValueType.OBJECT_TYPE, operator));
addEvaluator(ValueType.SHORT_TYPE, operator,
constructor.newInstance(ValueType.SHORT_TYPE, operator));
addEvaluator(ValueType.PSHORT_TYPE, operator,
constructor.newInstance(ValueType.PSHORT_TYPE, operator));
addEvaluator(ValueType.STRING_TYPE, operator,
constructor.newInstance(ValueType.STRING_TYPE, operator));
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}