Not really. :) I don't work with ruleflows. Attached is an implementation using BigDecimal. It should avoid any floating point problems. Make sure and use BigDecimal with the pure java version if you want a true measure of relative performance.
--- On Mon, 5/4/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: Monday, May 4, 2009, 6:00 PM > I have another little problem. I try to do the same with a > ruleflow > (and BMI rules in ruleflow group) and my ruleflow starts > running and > the rules are triggered, but I get no > system.out.println(...) and mijn > flow doesn't continue to the next node. > > Any suggestions? > > Femke > > Op 4-mei-09, om 23:30 heeft Greg Barton het volgende > geschreven: > > > > > I wonder if there's some way to introduce strictfp > into the drl. > > (I'm not even sure if that would even solve the > problem. But I > > figure it'd get you in the ballpark.) But I think > it would be > > useful to be able to inject strictfp into the > declarations of any > > generated drools classes. > > > > --- On Mon, 5/4/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: Monday, May 4, 2009, 4:11 PM > >> I solved the problem by removing every double in > the > >> sample.drl. How > >> van I fix the double problem if I really wanted to > :)? In > >> this case it > >> is unnecassary but there must be a solution? > >> > >> Thanks for all the help! > >> > >> Femke > >> > >> Op 4-mei-09, om 21:48 heeft Wolfgang Laun het > volgende > >> geschreven: > >> > >>> Don't use doubles. Try the same thing, > scaling the > >> random weights > >>> with 10 (or 100) convert to int, and use > suitably > >> scaled integer > >>> limits for your weight classes. > >>> > >>> I'm not at all sure, what various JVM > versions > >> and/or Rand > >>> generators could produce. > >>> > >>> The javadoc for java.lang.Math is an indicator > that > >> things might not > >>> be as simple as they ought to be. > >>> > >>> I could reproduce the problem this afternoon, > but now > >> I can't any > >>> more, on the same machine, same JVM and same > Drools > >> version. > >>> > >>> -W > >>> > >>> > >>> 2009/5/4 Femke De Backere > <[email protected]> > >>> So, I did as you suggested, with the following > as > >> result: > >>> BMI of 10 U heeft ondergewicht: 10 > >>> BMI of 30 U heeft ondergewicht: 30 > >>> BMI of 30 U heeft obesitas: 30 > >>> BMI of 20 U heeft ondergewicht: 20 > >>> > >>> So, I really don't get it, I used exactly > the same > >> code as you. > >>> > >>> Femke > >>> > >>> Op 4-mei-09, om 17:11 heeft Greg Barton het > volgende > >> geschreven: > >>> > >>>> > >>>> Well, approach this systematically, then: > Create a > >> new Drools > >>>> project in Eclipse and replace the > DroolsTest.java > >> and Sample.drl > >>>> with the ones I sent before. What results > do you > >> get? If they are > >>>> correct, modify the project one step at a > time > >> until the problem is > >>>> uncovered. > >>>> > >>>> One place to focus on: you're using > rule > >> flows. I have no idea how > >>>> that could cause a false positive result > like > >> you've seen, but you > >>>> never know. > >>>> > >>>> Finally, if you're comparing > performance of > >> rules vs plain java, > >>>> plain java will easily win in this case. > The rule > >> of thumb is > >>>> this: if you can easily write a plain > non-rules > >> algorithm it will > >>>> outperform the rules solution. > >>>> > >>>> --- On Mon, 5/4/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: Monday, May 4, 2009, 6:53 AM > >>>>> I'm using this: > >>>>> package bmi > >>>>> > >>>>> import bmi.DroolsTest.BMIClass; > >>>>> > >>>>> rule "Overgewicht" > >>>>> ruleflow-group "BMIrules" > >>>>> when > >>>>> bmiClass : BMIClass($bmi: bmi >= > 25, bmi > >> < 30) > >>>>> then > >>>>> System.out.println("U heeft > >> overgewicht: " + > >>>>> $bmi); > >>>>> end > >>>>> > >>>>> rule "Obesitas" > >>>>> ruleflow-group "BMIrules" > >>>>> when > >>>>> bmiClass : BMIClass($bmi : bmi >= > 30) > >>>>> then > >>>>> System.out.println("U heeft > obesitas: > >> " + > >>>>> $bmi); > >>>>> end > >>>>> > >>>>> > >>>>> rule "Ondergewicht" > >>>>> ruleflow-group "BMIrules" > >>>>> when > >>>>> bmiClass : BMIClass($bmi : bmi < > 18.5) > >>>>> then > >>>>> System.out.println("U heeft > >> ondergewicht: " + > >>>>> $bmi); > >>>>> end > >>>>> > >>>>> > >>>>> rule "Normaal" > >>>>> ruleflow-group "BMIrules" > >>>>> when > >>>>> bmiClass : BMIClass($bmi : bmi >= > 18.5, > >> bmi < 25) > >>>>> then > >>>>> System.out.println("U heeft een > normaal > >> gewicht: > >>>>> " + $bmi); > >>>>> end > >>>>> > >>>>> But in essence it is the same as > Greg's. I > >> get outputs > >>>>> as: > >>>>> > >>>>> U heeft ondergewicht: 25.0 > >>>>> U heeft overgewicht: 25.0 > >>>>> > >>>>> But I really don't see the > problem. > >>>>> > >>>>> Femke > >>>>> > >>>>> Op 4-mei-09, om 13:25 heeft Wolfgang > Laun het > >> volgende > >>>>> geschreven: > >>>>> > >>>>>> I cannot reproduce your findings. > >>>>>> Which bmi values cause the firing > of two > >> rules? > >>>>>> Are you using exactly the same > .drl file > >> Greg sent? > >>>>>> -W > >>>>>> > >>>>>> 2009/5/4 Femke De Backere > >> <[email protected]> > >>>>>> Thanks for the answer! But I > sometimes get > >> 2 rules > >>>>> that are correct, > >>>>>> and that is impossible. I embedded > >> Greg's > >>>>> DroolsTest class in my > >>>>>> class (I am doing some test on > performance > >> and > >>>>> studying the > >>>>>> difference between performance > with a > >> plain java > >>>>> class), and it > >>>>>> still doesn't work, and I > really > >> don't see > >>>>> why. Anyone any > >>>>>> suggestions, in attachement, my > class. > >>>>>> > >>>>>> Thx, > >>>>>> > >>>>>> Femke > >>>>>> > >>>>>> > >>>>>> > >>>>>> > >>>>>> Op 4-mei-09, om 02:47 heeft Greg > Barton > >> het volgende > >>>>> geschreven: > >>>>>> > >>>>>> 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 > >>>>>> > >>>>>> > >>>>>> < > >>>>>> DroolsTest > >>>>>> > >>>>> > >> > .java><Sample.drl>_______________________________________________ > >>>>>> 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 > >>>>> > >>>>> > >> _______________________________________________ > >>>>> 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 > >>> > >>> > >>> > _______________________________________________ > >>> 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 > > _______________________________________________ > rules-users mailing list > [email protected] > https://lists.jboss.org/mailman/listinfo/rules-users
Sample.drl
Description: Binary data
package com.sample;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigDecimal;
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(new BigDecimal("20")) );
workingMemory.insert( new BMIClass(new BigDecimal("30")) );
workingMemory.insert( new BMIClass(new BigDecimal("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 {
public static final BigDecimal OBESE = new BigDecimal("30");
public static final BigDecimal OVERWEIGHT = new BigDecimal("25");
public static final BigDecimal UNDERWEIGHT = new BigDecimal("18.5");
private final BigDecimal bmi;
public BMIClass(BigDecimal bmi) {
this.bmi = bmi;
}
public BigDecimal getBmi() {
return this.bmi;
}
public String toString() {
return "BMI of " + bmi;
}
}
}
_______________________________________________ rules-users mailing list [email protected] https://lists.jboss.org/mailman/listinfo/rules-users
