0

我是流口水和 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"
                }
4

3 回答 3

0

我同意 laune 的观点,您应该阅读规则属性的含义(参见文档 [1] 中的 ere)。我认为这是因为您对规则进行了锁定。请参阅我链接的文档。

[1] https://docs.jboss.org/drools/release/6.5.0.Final/drools-docs/html/ch08.html#d0e9196

于 2017-04-21T07:16:16.007 回答
0

尝试使用更新功能“

然后

    RuleResult $r = new RuleResult();
    $r.setRuleName( "RuleOne" );
    $r.setState( false );
    update ( $r );

"

于 2017-04-21T20:42:25.747 回答
0

如果您不知道这些规则属性的用途,您应该阅读文档。然后,我想,你会意识到你可以删除它们。

salience 100 or 10
no-loop true or false
lock-on-active true
于 2017-04-21T06:09:45.537 回答