Drools - 使用累积来查找最小值和最大值

发布于 2024-12-01 09:35:42 字数 610 浏览 2 评论 0原文

我有一个流口水的问题,已经困扰我一段时间了。我想使用累积从 Item 对象列表(包含价格)中找出最低和最高价格。插入一个包含 Item 列表的 Member 对象(包含 Item 对象列表)。

groovy/java source pseudo code
-------------------------------
class Item {
   BigDecimal price
}

class Member {
   List<Item>> items
}

...
droolsStatefulSession.insert(member)
session.fireAllRules()
...

rule.drl
---------
rule "rule1"
when 
   member : Member ($itemList : items)
/*

*/
then
 System.out.println("condition met...")
end

现在的问题是上述规则是否可能,如果可以,我如何使用流口水累积功能找出具有最低价格和最高价格的商品。我不想使用 java/groovy 实用函数。

我看到“收集”功能允许使用“来自”,然后使用数据源。我想知道“积累”是否与收集相似。

I have a drools question which has been troubling me for some time. I want to find out the min and max price from a list of Item objects (contains price) using accumulate. A Member object (which contails list of Item objects) is inserted which contains the list of Items.

groovy/java source pseudo code
-------------------------------
class Item {
   BigDecimal price
}

class Member {
   List<Item>> items
}

...
droolsStatefulSession.insert(member)
session.fireAllRules()
...

rule.drl
---------
rule "rule1"
when 
   member : Member ($itemList : items)
/*

*/
then
 System.out.println("condition met...")
end

Now the questions is in the above rule is it possible to if so how do I find out the item with the minimum Price and maximum price using drools accumulate feature. I do not want to use an java/groovy utility functions.

I see the "collect" feature allows to use "from" and then a datasource. I wonder if "accumulate" is similar to collect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痞味浪人 2024-12-08 09:35:42

无需使用“accumulate”,只需执行类似

Item($lowestPrice : price, $id : id)
not Item(price > $lowestPrice, id < $id)

“That's if your Items is inserted into theworking memory”的操作即可。

No need to use accumulate, just do something like

Item($lowestPrice : price, $id : id)
not Item(price > $lowestPrice, id < $id)

That's if your Items are inserted into the working memory.

梦纸 2024-12-08 09:35:42

我是流口水规则的新手,我正在尝试解决这个问题。

您可以简单地找出商品的最低和最高价格 - 您必须编写一些规则并需要在 Order 类中添加两个变量:

Class Order
{
    private List<Item> itemList;
    private int highPrice;
    private int lowPrice;
}

使用以下规则您可以计算商品的最低和最大值:

package com.sample

import com.sample.DroolsTest.Message;

rule "rule1"

    when
        $order : Order($itemList:itemList)

         $item:Item() from $order.itemList
    then
              insertLogical($item);
    end

rule "highPriceRule"
    when
         $order : Order()

         $item:Item($price:price,price>=$order.getHighPrice())

    then
        $order.setHighPrice($item.getPrice());
end

rule "lowPriceRule"

    when

         $order : Order()


         $item:Item($price:price,price<=$order.getLowPrice()||$order.getLowPrice()==0)

    then

        $order.setLowPrice($item.getPrice());

end

rule "highPrice"

salience -1

    when

         $order : Order()

    then

        System.out.println( "higher Item Price is  "+$order.getHighPrice());

end
rule "LowPrice"
salience -1
    when
         $order : Order()
    then
        System.out.println( "Lower Item Price is  "+$order.getLowPrice());
end


in main method you have to write this code and run it


List<Item>  items=new ArrayList<Item>();

        Item item1=new Item();
        item1.setPrice(10);

        Item item2=new Item();
        item2.setPrice(20);


        Item item3=new Item();
        item3.setPrice(10);

        Item item4=new Item();
        item4.setPrice(5);

        items.add(item1);
        items.add(item2);
        items.add(item3);
        items.add(item4);


           Order order=new Order();
           order.setItemList(items);
            ksession.insert(order);
            ksession.fireAllRules();

输出:---

Lower Item Price is  5


higher Item Price is  20

As我是流口水规则的新手,我想问这是否是正确的程序?或者有其他方法可以解决这个问题吗?

I'm new to the drool rule and I'm trying to solve this issue.

You can find out min and max price of item simply - you have to write some rules and need to add two variable in the Order class:

Class Order
{
    private List<Item> itemList;
    private int highPrice;
    private int lowPrice;
}

Using the following rule you can calculate min and max value of an item:

package com.sample

import com.sample.DroolsTest.Message;

rule "rule1"

    when
        $order : Order($itemList:itemList)

         $item:Item() from $order.itemList
    then
              insertLogical($item);
    end

rule "highPriceRule"
    when
         $order : Order()

         $item:Item($price:price,price>=$order.getHighPrice())

    then
        $order.setHighPrice($item.getPrice());
end

rule "lowPriceRule"

    when

         $order : Order()


         $item:Item($price:price,price<=$order.getLowPrice()||$order.getLowPrice()==0)

    then

        $order.setLowPrice($item.getPrice());

end

rule "highPrice"

salience -1

    when

         $order : Order()

    then

        System.out.println( "higher Item Price is  "+$order.getHighPrice());

end
rule "LowPrice"
salience -1
    when
         $order : Order()
    then
        System.out.println( "Lower Item Price is  "+$order.getLowPrice());
end


in main method you have to write this code and run it


List<Item>  items=new ArrayList<Item>();

        Item item1=new Item();
        item1.setPrice(10);

        Item item2=new Item();
        item2.setPrice(20);


        Item item3=new Item();
        item3.setPrice(10);

        Item item4=new Item();
        item4.setPrice(5);

        items.add(item1);
        items.add(item2);
        items.add(item3);
        items.add(item4);


           Order order=new Order();
           order.setItemList(items);
            ksession.insert(order);
            ksession.fireAllRules();

output:---

Lower Item Price is  5


higher Item Price is  20

As I'm new to the drool rule I would like to ask if this is the correct procedure? Or is there another way to solve this problem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文