停用drools中的规则流组

停用drools中的规则流组,drools,Drools,我有一个drl文件,它在两个规则流组中包含规则:“第一个规则流组”和“第二个规则流组”。这些组的激活取决于“规则A”和“规则B”。是否有任何方法可以在规则A条件匹配时停用规则B以触发,从而仅将焦点设置为“第一个规则流组” 改变你的规则依赖于排他性条件 简单的例子。假设我们有一个处理日历事件的应用程序。我们对公众假期有规定。我们对宗教节日有规定。有些宗教节日也是公共假日;对于这些,我们只想取消公共假日规则 rule "Public Holidays" when Holida

我有一个drl文件,它在两个规则流组中包含规则:“第一个规则流组”和“第二个规则流组”。这些组的激活取决于“规则A”和“规则B”。是否有任何方法可以在规则A条件匹配时停用规则B以触发,从而仅将焦点设置为“第一个规则流组”


改变你的规则依赖于排他性条件

简单的例子。假设我们有一个处理日历事件的应用程序。我们对公众假期有规定。我们对宗教节日有规定。有些宗教节日也是公共假日;对于这些,我们只想取消公共假日规则

rule "Public Holidays"
when
  Holiday( isPublic == true )
then
  drools.setFocus("publicholiday-flow");
end

rule "Religious Holidays"
when
  Holiday( isReligious == true,
           isPublic == false )
then
  drools.setFocus("religiousholiday-flow");
end
因此,基本上,修改“规则B”以包含否定规则a条件的条件,这样,如果规则a匹配,则规则B必然不匹配


第二种解决方案是让第一个规则流中的规则(由A触发)设置一个条件,使规则B不会触发。它基本上与前面的解决方案相似,只是保持规则B不触发的条件是动态的

作为一个例子,想象一个应用程序,它确定一个人因停车欠多少钱。停车费是按天收费的——周一到周五有一种收费方式,周六有一种收费方式,周日是免费的。此外,还可以享受其他折扣,例如老年人,或者如果总停车时间少于5分钟。使用第一个规则流(A)确定星期日费率,使用第二个规则流(B)确定折扣。如果是星期天,没有理由取消第二套规则

您可以将折扣规则流触发器编写为显式不针对星期日触发,也可以让星期日费率规则收回或插入数据,使折扣规则不再对运行有效

rule "Day Rates"
when
then
  drools.setFocus("dayrates-flow");
end

rule "Discounts"
when
  exists(Rate( hourly > 0 ))
then
  drools.setFocus("discounts-flow");
end

// The Sunday rule, part of the dayrates-flow, sets hourly = 0, which makes
// the "Discounts" rule no longer valid to fire.
rule "Sunday Rate"
ruleflow-group "dayrates-flow"
when
  not(Rate())
  Request( day == DayOfWeek.SUNDAY ) // when parking on Sunday...
then
  Rate rate = new Rate();
  rate.setHourlyRate(0.0);
  insert(rate);
end

另一种选择是从第一个规则流中触发第二个规则流,但仅在需要时触发

将前面的示例重新用于停车:

rule "Day Rates"
when
then
  drools.setFocus("dayrates-flow");
end


// Here are some day rate rules. Most are omitted for brevity. We include three (3)
// to show regular rates, the special case of Sunday, and how we trigger the discount
// rate rules

rule "Saturday Rate"
ruleflow-group "dayrates-flow"
when
  not(Rate())
  Request( day == DayOfWeek.SATURDAY )
then
  Rate rate = new Rate();
  rate.setHourly(1.0); // Saturday rate: $1/hr
  insert(rate);
end

rule "Sunday Rate"
ruleflow-group "dayrates-flow"
when
  not(Rate())
  Request( day == DayOfWeek.SUNDAY )
then
  Rate rate = new Rate();
  rate.setHourlyRate(0.0); // Sunday rate: free
  insert(rate);
end

// This rule only triggers the next rule flow when the rate is positive (eg not Sunday)
rule "Transition to Discounts"
ruleflow-group "dayrates-flow"
when
  exists(Rate( hourly > 0 ))
then
  drools.setFocus("discount-flow");
end
注意:
eval(true)
是不必要的。只需将when子句留空即可。
rule "Day Rates"
when
then
  drools.setFocus("dayrates-flow");
end


// Here are some day rate rules. Most are omitted for brevity. We include three (3)
// to show regular rates, the special case of Sunday, and how we trigger the discount
// rate rules

rule "Saturday Rate"
ruleflow-group "dayrates-flow"
when
  not(Rate())
  Request( day == DayOfWeek.SATURDAY )
then
  Rate rate = new Rate();
  rate.setHourly(1.0); // Saturday rate: $1/hr
  insert(rate);
end

rule "Sunday Rate"
ruleflow-group "dayrates-flow"
when
  not(Rate())
  Request( day == DayOfWeek.SUNDAY )
then
  Rate rate = new Rate();
  rate.setHourlyRate(0.0); // Sunday rate: free
  insert(rate);
end

// This rule only triggers the next rule flow when the rate is positive (eg not Sunday)
rule "Transition to Discounts"
ruleflow-group "dayrates-flow"
when
  exists(Rate( hourly > 0 ))
then
  drools.setFocus("discount-flow");
end