如何获取在Guvnor中创建BRL并查询Drools服务器的事实



我被Guvnor中的BRL规则卡住了。。我正试图使用Drools Server从我的应用程序中执行规则(这个解决方案是因为在生产中我可以使用更多的服务器,也许可以提高性能。对此不确定,因为这是我公司第一次使用Drool)。。

所以基本上规则是。。给定一个对象Route设置属性"selectedOutboundJourney",我在guvnor或jar中上传了该属性,我想获得另一个设置属性"selectedReturnJourney"的对象。。(但是有可能得到同样的物体吗?)实际上,我得到一个Route对象,其中selectedReturnJourney为null。

考虑到我遇到的麻烦,我不确定使用BRL是否是一个好的解决方案。。对于那些可能想要更改规则或创建新规则的非技术人员来说,它似乎很容易使用。

不管怎样。。

这是我在Guvnor:中创建的BRL

rule "Selected Return for Dover - Calais"
   dialect "mvel"
    when
        Route( selectedOutboundJourney == "DOCA" )
    then
        Route fact0 = new Route();
        fact0.setSelectedReturnJourney( "CADO" );
        insertLogical( fact0 );
        end

这是我正在使用的代码:

final List<Command> commands = new ArrayList<Command>();
final Command insertObjectCommand = CommandFactory.newInsert(input, RESULT, true, "default");
final Command getObjectCommand = CommandFactory.newGetObjects();
final Command fireAllRulesCommand = CommandFactory.newFireAllRules();
commands.add(insertObjectCommand);
commands.add(getObjectCommand);
commands.add(fireAllRulesCommand);
final ExecutionResults executionResults = droolsHttpClient.callDroolsServer(commands);
return executionResults.getValue(RESULT);

DroolsHttpClient类是:

public ExecutionResults callDroolsServer(final List<Command> commands) throws  DroolsException
{
    PostMethod postMethod = null;
    try
    {
        final HttpClient httpClient = new HttpClient();
        final String droolsServerHost = Config.getString(PoferriesrulesengineConstants.DROOLS_SERVER_HOST, "");
        final int droolsServerPort = Config.getInt(PoferriesrulesengineConstants.DROOLS_SERVER_PORT, 0);
        httpClient.getHostConfiguration().setHost(droolsServerHost, droolsServerPort);
        final String droolsServerUrl = Config.getString(PoferriesrulesengineConstants.DROOLS_SERVER_URL, "");
        postMethod = new PostMethod(droolsServerUrl);
        final BatchExecutionCommand command = CommandFactory.newBatchExecution(commands, PoferriesrulesengineConstants.DROOLS_SESSION);
        final XStream xStreamMarshaller = BatchExecutionHelper.newXStreamMarshaller();
        final String xmlCommand = xStreamMarshaller.toXML(command);
        final StringRequestEntity request = new StringRequestEntity(xmlCommand, MediaType.TEXT_PLAIN_VALUE, CharEncoding.UTF_8);
        postMethod.setRequestEntity(request);
        httpClient.executeMethod(postMethod);
        if (postMethod.getStatusCode() != 200)
        {
            throw new RuntimeException("Drools Communication Error, code: " + postMethod.getStatusCode());
        }
        final String response = postMethod.getResponseBodyAsString();
        final ExecutionResults executionResults = (ExecutionResults) xStreamMarshaller.fromXML(response);
        return executionResults;
    }
    catch (final Exception e)
    {
        throw new DroolsException(e.getMessage());
    }
    finally
    {
        postMethod.releaseConnection();
    }
}

如果我使用下面这样的DRL,那么在不使用getObjectCommand:的情况下,它的单词会非常完美

rule "Selected Return Routes for Dover Calais"
    when
    r : Route(selectedOutboundJourney == "DOCA")
then
    r.setSelectedReturnJourney("CADO")
end

有人能帮我吗?

假设在执行以下规则后,在知识会话开始时只有一个事实

rule "Selected Return for Dover - Calais"
dialect "mvel"
when
    Route( selectedOutboundJourney == "DOCA" )
then
    Route fact0 = new Route()
    fact0.setSelectedReturnJourney( "CADO" )
    insert( fact0 )
end

您的会话中将有两个Route事实,因为您刚刚插入了第二个事实。

Route: selectedOutboundJourney = "DOCA", selectedReturnJourney=null
Route: selectedOutboundJourney = null, selectedReturnJourney="DACO"

如果您想修改原始事实,请使用以下规则:

rule "Selected Return Routes for Dover Calais"
when
   $r : Route(selectedOutboundJourney == "DOCA")
then
   modify ($r) {
     selectedReturnJourney = "CADO"
   }
end

最新更新