Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drools 4

 
John Louis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,
I have been reading up on Drools 4 and am a complete newbie to the concept. I have chanced upon quite a lot of examples, but unfortunately all depend on Eclipse to set up a Drools project. I downloaded the Drools4 dependencies, the jar files, and tried a standalone java program, a simple hello world, the one that was running with amazing success in eclipse, but I am unable to replicate the same success here.
In most of the examples, they use a rather simple mechanism of creating a RuleBase and that would be:
Reader source=new InputStreamReader(this.Class.getResourceAsReader('MyDrl.drl'));

PackageBuilder p=new PackageBuilder();
This throws a null pointer exception when run outside eclipse.

And the same appln seems to throw an error parsing the drl file too. Is there a difference in the ways I need to confront Drools with and without eclipse?

I'm still trying to build an independent appln. After that my next attempt would be to integrate that to Spring.

John
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Is the file MyDrl.drl in the classpath when you run the code? That's where getResourceAsReader would be looking for it (as opposed to the current directory, where file I/O classes would look).
 
John Louis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
At the moment I seem to be stuck with the

PackageBuilder p=new PackageBuilder();
builder.addPackageFromDrl(
new InputStreamReader(
MyDrools.class.getResourceAsStream( "MyDrl.drl" )
)
)

throwing a NullPointerException. I had somehow worked around this problem at work to find p.hasErrors() returning true. I am trying to replicate that problem now.
 
John Louis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Please help me out in this...


MyDrools.java
import java.io.*;
import org.drools.compiler.*;
import org.drools.*;
import org.drools.rule.Package;
import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;

public class MyDrools{
public void initRuleBase(){
Reader source = new InputStreamReader( MyDrools.class.getResourceAsStream( "MyRule.drl" ) );

JavaDialectConfiguration javaDialectConf=new JavaDialectConfiguration();
javaDialectConf.setCompiler(JavaDialectConfiguration.JANINO);
javaDialectConf.setJavaLanguageLevel("1.5");


PackageBuilderConfiguration conf=javaDialectConf.getPackageBuilderConfiguration();

PackageBuilder builder=new PackageBuilder(conf);

try{
//this wil parse and compile in one step
builder.addPackageFromDrl( source );

// Check the builder for errors
if ( builder.hasErrors() ) {
System.out.println( builder.getErrors().toString() );
throw new RuntimeException( "Unable to compile \"MyRule.drl\".");
}

//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 );

StatefulSession session = ruleBase.newStatefulSession();

session.insert( new Bob() );

session.fireAllRules();
}catch(Exception e){
e.printStackTrace();
}


}

public static void main(String a[]){
new MyDrools().initRuleBase();
}
}

Bob.java
public class Bob{
public String name="";

public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}

public boolean likesCheese(){
return true;
}
}


MyRule.drl

package rules;

rule "bob likes cheese"
when
bob: Bob(1==1)
then
System.out.println("hi");
end


This compiles successfully but I am getting this at Runtime... I am using the Drools 4.0.7 jars...


Exception in thread "main" org.drools.RuntimeDroolsException: Unable to load dialect 'org.drools.rul
e.builder.dialect.java.JavaDialectConfiguration:java'
at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuilderConfiguration.ja
va:160)
at org.drools.compiler.PackageBuilderConfiguration.buildDialectConfigurationMap(PackageBuild
erConfiguration.java:146)
at org.drools.compiler.PackageBuilderConfiguration.init(PackageBuilderConfiguration.java:121
)
at org.drools.compiler.PackageBuilderConfiguration.<init>(PackageBuilderConfiguration.java:9
8)
at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:124)
at org.drools.compiler.PackageBuilder.<init>(PackageBuilder.java:108)
at MyDrools.initRuleBase(MyDrools.java:18)
at MyDrools.main(MyDrools.java:50)
Caused by: java.lang.RuntimeException: The Eclipse JDT Core jar is not in the classpath
at org.drools.rule.builder.dialect.java.JavaDialectConfiguration.setCompiler(JavaDialectConf
iguration.java:91)
at org.drools.rule.builder.dialect.java.JavaDialectConfiguration.init(JavaDialectConfigurati
on.java:52)
at org.drools.compiler.PackageBuilderConfiguration.addDialect(PackageBuilderConfiguration.ja
va:156)
... 7 more


Can someone help me out with this?

John
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caused by: java.lang.RuntimeException: The Eclipse JDT Core jar is not in the classpath


You probably are missing some jar file in the classpath.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just found this in the drools user mailing list. It suggests, changing the initialization code to:

 
John Louis
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I am trying to run this outside eclipse and it's asking me to include the eclipse JDT core jar files too...

I found the code above too and have written the code I posted based on that (and many others ).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic