Try the attached code. --- On Sun, 5/3/09, Femke De Backere <[email protected]> wrote:
> From: Femke De Backere <[email protected]> > Subject: Re: [rules-users] Rule problem > To: "Rules Users List" <[email protected]> > Date: Sunday, May 3, 2009, 6:18 PM > Thx for the answer. I tried this but I doesn't seem to > work. For a BMI of 20, i get the result > "ondergewicht", and it should be > "normal". > > Op 4-mei-09, om 01:04 heeft Ingomar Otter het volgende > geschreven: > > > Femke, > > The "Overgewicht| rule matches for _two_ BMIClass > facts, one with bmi >- 25 and one for BMIClass <30. > > If you want to apply multiple constraints to a single > fact you have to list them as such: > > > >> rule "Overgewicht" > >> when > >> bmiClass : BMIClass(bmi >= 25 && bmi > <30) > >> then > >> System.out.println("U heeft > overgewicht"); > >> end > > > > > > Cheers, > > Ingomar > > > > > > Am 04.05.2009 um 00:48 schrieb Femke De Backere: > > > >> Hi! > >> > >> I want to make en BMI rule application, so I need > to test the BMI values. But in rule "Overgewicht" > and "Normaal", I need to use an AND, but it > doesn't seem to work in any way I tried. > >> > >> Does anybody see the problem? The rest of the > application is based on de sample project (created when > making a new Drools project). > >> > >> Thx, > >> > >> Femke > >> > >> package bmi > >> > >> import bmi.DroolsTest.BMIClass; > >> > >> rule "Overgewicht" > >> when > >> bmiClass : BMIClass(bmi >= 25) > >> bmiClass : BMIClass(bmi < 30) > >> then > >> System.out.println("U heeft > overgewicht"); > >> end > >> > >> rule "Obesitas" > >> when > >> bmiClass : BMIClass($bmi : bmi >= 30) > >> then > >> System.out.println("U heeft > obesitas"); > >> end > >> > >> rule "Ondergewicht" > >> when > >> bmiClass : BMIClass($bmi : bmi < 18.5) > >> then > >> System.out.println("U heeft > ondergewicht"); > >> end > >> > >> rule "Normaal" > >> when > >> bmiClass : BMIClass(bmi >= 18.5) > >> bmiClass : BMIClass(bmi < 25) > >> then > >> System.out.println("U heeft een normaal > gewicht"); > >> > end _______________________________________________ > >> rules-users mailing list > >> [email protected] > >> > https://lists.jboss.org/mailman/listinfo/rules-users > > > > _______________________________________________ > > rules-users mailing list > > [email protected] > > https://lists.jboss.org/mailman/listinfo/rules-users > > _______________________________________________ > rules-users mailing list > [email protected] > https://lists.jboss.org/mailman/listinfo/rules-users
package com.sample;
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;
/**
* This is a sample file to launch a rule package from a rule source file.
*/
public class DroolsTest {
public static final void main(String[] args) {
try {
//load up the rulebase
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newStatefulSession();
//go !
workingMemory.insert( new BMIClass(20) );
workingMemory.insert( new BMIClass(30) );
workingMemory.insert( new BMIClass(10) );
workingMemory.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Please note that this is the "low level" rule assembly API.
*/
private static RuleBase readRule() throws Exception {
//read in the source
Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Sample.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...
PackageBuilder builder = new PackageBuilder();
//this will 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)
Package pkg = builder.getPackage();
//add the package to a rulebase (deploy the rule package).
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
public static class BMIClass {
private final int bmi;
public BMIClass(int bmi) {
this.bmi = bmi;
}
public int getBmi() {
return this.bmi;
}
public String toString() {
return "BMI of " + bmi;
}
}
}
Sample.drl
Description: Binary data
_______________________________________________ rules-users mailing list [email protected] https://lists.jboss.org/mailman/listinfo/rules-users
