[rules-users] Unsatisfied rule fires - Drools bug?

matvey1414 matvey1414 at gmail.com
Mon Sep 26 16:51:11 EDT 2011


Hi, 

I am working with Drools to implement a high-profile rules-engine. I have
two rules defined. Clearly, only the first should fire, but both do. Here is
my DRL: 


package com.sample 

import com.sample.DroolsTest.Request; 

rule "ExpensiveCanonShopper0" 
when 
        Request( attributeMap["camera0"] == "canon" &&
attributeMap["price0"] >= 500 ) 
then 
        System.out.println("ExpensiveCanonShopper0"); 
end 

rule "ExpensiveCanonShopper1" 
when 
        Request( attributeMap["camera1"] == "canon" &&
attributeMap["price1"] >= 500 ) 
then 
        System.out.println("ExpensiveCanonShopper1"); 
end 

And the Java class to execute it: 

public class DroolsTest { 

        @SuppressWarnings({ "rawtypes", "unchecked" }) 
        public static final void main(String[] args) { 
                try { 
                        //Loading the Rules 
                        System.out.println("Loading rules"); 
                        RuleBase ruleBase = readRule();	
                        StatelessSession workingMemory =
ruleBase.newStatelessSession(); 
                        
                        System.out.println("Firing rules"); 
                        
                        Map map = new HashMap(); 
                        map.put("camera0", "canon"); 
                        map.put("price0", 600);	
                        
                        Request request = new Request();	
                        request.setAttributeMap(map);	
                        workingMemory.execute(request); 
                        
                } 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 FileReader(new File("drl",
"Generated.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 wil parse and compile in one step 
                //NOTE: There are 2 methods here, the one argument one is
for normal DRL. 
                builder.addPackageFromDrl( source ); 
                if (builder.hasErrors()) { 
                        PackageBuilderErrors errors = builder.getErrors();	
                        throw new RuntimeException("Error adding package to
builder: " + errors.toString()); 
                } 

                //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 Request { 
                private Map attributeMap; 

                public Map getAttributeMap() { 
                        return attributeMap; 
                } 

                public void setAttributeMap(Map attributeMap) { 
                        this.attributeMap = attributeMap; 
                } 
        } 
} 

The output is this, meaning both rules fired: 
Loading rules 
Firing rules 
ExpensiveCanonShopper1 
ExpensiveCanonShopper0 

I have two questions: 

1. Is this a bug, or am I doing something wrong? Only
"ExpensiveCanonShopper0" should fire. 

2. I am pretty sure this is somehow related to the fact that I'm using Map
attributes, and not POJO to get "price0" and "camera0". My issue is that I
won't know the parameters in advance (they are coming in a URL), so I can't
pre-declare them, and thus need something dynamic like a Map. Is this how
Drools is intended to be used? The documentation appears very POJO-centric. 

I am using Drools 5.2 

Thank you! 
-Matt

--
View this message in context: http://drools.46999.n3.nabble.com/Unsatisfied-rule-fires-Drools-bug-tp3370653p3370653.html
Sent from the Drools: User forum mailing list archive at Nabble.com.




More information about the rules-users mailing list