英文:
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 " 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
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 " data value > 50 for 10 seconds "
dialect "java"
when
data: MyData(name.contains("double") && getValue()> 50) from entry-point "dataEntryPoint"
then
doSmthg();
end
答案1
得分: 2
有趣的是,它只在&&
运算符的两个部分都是同一变量的约束条件时才起作用。
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 &&
operator are constraints of the same variable.
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论