Search code examples
droolsdrools-guvnordrools-plannerdrools-fusiondrools-flow

facing issue to iterate list in drools


facing problem with List iteration in drools

GoodsShipment has the list of GoodsItems and GoodsItem has the list of Documents

my requirement is, need to check atleast one document is available or no.

iam tried this but failed

writen a class to checking purpose

public class CheckDocument {

    public boolean flag = false;
    public CheckPreviousDocument() {
    }
    public boolean getPreviousDocument(GoodsShipment goodsshipment) {
        List<GoodsItem> list = goodsshipment.getGoodsItems();
        Iterator<GoodsItem> itr = list.iterator();
        while (itr.hasNext()) {
            GovernmentAgencyGoodsItem document = itr.next();
            if (document.getDocuments().size() > 0) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}




rule "previousDocuments minimum 1"

    when
        $o: GoodsShipment()
        %x: CheckPreviousDocuments(previousDocuments($o) == false)
    then
        insert(-------------)
end

can anyone please help me.. thanks in advance


Solution

  • Your code is somewhat unusual, but thus rule should do. Note that I have used Document as the type for the elements in the list returned by GovernmentAgencyGoodsItem.getDocuments().

    rule atLeastOne
    when
        $gs: GoodsShipment()
        List( size > 0 )
        from accumulate ( $gi: GoodsItem() from $gs.getGoodsItems() and
                          $d: Document() from $gi.getDocuments();
                          collectList( $d ) )
    then
        // $gs contains at least one Document
    end