我是流口水和 kie 服务器的新手。我遇到了关于如何通过触发规则 RHS(然后是操作)在工作内存中插入新事实然后通过这些动态插入的事实激活其他规则的问题。
我期望的是当 BaseFeature 从外部插入工作内存并激活 RuleOne 时,在 RuleOne 内部“然后”它将插入新的事实 RuleResult,并期望激活 RuleTwo,但它没有激活 RuleTwo,只是激活了 RuleOne,那是我做错了什么吗?
在这里,我有两个规则和一组事实:
事实定义:
public class AppResult implements java.io.Serializable
{
static final long serialVersionUID = 1L;
private java.lang.String key;
private java.lang.String value;
//getters and setters ...
}
public class BaseFeature implements java.io.Serializable
{
static final long serialVersionUID = 1L;
private int age;
//getters and setters ...
}
public class RuleResult implements java.io.Serializable
{
static final long serialVersionUID = 1L;
private java.lang.String ruleName;
private java.lang.Long score;
private boolean state;
//getters and setters ...
}
规则定义:
rule "RuleOne"
dialect "mvel"
salience 100
no-loop true
lock-on-active true
when
$b : BaseFeature( age < 22 || age > 40 )
then
RuleResult $r = new RuleResult();
$r.setRuleName( "RuleOne" );
$r.setState( false );
insertLogical( $r );
end
rule "RuleTwo"
dialect "mvel"
no-loop false
lock-on-active true
salience 10
when
$r : RuleResult( ruleName == "RuleOne" , state == false )
$a : AppResult( )
then
$a.setKey( "PASS" );
$a.setValue( "false" );
end
如何实现触发规则插入新事实并触发其他规则动态?预先感谢!
我正在使用 kie-server(6.5 Final) 进行测试,POST 和 Reponse 如下:
邮政:
{
"lookup": "RuleChainTestStateless",
"commands": [
{
"insert": {
"return-object": false,
"object": {
"com.qf.rulechaintest.BaseFeature": {
"age": "10"
}
}
}
},
{
"insert": {
"return-object": true,
"out-identifier": "AppResult",
"object": {
"com.qf.rulechaintest.AppResult": {
}
}
}
},
{
"fire-all-rules": ""
},
{
"get-objects":{
"out-identifier":"allFactsInWrokingMemory"
}
}
]
}
回应:
{
"type": "SUCCESS",
"msg": "Container RuleChainTest1.1 successfully called.",
"result": {
"execution-results": {
"results": [
{
"key": "",
"value": 1
},
{
"key": "AppResult",
"value": {
"com.qf.rulechaintest.AppResult": {
"key": null,
"value": null
}
}
},
{
"key": "allFactsInWrokingMemory",
"value": [
{
"com.qf.rulechaintest.BaseFeature": {
"age": 10
}
},
{
"com.qf.rulechaintest.AppResult": {
"key": null,
"value": null
}
},
{
"com.qf.rulechaintest.RuleResult": {
"ruleName": "RuleOne",
"score": null,
"state": false
}
}
]
}
],
"facts": [
{
"key": "AppResult",
"value": {
"org.drools.core.common.DefaultFactHandle": {
"external-form": "0:2:1542374590:1542374590:2:DEFAULT:NON_TRAIT:com.qf.rulechaintest.AppResult"
}
}
}
]
}
}
}
请注意,响应:
"com.qf.rulechaintest.AppResult": {
"key": null,
"value": null
}
它的预期是:
"com.qf.rulechaintest.AppResult": {
"key": "PASS",
"value": "false"
}