[rules-users] RCP and Drools

J Michael Dean mdean77 at comcast.net
Fri Apr 4 08:56:14 EDT 2008


I have continued to try to isolate the problem that I have previously  
described, where my RCP application that calls Drools runs properly,  
but am unable to Unit test.  Can reproduce the problem simply and am  
hoping someone can lend some insight.

I started by creating a simple Drools project that had a JUnit 4 test  
in it;  this runs.  It contains two java files and a DRL file. (Shown  
at very bottom of this post)

I then created a Hello World RCP application from the Eclipse  
templates.  I copied the Drools project files (2 Java files and 1 DRL  
file) into the same package as my RCP stuff.  When I run the test, I  
get the following error, which is exactly the same error I have been  
getting with my much more complicated project:

java.lang.NullPointerException
	at  
org.mvel.optimizers.OptimizerFactory.<clinit>(OptimizerFactory.java:43)
	at  
org.drools.rule.builder.dialect.mvel.MVELDialect.init(MVELDialect.java: 
142)
	at org.drools.compiler.DialectRegistry.initAll(DialectRegistry.java:49)
	at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:146)
	at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:108)
	at rcp_with_drool.mikeTest..setUp(mikeTest.java:39)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at  
sun 
.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 
39)
	at  
sun 
.reflect 
.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 
25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at  
org 
.junit 
.internal 
.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
	at  
org 
.junit 
.internal 
.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
	at  
org 
.junit 
.internal 
.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
	at  
org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
	at  
org 
.eclipse 
.jdt 
.internal 
.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
	at  
org 
.eclipse 
.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at  
org 
.eclipse 
.jdt 
.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java: 
460)
	at  
org 
.eclipse 
.jdt 
.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java: 
673)
	at  
org 
.eclipse 
.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java: 
386)
	at  
org 
.eclipse 
.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java: 
196)
[MVEL] Notice: Possible incorrect version of ASM present (3.0  
required).  Disabling JIT compiler.  Reflective Optimizer will be used.

I then removed all the source files relating to the RCP itself, and  
the error persists.  If I now remove the plugin dependencies from this  
project from the build path, the problem goes away and the tests run.   
If I add the plugin dependencies back into the project, the error  
resumes.  Finally, if I make my ORIGINAL Drools project depend on this  
second one with the plugin dependencies, the test fails there too.  If  
I remove the plugin dependencies, the tests run.

The plugin dependencies are:
org.eclipse.ui
org.eclipse.swt
CodeProSWTFragment
org.eclipse.swt.carbon.macosx
org.eclipse.jface
org.eclipse.core.commands
org.eclipse.ui.workbench
org.eclipse.core.runtime
org.eclipse.osgi
org.eclipse.equinox.common
org.eclipse.core.jobs
runtime_registry_compatibility
org.eclipse.equinox.registry
org.eclipse.equinox.preferences
org.eclipse.core.contenttype
org.eclipse.equinox.app

Thanks for any assistance.

Here are the two java files and the rules file:

package rcp_with_drool;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import rcp_with_drool.DroolsTest.Message;

public class mikeTest {
	public static RuleBase ruleBase;
	public static WorkingMemory workingMemory;
	public static Message message;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		message = new Message();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		Reader source = new  
InputStreamReader 
( DroolsTest.class.getResourceAsStream( "Sample.drl" ) );
		PackageBuilder builder = new PackageBuilder();
		try {
			builder.addPackageFromDrl(source);
		} catch (DroolsParserException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		Package pkg = builder.getPackage();	
		ruleBase = RuleBaseFactory.newRuleBase();
		try {
			ruleBase.addPackage( pkg );
		} catch (Exception e) {
			e.printStackTrace();
		}
		workingMemory = ruleBase.newStatefulSession();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testMain() {
         //Message message = new Message();
         message.setMessage(  "Hello World" );
         message.setStatus( Message.HELLO );
         workingMemory.insert( message );
         assertEquals("Message status set", message.getStatus(),  
Message.HELLO);
         workingMemory.fireAllRules();
         assertEquals("Message status  
set",message.getStatus(),Message.GOODBYE);
	}

	@Test
	public void testMain1() {
         //Message message = new Message();
         message.setMessage(  "Hello World" );
         message.setStatus( Message.HELLO );
         workingMemory.insert( message );
         assertEquals("Message status set", message.getStatus(),  
Message.HELLO);
         workingMemory.fireAllRules();
         assertEquals("Message status  
set",message.getStatus(),Message.GOODBYE);
	}
	@Test
	public void testMain2() {
         //Message message = new Message();
         message.setMessage(  "Hello World" );
         message.setStatus( Message.HELLO );
         workingMemory.insert( message );
         assertEquals("Message status set", message.getStatus(),  
Message.HELLO);
         workingMemory.fireAllRules();
         assertEquals("Message status  
set",message.getStatus(),Message.GOODBYE);
	}
	@Test
	public void testMain3() {
         //Message message = new Message();
         message.setMessage(  "Hello World" );
         message.setStatus( Message.HELLO );
         workingMemory.insert( message );
         assertEquals("Message status set", message.getStatus(),  
Message.HELLO);
         workingMemory.fireAllRules();
         assertEquals("Message status  
set",message.getStatus(),Message.GOODBYE);
	}
	
}

package rcp_with_drool;

import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;

/**
  * This is a sample file to launch a rule package from a rule source  
file.
  */
public class DroolsTest {

     public static final void main(String[] args) {
         try {
         	
         	//load up the rulebase
             RuleBase ruleBase = readRule();
             WorkingMemory workingMemory =  
ruleBase.newStatefulSession();

             //go !
             Message message = new Message();
             message.setMessage(  "Hello World" );
             message.setStatus( Message.HELLO );
             workingMemory.insert( message );
             workingMemory.fireAllRules();


         } catch (Throwable t) {
             t.printStackTrace();
         }
     }

     /**
      * Please note that this is the "low level" rule assembly API.
      */
	private static RuleBase readRule() throws Exception {
		//read in the source
		Reader source = new  
InputStreamReader( DroolsTest.class.getResourceAsStream( "/ 
Sample.drl" ) );
		
		//optionally read in the DSL (if you are using it).
		//Reader dsl = new  
InputStreamReader( DroolsTest.class.getResourceAsStream( "/ 
mylang.dsl" ) );

		//Use package builder to build up a rule package.
		//An alternative lower level class called "DrlParser" can also be  
used...
		
		PackageBuilder builder = new PackageBuilder();

		//this wil parse and compile in one step
		//NOTE: There are 2 methods here, the one argument one is for normal  
DRL.
		builder.addPackageFromDrl( source );

		//Use the following instead of above if you are using a DSL:
		//builder.addPackageFromDrl( source, dsl );
		
		//get the compiled package (which is serializable)
		Package pkg = builder.getPackage();
		
		//add the package to a rulebase (deploy the rule package).
		RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		ruleBase.addPackage( pkg );
		return ruleBase;
	}
	
	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;
		}
	}

}

package glucose.rules

import rcp_with_drool.DroolsTest.Message;

rule "Hello World"
	when
		m : Message( status == Message.HELLO, message : message )
	then
		System.out.println( message );
		m.setMessage( "Goodbye crueller world" );
		m.setStatus( Message.GOODBYE );
		update( m );
end

rule "GoodBye"
	no-loop true
	when
		m : Message( status == Message.GOODBYE, message : message )
	then
		System.out.println( message );
		m.setMessage( message );	
end

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080404/0a807235/attachment.html 


More information about the rules-users mailing list