[rules-users] Drools fusion how to I catch an Exception

Wolfgang Laun wolfgang.laun at gmail.com
Wed Sep 5 09:46:40 EDT 2012


@Geoffrey: But Richard's code does check for errors and aborts by
throwing an Exception.

And it works - see my other email.

-W


On 05/09/2012, Geoffrey De Smet <ge0ffrey.spam at gmail.com> wrote:
> Take a look at this open jira:
>    https://issues.jboss.org/browse/JBRULES-2079
>    ruleBase.addPackage(packageBuilder.getPackage()); should fail-fast
> with a clear error message if the packageBuilder has any errors
>
> And take a look at PackageBuilder.hasErrors()
>
> Op 05-09-12 15:14, RichardAmbridge schreef:
>> If I have a rule that has an error in it, the error isn't being caught by
>> the
>> compiler and the error doesn't happen as the Object is inserted into the
>> session, it only happens after a time period completes, then Drools
>> silently
>> fails.
>> How can I catch these silent failures.
>>
>> See below for test case.
>>
>> Notice in the After5 rule there is a mistake in the then part.
>> m.status=$m.status;
>>
>> $m doesn't exist.
>>
>> If i change this to $m.status=m.status, the compiler detects the error,
>> but
>> this way of m.status=$m.status is not found.
>>
>> When the rule fires it just dies.
>> I need to be able to catch the Exception somehow and report it..
>>
>> Any help really appreciated..
>> Thanks
>> Ric
>>
>>
>> ----------
>> sample.drl:-
>> ----------
>>
>> package com.sample
>>
>> import com.sample.DroolsTest.Message;
>>
>> declare Message
>>   @role ( event )
>> end
>> rule "Hello"
>>   dialect "mvel"
>>
>> 	when
>> 		m : Message( status == Message.HELLO, myMessage : message )		
>> 		
>> 	then
>> 		System.out.println( "Hello added" );		
>> end
>>
>> rule "After5"
>>   dialect "mvel"
>>
>> 	when
>> 		m : Message( status == Message.HELLO, myMessage : message )
>> 		not( Message( status == Message.GOODBYE , this after [0s,5s] m ))
>> 		
>> 	then
>> 		System.out.println( "After5 then start" );
>> 		m.status=$m.status;
>> 		System.out.println( "After5 then stop" );
>> 		retract(m);
>> end
>>
>> ----------
>> DroolsTest.java:
>> ----------
>> package com.sample;
>>
>> import org.drools.KnowledgeBase;
>>
>> /**
>>   * This is a sample class to launch a rule.
>>   */
>> public class DroolsTest {
>>
>> 	public static final void main(String[] args) {
>> 		try {
>> 			// load up the knowledge base
>> 			KnowledgeBase kbase = readKnowledgeBase();
>> 			StatefulKnowledgeSession ksession = kbase
>> 					.newStatefulKnowledgeSession();
>> 			KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory
>> 					.newFileLogger(ksession, "test");
>> 			// go !
>> 			Message message = new Message();
>> 			message.setMessage("Hello World");
>> 			message.setStatus(Message.HELLO);
>> 			System.out.println("Inserting message");
>> 			ksession.insert(message);
>> 			System.out.println("Fire all rules");
>> 			ksession.fireAllRules();
>> 			System.out.println("Sleep 10");
>> 			Thread.sleep(10000);
>> 			System.out.println("End sleep");
>> 			logger.close();
>> 		} catch (Throwable t) {
>> 			t.printStackTrace();
>> 		}
>> 	}
>>
>> 	private static KnowledgeBase readKnowledgeBase() throws Exception {
>> 		KnowledgeBaseConfiguration conf = KnowledgeBaseFactory
>> 				.newKnowledgeBaseConfiguration();
>> 		conf.setOption(EventProcessingOption.STREAM); // STREAM mode as we are
>> 														// dealing with Events	
>>
>> 		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
>> 				.newKnowledgeBuilder();
>> 		kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"),
>> 				ResourceType.DRL);
>> 		KnowledgeBuilderErrors errors = kbuilder.getErrors();
>> 		if (errors.size() > 0) {
>> 			for (KnowledgeBuilderError error : errors) {
>> 				System.err.println(error);
>> 			}
>> 			throw new IllegalArgumentException("Could not parse knowledge.");
>> 		}
>> 		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(conf);
>> 		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
>> 		return kbase;
>> 	}
>>
>> 	public static class Message {
>>
>> 		public static final int HELLO = 0;
>> 		public static final int GOODBYE = 1;
>>
>> 		private String message;
>>
>> 		private int status;
>>
>> 		public String getMessage() {
>> 			return this.message;
>> 		}
>>
>> 		public void setMessage(String message) {
>> 			this.message = message;
>> 		}
>>
>> 		public int getStatus() {
>> 			return this.status;
>> 		}
>>
>> 		public void setStatus(int status) {
>> 			this.status = status;
>> 		}
>>
>> 	}
>>
>> }
>>
>>
>>
>> --
>> View this message in context:
>> http://drools.46999.n3.nabble.com/Drools-fusion-how-to-I-catch-an-Exception-tp4019597.html
>> Sent from the Drools: User forum mailing list archive at Nabble.com.
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>


More information about the rules-users mailing list