处理Drools文件中的异常 [英] Handling Exceptions in Drools Files

查看:506
本文介绍了处理Drools文件中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何处理Drools文件中的异常?

How do we handle exceptions in Drools files?

示例:我的规则使用共享的Java方法。

Example: My rule uses a shared Java method.

rule "002.17.1"
    dialect "mvel"
    when
        comp : Component()
    then        
        boolean validstate = SharedCodeUtil.hasValidateStateCountryUS(comp.address);

        errors.add(new ValidationFailure("002.17.1", comp.getSubApplicationGroupID(), comp.getSubApplicationID(),tokens));

end

Java方法SharedCodeUtil,可能有例外:

Java method SharedCodeUtil, may have exceptions:

    if (m != null)
    {
        try
        {
            Object o = m.invoke(form,(Object[])null);
            if(BudgetType.Project.equals(o))
                count++;
        }
        catch (Exception e)
        {
            LOG.error("Error", e);
            // re-throw? who will catch?
        }

Drools文件调用的共享Java方法中有一个Exception条件。谁会捕获它,除了记录它外,我还应该做什么?

There's an Exception condition in the shared Java method called by the Drools file. Who is going to catch it, and what else should I do besides logging it?

推荐答案

捕获异常没有特别的问题在规则的右侧:这是Java代码。

There is no particular problem with catching exceptions on the right hand side of a rule: this is Java code.

when
    //...
then
    try { /* some code */
    } catch( SomeException ex ){ /* handle ex */ }
end

是否需要重新抛出是设计决定,具体取决于异常的种类。如果反射出错,则代码中将存在一个基本问题,并且继续很可能毫无意义。如果您重新抛出,则将在下一个级别调用规则引擎:

Whether you need to re-throw is a design decision, depending on the kind of exception. If reflection goes wrong, you have a fundamental problem in the code, and continuation is very likely pointless. If you rethrow, the next level is where the rule engine is called:

try {
    kSession.fireAllRules();
} catch( Exception e ){
    //... handle exception
}

您可能必须在记录严重错误后终止应用程序。

You'll probably have to terminate the application after logging a severe error.

当在执行期间执行的代码中引发异常时,情况就不同了规则评估。

The situation is different when exceptions are thrown in code that is executed during rule evaluation. This cannot be caught "inside", and it will unwind to a handler around fireAllRules.

请注意,否定的验证结果最好不要通过异常传播。

Note that negative validation results should better not be propagated via exceptions.

这篇关于处理Drools文件中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆