Правило слюней для подсчета количества предметов - PullRequest
0 голосов
/ 06 июня 2018

Я новичок в Drools и пытаюсь создать правило, которое бы отображало клиента, купившего максимум ноутбук dell.Пожалуйста, помогите мне исправить правило ниже:

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

1 Ответ

0 голосов
/ 06 июня 2018

Принимая во внимание очень упрощенную модель, один из самых простых способов добиться этого - использовать комбинацию из двух правил: одно вычисляет общее количество ноутбуков, купленных клиентом, и другое, чтобы получить покупателя, который купил больше всего.Вероятно, вы можете достичь того же самого в одном правиле, но результат будет сложнее читать и поддерживать.

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

Надеюсь, это поможет,

...