[rules-users] can't get samples to work from Drools 5 drools-doc-expert

Darrin Mison dmison at redhat.com
Fri Apr 24 01:20:55 EDT 2009


Hi, I'm very new to Drools and I'm trying to get code running based on  
the first sample code from the QuickStart chapter of the drools-doc- 
expert.

It compiles and runs (tried on commandline & in JBoss Dev Studio  
2.0.0.GA) but does not work, ie it always says "rejected" instead of  
"accepted".

I've had to fill out a bit of code to get it to compile so I may be  
doing something completely wrong. I've included the code (rule, model,  
main) below.

Any help would be really appreciated.

Darrin



#Sample.drl
package com.company.license
rule "Is of valid age"
when
     $a : Applicant( age > 18);
then
     $a.setValid( true );
	System.out.println("true");
end

//Applicant.java
package com.company.license;
public class Applicant
{
     private String name;
     private int Age;
     private boolean valid;

     // getter and setter methods here
	public String getName() { return name; }
	public int getAge()	{ return Age; }
	public boolean getValid(){ return valid; }
	public void setName(String s){ this.name = s; }
	public void setAge(int i){ this.Age = i; }
	public void setValid(boolean v){ this.valid = v; }
	
}


//ruletest.java
package com.company.license;
import org.drools.builder.*;
import org.drools.io.*;
import org.drools.runtime.*;
import org.drools.*;

public class ruletest
{
	public ruletest(){}
	
	public static void main(String args[])
	{
		ruletest rt = new ruletest();
		rt.go();
	}

	public void go()
	{
		Applicant dude = new Applicant();
		dude.setName("Rufus");
		dude.setAge(32);

		System.out.println(dude.getName());
		System.out.println("test: "+dude.getValid());
		
		KnowledgeBuilder kbuilder =  
KnowledgeBuilderFactory.newKnowledgeBuilder();
		Resource ruleResource =  
ResourceFactory.newClassPathResource( "Sample.drl", getClass() );

		kbuilder.add( ruleResource, ResourceType.DRL );

		if ( kbuilder.hasErrors() )
		    System.err.println( kbuilder.getErrors().toString() );

		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		StatelessKnowledgeSession ksession =  
kbase.newStatelessKnowledgeSession();

		System.out.println( dude.getValid() ); //should display false
		ksession.execute( dude );
		
		if (dude.getValid())
		    System.out.println("Application approved"); //should display due  
to hardcoded as of 32
		  else
		    System.out.println("Application rejected");
	}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090424/6d902c79/attachment.html 


More information about the rules-users mailing list