问题
Here is my rule
rule "Multiple bookings via same mobile"
when
(stayDateGroupingIteration : StayDateGroupingDto($stayGroupedBookings : stayGroupedBookings)) and (QueryTypeDto( queryType == "multiple" ))
$travellerCount :Number() from accumulate(BookingSummaryDtoList( $bookingSummaryDtoList : bookingSummaryDtoList) from $stayGroupedBookings,
init( int count=0; List<String> globalList= new ArrayList(); Set<String> duplicateSet=new HashSet();),
action(
for(Object bookingSummary : $bookingSummaryDtoList)
{
if(((BookingSummaryDto)bookingSummary).getTravellerId()!=null)
{
String travellerId=((BookingSummaryDto)bookingSummary).getTravellerId().toString();
Set<String> finalDuplicateSet=MultiBookingFraudServiceImpl.checkDuplicates(travellerId,globalList,duplicateSet);
count=count+1;
}
}
),
result( new Integer(count)))
then
//some action to be taken here
System.out.println($travellerCount);
end
How do I return the set
finalDuplicateSet
from the accumulate in place of count, I don't want to use any global variables or static variables in my java class also. can this be done or do I need to follow some other approach?
回答1:
Hope this code Help you in getting what you want :
rule "Multiple bookings via same mobile"
when
(stayDateGroupingIteration : StayDateGroupingDto($stayGroupedBookings : stayGroupedBookings)) and (QueryTypeDto( queryType == "multiple" ))
$duplicateTravellerList :List() from accumulate(BookingSummaryDtoList( $bookingSummaryDtoList : bookingSummaryDtoList) from $stayGroupedBookings,
init( int count=0; List<String> globalList= new ArrayList(); Set<String> duplicateSet=new HashSet(); List<String> finalDuplicateSet=new ArrayList();),
action(
for(Object bookingSummary : $bookingSummaryDtoList)
{
if(((BookingSummaryDto)bookingSummary).getTravellerId()!=null)
{
String travellerId=((BookingSummaryDto)bookingSummary).getTravellerId().toString();
finalDuplicateSet.add(MultiBookingFraudServiceImpl.checkDuplicates(travellerId,globalList,duplicateSet));
count=count+1;
}
}
),
result( new ArrayList(finalDuplicateSet)))
then
//some action to be taken here
System.out.println($duplicateTravellerList);
end
来源:https://stackoverflow.com/questions/51653648/how-to-return-a-list-from-inside-of-accumulate-in-drl-drools