drools计算项目数量的规则

时间:2018-06-06 04:06:52

标签: drools

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

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

1 个答案:

答案 0 :(得分:0)

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

//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

如果有更多的爱,你可以概括这两条规则,这样它们就可以用于戴尔笔记本电脑以外的物品了。

希望它有所帮助,