流口水:仅在存在的情况下进行Modify()或update(),否则添加 [英] Drools: modify() or update() only if exists, otherwise add

查看:98
本文介绍了流口水:仅在存在的情况下进行Modify()或update(),否则添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的规则:

rule "Set value of LeftArm fluent" 
when
    $ev: Start()
    $fl:LeftArm()
then
    Sample s = new Sample();
    s.setFluent($fl);
    s.setValue(-1.0);
    insert(s);
end

好,但是如果我想设置样本的值没有具有相同$ fl的Sample并修改了Sample的值,我该怎么办?
我有义务写2条规则吗?

Ok, but if I want to set the value of the sample only if I haven't Samples with the same $fl AND otherwise modify the value of Sample, how can I do it? Am I obliged to write 2 rules?

推荐答案

不,您只需添加应该禁止插入的条件一个新样本:

No, you just add the condition that should inhibit the insertion of a new sample:

rule "Set value of LeftArm fluent" 
when
  $ev: Start()
  $fl:LeftArm()
  not Sample( fluent == $fl )
then
  Sample s = new Sample();
  s.setFluent($fl);
  s.setValue(-1.0);
  insert(s);
end

修改Q后
如果已经有一个示例事实,并且需要将Start / LeftArm组合的setValue设置为-1.0,则需要两个规则,但是可以使用扩展:

After modification of the Q If you already have a Sample fact and need to setValue to -1.0 for the combination Start/LeftArm you'll need two rules, but you can use extends:

rule "StartLeftArm" 
when
  $ev: Start()
  $fl:LeftArm()
then
end

rule "create Sample" extends "StartLeftArm"
when
    not Sample( fluent == $fl )
then
    Sample s = new Sample();
    s.setFluent( $fl );
    insert( s );
end
rule "set Sample Value" extends "StartLeftArm"
when
    $s: Sample( fluent == $fl, value != -1.0 )
then
    modify( $s ){ setValue( -1.0 ) }
end

这篇关于流口水:仅在存在的情况下进行Modify()或update(),否则添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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