Drools6:规则互斥
在Drools中,有的时候会出现一种情况,一个事实Fact,满足了两个规则。
比如Fact的POJO是下面的
public class Message { public static final int HELLO = 1; public static final int GOODBYE = 0; private String message; private int status; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
而规则文件的定义是下面这样的:
package com.china.lxl.droolstest.entity import com.china.lxl.droolstest.entity.Message rule "Hello" when m : Message(status == Message.HELLO, myMessage : message) then System.out.println(myMessage); m.setMessage("GoodBye"); m.setStatus(Message.GOODBYE); update(m); end rule "GoodBye" salience 1 activation-group "Bye" when m : Message(status == Message.GOODBYE, myMessage : message) then System.out.println("GoodBye"); end rule "GoodBye1" salience 2 activation-group "Bye" when m : Message(status == Message.GOODBYE, myMessage : message) then System.out.println("GoodBye1"); end
假如rule "GoodBye"和rule "GoodBye1"中都没有设定activation-group,那么当发布一个事实,status=Message.HELLO时,就会先执行rule "Hello",再执行"GoodBye1",然后再执行"GoodBye",因为"GoodBye1"设定的优先级salience比"GoodBye"高。
而activation-group设定了规则的激活组,那么当出现一个事实造成规则冲突时,同一个激活组下只会执行优先级高的规则。