KIE执行服务器 - 引导规则插入事实 - 如何在Java中获取它?

时间:2015-12-17 16:37:12

标签: java drools kie

我使用6.3.0 Drools Workbench和KIE执行服务器,从JAVA应用程序到KIE执行服务器进行 REST 通信。

我是使用Drools的新手。

以下是类似的问题,但没有解决问题(特别是对于REST和6.3.0组合):

好的,既然我们确定了基础,这真的不可能吗?或者我们都明白这个错了吗?别的什么? :)

事情是,这是应该经常遇到的事情,因为当你在Drools Workbench中使用Guided Rule时,它只在规则的那一部分提供了这种动作:" Insert Fact ... "和"逻辑插入事实......" (和通话方法)。

当添加事实("事务"在我的情况下)时,它会生成如下规则代码:

import java.lang.Number;

rule "BigAmount"
    dialect "mvel"
    when
        Transaction( amount > 10000.0 )
    then
        Transaction fact0 = new Transaction();
        fact0.setActivatedRule( "BigAmount" );
        insert( fact0 );
end

人们希望用Java来解决这个问题。 Java的结果是:

<fact-handle identifier="Transaction" external-form="0:9:338894407:338894407:9:DEFAULT:NON_TRAIT:hr.company.Transaction"/>

当试图像这样检索它时,你得到NULL:

    ... before is request sent ...
    KieServerCommand call = new CallContainerCommand(containerId, xStreamXml);

    List<KieServerCommand> cmds = Arrays.asList(call);
    CommandScript script = new CommandScript(cmds);

    for (int i=0; i<1; i++) {        
        ServiceResponsesList reply = client.executeScript(script);        

        for (ServiceResponse<? extends Object> r : reply.getResponses()) {
            System.out.println(r.getResult());

            ExecutionResultImpl result = (ExecutionResultImpl) BatchExecutionHelper.newXStreamMarshaller().fromXML( (String) r.getResult() );
            DefaultFactHandle obj = (DefaultFactHandle) result.getFactHandle("Transaction");
            Transaction t = (Transaction) obj.getObject();
            System.out.println("BU!");
        }
    };

或者这只是错误的做法?

谢谢!

1 个答案:

答案 0 :(得分:3)

玩了一段时间,这是我发现的......

我不确定提到的问题和答案中的建议解决方案是否因Drools / KIE版本不同或者这是与KIE服务器的REST通信这一事实不同,但它们在这里不适用。

在规则中插入对象(事实)

如果你有这样的规则:

rule "BigAmount"
    dialect "mvel"
    when
        Transaction( amount > 10000.0 )
    then
        Transaction fact0 = new Transaction();
        fact0.setActivatedRule( "BigAmount" );
        insert( fact0 );
end

你想通过REST在JAVA中检索它,你需要在BatchExecutionCommand中插入GetObjects命令:

    Transaction trans = new Transaction();      
    trans.setAmount(new Double(10001));

    // define commands
    InsertObjectCommand insertObjectCommand = new InsertObjectCommand(trans, "InputTransaction");

    GetObjectsCommand getObjectsCommand = new GetObjectsCommand();
    getObjectsCommand.setOutIdentifier("objects");

    FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand("RunAllRules");

    // insert commands into Command object (BatchExecutionCommand)
    List<GenericCommand<?>> commands = new ArrayList<GenericCommand<?>>();
    commands.add(insertObjectCommand);
    commands.add(fireAllRulesCommand);
    commands.add(getObjectsCommand);
    BatchExecutionCommand command = new BatchExecutionCommandImpl(commands);

    String xStreamXml = BatchExecutionHelper.newXStreamMarshaller().toXML(command); // actual XML request

所以,这里重要的部分是:

    GetObjectsCommand getObjectsCommand = new GetbjectsCommand();
    getObjectsCommand.setOutIdentifier("objects");

将告诉KIE服务器返回XML中的单独节点 - &lt; result identifier =“objects”&gt;,您将在其中找到KIE会话中的所有对象:

<execution-results>
  <result identifier="InputTransaction">
    <com.company.fm.Transaction>
      <amount>10001.0</amount>
      <activatedRule>
        <string>BigAmount</string>
      </activatedRule>
    </com.company.fm.Transaction>
  </result>
  <result identifier="RunAllRules">
    <int>1</int>
  </result>
  <result identifier="objects">
    <list>
       ... all objects ...
    </list>
  </result>
  <fact-handle identifier="InputTransaction" external-form="0:1:226077856:226077856:1:DEFAULT:NON_TRAIT:com.company.fm.Transaction"/>
</execution-results>

之后,您可以从会话中获取所有对象:

    // define kie client
    KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(
            "http://docklin:8180/kie-server/services/rest/server",
            "ks-user",      //user must have role "kie-server" assigned
            "ks-user");
    config.setMarshallingFormat(MarshallingFormat.XSTREAM);     

    KieServicesClient client = KieServicesFactory.newKieServicesClient(config);     

    // set container for execution and prepare the call
    String containerId = "fm";
    KieServerCommand call = new CallContainerCommand(containerId, xStreamXml);        
    List<KieServerCommand> cmds = Arrays.asList(call);
    CommandScript script = new CommandScript(cmds);

    ServiceResponsesList reply = client.executeScript(script);        

    for (ServiceResponse<? extends Object> r : reply.getResponses()) {
            System.out.println(r.getResult());

            if (r.getResult() != null) {
                ExecutionResultImpl result = (ExecutionResultImpl) BatchExecutionHelper.newXStreamMarshaller().fromXML((String) r.getResult());
                // getting the same object that was sent but with filled in values
                trans = (Transaction) result.getResults().get("InputTransaction");
                // Objects From insert(fact0) in rule. The problem is that they are staying and multiplying there in Drools, don't know yet how to manage it. ToDo.
                ArrayList<Object> objects = (ArrayList<Object>) result.getResults().get("objects");                 
                System.out.println(objects); 
    }
    else
                System.out.println("Empty result...?");
    }

如评论中所述,这将返回会话中的所有对象。如果多次调用该程序,它将向会话添加对象而不删除旧对象。我没有更多的时间来玩它来弄清楚如何解决这个问题只能从这一次调用中获得。撤回或可能的东西。

我实际上发现我根本不需要这个(并且插入(事实)可能对我的用例不好)但是我希望将我用填充值发送的相同输入对象返回到它的属性中。

将属性设置为已发送的输入对象并将此对象检索回Java

我发现(告诉你我是Drools的新手)我实际上可以在Drools workbench-&gt; Guided Rule中为变量指定一个变量。通过“向规则添加条件...”在规则的“WHEN”部分添加对象时,只需设置变量名称(在弹出窗口的底部),它将在“THEN”部分中提供其他选项,例如“修改”,“更改字段值”,......

如果您选择“更改字段...”,它将创建如下规则:

rule "BigAmount"
    dialect "mvel"
    when
        f : Transaction( amount >= 10000 )
    then
        f.addActivatedRule( "BigAmount" );
end

然后你实际上会在你发送的输入对象中得到这个值(在我的例子中是“InputTransaction”),这实际上是我的目标,并且不会在会话中插入新的对象(事实)。 这就是代码中的这一部分:

// getting the same object that was sent but with filled in values
trans = (Transaction) result.getResults().get("InputTransaction");

我希望这有助于某人。

此致 埃迪