Drools JBOSS规则嵌套IF [英] Drools JBOSS rule Nested IF's

查看:548
本文介绍了Drools JBOSS规则嵌套IF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Jboss Rule的新手。我已经阅读了文档,但是找不到用于为以下代码示例编写规则的方法。

I am a newbie with Jboss Rule. I have gone through the documentation but I fail to find the approach to begin writing rule for following code sample.

For each User code in the list 
{
    If User code = ‘11’ 
    {
        If User code ‘17’ present in the group 
        {
            Add letter id 1
        }
        Else If User code ‘18’ present in the group 
        {
            Add letter id 2
        }
    }
    Else 
    {
        Add letter id 3 which is the letter need to be sent for code 11
    }
}

如果有人能给我一个提示/想法,我将不胜感激?

I would really appreciate if anyone can give me a hint/idea how to go about it?

编辑:

到目前为止,这是我可以为讨论的用户案例开发的

So far this is what i can develop for the user case discussed above.


  1. 我正在将( UserDetailVo 的列表)插入到drools会话中。

  2. 对象( UserDetail Vo )包含( UserInfoVo 的列表)。每个 UserInfoVo 都包含一个代码。

  1. I am inserting (List of UserDetailVo) to the drools session.
  2. Object (UserDetailVo) contains (List of UserInfoVo). Every UserInfoVo contains a code.

现在我要遍历( UserInfoVo )的列表并更新( letterId )到每个( UserDetailVo ),如下所示。

Now I want to iterate over the (List of UserInfoVo) and update (letterId) to every (UserDetailVo) as i am trying to do below.

案例1:当codeList包含110,121

rule "USER LETTER GROUPING 110,121" 
salience 300
no-loop true

    when
        userDetailVo : UserDetailVo ()
        UserInfoVo(code=="110") from userDetailVo.codeList
        UserInfoVo(code=="121") from userDetailVo.codeList
    then
        userDetailVo.addLetterId(1);
        //modify(trrDetailRequestVo)
end

情况2:何时codeList有110,127

rule "USER LETTER GROUPING 110,127" 
        salience 300
        no-loop true

            when
                userDetailVo : UserDetailVo ()
                UserInfoVo(code=="110") from userDetailVo.codeList
                UserInfoVo(code=="127") from userDetailVo.codeList
            then
                userDetailVo.addLetterId(2);
                //modify(trrDetailRequestVo)
        end

案例3:何时codeList只有110

rule "USER LETTER GROUPING 110" 
    salience 300
    no-loop true

        when
            userDetailVo : UserDetailVo (this.letterID.size() == 0) // Checking size of the list
            UserInfoVo(code=="110") from userDetailVo.codeList
        then
            userDetailVo.addLetterId(3);
            //modify(trrDetailRequestVo)
    end

我面临的问题是如果我在规则末尾修改/更新。它进入无限循环。如果我删除了修改/更新,则在情况3中,尽管列表大小大于0仍然会触发规则。

Issues that I am facing is if I user modify/update at the end of rule. It goes into an infinite loop. If I remove modify/update, in case 3 though list size if greater than 0 still rule is fired.

推荐答案

其他并没有真正直接翻译成声明性语言。相反,您必须明确定义其他对数据的真正含义,并根据该条件编写一个或多个规则。例如,请考虑以下条件:

The concept of "else" doesn't really translate directly to a declarative language. Instead, you have to explicitly define what "else" truly means in terms of your data and write a rule (or rules) based on that condition. As an example, consider the following conditions:

If Order Total >= 1000
  Discount = 15%
Else If Order Total >= 500
  Discount = 10%
Else If Order Total >= 250
  Discount = 5%
Else
  Discount = 0%

针对这些条件的规则的自然首次尝试可能是:

The natural first attempt at rules for these conditions might be:

rule "15% discount"
  when
    $o : Order( total >= 1000 )
  then
    modify($o) { setDiscount(.15); }
end

rule "10% discount"
  when
    $o : Order( total >= 500 )
  then
    modify($o) { setDiscount(.10); }
end

etc...

问题这种方法是10%和15%的规则都将匹配总数为1200的订单。这是因为 else if的 else部分固有地包含了订单总数不大于1000的条件。我们改写这样的规则:

The problem with this approach is that both the 10% and 15% rules will match for an Order whose total is 1200. This is because the "else" part of "else if" inherently includes the condition that the order total is not greater than 1000. So we write our rules like this instead:

rule "15% discount"
  when
    $o : Order( total >= 1000, discount == null )
  then
    modify($o) { setDiscount(.15); }
end

rule "10% discount"
  when
    $o : Order( total >= 500, total < 1000, discount == null )
  then
    modify($o) { setDiscount(.10); }
end

rule "5% discount"
  when
    $o : Order( total >= 250, total < 500, discount == null )
  then
    modify($o) { setDiscount(.05); }
end

rule "no discount"
  when
    $o : Order( total < 250, discount == null )
  then
    modify($o) { setDiscount(0); }
end

(您会注意到我还添加了 Discount == null 条件。这是为了防止在更新订单事实时重新激活规则。)

(You'll notice I also added a discount == null condition. This is to prevent the rule from reactivating when the Order fact is updated.)

我一直关注一个简单的示例而不是您的特定用例来演示编写条件来模仿其他的概念。我仍然不确定我是否完全理解您的用例,但希望我已经回答了您问题的关键概念部分。

I've been focusing on a simple example rather than your specific use case to demonstrate the concept of writing a condition to mimic an "else". I'm still not sure I completely understand your use case, but hopefully I've answered the key conceptual part of your question.

这篇关于Drools JBOSS规则嵌套IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆