Please study the documentation ("Expert") and the examples about how to
write conditions and their components. See below for some pointers.
2011/8/5 Gaurav Silakari <gaurav.silakari(a)tcs.com>
Below is my drl and the Pojo that I am using, please guide me on this :
#created on: Aug 5, 2011
package com.sample
import com.sample.Customer
rule "Below 100"
when
$c:Customer()
$c.getSi() < 100
$c: Customer( si < 100 )
This is the proper way of making a constraint on a fact attribute. "getSi"
and "si" relate according to JavaBeans.
then
$c.setVal("<100");
end
rule "Below 200"
when
$c:Customer()
$c.getSi() < 200
# Same as above.
You realize that <100 also means <200; if so, both rules will fire and
"val"
could be set either way.
then
$c.setVal("<200");
end
rule "Below 300"
when
$c:Customer()
$c.getSi() < 300
# see comment on rule "Below 200"
-W
then
$c.setVal("<300");
end
*Pojo :*
package com.sample;
public class Customer {
private double si;
private String val;
public double getSi() {
return si;
}
public double setSi(double p,double r, double t) {
si = (p*r*t)/100;
return si;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
}
The error that I get while executing is :
[9,13]: unknown:9:13 mismatched token: [@33,126:126='<',<79>,9:13];
expecting type THEN[20,13]: unknown:20:13 mismatched token:
[@67,224:224='<',<79>,20:13]; expecting type THEN[32,13]: unknown:32:13
mismatched token: [@101,323:323='<',<79>,32:13]; expecting type THEN
java.lang.RuntimeException: Unable to compile "methodCall.drl".
at com.sample.Calling.createRuleBase(Calling.java:48)
at com.sample.Calling.main(Calling.java:24)
and my calling class is :
package com.sample;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
public class Calling {
public static void main(String[] args)throws IOException {
Customer c1 = new Customer();
c1.setSi(1000, 5, 2);
try {
RuleBase ruleBase = createRuleBase();
StatefulSession session = ruleBase.newStatefulSession ();
session.insert(c1);
session.fireAllRules();
session.dispose();
System.out.println("Interest is : "+c1.getVal());
}
catch (Throwable t) {
t.printStackTrace();
}
}
private static RuleBase createRuleBase() throws Exception
{
Reader source = new InputStreamReader(Calling.class.getResourceAsStream(
"methodCall.drl" ) );
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( source );
if ( builder.hasErrors() ) {
System.out.println( builder.getErrors().toString() );
throw new RuntimeException( "Unable to compile
\"methodCall.drl\".");
}
Package pkg = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
}