0

我在 Guvnor 中遇到了 BRL 规则。我正在尝试使用 Drools Server 从我的应用程序中执行规则(这个解决方案,因为在生产中我可以使用更多的服务器并且可能会提高性能。不确定因为这是第一次在我的公司中,我们正在使用 Drools)..

所以基本上规则是..给定一个对象Route设置我在guvnor中上传的jar中的属性“selectedOutboundJourney”,我想获得另一个设置了属性“selectedReturnJourney”的对象..(但是否有可能得到同一个对象??)实际上我得到了一个 Route 对象,其中 selectedReturnJourney 为空。

考虑到我遇到的麻烦,我不确定使用 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();
    }
}

如果我在不使用 getObjectCommand 的情况下使用如下所示的 DRL:

rule "Selected Return Routes for Dover Calais"

    when
    r : Route(selectedOutboundJourney == "DOCA")
then
    r.setSelectedReturnJourney("CADO")

end

任何人都可以帮助我吗?

4

1 回答 1

0

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

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
于 2013-09-04T10:29:49.027 回答