2013-02-21 49 views
1

我一直在尋找一段時間,但我無法在任何地方找到我的問題的確切答案,或者如果我找到類似的東西,它不起作用。如何在drools規則的'when'部分中調用Java函數?

我想在部分規則中調用簡單的java方法。

我的代碼如下所示:

rule "Ret Rule" 
when 
    Map(this["LOYAL"] == "true") 
    Map(this["LOYALTYPROMORETENTION"] == "true") 
    PromotionValidityPeriod(promotionName == "VIVACLUB Loyalty Promo 2013 25 percent") 
    $customer: Customer(segment == "Residential" , $assets : assets) 
    $o: Order(((DPOrderType == 17 && retentionReason == "RET") || (DPOrderType == 2 && reason == "557")) , $ct: contractTerms == 24, $olis: orderLineItems) 
    $tariff: OrderLI(part in ("DT2319", "DT2320"), actionCode not in ("Delete", "INVALID"), $parentId : parentId) from $olis 
    OrderLI(part == "DT2316", nodeId == $parentId, actionCode not in ("Delete", "INVALID"), $assetId : assetId) from $olis 
    /*Asset(assetId == $assetId, 
     ((contractTerms != null && contractEndDate != null && eval(CalculationsHelper.getFullMonthDifference(new Date(), contractEndDate) < 3)) 
      || (contractTerms == null))) from $assets*/ 
    $li : OrderLI($newTariff : part in ("DT2319", "DT2320"), parentId == $parentnodeid, actionCode == "Add") from $olis 
    $del : OrderLI($oldTariff : part, parentId == $parentnodeid, actionCode == "Delete", productType == "Calling Plan") from $olis 
    eval(OrderDwrController.setTransitionCondition(fromTariff == $oldTariff, toTariff == $newTariff) == true 
then 
    Offer of = new Offer("DT2331", $parentId, 7); 
    System.out.println($tariffOld); 
    of.getOrderLineItemAttributes().add(new OrderLIAttribute("DURATION", "" + $ct)); 
    of.getOrderLineItemAttributes().add(new OrderLIAttribute("Discount of MRC", "25%")); 
    of.getOrderLineItemAttributes().add(new OrderLIAttribute("VIVACOM TV Package", $tariff.getProductNameENU())); 
    of.setProductNameENU("VIVACLUB Loyalty Promo 2013 25 percent"); 
    $o.addOffer(of); 
    of.setLoyaltyPromo(true); 
    $o.addTextForOffer(of, new Integer[]{173}); 
end 

在那裏我有一個問題是最後一個在when部分的特定行:

eval(OrderDwrController.setTransitionCondition(
    fromTariff == $oldTariff, toTariff == $newTariff) == true 

我只是想調用一個簡單的函數

(OrderDwrController.setTransitionCondition(
    fromTariff == $oldTariff, toTariff == $newTariff)) 

像上面那樣的我

(eval(CalculationsHelper.getFullMonthDifference(
    new Date(), contractEndDate) < 3)) 

函數是靜態的,返回一個布爾值。我已經在文件的開頭導入了類。

我在做什麼錯?

+0

會發生什麼?不建立,不做你認爲應該的,或者是什麼? – gustafc 2013-02-21 12:39:42

+0

你使用的是什麼版本的流口水? – radai 2013-02-21 12:43:28

+0

我正在使用Drools 5.我收到一個奇怪的錯誤...我可以在我的程序中看到該規則不起作用。我的方法也有一個斷點,但我沒有達到它。我不知道我是否在調用像我應該......的功能? – hantonov 2013-02-21 12:57:42

回答

1

首先你沒有關閉eval()。

第二,如果你升級你的Drools,你可以只寫Java表達式在當時的部分,它會比eval()函數更快

+0

嗯..你是對的失蹤')'!謝謝!現在我收到錯誤:「只能導入一個類型,bg.vivacom.dp.controller.dwr.OrderDwrController解析爲一個包」。有任何想法嗎? – hantonov 2013-02-21 13:44:07

+0

您還沒有在上面的drl中包含導入,但是您是否檢查過以確保該名稱的類存在? – radai 2013-02-21 17:44:16

+0

是的,您應該檢查包裝拼寫是否正確,並且它存在於應用程序類路徑中。 – salaboy 2013-02-22 13:19:07

1

對不起張貼的答案,而不是隻回答你(不要有足夠的聲譽:))

無論你放在一個eval必須評估爲布爾值。我不確定你是誰。這可能是問題所在。

希望幫助

乾杯,

阿維納什