gpt4 book ai didi

java - "if then else "使用规则引擎

转载 作者:行者123 更新时间:2023-12-04 11:49:34 25 4
gpt4 key购买 nike

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

if (Condition)
{
a = true;
}
else
{
a = false;
}

最好的方法是什么?

目前我有两个选择:

1.创建2条条件而不是contidition规则(如果...那么...,如果不是...那么...)
rule "test"
where
$o: Object( Condition)
then
$o.a = true;
end


rule "test2"
where
$o: Object( not Condition)
then
$o.a = false
end

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

最佳答案

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

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

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

关于java - "if then else "使用规则引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7993659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com