Search code examples
drools

Binding to Map entry


I have a rule maxNumConsecutiveCubicCustomersPerLocation as such:

Customer(..., $location: location, $consecAndWaitingMap: consecAndWaitingMap)

and all I want to do is to retrieve the entry of $consecAndWaitingMap with key $location (which is an enum type).

In the then part of the rule I can easily print the entry as so System.out.println($location+": "+$consecAndWaitingMap.get($location));, but I may not bind to it:

$consec: $consecAndWaitingMap.get($location)

The compile time error given:

Unable to resolve ObjectType '$consecAndWaitingMap.get'

I have imported all the necessary classes (import java.util.Map; import ...Customer;) so that can't be the problem. Other workarounds, such as fixing $location, or accessing by [], or by first binding to Customerand then retrieving my map using consecAndWaitingMap: HashMap() from $customer.getConsecAndWaitingMap() give similar errors.

How can I bind to $consecAndWaitingMap.get($location)? If that's not possible, any ideas for a workaround?


Solution

  • You can bind your entry in the same line as the other variables. The following code should work :

    Customer(..., $location: location, $consecAndWaitingMap: consecAndWaitingMap, $mapEntry : consecAndWaitingMap.get(location))
    

    Note that you can't use the other variables when binding the new one, you need to use the actual "location" field. (replacing "location" with "$location" will give an error)