Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my project (I think I'll have to write about it on my blog <a href="http://toomuchcoding.blogspot.com" rel="nofollow">http://toomuchcoding.blogspot.com</a> where I have some articles about Drools) I wrote a custom Listener in the following manner (I think I found a nice tutorial over here <a href="http://members.inode.at/w.laun/drools/CustomConsequenceExceptionHandlingHowTo.html" rel="nofollow">http://members.inode.at/w.laun/drools/CustomConsequenceExceptionHandlingHowTo.html</a>)</p> <p>I defined my KnowledgeBase as a bean in my applicationContext:</p> <pre><code>&lt;drools:kbase id="fxKBase"&gt; &lt;drools:resources&gt; &lt;drools:resource type="DRL" source="classpath:path/first.drl"/&gt; &lt;drools:resource type="DRL" source="classpath:path/second.drl"/&gt; &lt;/drools:resources&gt; &lt;drools:configuration&gt; &lt;drools:consequenceExceptionHandler handler="a.b.c.MyConsequenceExceptionHandler" /&gt; &lt;/drools:configuration&gt; &lt;/drools:kbase&gt; </code></pre> <p>Then I defined MyConsequenceExceptionHandler</p> <pre><code>public class MyConsequenceExceptionHandler implements ConsequenceExceptionHandler { @Override public void handleException(Activation activation, WorkingMemory workingMemory, Exception exception) { throw new MyConsequenceException(activation, workingMemory, exception); } </code></pre> <p>and the MyConsequenceException:</p> <pre><code>public class MyConsequenceException extends RuntimeException { private final WorkingMemory workingMemory; private final Activation activation; public MyConsequenceException(final Activation activation, final WorkingMemory workingMemory, final Exception exception) { super(exception); this.activation = activation; this.workingMemory = workingMemory; } @Override public String getMessage() { StringBuilder sb = new StringBuilder( "Exception executing consequence for " ); if( activation != null &amp;&amp; ( activation.getRule() ) != null ){ Rule rule = activation.getRule(); String ruleName = rule.getName(); sb.append("rule [\"").append( ruleName ).append( "\"]. " ); } else { sb.append( "rule, name unknown" ); } Throwable throwable = ExceptionUtils.getRootCause(getCause()); sb.append("The thrown exception is [").append(throwable).append("]. "); return sb.toString(); } @Override public String toString() { return getMessage(); } </code></pre> <p>}</p> <p>In that way when an exception is thrown you will get a custom message of your choosing. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload