This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other Java Products and Servers and the fly likes Drools 4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "Drools 4" Watch "Drools 4" New topic
Author

Drools 4

John Louis
Greenhorn

Joined: Aug 14, 2008
Posts: 10
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
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
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).


Ping & DNS - my free Android networking tools app
John Louis
Greenhorn

Joined: Aug 14, 2008
Posts: 10
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

Joined: Aug 14, 2008
Posts: 10
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

Joined: Jul 20, 2005
Posts: 10240
    
168

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.


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Just found this in the drools user mailing list. It suggests, changing the initialization code to:

John Louis
Greenhorn

Joined: Aug 14, 2008
Posts: 10
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 ).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drools 4