具有嵌套成员的 DROOLS 局部变量赋值
DROOLS local variable assignment with nested members
我在 DROOLS 6.2 和带有两个 Java 类 的 Optaplanner 中分配嵌套 members/objects 的局部变量时遇到问题。我试图确定两个 facts/instances 嵌套成员何时具有相同的值。下面的测试用例很简单,实际上我正在尝试比较规则中的多个嵌套成员。
public class A {
private B x;
//public B getX(), public void setX(B b) follow ...
}
public class B {
private int y;
//public int getY(), public void setY(int y) follow ...
}
rule "testnestedmembers"
when
A(x.y : y, $x : x)
A(x2.y == y, $x : x2)
then
scoreHolder.addHardConstraintMatch(kcontext,-1000);
Message [id=1, level=ERROR, path=org/somebody/project/planner/solver/planScoreRules.drl, line=16, column=0
text=[ERR 102] Line 16:49 mismatched input ':' in rule "testnestedmembers"]
Message [id=2, level=ERROR, path=org/somebody/project/planner/solver/planScoreRules.drl, line=0, column=0 text=Parser returned a null Package]
---
Warning Messages:
---
Info Messages:
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildKieBase(ScoreDirectorFactoryConfig.java:387)
我已经查看了一些答案,例如:
Drools Rule Expression : Access Nested class data member
Geoffrey De Smet 的回答说明了条件分配,而不是本地分配。
我尝试了不同的变体,但没有运气。感谢您的任何建议。
编辑: 我应该说 创建绑定 而不是分配局部变量。
你不是这个意思吧:
when
$a1 : A($y1 : x.y)
A(this != $a1, x.y == $y1)
then
...
正如 Geoffrey De Smet 所说:
In DRL, ==
means equals, not same. With $varB : b
you do something like B varB = a.getB();
首先我会指出导致编译器错误的原因。
rule "testnestedmembers"
when
A(x.y : y, $x : x) // (1)
A(x2.y == y, $x : x2) // (2) (3)
(1) 绑定的格式为 <variable> : <field>
,但 x.y
不是有效的变量名。
(2) 同x2.y
。此外,x2
不是 A 中的字段。
(3) 没有什么能阻止规则引擎将相同的事实与 class A 的两个模式匹配。这意味着规则将针对 class A 的每个事实触发,因为(如您所愿) A.x.y 总是等于它自己。
正确的是
rule "testnestedmembers"
when
$a1: A( $x1: x )
A( this != $a1, $x2: x, $x1.getY() == $x2.getY() )
then
...
但是!此规则触发两次,一次将一个事实绑定到 $a1,一次将另一个(匹配的)事实绑定到 $a1。一种可能性是测试这样一对(或簇!)的存在:
rule "testnestedmembers"
when
$a1: A( $x1: x )
exists A( this != $a1, $x2: x, $x1.getY() == $x2.getY() )
then
...
另一种选择是通过测试属性来确保排序:
rule "testnestedmembers"
when
$a1: A( $x1: x, $id: id )
exists A( id > $id, $x2: x, $x1.getY() == $x2.getY() )
then
现在,当第二个 A 的 ID 更大(唯一!)时,它会触发每对 A。
我在 DROOLS 6.2 和带有两个 Java 类 的 Optaplanner 中分配嵌套 members/objects 的局部变量时遇到问题。我试图确定两个 facts/instances 嵌套成员何时具有相同的值。下面的测试用例很简单,实际上我正在尝试比较规则中的多个嵌套成员。
public class A {
private B x;
//public B getX(), public void setX(B b) follow ...
}
public class B {
private int y;
//public int getY(), public void setY(int y) follow ...
}
rule "testnestedmembers"
when
A(x.y : y, $x : x)
A(x2.y == y, $x : x2)
then
scoreHolder.addHardConstraintMatch(kcontext,-1000);
Message [id=1, level=ERROR, path=org/somebody/project/planner/solver/planScoreRules.drl, line=16, column=0
text=[ERR 102] Line 16:49 mismatched input ':' in rule "testnestedmembers"]
Message [id=2, level=ERROR, path=org/somebody/project/planner/solver/planScoreRules.drl, line=0, column=0 text=Parser returned a null Package]
---
Warning Messages:
---
Info Messages:
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildKieBase(ScoreDirectorFactoryConfig.java:387)
我已经查看了一些答案,例如: Drools Rule Expression : Access Nested class data member
Geoffrey De Smet 的回答说明了条件分配,而不是本地分配。 我尝试了不同的变体,但没有运气。感谢您的任何建议。
编辑: 我应该说 创建绑定 而不是分配局部变量。
你不是这个意思吧:
when
$a1 : A($y1 : x.y)
A(this != $a1, x.y == $y1)
then
...
正如 Geoffrey De Smet 所说:
In DRL,
==
means equals, not same. With$varB : b
you do something likeB varB = a.getB();
首先我会指出导致编译器错误的原因。
rule "testnestedmembers"
when
A(x.y : y, $x : x) // (1)
A(x2.y == y, $x : x2) // (2) (3)
(1) 绑定的格式为 <variable> : <field>
,但 x.y
不是有效的变量名。
(2) 同x2.y
。此外,x2
不是 A 中的字段。
(3) 没有什么能阻止规则引擎将相同的事实与 class A 的两个模式匹配。这意味着规则将针对 class A 的每个事实触发,因为(如您所愿) A.x.y 总是等于它自己。
正确的是
rule "testnestedmembers"
when
$a1: A( $x1: x )
A( this != $a1, $x2: x, $x1.getY() == $x2.getY() )
then
...
但是!此规则触发两次,一次将一个事实绑定到 $a1,一次将另一个(匹配的)事实绑定到 $a1。一种可能性是测试这样一对(或簇!)的存在:
rule "testnestedmembers"
when
$a1: A( $x1: x )
exists A( this != $a1, $x2: x, $x1.getY() == $x2.getY() )
then
...
另一种选择是通过测试属性来确保排序:
rule "testnestedmembers"
when
$a1: A( $x1: x, $id: id )
exists A( id > $id, $x2: x, $x1.getY() == $x2.getY() )
then
现在,当第二个 A 的 ID 更大(唯一!)时,它会触发每对 A。