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
{\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf430
{\fonttbl\f0\fnil\fcharset0 Monaco;}
{\colortbl;\red255\green255\blue255;\red127\green0\blue85;\red63\green95\blue191;\red0\green0\blue192;
\red42\green0\blue255;\red63\green127\blue95;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\deftab720
\pard\pardeftab720
\f0\fs22 \cf2 package\cf0 bmi;\
\
\cf2 import\cf0 java.io.BufferedWriter;\
\cf2 import\cf0 java.io.File;\
\cf2 import\cf0 java.io.FileWriter;\
\cf2 import\cf0 java.io.IOException;\
\cf2 import\cf0 java.io.InputStreamReader;\
\cf2 import\cf0 java.io.Reader;\
\cf2 import\cf0 java.io.Writer;\
\cf2 import\cf0 java.util.Random;\
\
\cf2 import\cf0 \ul org.drools.RuleBase\ulnone ;\
\cf2 import\cf0 \ul org.drools.RuleBaseFactory\ulnone ;\
\cf2 import\cf0 \ul org.drools.WorkingMemory\ulnone ;\
\cf2 import\cf0 \ul org.drools.compiler.PackageBuilder\ulnone ;\
\cf2 import\cf0 \ul org.drools.rule.Package\ulnone ;\
\
\pard\pardeftab720
\cf3 /**\cf0 \
\cf3 *\cf0 \cf3 This\cf0 \cf3 is\cf0 \cf3 a\cf0 \cf3 sample\cf0 \cf3 class\cf0 \cf3 to\cf0 \cf3 launch\cf0 \cf3 a\cf0 \cf3 rule.\cf0 \
\cf3 */\cf0 \
\pard\pardeftab720
\cf2 public\cf0 \cf2 class\cf0 DroolsTest \{\
\
\cf2 private\cf0 \cf2 static\cf0 Writer \cf4 output\cf0 = \cf2 null\cf0 ;\
\
\cf2 public\cf0 \cf2 static\cf0 \cf2 final\cf0 \cf2 void\cf0 main(String[] args) \cf2 throws\cf0 IOException \{\
Random rand= \cf2 new\cf0 Random();\
\cf2 double\cf0 bmi = 0;\
BMIClass[] array = \cf2 new\cf0 BMIClass[10000];\
File file = \cf2 new\cf0 File(\cf5 "writeDroolsRules.txt"\cf0 );\
\cf4 output\cf0 = \cf2 new\cf0 BufferedWriter(\cf2 new\cf0 FileWriter(file));\
\cf2 for\cf0 (\cf2 int\cf0 i = 0; i < 10000; i++) \{\
bmi = rand.nextDouble();\
bmi = bmi * 50 + 15;\
BMIClass bmiClass = \cf2 new\cf0 BMIClass();\
bmiClass.setBmi(bmi);\
array[i] = bmiClass;\
\}\
\cf2 for\cf0 (\cf2 int\cf0 i = 0; i < 10000; i++) \{\
BMIClass bmiClass = array[i];\
\cf2 try\cf0 \{\
\cf6 //load up the \cf0 rulebase\
\ul RuleBase\ulnone ruleBase = readRule();\
\ul WorkingMemory\ulnone workingMemory = \ul ruleBase.newStatefulSession()\ulnone ;\
\cf6 // go !\cf0 \
\ul workingMemory.insert(bmiClass)\ulnone ;\
\cf2 long\cf0 before = System.nanoTime();\
\ul workingMemory.fireAllRules()\ulnone ; \
\cf2 long\cf0 after = System.nanoTime();\
\cf2 long\cf0 total = after-before;\
\cf4 output\cf0 .write(total + \cf5 "\\t"\cf0 + bmiClass.getBmi());\
((BufferedWriter) \cf4 output\cf0 ).newLine();\
\} \cf2 catch\cf0 (Throwable t) \{\
t.printStackTrace();\
\}\
\}\
\}\
\
\cf2 private\cf0 \cf2 static\cf0 \ul RuleBase\ulnone readRule() \cf2 throws\cf0 Exception \{\
\cf6 //read in the source\cf0 \
Reader source = \cf2 new\cf0 InputStreamReader( DroolsTest.\cf2 class\cf0 .getResourceAsStream( \cf5 "/Sample.drl"\cf0 ) );\
\
\cf6 //optionally read in the DSL (if you are using it).\cf0 \
\cf6 //Reader \ul dsl\ulnone = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/\ul mylang\ulnone .\ul dsl\ulnone " ) );\cf0 \
\
\cf6 //Use package builder to build up a rule package.\cf0 \
\cf6 //An alternative lower level class called "DrlParser" can also be used...\cf0 \
\
\ul PackageBuilder\ulnone builder = \cf2 \ul \ulc2 new\cf0 \ulc0 PackageBuilder()\ulnone ;\
\
\cf6 //this will parse and compile in one step\cf0 \
\cf6 //NOTE: There are 2 methods here, the one argument one is for normal DRL.\cf0 \
\ul builder.addPackageFromDrl( source )\ulnone ;\
\
\cf6 //Use the following instead of above if you are using a DSL:\cf0 \
\cf6 //builder.addPackageFromDrl( source, \ul dsl\ulnone );\cf0 \
\
\cf6 //get the compiled package (which is \ul serializable\ulnone )\cf0 \
\ul Package\ulnone pkg = \ul builder.getPackage()\ulnone ;\
\
\cf6 //add the package to a \ul rulebase\ulnone (deploy the rule package).\cf0 \
\ul RuleBase\ulnone ruleBase = \ul RuleBaseFactory.newRuleBase()\ulnone ;\
\ul ruleBase.addPackage( pkg )\ulnone ;\
\cf2 return\cf0 ruleBase;\
\} \
\
\cf2 public\cf0 \cf2 static\cf0 \cf2 class\cf0 BMIClass \{\
\
\cf2 private\cf0 \cf2 double\cf0 \cf4 bmi\cf0 ;\
\
\cf2 public\cf0 BMIClass(\cf2 double\cf0 bmi) \{\
\cf2 this\cf0 .\cf4 bmi\cf0 = bmi;\
\}\
\
\cf2 public\cf0 BMIClass() \{\
\}\
\
\cf2 public\cf0 \cf2 double\cf0 getBmi() \{\
\cf2 return\cf0 \cf4 bmi\cf0 ;\
\}\
\
\cf2 public\cf0 \cf2 void\cf0 setBmi(\cf2 double\cf0 bmi) \{\
\cf2 this\cf0 .\cf4 bmi\cf0 = bmi;\
\}\
\}\
\}\
}
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