views:

286

answers:

2

I'm having trouble compiling a drools 4 project. I'm getting errors in the rules file saying

Only a type can be imported. <<MyClassName>> resolves to a package

The incremental compiler isn't working because of this. How do I fix the errors or get eclipse to ignore them?

+1  A: 

This issue was mentioned for a migration from drools 3.06 to 4.0.7, so what version of eclipse and drools are you using?

This might be related to a classpath issue:

Using the debugger I realized that the Drools PackageBuilder tried to load the classes from the

Thread.currentThread().getContextClassLoader();

This ClassLoader does not contain my agent classes! Even the system class loader does not contain my classes.

The solution was:

Instead of creating plain PackageBuilder and RuleBase instances, one has to create them with a PackageBuilderConfiguration and a RuleBaseConfiguration both with the current classLoader configured:

ClassLoader classLoader = this.getClass().getClassLoader();

PackageBuilderConfiguration configuration = new PackageBuilderConfiguration();
configuration.setClassLoader(classLoader);

PackageBuilder builder = new PackageBuilder(configuration);

builder.addPackageFromDrl(source);

RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration();
ruleBaseConfiguration.setClassLoader(classLoader);

ruleBase = RuleBaseFactory.newRuleBase(ruleBaseConfiguration);
ruleBase.addPackage(builder.getPackage());
VonC
@Rich thank you for fixing this typo.
VonC
Hi, Thanks for the response. My work collegue has just informed me that we didn't migrate and that we have always been on Drools version 4. Sorry, I should have made this clear to save you typing that out.
Tarski
A: 

Hmmm, I cleaned the project and that resolved the problem.

Tarski
It's a bit of a jerk move to accept your own answer from your own mistake :/
Lewisham