Drools:如何在not表达式内部实现和(&&)条件?

huangapple go评论81阅读模式
英文:

Drools: how to implement and ( && ) condition inside not expression?

问题

我有以下规则:

rule " data value > 50 for 10 seconds "
dialect "java"
when
data: MyData(name.contains("double") && getValue() > 50) from entry-point "dataEntryPoint"
not(
myData(this.name==dataName,
getValue() < 50 ,
this after[0,10s] data ) from entry-point "dataEntryPoint" )

then
doSmthg();
end


数据类型包含在名称中,我只想针对双精度值编写此规则。
然而,在第二个表达式(not 内部)中,即使 this.name != dataName,也会调用 getValue(),这会导致错误,因为该值是一个字符串。

基本上,我希望只有在第一个条件为真时(&&)才评估 getValue()。

只有在删除 not() 条件时,getValue() > 50 才会在第一个条件为真时才被评估。

rule " data value > 50 for 10 seconds "
dialect "java"
when
data: MyData(name.contains("double") && getValue() > 50) from entry-point "dataEntryPoint"

then
doSmthg();
end

英文:

I have the following rule :

rule &quot; data value &gt; 50 for 10 seconds &quot;
dialect &quot;java&quot;
when
	data: MyData(name.contains(&quot;double&quot;) &amp;&amp; getValue()&gt; 50) from entry-point &quot;dataEntryPoint&quot;	
	not(
		myData(this.name==dataName,
		getValue() &lt; 50 ,
		this after[0,10s] data ) from entry-point &quot;dataEntryPoint&quot; )
	
then
	doSmthg();
end

The data type is included in the name and I want to write this rule only on double values.
However, it seems that in the second expression ( inside the not) , getValue() is being called even when this.name != dataName which causes an error since the value is a String.

Basically, I want it to only evaluate getValue() if the first condition is true ( && ).

I'm only having this issue when I add the not() condition.

When I remove it, the getValue() > 50 is only evaluated when the first condition is true.

rule &quot; data value &gt; 50 for 10 seconds &quot;
dialect &quot;java&quot;
when
	data: MyData(name.contains(&quot;double&quot;) &amp;&amp; getValue()&gt; 50) from entry-point &quot;dataEntryPoint&quot;	
	
then
	doSmthg();
end

答案1

得分: 2

有趣的是,它只在&amp;&amp;运算符的两个部分都是同一变量的约束条件时才起作用。

rule "data value > 50 for 10 seconds"
dialect "java"
when
    data: MyData(name contains "double" && value > 50) from entry-point "dataEntryPoint"
    not(
        MyData(name == data.name, 
        value instanceof Number && value < 50,
        this after[0,10s] data) from entry-point "dataEntryPoint")
then
    System.out.println("alert");
end
英文:

Interestingly, it works only when both parts of &amp;&amp; operator are constraints of the same variable.

rule &quot;data value &gt; 50 for 10 seconds&quot;
dialect &quot;java&quot;
when
    data: MyData(name contains &quot;double&quot; &amp;&amp; value &gt; 50) from entry-point &quot;dataEntryPoint&quot;   
    not(
        MyData(name == data.name, 
        value instanceof Number &amp;&amp; value &lt; 50,
        this after[0,10s] data) from entry-point &quot;dataEntryPoint&quot;)
then
    System.out.println(&quot;alert&quot;);
end

huangapple
  • 本文由 发表于 2020年9月29日 19:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64118306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定