[rules-users] Simple rulebase not behaving as expected

Dean Jones dean.m.jones at gmail.com
Wed Aug 22 11:58:54 EDT 2007


Hi,

I have the following simple rulebase:

package org.drools.examples
rule "Hello World"
        when
            o : Object( )
        then
            System.out.println("Hello world (o=" + o + ")");
end
rule "Goodbye World"
        when
            not Object( )
        then
            System.out.println("Goodbye cruel world");
end

and the following code:

public class TestReadRuleFile {

public static void main(String[] args) throws Exception {
  PackageBuilder builder = new PackageBuilder();
  builder.addPackageFromDrl(
    new InputStreamReader(
        TestReadRuleFile.class.getResourceAsStream("test.drl")));
  Package pkg = builder.getPackage();
  RuleBase rulebase = RuleBaseFactory.newRuleBase();
  rulebase.addPackage(pkg);
  StatefulSession session = rulebase.newStatefulSession();
  session.fireAllRules();
}

}

which produces the following output:

Hello world (o=org.drools.reteoo.InitialFactImpl at 4dde85f0)

I expected the "Goodbye World" rule to fire, as there should not be
any Objects in the working memory. If I change my code to insert an
object into the working memory as follows:

  session.insert(new Object());

I now get the following output:

Hello world (o=org.drools.reteoo.InitialFactImpl at 4dde85f0)
Hello world (o=java.lang.Object at 17ba38f)

In this case, I expected the "Hello World" rule to fire only once. Any
thoughts on what I'm doing wrong here?

Thanks,

Dean.



More information about the rules-users mailing list