计算项目数量的drools规则

计算项目数量的drools规则,drools,Drools,我是Drools的新手,尝试创建一个规则来显示购买最多戴尔笔记本电脑的客户。请帮助我修复以下规则: $c : Customer() $item : Item() Number( intValue > 1 ) from accumulate( $item ( this.bought="Dell laptops", this.customer=$c.getname(),count(1) ) 假设一个非常简单的模型,实现这一点的最简单方法之一是使用两个规则的组合:一个是计算客户购买的笔记本电脑

我是Drools的新手,尝试创建一个规则来显示购买最多戴尔笔记本电脑的客户。请帮助我修复以下规则:

$c : Customer()
$item : Item()
Number( intValue > 1 )
from accumulate( $item ( this.bought="Dell laptops", this.customer=$c.getname(),count(1) )

假设一个非常简单的模型,实现这一点的最简单方法之一是使用两个规则的组合:一个是计算客户购买的笔记本电脑总数,另一个是计算购买最多的客户。您可能可以在单个规则中实现相同的功能,但结果将更加复杂,难以读取和维护

//We create a declared type to keep the information about the total
//amount of items purchased by a single client
declare TotalItemsByCustomer
  customer : Customer
  itemType : String
  amount   : Integer
end

rule "Dell Laptops by Customer"
when
  $c : Customer()
  $amount: Number() from accumulate( 
    Item(bought="Dell laptops", customer=$c.name, count(1))
then
  insert(new TotalItemsByCustomer($c, "Dell laptops", $amount));
end

rule "Customer who bought the most Dell laptops"
when
  $max: TotalItemsByCustomer(
    itemType == "Dell laptops"
  )
  not TotalItemsByCustomer(
    itemType == $max.itemType, 
    amount > $max.amount
  )
then
  System.out.println("Customer "+ $max.getCustomer().getName() + " is the one who bought more "+ $max.getItemType());
end
只要多一点爱,你就可以推广这两条规则,这样它们就可以用于“戴尔笔记本电脑”以外的物品


希望能有所帮助,

非常感谢。在第二条规则中,$max是如何工作的?TotalItemsByCustomer似乎有一个所有客户及其各自国家的列表,每个客户都有一个
TotalItemsByCustomer
。第二条规则基本上是这样的:
如果有一个TotalItemsByCustomer,而没有其他TotalItemsByCustomer的金额更大,那么…