Domanda

I am creating a rule which needs to fire when one or more conditions are met. The rule looks as follows:

rule "Demo Rule"
    when
         $data : Data (val == 1 || val == 2)
    then
        System.out.println($data);
end

I have a test for the rule, which has two matching Data objects (val 1 and val 2). The rule fires correctly, as it gets to the print statement. However, I can't find a way to parse $data and get access to the individual matching Data objects. The println results as follows:

custom.package.Data< val: 1, text:'Test1' >
custom.package.Data< val: 2, text:'Test2' >

So, I can see that multiple entries are present. But attempting to call as an array ($data[0]) or $data$1 or anything I could think of all result in parsing errors by Drools (complaining that $data is a Data object, not an array or list or other iterable).

È stato utile?

Soluzione

The rule is fired once per each object matching your condition. So, you'll need to access the Data object directly as $data.val and data.text

If you need/want the objects in a list, you can use collect for that. Then your rule would be like

rule "Demo Rule"
    when
         $dataList : ArrayList() from collect( Data (val == 1 || val == 2) )
    then
        System.out.println($dataList);
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top