2011-03-18 40 views
1

時,執行以下邏輯:如何使規則的流量預計在Drools中

if (order.getPrice()<200) { 
    order.setPrice(order.getPrice()-10); 
} else { 
    order.setPrice(order.getPrice()-20); 
    if (order.getPrice()<200) { 
    //do nothing 
    }else { 
     order.setFreeShip("true"); 
    } 
} 

以上邏輯,如果我想在Drools的規則來實現。

rule "rule 1" 
when 
    $o:Order (amount<200); 
then 
    $o.setPrice($o.getPrice()-10); 
end 

rule "rule 2" 
when 
    $o:Order (amount>200); 
then 

    $o.setPrice($o.getPrice()-20); 

end 

如果事實的價格是210,則激活規則2,然後規則1將被激發。這不是預期的。我不想收回()。那麼這個問題有沒有更好的解決方案?

而且我可以指定一個規則執行後的順序規則,就像一個令牌機制一樣。

謝謝。

+0

注意:想想如果你的'金額'**等於**會發生什麼。 – 2015-11-19 12:29:37

回答

1

不,您不能爲一個規則或一組規則指定順序。你需要創建一個標誌,並且把它作爲一個後衛,例如:

rule "rule 1" 
when 
    $o:Order (amount<200); 
    not: Flag(object=$o) 
then 
    $o.setPrice($o.getPrice()-10); 
    insert(new Flag($o)); 
end 

rule "rule 2" 
when 
    $o:Order (amount>200); 
    not: Flag(object=$o) 
then 
    $o.setPrice($o.getPrice()-20); 
    insert(new Flag($o)); 
end 

public class Flag { 
    private final Object object; 
    public Flag(Object o) { 
    this.object = o; 
    } 

    //add getter for object 
    //delegate equals and hashcode to object 
} 
1

規則 「NYuser_Rule」

no-loop true 
ruleflow-group "EvalLoopcondition" 
when 
    m:HelloProcessModel(userlocation in ("NewYorkUser"), count < 4) 
then 
    m.setLoopcondition(6);update(m); 

規則「ChileUser_Rule」

no-loop true 
ruleflow-group "EvalLoopcondition" 
when 
    m:HelloProcessModel(userlocation in ("ChileUser"), count < 3) 
then 
    m.setLoopcondition(5);update(m); 

結束

這樣的事情可以幫助你。