Принимая во внимание очень упрощенную модель, один из самых простых способов добиться этого - использовать комбинацию из двух правил: одно вычисляет общее количество ноутбуков, купленных клиентом, и другое, чтобы получить покупателя, который купил больше всего.Вероятно, вы можете достичь того же самого в одном правиле, но результат будет сложнее читать и поддерживать.
//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
С немного большей любовью вы можете обобщить эти 2 правила, чтобы их можно было использовать дляпредметы, кроме «Ноутбуков Dell».
Надеюсь, это поможет,