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

时间:2018-07-06 18:29:40

标签: drools kie drools-guvnor drools-kie-server

我正在创建库存对象并按如下所示插入库存值

  1. 客户1有2个库存4和5
  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的两支股票的总和 我希望结果为12

谢谢

1 个答案:

答案 0 :(得分:1)

如果您要做的就是为所有客户计算所有库存的总和,那么您可以尝试执行以下操作:

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

希望有帮助