[rules-users] Complexity Conflict Resolver not working as expected

FrankVhh frank.vanhoenshoven at agserv.eu
Mon Mar 26 09:58:07 EDT 2012


Hi,

To my knowledge, conflict resolvers are only used to determine priority of
rule activations. It does not prevent rules from firing.

You should be able to get the behavior you want by adding activation-groups.
In an activation group, only one rule can fire. See documentation.

Regards,
Frank


vanithap wrote
> 
> I have 3 rules
> 
> R1 - state=GA, policy type = Home Owner, Rating program  = Standard, then
> print a
> R2 - State=GA, Rating Program = Standard, then print b
> R3 - State=GA, then print c
> 
> Now I insert the fact with state = GA, policy = HOwner, rating program =
> Standard,
> it matches on all the above 3 rules.
> 
> So I wrote my own ComplexityConflictresolver so only one rule should fire
> based on the most matched conditions. But it is firing all the above three
> rules.
> 
> This is the piece of code I have in complexity resolver
> 
> public int compare(Activation lhs,
>                        Activation rhs)
>     {
>         Rule lhsRule = lhs.getRule();
>         int numLhsConstraints = getNumConstraintsForRule(lhsRule);
>         
>         Rule rhsRule = rhs.getRule();
>         int numRhsConstraints = getNumConstraintsForRule(rhsRule);
> 
>         //return numRhsConstraints - numLhsConstraints;
>         
>         if ( numLhsConstraints > numRhsConstraints ) {
>             return 1;
>         } else if ( numLhsConstraints < numRhsConstraints ) {
>             return -1;
>         } else {
>             return 0;
>         }  
>     }
>     
>     private int getNumConstraintsForRule(Rule rule)
>     {
>         int numConstraints = 0;
>         
>         Declaration[] declarations = rule.getDeclarations();
>         for (Declaration declaration : declarations)
>         {
>             List constraints = declaration.getPattern().getConstraints();
>             if (null != constraints)
>             {
>                 numConstraints += constraints.size();
>             }
>         }
>         return numConstraints;
>     }
> 
> 
> 
> This is the piece of code where I hook in the resolver
>         RuleBaseConfiguration configuration = new RuleBaseConfiguration();
>         
>         ConflictResolver[] conflictResolvers = new ConflictResolver[] { 
>            
> com.test.rulesengine.conflict.ComplexityConflictResolver.getInstance()
>         };
>         CompositeConflictResolver resolver = new
> CompositeConflictResolver(conflictResolvers);
>         configuration.setConflictResolver(resolver);
>         
>         
>         KnowledgeBase knowledgeBase =
> KnowledgeBaseFactory.newKnowledgeBase(configuration);
> 
> 
> It looks like it is adding all three activations to Agenda and firing each
> one of them.
> 
> What am I doing wrong. Any help will be appreciated.
> 
> Thanks
> 


--
View this message in context: http://drools.46999.n3.nabble.com/Complexity-Conflict-Resolver-not-working-as-expected-tp3851854p3858196.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list