Here is the unit test showing how it works, this uses the programmatic api, you can also use the drools.consequenceExceptionHandler property. This will be in the 4.0.1 release, which is out this monday.
http://jira.jboss.org/jira/browse/JBRULES-1123

public void testCustomConsequenceException() throws Exception { final PackageBuilder builder = new PackageBuilder(); builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_ConsequenceException.drl" ) ) );
       final Package pkg = builder.getPackage();

       RuleBaseConfiguration conf = new RuleBaseConfiguration();
CustomConsequenceExceptionHandler handler = new CustomConsequenceExceptionHandler();
       conf.setConsequenceExceptionHandler( handler );
final RuleBase ruleBase = getRuleBase(conf);
       ruleBase.addPackage( pkg );
       final WorkingMemory workingMemory = ruleBase.newStatefulSession();

       final Cheese brie = new Cheese( "brie",
                                       12 );
       workingMemory.insert( brie );

       workingMemory.fireAllRules();
assertTrue( handler.isCalled() );
   }
public static class CustomConsequenceExceptionHandler implements ConsequenceExceptionHandler { private boolean called;

       public void handleException(Activation activation,
                                   WorkingMemory workingMemory,
                                   Exception exception) {
this.called = true; } public boolean isCalled() {
           return this.called;
} }

Mark
Mark Proctor wrote:
ok I have added a ConsequenceExceptionHandler to RuleBaseConfiguration, default just wraps and re-throws as a runtime exception. You can override this to provide a custom consequence exception handler. But do be aware that if you swallow the working memory integrity may be invalid, if the error happened during a working memory action.

Mark
Anstis, Michael (M.) wrote:
For what it's worth I think this would be a good idea too.
Perhaps the default ASM wrapper around the RHS could use a try...catch block and log any exceptions of a rules' activation in a (drools) accessible log? Heck you could even allow certain accepted exceptions to be defined as a property of the rule; and any other non-defined exception types cause the session to become invalidated. If we're ever to let "the business" define rules we need to accept they might make mistakes that we're better off capturing and report back than invalidate the whole session. Why should a whole session be invalidated because a single rule activation failed anyway? With kind regards, Mike
    ------------------------------------------------------------------------
    *From:* [EMAIL PROTECTED]
    [mailto:[EMAIL PROTECTED] *On Behalf Of *Yang Song
    *Sent:* 24 August 2007 16:16
    *To:* Rules Users List
    *Subject:* Re: [rules-users] How to catch Exceptions when firing
    rules

    Thanks a lot for the answer, Mark. But I don't think it makes sense.
Because in some scenarios, you cannot guarantee the consequence
    part of rule is 100% correct -- there could be errors happening
    in run-time which are hard to predict, especially when a complex
    action or logic will be executed as the concequece.
There should at least be some mechanisms to tell whoever fires
    the rule that there is something wrong during the rule firing
    process, then and he can do something, e.g. create a new session.
    Also it should enable the rule firer to catch these exceptions
    and do the clean up work silently -- instead of leaving these
    things on the stderr even cannot be seen in the logs. This will
    make the program depending on the JBoss Rules to be more robust.
What do you think? If JBoss Rules already has the ability to do
    this job, can you please let me know?
Thanks again,
    Yang

On 8/24/07, *Mark Proctor* <[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>> wrote:

        Once an exception is thrown on a conseuqence the current
        session is considered invalid. You'll need to add the try
        catch inside of the actual consequence.

        Mark
        Yang Song wrote:
        Hi,
Anyone knows how to catch the exception when firing the rules? I wrapped the session.fireAllRules() method using
        try...catch, however it doesn't work: when someone wrote bad
        code in the rule's action part, the Exception will be thrown
        and printed to the stderr, and this will make the rule
        engine stop working -- the try...catch outside doesn't help
        anything.
If the exception thrown from the rule's action part can be
        caught externally, the system can be protected from
        interrupting Exception.

        try {
                _log.debug("Firing rules in : " + getName());

                session.fireAllRules();

        } catch (Exception e) {
                _log.info("Error when firing rules: ", e);
        }

        Thanks,
        Yang

        ------------------------------------------------------------------------

        _______________________________________________
        rules-users mailing list
        rules-users@lists.jboss.org <mailto:rules-users@lists.jboss.org>
        https://lists.jboss.org/mailman/listinfo/rules-users

        _______________________________________________
        rules-users mailing list
        rules-users@lists.jboss.org <mailto:rules-users@lists.jboss.org>
        https://lists.jboss.org/mailman/listinfo/rules-users


------------------------------------------------------------------------

_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users

------------------------------------------------------------------------

_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users

_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to