在规则 LHS 中 'or' 的情况下将变量绑定到不同的数据类型
Binding variables to different data types in case of 'or' in the rule LHS
如何将变量绑定到规则 LHS 中 or
组中匹配的不同类型的事实?
例如,如果我有以下规则文件:
package com.sample
rule "Rule1"
when
object1: ObjectType1( id == 1) or
object2: ObjectType2( id == 2)
then
System.out.println(object1.getId());
System.out.println(object2.getId());
end
我使用这个驱动程序代码:
package com.sample;
import org.kie.api.runtime.KieSession;
public class DroolsTest {
public static final void main(String[] args) {
try {
String ruleFilePath = "src/main/resources/rules/ruleFile.drl";
KieSession kSession = KSessionUtil.buildKSession(ruleFilePath);
ObjectType1 o1 = new ObjectType1(1);
ObjectType2 o2 = new ObjectType2(2);
kSession.insert(o1);
kSession.insert(o2);
kSession.fireAllRules();
System.out.println("Bye");
} catch (Throwable t) {
t.printStackTrace();
}
}
}
ObjectType1.java
:
package com.sample;
public class ObjectType1 {
public ObjectType1(int i) {
super();
this.id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int id;
}
ObjectType2.java
:
package com.sample;
public class ObjectType12 {
public ObjectType2(int i) {
super();
this.id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int id;
}
我从 Drools Eclipse 插件中收到语法错误:
object1 cannot be resolved.
object2 cannot be resolved.
如果我将规则 LHS 中的 or
更改为 and
,错误就会消失。
我正在使用 Drools 6.2.0。
棘手的部分是 Drools 如何处理模式之间的 or
操作数。在您的示例中,Drools 会将您的规则分解为 2 个独立的规则:
rule "Rule1_A"
when
object1: ObjectType1( id == 1)
then
System.out.println(object1.getId());
System.out.println(object2.getId());
end
rule "Rule1_B"
when
object2: ObjectType2( id == 2)
then
System.out.println(object1.getId());
System.out.println(object2.getId());
end
如您所见,错误现在变得更加明显。
Drools 处理方式的一个副作用 or
也是在这个操作中没有短路:如果两个对象都存在于您的会话中,规则将被执行两次。
希望对您有所帮助,
如中所述,无法以这种方式绑定变量。
相反,我们可以做的是创建一个包装器 class,它将包含一个对象,每个对象都需要匹配各种不同的数据类型:
public class ObjectType {
ObjectType1 ob1;
ObjectType2 ob2;
// setters and getters
}
现在,如果我们想将 ObjectType1
的对象插入到知识会话中:
ObjectType1 object1 = new ObjectType1();
kSession.insert(object1);
我们可以改为将 ObjectType.ob1
设置为引用 object1
,然后将新的 ObjectType
对象插入到会话中:
ObjectType1 object1 = new ObjectType1();
ObjectType object = new ObjectType();
object.setOb1(object1);
kSession.insert(object);
现在在规则文件中,我们需要将 ObjectType1
类型的对象与 ObjectType(getOb1() != null)
而不是 ObjectType1()
进行匹配:
rule "Rule1"
when
object: ( ObjectType( ob1 != null && ob1.getId() == 1 ) or
ObjectType( ob2 != null && ob2.getId() == 2 ) )
then
if ( object.getOb1() != null )
{
System.out.println(object.getOb1().getId());
}
else
{
System.out.println(object.getOb2().getId());
}
end
如何将变量绑定到规则 LHS 中 or
组中匹配的不同类型的事实?
例如,如果我有以下规则文件:
package com.sample
rule "Rule1"
when
object1: ObjectType1( id == 1) or
object2: ObjectType2( id == 2)
then
System.out.println(object1.getId());
System.out.println(object2.getId());
end
我使用这个驱动程序代码:
package com.sample;
import org.kie.api.runtime.KieSession;
public class DroolsTest {
public static final void main(String[] args) {
try {
String ruleFilePath = "src/main/resources/rules/ruleFile.drl";
KieSession kSession = KSessionUtil.buildKSession(ruleFilePath);
ObjectType1 o1 = new ObjectType1(1);
ObjectType2 o2 = new ObjectType2(2);
kSession.insert(o1);
kSession.insert(o2);
kSession.fireAllRules();
System.out.println("Bye");
} catch (Throwable t) {
t.printStackTrace();
}
}
}
ObjectType1.java
:
package com.sample;
public class ObjectType1 {
public ObjectType1(int i) {
super();
this.id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int id;
}
ObjectType2.java
:
package com.sample;
public class ObjectType12 {
public ObjectType2(int i) {
super();
this.id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int id;
}
我从 Drools Eclipse 插件中收到语法错误:
object1 cannot be resolved.
object2 cannot be resolved.
如果我将规则 LHS 中的 or
更改为 and
,错误就会消失。
我正在使用 Drools 6.2.0。
棘手的部分是 Drools 如何处理模式之间的 or
操作数。在您的示例中,Drools 会将您的规则分解为 2 个独立的规则:
rule "Rule1_A"
when
object1: ObjectType1( id == 1)
then
System.out.println(object1.getId());
System.out.println(object2.getId());
end
rule "Rule1_B"
when
object2: ObjectType2( id == 2)
then
System.out.println(object1.getId());
System.out.println(object2.getId());
end
如您所见,错误现在变得更加明显。
Drools 处理方式的一个副作用 or
也是在这个操作中没有短路:如果两个对象都存在于您的会话中,规则将被执行两次。
希望对您有所帮助,
如
相反,我们可以做的是创建一个包装器 class,它将包含一个对象,每个对象都需要匹配各种不同的数据类型:
public class ObjectType {
ObjectType1 ob1;
ObjectType2 ob2;
// setters and getters
}
现在,如果我们想将 ObjectType1
的对象插入到知识会话中:
ObjectType1 object1 = new ObjectType1();
kSession.insert(object1);
我们可以改为将 ObjectType.ob1
设置为引用 object1
,然后将新的 ObjectType
对象插入到会话中:
ObjectType1 object1 = new ObjectType1();
ObjectType object = new ObjectType();
object.setOb1(object1);
kSession.insert(object);
现在在规则文件中,我们需要将 ObjectType1
类型的对象与 ObjectType(getOb1() != null)
而不是 ObjectType1()
进行匹配:
rule "Rule1"
when
object: ( ObjectType( ob1 != null && ob1.getId() == 1 ) or
ObjectType( ob2 != null && ob2.getId() == 2 ) )
then
if ( object.getOb1() != null )
{
System.out.println(object.getOb1().getId());
}
else
{
System.out.println(object.getOb2().getId());
}
end