Sudipta,
I believe the problem is that you can't import classes from the default package into your rules file, but I'm not sure.
Try making Message a top level class (not an inner class) and put it in a package (not the default one). Import it in the DRL and you shall be fine.
We need a closer look at this before releasing next major final version.
[]s
Edson
Sir,
I want to run a simple drools example i.e . HelloWorldExample from command line
My HelloWorldExample.java--->
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;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.compiler.*;
import java.lang.String;
import java.io.*;
/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class HelloWorldExample
{
public static final void main(final String[] args)
{
try
{
//load up the rulebase
final RuleBase ruleBase = loadRules();
final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
//go !
final Message message = new Message();
message.setMessage( "Hello World " );
message.setStatus( Message.HELLO );
workingMemory.assertObject ( message );
workingMemory.fireAllRules();
}
catch ( final Throwable t )
{
t.printStackTrace();
}
}
public static RuleBase loadRules()
{
RuleBase ruleBase=null;
try
{
String [] ruleFiles = new String [1];
ruleFiles[0] = "HelloWorld.drl";
PackageBuilderConfiguration pkgBuilderCfg = new PackageBuilderConfiguration();
pkgBuilderCfg.setCompiler(PackageBuilderConfiguration.JANINO);
PackageBuilder builder = new PackageBuilder(pkgBuilderCfg);
for( int i = 0, j = ruleFiles.length; i<j; i++ )
{
builder.addPackageFromDrl(new InputStreamReader(HelloWorldExample.class.getResourceAsStream( ruleFiles[i] )) ); //HelloWorldExample.class.getResourceAsStream( "HelloWorld.drl " )
//new InputStreamReader(getClass().getResourceAsStream( ruleFiles[i] ) )
}
final Package pkg = builder.getPackage();
ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage (pkg);
}
catch (Exception fe)
{
fe.printStackTrace();
}
return ruleBase;
}
/**
* Please note that this is the "low level" rule assembly API.
*/
private static RuleBase readRule() throws Exception
{
//read in the source
final Reader source = new InputStreamReader( HelloWorldExample.class.getResourceAsStream( "HelloWorld.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...
final 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)
final Package pkg = builder.getPackage();
//add the package to a rulebase (deploy the rule package).
final 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(final String message)
{
this.message = message;
}
public int getStatus()
{
return this.status;
}
public void setStatus(final int status)
{
this.status = status;
}
}
}
My HelloWorld.drl-->
package drools_test;
import HelloWorldExample.*;
rule "Hello World"
when
m : Message( status == Message.HELLO, message : message )
then
System.out.println( message );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
modify( m );
end
rule "GoodBye"
no-loop true
when
m : Message( status == Message.GOODBYE, message : message )
then
System.out.println( message );
m.setMessage( message );
end
Now compilation works absolutely fine but when i am trying to run it some errors are coming
Warning: An error occurred compiling a semantic invoker. Errors should have been reported elsewhere.
Warning: An error occurred compiling a semantic invoker. Errors should have been reported elsewhere.
org.drools.rule.InvalidRulePackage: Rule Compilation error Class "HelloWorldExample.Message" not found Class "HelloWorldExample.Message" not found Class "java.lang.Object" has no method named "setMessage" Exception:0
Rule Compilation error Class "HelloWorldExample.Message" not found Class "HelloWorldExample.Message" not found Class "java.lang.Object" has no method named "setMessage" Exception:0
at org.drools.rule.Package.checkValidity(Unknown Source)
at org.drools.common.AbstractRuleBase.addPackage(Unknown Source)
at HelloWorldExample.loadRules(HelloWorldExample.java:68)
at HelloWorldExample.main (HelloWorldExample.java:29)
I put all the jar files in the classpath. Please help me.
With regards,
Sudipta Deb. (+919831606520)
Kolkata,India
Here�s a new way to find what you're looking for - Yahoo! Answers
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users