Take the 2-minute tour ×

I am very new to Drools and I have completed some of the tutorials found online and read the docs. I am sure what I am about to explain has a trivial solution that I have missed but I will lay it out regardless.

I have a single rule in my drools file and an object that reads its info from a text file.

The code looks similar to this:

   public static final void main(String[] args) {
        try {
            ObjectArchive arch = ArchiveFactory.openArchive(new File("dwc.txt"));
            Iterator<Object> iter = arch.iteratorDwc();

            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger =  KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            while(iter.hasNext()) {
                Object dwcRecord = iter.next();
        } catch (Throwable t) {

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("dwcrules.drl"), ResourceType.DRL);
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        return kbase;

I have a simple rules file that checks if a property of said object is not null and prints out the object if this is true.

Now, the problem.

If I add one object (no while loop) then the rule is fired and all is well. However, if I add multiple objects to the session and then fire the rules it tends to just grab the last one, or fire but print out a single null.

So, why is it that I am unable to add multiple facts of the same type? Or what am I missing that stops me from being able to fire the same rule on multiple facts?

Thanks in advance.

EDIT: Sorry, here is the rule:

rule "Scientific Name"
    dwc : Object(dwc.getScientificName != null)
share|improve this question
Can you please paste your rule? –  Edson Tirelli Jul 12 '12 at 16:27
Small update, created another class just to test (basic class with one string property) and it works fine. Seems like there could be something to do with that particular class that prevents having multiple in the session? –  Christopher Gwilliams Jul 13 '12 at 14:35
By default, Drools uses identity to store objects in the working memory. Unless you changed the configuration option to use equality, you can have as many instances of the same class as you want/need. Suggestion: run your test using the WorkingMemoryConsoleLogger to print out your audit log into the console and paste the console log in here... it would also be good to print your object's scientificName property for each object before inserting them into working memory just to be sure they are not null. –  Edson Tirelli Jul 13 '12 at 14:43

1 Answer 1

up vote 1 down vote accepted

I don't know exactly what is happening, as I don't have your object file to run the test, but just open your log file in the eclipse audit view and you will immediately see the objects that were inserted and which ones activate the rule.

The rule will fire once for each object that have scientificName != null.

Also, your rule should not be trying to match Object if you have a more specialized class/interface that define the getScientificName() method. For instance, if you have a class named "MyDwcClass", you should write the rule like this:

rule "Scientific Name"
    dwc : MyDwcClass( scientificName != null )
share|improve this answer
Yes, I probably should have attempted a more generic example. I have modified the rule as above but the audit shows no output and the rule is still only fired once. If I fire all the rules within the while loop, it still only fires once but gets the right info. –  Christopher Gwilliams Jul 13 '12 at 8:37
OK, so it seems that the issue is with the library that is being used. I am accepting your answer because it is a more efficient way to write the rule. Thanks for the help! –  Christopher Gwilliams Jul 17 '12 at 7:27

Your Answer


