在本章中,我們將創(chuàng)建一個(gè)Drools項(xiàng)目下列問題說明:
根據(jù)不同的城市和各類產(chǎn)品(城和產(chǎn)品的組合),找出當(dāng)?shù)囟悇?wù)相關(guān)的城市。
我們將有兩個(gè)DRL文件為我們的Drools項(xiàng)目。這兩個(gè)文件的DRL將意味著兩個(gè)城市在考慮(Pune和Nagpur)和四類產(chǎn)品(雜貨,藥品,手表和奢侈品)。
我們已經(jīng)使用了相同的售價(jià)以證明不同的輸出。請(qǐng)注意,所有的規(guī)則都被觸發(fā)了應(yīng)用。
這里是模型用來保存每個(gè)項(xiàng)目類型:
package com.sample; import java.math.BigDecimal; public class ItemCity { public enum City { PUNE, NAGPUR } public enum Type { GROCERIES, MEDICINES, WATCHES, LUXURYGOODS } private City purchaseCity; private BigDecimal sellPrice; private Type typeofItem; private BigDecimal localTax; public City getPurchaseCity() { return purchaseCity; } public void setPurchaseCity(City purchaseCity) { this.purchaseCity = purchaseCity; } public BigDecimal getSellPrice() { return sellPrice; } public void setSellPrice(BigDecimal sellPrice) { this.sellPrice = sellPrice; } public Type getTypeofItem() { return typeofItem; } public void setTypeofItem(Type typeofItem) { this.typeofItem = typeofItem; } public BigDecimal getLocalTax() { return localTax; } public void setLocalTax(BigDecimal localTax) { this.localTax = localTax; } }
正如前面所說,我們已經(jīng)用了兩個(gè)DRL文件在這里:Pune.drl 和Nagpur.drl.
這是對(duì)Pune執(zhí)行規(guī)則的DRL文件。
// created on: Dec 24, 2014 package droolsexample // list any import classes here. import com.sample.ItemCity; import java.math.BigDecimal; // declare any global variables here dialect "java" rule "Pune Medicine Item" when item : ItemCity (purchaseCity == ItemCity.City.PUNE, typeofItem == ItemCity.Type.MEDICINES) then BigDecimal tax = new BigDecimal(0.0); item.setLocalTax(tax.multiply(item.getSellPrice())); end rule "Pune Groceries Item" when item : ItemCity(purchaseCity == ItemCity.City.PUNE, typeofItem == ItemCity.Type.GROCERIES) then BigDecimal tax = new BigDecimal(2.0); item.setLocalTax(tax.multiply(item.getSellPrice())); end
這是對(duì)Nagpur市執(zhí)行規(guī)則的DRL文件。
// created on: Dec 26, 2014 package droolsexample // list any import classes here. import com.sample.ItemCity; import java上一篇:Drools規(guī)則編寫下一篇:Drools Eclipse插件