流口水规则:算术运算
Drool Rules: Arithmetic Operations
我正在尝试编写一个简单的规则。执行一组计算。我知道可以通过函数处理计算。但是,业务逻辑涉及计算。
package com.sample.rules
import com.sample.rules.Test
rule "Calculate"
when
Test(calculate == true)
then
finalValue = Test(InitialValue) + 10;
Test.setFinalValue(finalValue)
System.out.println("FinalValue=" + Test.getFinalValue();
end
带有变量
的 Class 文件
public class Test {
private int finalValue;
private int initialValue;
private boolean calculate;
public int getFinalValue() {
return FinalValue;
}
public void setFinalValue(int FinalValue) {
this.FinalValue = FinalValue;
}
public int getInitialValue() {
return InitialValue;
}
public void setInitialValue(int InitialValue) {
this.InitialValue = InitialValue;
}
public boolean isCalculate() {
return calculate;
}
public void setCalculate(boolean calculate) {
this.calculate = calculate;
}
}
App文件如下图
public class CalculationApp {
public static void main(String[] args) {
System.out.println( "Bootstrapping the Rule Engine ..." );
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
Test test = new Test();
test.setInitialValue(20);
test.setCalculate(true);
kSession.insert(test);
int fired = kSession.fireAllRules();
System.out.println( "Number of Rules executed = " + fired );
}
}
规则文件抛出错误:
Rule Compilation error finalValue cannot be resolved to a variable
Cannot make a static reference to the non-static method getInitialValue() from the type Test
finalValue cannot be resolved to a variable
Cannot make a static reference to the non-static method getFinalValue() from the type Test
虽然我通过反复试验找到了答案:我正在尝试理解其背后的逻辑。我的意思是,在 when
条件下分配 variable
(我知道变量有不同的含义)/assignment
是可以理解的,但是 Then
部分是如何工作的.我的意思是,item.setFinalValue(...)
部分。
有效的是:
rule "Calculate"
when
item: Test(calculate == true)
then
item.setFinalValue(item.getInitialValue() + 10)
System.out.println("FinalValue=" + item.getFinalValue();
end
如错误所示,您的规则存在多个问题。
Rule Compilation error finalValue cannot be resolved to a variable
finalValue cannot be resolved to a variable
第一个问题是您从未声明 [=13=],只是尝试在右侧分配给它。要解决此问题,只需声明 finalValue。假设它是一个整数:
int finalValue = ...
这与它在 Java 中的工作方式相同。
但是考虑到原始规则的样子,我认为您正试图从 Test 对象中提取初始值。为此,您需要执行以下操作:
rule "Calculate"
when
Test( calculate == true,
$initialValue: initialValue ) // assign the $initialValue variable
then
int finalValue = $initialValue + 10; // set finalValue by adding 10 to $initialValue
Cannot make a static reference to the non-static method getInitialValue() from the type Test
Cannot make a static reference to the non-static method getFinalValue() from the type Test
这两个错误都表明您正在尝试调用没有对象实例的实例方法。这再次与 Java 相同,如果您有实例方法,则必须针对实例调用它。
假设这些是 Test
class 上的方法,您可以将规则更改为:
package com.sample.rules
import com.sample.rules.Test
rule "Calculate"
when
$test : Test( calculate == true,
$initialValue: initialValue )
then
int finalValue = $initialValue + 10;
$test.setFinalValue(finalValue); // call against $test instance
System.out.println("FinalValue=" + $test.getFinalValue()); // you were also missing a parenthesis
end
我正在尝试编写一个简单的规则。执行一组计算。我知道可以通过函数处理计算。但是,业务逻辑涉及计算。
package com.sample.rules
import com.sample.rules.Test
rule "Calculate"
when
Test(calculate == true)
then
finalValue = Test(InitialValue) + 10;
Test.setFinalValue(finalValue)
System.out.println("FinalValue=" + Test.getFinalValue();
end
带有变量
的 Class 文件public class Test {
private int finalValue;
private int initialValue;
private boolean calculate;
public int getFinalValue() {
return FinalValue;
}
public void setFinalValue(int FinalValue) {
this.FinalValue = FinalValue;
}
public int getInitialValue() {
return InitialValue;
}
public void setInitialValue(int InitialValue) {
this.InitialValue = InitialValue;
}
public boolean isCalculate() {
return calculate;
}
public void setCalculate(boolean calculate) {
this.calculate = calculate;
}
}
App文件如下图
public class CalculationApp {
public static void main(String[] args) {
System.out.println( "Bootstrapping the Rule Engine ..." );
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
Test test = new Test();
test.setInitialValue(20);
test.setCalculate(true);
kSession.insert(test);
int fired = kSession.fireAllRules();
System.out.println( "Number of Rules executed = " + fired );
}
}
规则文件抛出错误:
Rule Compilation error finalValue cannot be resolved to a variable
Cannot make a static reference to the non-static method getInitialValue() from the type Test
finalValue cannot be resolved to a variable
Cannot make a static reference to the non-static method getFinalValue() from the type Test
虽然我通过反复试验找到了答案:我正在尝试理解其背后的逻辑。我的意思是,在 when
条件下分配 variable
(我知道变量有不同的含义)/assignment
是可以理解的,但是 Then
部分是如何工作的.我的意思是,item.setFinalValue(...)
部分。
有效的是:
rule "Calculate"
when
item: Test(calculate == true)
then
item.setFinalValue(item.getInitialValue() + 10)
System.out.println("FinalValue=" + item.getFinalValue();
end
如错误所示,您的规则存在多个问题。
Rule Compilation error finalValue cannot be resolved to a variable
finalValue cannot be resolved to a variable
第一个问题是您从未声明 [=13=],只是尝试在右侧分配给它。要解决此问题,只需声明 finalValue。假设它是一个整数:
int finalValue = ...
这与它在 Java 中的工作方式相同。
但是考虑到原始规则的样子,我认为您正试图从 Test 对象中提取初始值。为此,您需要执行以下操作:
rule "Calculate"
when
Test( calculate == true,
$initialValue: initialValue ) // assign the $initialValue variable
then
int finalValue = $initialValue + 10; // set finalValue by adding 10 to $initialValue
Cannot make a static reference to the non-static method getInitialValue() from the type Test
Cannot make a static reference to the non-static method getFinalValue() from the type Test
这两个错误都表明您正在尝试调用没有对象实例的实例方法。这再次与 Java 相同,如果您有实例方法,则必须针对实例调用它。
假设这些是 Test
class 上的方法,您可以将规则更改为:
package com.sample.rules
import com.sample.rules.Test
rule "Calculate"
when
$test : Test( calculate == true,
$initialValue: initialValue )
then
int finalValue = $initialValue + 10;
$test.setFinalValue(finalValue); // call against $test instance
System.out.println("FinalValue=" + $test.getFinalValue()); // you were also missing a parenthesis
end