Drools規則引擎drl文件語法

參考博客鏈接:https://www.cnblogs.com/atomicbomb/p/7251814.html

drl文件內包含:包路徑,引用,規則體

package:包路徑,改路徑是邏輯路徑(第一行)

import:導入外部變量,類(可以是類的靜態方法)

globals:全局變量

functions:函數

queries:查詢

rule:規則體,以rule開頭,已end結尾,每個文件可以包含多個rule,規則體有三個部分:LHS,RHS,屬性

    LHS:(Left Hand Side) 條件部分,在when和then中間的部分;可以包含0~n個條件;爲空的話引擎會自動添加一個eval(true)的條件。

     RHS:(right hand side) 在then後的部分,滿足LHS的條件,執行RHS;這部分可以直接編寫java代碼;可以對fact進行增刪改的操作


 

 

import com.sutpc.maas.orderbook.domain.Order;
import com.sutpc.maas.orderbook.domain.StaffType;import com.sutpc.maas.orderbook.domain.OrderType;
import com.sutpc.maas.orderbook.domain.ConnectionType;
import com.sutpc.maas.orderbook.domain.Price;
import com.sutpc.maas.orderbook.domain.DiscountType
import java.math.BigDecimal;

function  Boolean containStr(String routeNo,String routeNos){
        Boolean isContain = routeNos.contains(routeNo);
        return isContain;
}

function Price queryPrice(Double routePrice){
    Price price = new Price();
    price.setPrice(routePrice);
    price.setActualPrice(routePrice);
    price.setDiscountType(DiscountType.NON);
    return price;
}
function Price calculatePrice(Double amount,Double discount){
    Price price = new Price();
    BigDecimal b = new BigDecimal(amount);
    amount = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    price.setPrice(amount);
    Double actualPrice = amount*discount;
    actualPrice = new BigDecimal(actualPrice).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    price.setActualPrice(actualPrice);
    price.setDiscountType(DiscountType.DISCOUNT);
    return price;
}
rule "rule1"
    no-loop true
    when
        $order:Order(StaffType.INTERNAL==passenger.getType()&&orderType==OrderType.IMMED&&containStr(route.routeNo,"K4"))
    then
        System.out.println("進入rule1");
        Price price = new Price();
        price.setPrice($order.getRoute().getRoutePrice());
        price.setActualPrice(2.4);
        price.setDiscountType(DiscountType.FIX);
        $order.setPrice(price);
        update($order);
     end
rule "rule2"
    no-loop true
    when
        $order:Order(StaffType.EXTERNAL==passenger.getType()&&orderType==OrderType.IMMED&&containStr(route.routeNo,"K4"))
    then
        System.out.println("進入rule2");
        Price price = queryPrice($order.getRoute().getRoutePrice());
        $order.setPrice(price);
        update($order);
    end
rule "rule3"
    no-loop true
    when
        $order:Order(StaffType.EXTERNAL==passenger.getType()&&orderType==OrderType.IMMED&&containStr(route.routeNo,"K1,K2"))
    then
        System.out.println("進入rule3");
        Price price = queryPrice($order.getRoute().getRoutePrice());
        $order.setPrice(price);
        update($order);
    end
rule "rule4"
    no-loop true
    when
        $order:Order(StaffType.INTERNAL==passenger.getType()&&orderType==OrderType.IMMED&&containStr(route.routeNo,"K1,K2"))
    then
        System.out.println("進入rule4");
        Price price = new Price();
        price.setPrice($order.getRoute().getRoutePrice());
        price.setActualPrice(0.0);
        price.setDiscountType(DiscountType.FIX);
        $order.setPrice(price);
        update($order);
    end
rule "rule5"
    no-loop true
    when
        $order:Order(orderType==OrderType.IMMED&&containStr(route.routeNo,"C1-2")&&ConnectionType.UNITE==connectedTrip.connectionType&&numOfPeople==1)
    then
        System.out.println("進入rule5");
        Price price = queryPrice($order.getRoute().getRoutePrice());
        $order.setPrice(price);
        update($order);
    end
rule "rule6"
    no-loop true
    when
        $order:Order(orderType==OrderType.IMMED&&containStr(route.routeNo,"C1-2")&&ConnectionType.UNITE==connectedTrip.connectionType&&numOfPeople==2)
    then
        System.out.println("進入rule6");
        Double amount = $order.getRoute().getRoutePrice()*$order.getNumOfPeople();
        Price price = calculatePrice(amount,0.8);
        $order.setPrice(price);
        update($order);
    end
rule "rule7"
    no-loop true
    when
        $order:Order(orderType==OrderType.IMMED&&containStr(route.routeNo,"C1-2")&&ConnectionType.UNITE==connectedTrip.connectionType&&numOfPeople==3)
    then
        System.out.println("進入rule6");
        Double amount = $order.getRoute().getRoutePrice()*$order.getNumOfPeople();
        Price price = calculatePrice(amount,0.6);
        $order.setPrice(price);
        update($order);
    end
rule "rule8"
    no-loop true
    when
        $order:Order(orderType==OrderType.IMMED&&containStr(route.routeNo,"C1-2")&&ConnectionType.UNITE==connectedTrip.connectionType&&numOfPeople>3)
    then
        System.out.println("進入rule6");
        Double amount = $order.getRoute().getRoutePrice()*$order.getNumOfPeople();
        Price price = calculatePrice(amount,0.5);
        $order.setPrice(price);
        update($order);
    end

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章