Assuming that you call kSession.fireAllRules() only once after the insertion
of all facts, you have to select a single representative for all Alert facts
with identical case number.
Again assuming that the alertNo is unique, you can write
when
Alert( $caseNumber : caseNumber, $alertNo: alertNo )
not Alert( alertNo < $alertNo )
$list : List( ) from collect($alertalert : Alert( caseNumber ==
$caseNumber))
then
-W
On 20/12/2012, devan.vanreenen(a)gmail.com <devan.vanreenen(a)gmail.com> wrote:
Hi there,
I have checked existing posts and able to find problems which are similar
to
mine but not exactly.
I have an Alert object as per below, several alert instances may have the
same caseNumber
I am trying to collect all the Alert instances that have the same
caseNumber, the rule is below, the rule below collects the items correctly
but the execution happens for each Alert object inserted into the knowledge
session. How can I get around this?
public class Alert {
int caseNumber;
short alertNo;
public int getCaseNumber() {
return caseNumber;
}
public void setCaseNumber(int caseNumber) {
this.caseNumber = caseNumber;
}
public short getAlertNo() {
return alertNo;
}
public void setAlertNo(short alertNo) {
this.alertNo = alertNo;
}
@Override
public String toString() {
return "Alert [caseNumber=" + caseNumber + ", alertNo=" + alertNo +
"]";
}
}
rule 'Collect Alerts for a specific case'
dialect 'java'
when
Alert( $caseNumber : caseNumber)
$list : List( ) from collect($alertalert : Alert( caseNumber ==
$caseNumber))
then
for(Object a : $list){
System.out.println(a);
}
end
I insert 5 alert objects as per below
Alert alert1 = new Alert();
alert1.setAlertNo((short)1);
alert1.setCaseNumber(830826);
Alert alert4 = new Alert();
alert4.setAlertNo((short)4);
alert4.setCaseNumber(830826);
Alert alert7 = new Alert();
alert7.setAlertNo((short)7);
alert7.setCaseNumber(830826);
Alert alert3 = new Alert();
alert3.setAlertNo((short)3);
alert3.setCaseNumber(820424);
Alert alert5 = new Alert();
alert5.setAlertNo((short)5);
alert5.setCaseNumber(820424);
I would like the output to be as follows
Alert [caseNumber=820424, alertNo=3]
Alert [caseNumber=820424, alertNo=5]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=4]
Alert [caseNumber=830826, alertNo=1]
but instead the output is
Alert [caseNumber=820424, alertNo=5]
Alert [caseNumber=820424, alertNo=5]
Alert [caseNumber=820424, alertNo=3]
Alert [caseNumber=820424, alertNo=5]
Alert [caseNumber=820424, alertNo=3]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=4]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=4]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=4]
Alert [caseNumber=830826, alertNo=1]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=4]
Alert [caseNumber=830826, alertNo=1]
Alert [caseNumber=830826, alertNo=7]
Alert [caseNumber=830826, alertNo=4]
Alert [caseNumber=830826, alertNo=1]
--
View this message in context:
http://drools.46999.n3.nabble.com/Collecting-items-in-a-List-tp4021256.html
Sent from the Drools: User forum mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users