“if then else”规则引擎

Ric*_*bby 5 java rule-engine rules drools

我是 drools 的新手,并给出了一个条件 (Condition) 和一个布尔变量 "a" ,我想用 drools 创建以下规则:

if (Condition)
   { 
    a = true;
   }
else
   {
    a = false;
   }
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?

目前我有两个选择:

1.用条件而不是条件创建2条规则(如果......那么......,如果不是......那么......)

rule "test"
where
  $o: Object( Condition)
then 
  $o.a = true;
end


rule "test2"
where
  $o: Object( not Condition)
then 
  $o.a = false
end
Run Code Online (Sandbox Code Playgroud)

2.默认将变量a设置为false,然后触发规则

rule "test"
no loop
salience 100
where 
  $o: Object()
then 
  $o.a = false;
end


rule "test"
where
  $o: Object( not Condition)
then 
  $o.a = true;
end
Run Code Online (Sandbox Code Playgroud)

Per*_*ion 6

本质上,Rete 引擎会寻找正匹配,所以是的,您将需要多个规则,一个用于 if-then-else 块中的每个条件检查。你的第一个例子更清晰、更直观,我会这样做。

作为替代方案,如果您正在处理一个简单的逻辑否定 (if-else),其中您的变量值与条件匹配,那么您可以只使用一个规则:

rule "test"
where 
  $o: Object()
then 
  $o.a = (! Condition);
end
Run Code Online (Sandbox Code Playgroud)