在流口水的子类上应用累积

Applying accumulate on subclass in drools

我正在创建股票对象并插入股票值,如下所示

  1. 客户 1 有 2 只股票 stocks4 和 stocks5
  2. customer2 有 1 个股票 arrayListStocks1

在这里,我试图对给定会话的所有股票求和

Stocks stocks1=new Stocks("stock1", 1, 1);
            Stocks stocks2=new Stocks("stock2", 2, 2);

            Stocks stocks4=new Stocks("stock4", 4, 4);
            Stocks stocks5=new Stocks("stock5", 5, 5);

            ArrayList<Stocks> arrayListStocks1=new ArrayList<Stocks>();
            arrayListStocks1.add(stocks1);
            arrayListStocks1.add(stocks2);


            Customer customer1= new  Customer();
            customer1.setCustomerName("ACC");
            customer1.setStocks(arrayListStocks1);

            ArrayList<Stocks> arrayListStocks2=new ArrayList<Stocks>();
            arrayListStocks2.add(stocks4);
            arrayListStocks2.add(stocks5);


            Customer customer2= new  Customer();
            customer2.setCustomerName("JINDAL");
            customer2.setStocks(arrayListStocks2);

            kSession.insert(customer1);
            kSession.insert(customer2);


rule "Rule5"
 // agenda-group "promotions2"
dialect "java"
no-loop
salience 80
    when
         $list: List() from collect($customer:Customer($customerName:customerName,$stk:stocks))
         $customer:Customer($customerName:customerName,$stk:stocks)
         $stocks:Stocks() from $stk
         $listStocks: List() from collect(Stocks($stockNumber:stockNumber > 1) from $stk )
         $sum:Number() from accumulate (Stocks($stockNumber: stockNumber) from $stk,sum($stockNumber))
    then
       // System.out.println("Rule5 Stock Number- " + $stocks.getStockNumber());
       // System.out.println("Rule5  SIZE - " + $listStocks.size());
        System.out.println("Number - " +  $sum);
end

当我 运行 高于规则时,我得到低于输出 数量 - 9.0 数量 - 9.0 数量 - 3.0 数字 - 3.0

如何计算customer1和customer2的2只股票的总和 我期待结果 12

谢谢,

如果你只想计算所有客户的所有库存总和,那么你可以尝试这样的事情:

rule "Stocks Sum"
when
  $n: Number() from accumulate(
    Customer($stk:stocks) and
    Stocks($stockNumber: stockNumber) from $stk,
    sum($stockNumber)
  )
then
  //...
end

希望对您有所帮助