I believe this is a bug.  After I remove a rule or package from the ruleBase,
newly added rules fail to "see" objects in the working memory.  Attached is a
test case, and below is the output from it.

Drools 4.0.4
Java 1.5.0.13

Start
RuleBase created
Adding rules about 'tom':
package tom;
import test.Tester.Record;
rule "Find tom"
when
 $rec : Record( field1 == "tom" )
then
 System.out.println("Find 'tom' " + $rec.dump());
end

inserting record 1:"tom",
inserting record 2:"fred",
inserting record 3:"harry",
inserting record 4:"fred",
inserting record 5:"ed",
inserting record 6:"tom",
inserting record 7:"sreeni",
inserting record 8:"jill",
inserting record 9:"ed",
inserting record 10:"tom",

fireAllRules()
Find 'tom' 10:"tom",
Find 'tom' 6:"tom",
Find 'tom' 1:"tom",

adding rules about 'fred':
package fred;
import test.Tester.Record;
rule "Find fred"
when
 $rec : Record( field1 == "fred" )
then
 System.out.println("Find 'fred' " + $rec.dump());
end

Find 'fred' 4:"fred",
Find 'fred' 2:"fred",
As expected, 'fred' is found.

Removing package tom (same bug if you use "ruleBase.removeRule").
ruleBase.removePackage("tom");

adding rules about 'ed':
package ed;
import test.Tester.Record;
rule "Find ed"
when
 $rec : Record( field1 == "ed" )
then
 System.out.println("Find 'ed' " + $rec.dump());
end

No 'ed' is found!

Changing record 3 to 'ed'
fireAllRules()
Find 'ed' 3:"ed",
Only the newly updated 'ed' is found.



-- 
Dirk Bergstrom               [EMAIL PROTECTED]
_____________________________________________
Juniper Networks Inc.,          Computer Geek
Tel: 408.745.3182           Fax: 408.745.8905
package test;
import java.io.IOException;
import java.io.StringReader;

import org.drools.FactHandle;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;

public class Tester {

    private static String RULES_TOM =
        "package tom;\n" +
        "import test.Tester.Record;\n" +
        "rule \"Find tom\"\n" +
        "when\n" +
        " $rec : Record( field1 == \"tom\" )\n" +
        "then\n" +
        " System.out.println(\"Find 'tom' \" + $rec.dump());\n" +
        "end";

    private static String RULES_FRED =
        "package fred;\n" +
        "import test.Tester.Record;\n" +
        "rule \"Find fred\"\n" +
        "when\n" +
        " $rec : Record( field1 == \"fred\" )\n" +
        "then\n" +
        " System.out.println(\"Find 'fred' \" + $rec.dump());\n" +
        "end";

    private static String RULES_ED =
        "package ed;\n" +
        "import test.Tester.Record;\n" +
        "rule \"Find ed\"\n" +
        "when\n" +
        " $rec : Record( field1 == \"ed\" )\n" +
        "then\n" +
        " System.out.println(\"Find 'ed' \" + $rec.dump());\n" +
        "end";

    static String[][] MYDATA = {
        { "1", "tom", },
        { "2", "fred", },
        { "3", "harry", },
        { "4", "fred", },
        { "5", "ed", },
        { "6", "tom", },
        { "7", "sreeni", },
        { "8", "jill", },
        { "9", "ed", },
        { "10", "tom", },
    };

    static Record setup(WorkingMemory workingMemory) {
        Record testrec = null;
        for (String[] data: MYDATA) {
            Record rec = new Record(data);
            System.out.println("inserting record " + rec.dump());
            rec.setFactHandle(workingMemory.insert(rec));
            if (rec.id.equals("3")) testrec = rec;
        }
        return testrec;
    }

    public static void doit() throws Exception {
        System.out.println("Start");
        PackageBuilderConfiguration pkgBuilderCfg = new PackageBuilderConfiguration();
        JavaDialectConfiguration javaConf = (JavaDialectConfiguration)
            pkgBuilderCfg.getDialectConfiguration("java");
        javaConf.setCompiler(JavaDialectConfiguration.JANINO);
        javaConf.setJavaLanguageLevel("1.5");

        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        System.out.println("RuleBase created");
        System.out.println("Adding rules about 'tom':\n" + RULES_TOM + "\n");
        addRule(pkgBuilderCfg, ruleBase, RULES_TOM);
        WorkingMemory workingMemory = ruleBase.newStatefulSession();
        Record testrec = setup(workingMemory);

        System.out.println("\nfireAllRules()");
        workingMemory.fireAllRules();

        System.out.println("\nadding rules about 'fred':\n" + RULES_FRED + "\n");
        addRule(pkgBuilderCfg, ruleBase, RULES_FRED);
        System.out.println("As expected, 'fred' is found.");

        System.out.println("\nRemoving package tom (same bug if you use \"ruleBase.removeRule\").");
        System.out.println("ruleBase.removePackage(\"tom\");");
        ruleBase.removePackage("tom");

        System.out.println("\nadding rules about 'ed':\n" + RULES_ED + "\n");
        addRule(pkgBuilderCfg, ruleBase, RULES_ED);
        System.out.println("No 'ed' is found!");

        testrec.field1 = "ed";
        System.out.println("\nChanging record 3 to 'ed'");
        workingMemory.update(testrec.factHandle, testrec);
        System.out.println("fireAllRules()");
        workingMemory.fireAllRules();
        System.out.println("Only the newly updated 'ed' is found.");

    }

    private static void addRule(PackageBuilderConfiguration pkgBuilderCfg,
        RuleBase ruleBase, String rules) throws DroolsParserException,
        IOException, Exception {
        PackageBuilder rulePackageBuilder = new PackageBuilder(pkgBuilderCfg);
        rulePackageBuilder.addPackageFromDrl(new StringReader(rules));
        if (rulePackageBuilder.hasErrors()) {
            throw new Exception("Errors parsing rule : " +
                rulePackageBuilder.getErrors().toString());
        }
        ruleBase.addPackage(rulePackageBuilder.getPackage());
    }

    public static class Record {
        public String id;
        public String field1;
        public FactHandle factHandle;
        public Record(String[] values) {
            this.id = values[0];
            field1 = values[1];
        }
        public void setFactHandle(FactHandle factHandle) {
            this.factHandle = factHandle;
        }
        public String getfield1() {
            return field1;
        }
        public String dump() {
            return this.id + ":\"" + this.field1 + "\", ";
        }
    }

    public static final void main(String[] args) {
        try {
            doit();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
_______________________________________________
rules-users mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to