不規則性は円を形成せず、ソフトウェア分野でも同じことが言えます。たとえば、最も一般的な「メンバーシップポイントルール」。メンバー登録には元のポイントがあります。これは扱いやすいです。新しい値を作成するときに元の値を初期化します。その場合、元の値も通常登録、休日登録などとは異なり、元の値は異なります。これはルールです。また、登録の発効日もあります。たとえば、すぐに発効し、3日以内に発効します。これもルールです。会員消費通常消費、休日消費、誕生日消費などの複数のルールがあります。会員消費時間と割引も、休日消費と通常消費割引などの複数のルールを生成します。毎日の消費割引は異なります......。
このように、ルールは多様で目がくらむものとして説明でき、初心者はいくつかのif / elseで無限に判断できます。その後、将来のメンテナンスの難しさは言うまでもなく、これを行うことができます(ソースファイルにアクセスしない場合もあります)。 )、実行体だけで十分に膨らむことができます。経験の浅い人は、ファクトリパターン、アダプタパターンなど、いくつかのデザインパターンを選択する可能性があります。その後、問題が発生します。ルールだけです。無数の実装オブジェクトを作成します。そのようなルールがさらにいくつかある場合、システムは人々を窒息させるほど巨大です。
したがって、システム全体の構造に影響を与えることなく、これらの問題を解決するために、つまり、いわゆる疎結合を実現するために、いくつかの構成ファイルを変更する方法があるかどうか疑問に思います。それで、Droolsが誕生しました!
DroolsはJBOOSの下でのオープンソースビジネスです。オープンソースは無料であるだけでなく、プログラマーが私たちが見たものを十分に活用できるため、エキサイティングです。
ナンセンスではありません。メンバーシップポイントの小さな例を実装しましょう。謎が自然に現れます。
Eclipseの下でプロジェクトをビルドするには、すべての人の実装手順を整理しただけなので、単純なJavaプロジェクトのみをビルドしました。
droolsに必要なj関連のarパッケージをインポートします。スクリーンショットを参照してください。
次に、Javaクラスを追加して、最初に統合のパラメーターを宣言します。
package com.drools.demo.point;
/**
* 积分计算对象
* @author quzishen
*/
public class PointDomain {
// 用户名
private String userName;
// 是否当日生日
private boolean birthDay;
// 增加积分数目
private long point;
// 当月购物次数
private int buyNums;
// 当月退货次数
private int backNums;
// 当月购物总金额
private double buyMoney;
// 当月退货总金额
private double backMondy;
// 当月信用卡还款次数
private int billThisMonth;
public boolean isBirthDay() {
return birthDay;
}
public void setBirthDay(boolean birthDay) {
this.birthDay = birthDay;
}
public long getPoint() {
return point;
}
public void setPoint(long point) {
this.point = point;
}
public int getBuyNums() {
return buyNums;
}
public void setBuyNums(int buyNums) {
this.buyNums = buyNums;
}
public int getBackNums() {
return backNums;
}
public void setBackNums(int backNums) {
this.backNums = backNums;
}
public double getBuyMoney() {
return buyMoney;
}
public void setBuyMoney(double buyMoney) {
this.buyMoney = buyMoney;
}
public double getBackMondy() {
return backMondy;
}
public void setBackMondy(double backMondy) {
this.backMondy = backMondy;
}
public int getBillThisMonth() {
return billThisMonth;
}
public void setBillThisMonth(int billThisMonth) {
this.billThisMonth = billThisMonth;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
/**
* 记录积分发送流水,防止重复发放
* @param userName 用户名
* @param type 积分发放类型
*/
public void recordPointLog(String userName, String type){
System.out.println("增加对"+userName+"的类型为"+type+"的积分操作记录.");
}
}
ルールオブジェクトを定義したら、そのルールを定義し、srcディレクトリに新しいルールファイルaddpoint.drlを作成する必要があります。
編集:
package com.drools.demo.point
import com.drools.demo.point.PointDomain;
rule birthdayPoint
// 过生日,则加10分,并且将当月交易比数翻倍后再计算积分
salience 100
lock-on-active true
when
$pointDomain : PointDomain(birthDay == true)
then
$pointDomain.setPoint($pointDomain.getPoint()+10);
$pointDomain.setBuyNums($pointDomain.getBuyNums()*2);
$pointDomain.setBuyMoney($pointDomain.getBuyMoney()*2);
$pointDomain.setBillThisMonth($pointDomain.getBillThisMonth()*2);
$pointDomain.recordPointLog($pointDomain.getUserName(),"birthdayPoint");
end
rule billThisMonthPoint
// 2011-01-08 - 2011-08-08每月信用卡还款3次以上,每满3笔赠送30分
salience 99
lock-on-active true
date-effective "2011-01-08 23:59:59"
date-expires "2011-08-08 23:59:59"
when
$pointDomain : PointDomain(billThisMonth >= 3)
then
$pointDomain.setPoint($pointDomain.getPoint()+$pointDomain.getBillThisMonth()/3*30);
$pointDomain.recordPointLog($pointDomain.getUserName(),"billThisMonthPoint");
end
rule buyMoneyPoint
// 当月购物总金额100以上,每100元赠送10分
salience 98
lock-on-active true
when
$pointDomain : PointDomain(buyMoney >= 100)
then
$pointDomain.setPoint($pointDomain.getPoint()+ (int)$pointDomain.getBuyMoney()/100 * 10);
$pointDomain.recordPointLog($pointDomain.getUserName(),"buyMoneyPoint");
end
rule buyNumsPoint
// 当月购物次数5次以上,每五次赠送50分
salience 97
lock-on-active true
when
$pointDomain : PointDomain(buyNums >= 5)
then
$pointDomain.setPoint($pointDomain.getPoint()+$pointDomain.getBuyNums()/5 * 50);
$pointDomain.recordPointLog($pointDomain.getUserName(),"buyNumsPoint");
end
rule allFitPoint
// 特别的,如果全部满足了要求,则额外奖励100分
salience 96
lock-on-active true
when
$pointDomain:PointDomain(buyNums >= 5 && billThisMonth >= 3 && buyMoney >= 100)
then
$pointDomain.setPoint($pointDomain.getPoint()+ 100);
$pointDomain.recordPointLog($pointDomain.getUserName(),"allFitPoint");
end
必要なエンジンツールを生成するには、エンジンファクトリクラスを定義する必要があります
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
/**
* RuleBaseFacatory 单实例RuleBase生成工具
* @author quzishen
*/
public class RuleBaseFacatory {
private static RuleBase ruleBase;
public static RuleBase getRuleBase(){
return null != ruleBase ? ruleBase : RuleBaseFactory.newRuleBase();
}
}
次に、統合ルールのインターフェースを定義し、統合ルールの初期化、ルールの実行、およびルールの更新の3つを完了します。
import com.drools.demo.point.PointDomain;
/**
* 规则接口
* @author quzishen
*/
public interface PointRuleEngine {
/**
* 初始化规则引擎
*/
public void initEngine();
/**
* 刷新规则引擎中的规则
*/
public void refreshEnginRule();
/**
* 执行规则引擎
* @param pointDomain 积分Fact
*/
public void executeRuleEngine(final PointDomain pointDomain);
}
実装クラス:
import java.io.File;
/**
* 规则接口实现类
* @author quzishen
*/
public class PointRuleEngineImpl implements PointRuleEngine {
private RuleBase ruleBase;
/* (non-Javadoc)
* @see com.drools.demo.point.PointRuleEngine#initEngine()
*/
public void initEngine() {
// 设置时间格式
System.setProperty("drools.dateformat", "yyyy-MM-dd HH:mm:ss");
ruleBase = RuleBaseFacatory.getRuleBase();
try {
PackageBuilder backageBuilder = getPackageBuilderFromDrlFile();
ruleBase.addPackage(backageBuilder.getPackage());
} catch (DroolsParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see com.drools.demo.point.PointRuleEngine#refreshEnginRule()
*/
public void refreshEnginRule() {
ruleBase = RuleBaseFacatory.getRuleBase();
org.drools.rule.Package[] packages = ruleBase.getPackages();
for(org.drools.rule.Package pg : packages) {
ruleBase.removePackage(pg.getName());
}
initEngine();
}
/* (non-Javadoc)
* @see com.drools.demo.point.PointRuleEngine#executeRuleEngine(com.drools.demo.point.PointDomain)
*/
public void executeRuleEngine(final PointDomain pointDomain) {
if(null == ruleBase.getPackages() || 0 == ruleBase.getPackages().length) {
return;
}
StatefulSession statefulSession = ruleBase.newStatefulSession();
statefulSession.insert(pointDomain);
// fire
statefulSession.fireAllRules(new org.drools.spi.AgendaFilter() {
public boolean accept(Activation activation) {
return !activation.getRule().getName().contains("_test");
}
});
statefulSession.dispose();
}
/**
* 从Drl规则文件中读取规则
* @return
* @throws Exception
*/
private PackageBuilder getPackageBuilderFromDrlFile() throws Exception {
// 获取测试脚本文件
List<String> drlFilePath = getTestDrlFile();
// 装载测试脚本文件
List<Reader> readers = readRuleFromDrlFile(drlFilePath);
PackageBuilder backageBuilder = new PackageBuilder();
for (Reader r : readers) {
backageBuilder.addPackageFromDrl(r);
}
// 检查脚本是否有问题
if(backageBuilder.hasErrors()) {
throw new Exception(backageBuilder.getErrors().toString());
}
return backageBuilder;
}
/**
* @param drlFilePath 脚本文件路径
* @return
* @throws FileNotFoundException
*/
private List<Reader> readRuleFromDrlFile(List<String> drlFilePath) throws FileNotFoundException {
if (null == drlFilePath || 0 == drlFilePath.size()) {
return null;
}
List<Reader> readers = new ArrayList<Reader>();
for (String ruleFilePath : drlFilePath) {
File file=new File(ruleFilePath);
readers.add(new FileReader(file));
}
return readers;
}
/**
* 获取测试规则文件
*
* @return
*/
private List<String> getTestDrlFile() {
List<String> drlFilePath = new ArrayList<String>();
URL url=this.getClass().getResource("/");
drlFilePath
.add(url.getPath().replaceAll("\\\\", "/")+"addpoint.drl");
drlFilePath
.add(url.getPath().replaceAll("\\\\", "/")+"subpoint.drl");
return drlFilePath;
}
}
さて、それをテストしましょう:
import java.io.BufferedReader;
public class Test {
public static void main(String[] args) throws IOException {
PointRuleEngine pointRuleEngine = new PointRuleEngineImpl();
while(true){
InputStream is = System.in;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String input = br.readLine();
if(null != input && "s".equals(input)){
System.out.println("初始化规则引擎...");
pointRuleEngine.initEngine();
System.out.println("初始化规则引擎结束.");
}else if("e".equals(input)){
final PointDomain pointDomain = new PointDomain();
pointDomain.setUserName("hello kity");
pointDomain.setBackMondy(100d);
pointDomain.setBuyMoney(500d);
pointDomain.setBackNums(1);
pointDomain.setBuyNums(5);
pointDomain.setBillThisMonth(5);
pointDomain.setBirthDay(true);
pointDomain.setPoint(0l);
pointRuleEngine.executeRuleEngine(pointDomain);
System.out.println("执行完毕BillThisMonth:"+pointDomain.getBillThisMonth());
System.out.println("执行完毕BuyMoney:"+pointDomain.getBuyMoney());
System.out.println("执行完毕BuyNums:"+pointDomain.getBuyNums());
System.out.println("执行完毕BackNums:"+pointDomain.getBackNums());
System.out.println("执行完毕BackMondy:"+pointDomain.getBackMondy());
System.out.println("执行完毕规则引擎决定发送积分:"+pointDomain.getPoint());
} else if("r".equals(input)){
System.out.println("刷新规则文件...");
pointRuleEngine.refreshEnginRule();
System.out.println("刷新规则文件结束.");
}
}
}
}
結果:
s
初始化规则引擎...
初始化规则引擎结束.
e
增加对hello kity的类型为birthdayPoint的积分操作记录.
增加对hello kity的类型为buyMoneyPoint的积分操作记录.
增加对hello kity的类型为buyNumsPoint的积分操作记录.
增加对hello kity的类型为allFitPoint的积分操作记录.
增加对hello kity的类型为subBackNumsPoint的积分操作记录.
增加对hello kity的类型为subBackMondyPoint的积分操作记录.
执行完毕BillThisMonth:10
执行完毕BuyMoney:1000.0
执行完毕BuyNums:10
执行完毕BackNums:1
执行完毕BackMondy:100.0
执行完毕规则引擎决定发送积分:490
r
刷新规则文件...
刷新规则文件结束.
完全なプロジェクトのダウンロードリンク:http://download.csdn.net/detail/huxiang19851114/4697914