Search code examples
drools

Error when trying to insert the result of a accumulate function(intValue) into a hashmap<String, Integer>()


I tried to insert a value into a Hashmap() but my test case fails when I tried to put the result of an accumulate function($totals in the drl file) as the value of the Hashmap. I've also tried Integer.valueOf($totals) instead of $totals without luck.

My drl file contains:

rule "calculate total per item"
    no-loop 
    when
        $o  : Order( $lines : orderLines)
        $ol : OrderLine(this memberOf $lines.values() , quantity > 0)
        $totals : Number(intValue > 0) from accumulate (
                    OrderLine($qty : quantity, item.barcode == $ol.getItem().getBarcode()) from $lines.values(),
                    sum($qty)
                )
    then
        System.out.println("total: " + $totals); //prints out total: 7.0
        modify($o){
            getPerItems().put($ol.getItem().getBarcode(), $totals); //this line throws an error
            //getPerItems().put($ol.getItem().getBarcode(), 1 ); // If I replace $totals with a number, it works

        }

end

My Order class

public class Order {
    private HashMap<Integer, OrderLine> orderLines = new HashMap<Integer, OrderLine>();
    private HashMap<String, Integer> perItems = new HashMap<String, Integer>();
    ...
    public HashMap<String, Integer> getPerItems() {
        return perItems;
    }
}

Any advice to overcome this issue?


Solution

  • Try this binding:

    Number($totals : intValue > 0) 
    

    It would help if you add the error message verbatim to your question.

    "Throws an error" - does it mean "throws an exception" or "reports an error". When and where ...?