如何在Guvnor中创建BRL并查询Drools Server

时间:2013-09-04 10:08:50

标签: drools drools-guvnor guvnor

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

所以基本上规则是..给定一个对象Route设置我在jar中guvnor上传的属性“selectedOutboundJourney”,我想得到另一个具有属性“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

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分: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