Search code examples
parametersenumshashmapdrools

Drools condition cannot retrieve object from Map with Enum key


I'm migrating an application that used drools 5.3 on the newer drools 6.5 version. Here is an extract of my fact :

public class Convention {
  [...]
  private Map<ECommissionType, List<Commission>> commissions = new HashMap<>();
  [...]
}

and a very simple enumeration :

public enum ECommissionType {
  ACQ,RIS,POF,[...]
}

I'm using a decision table, and one of the condition header is the following (assuming $convention is properly initialized in a former condition and is an instance of the Convention class shown) :

$comm : Commission() from $convention.getCommissions().get(ECommissionType.$1)

In the lines defining my rules, I'm using one of the values of the enumeration, for instance POF or RIS. Drools 5.3 is compiling this condition, but drools 6.5 is not. It fails with this error message :

Unable to Analyse Expression $convention.getCommissions().get(ECommissionType.RIS)):
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class : [Rule name='B_CommissionLigne_98']

Any insights to go through this error ?


Solution

  • You have

    $conv : Commission from $convention.getCommissions().get(ECommissionType.$1)
    

    You need to add a () after Commission.

    It's possible that something in the considerable change in the parser and expression evaluation mechanics between 5.3 and 6.0 has introduced a bug.

    Edit Yes, a bug. Shortening some names but using the same types, this avoids the problem:

    rule comms
    when
      $conv: Convention()
      $comm: Comm() from (ArrayList)($conv.getComms()).get(Type.AAA)
    then
    

    (Why don't you use an EnumMap??)