java规则引擎实例_JAVA规则引擎[Drools]--实例篇

更新时间:2023-05-22 05:29:26 阅读: 评论:0

java规则引擎实例_JAVA规则引擎[Drools]--实例篇
随着互联⽹应⽤的飞速发展,各种业务需求也应运⽽⽣,对于不断变更和⾰新的业务规则⽽⾔,项⽬的开发就很有必要把规则部分独⽴提取出来,此时Drools的价值就得到了体现。废话不多说,看⼀个简单的例⼦。
这⾥举⼀个⼿机话费的例⼦。
⼀、定义规则:
⾸先要分析⾃⼰的业务逻辑,根据业务⽽制定出不同的规则,这⾥我们假设有3个规则。
1、对于新开户的⼿机⽤户送20元话费。
2、在2014年10⽉到12期间充值的⽤户,不论⾦额多少充值满3次就赠送5元话费。
3、当⽉充值⾦额达到100以上,每达到⼀次赠送话费10元。
take out
⼆、创建Fact对象:
分析这些规则,并把他们抽象成⼀个Fact对象。
drools;
import java.util.UUID;
/**
* EntityRule-Model
* @author Candy
*
*/
public class EntityRule {
private String urname;
/** Whether for new account. */
private boolean account;
/
** The number of add. */
private int addtime;
/** The sum of the current account. */
private double currentmoney;
/** The totail amount added. */
private double totailaddmoney;
/**
* Record the rial number of the operation.
* @param urname
* @param currentmoney
*/孤独患者英文
public void getSerialnumber(String urname,double currentmoney){
System.out.println("Account:"+urname+" Balance:¥"+currentmoney);
System.out.println("Serial Number:"+UUID.randomUUID().toString());
}
三、定义规则引擎:
scratchpad业务和规则都整理清楚了我们就可以开始规则引擎的核⼼部分啦,这⾥我定义的是接⼝和实现类。drools;
/**
* RuleEngine-Interface
* @author Candy
*
*/
public interface RuleEngine {
/**
* Initializes the rules engine.
*/
public void init();
/**
*  Refresh the rules engine.
*/
public void refresh();
/**
* Execute the rules engine.
*/
public void execute(final EntityRule entityRule);
}
drools;
import java.io.File;2013年考研复试分数线
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.drools.RuleBa;
import org.drools.StatefulSession;
天天向上学习网import piler.DroolsParrException;
import piler.PackageBuilder;
import org.drools.rule.Package;
import org.drools.spi.Activation;
/**
* RuleEngine-Implements
* @author Candy
初次见面英语怎么说*
*/
public class RuleEngineImpl implements RuleEngine{
private RuleBa ruleBa;
/*
* (non-Javadoc)
* @drools.RuleEngine#init()
*/
@Override
public void init() {
/** Sets the system time format. */
System.tProperty("drools.dateformat", "yyyy-MM-dd HH:mm:ss"); /** Get the ba factory. */
ruleBa =RuleBa();
try {
/** Get the rule files has bean read. */
PackageBuilder backageBuilder = getPackageBuilderFile();
/** Add the package to the 'RuleBa'. */
ruleBa.Packages());
} catch (DroolsParrException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {globaltest
e.printStackTrace();
}
}小学课堂纪律
/*
* (non-Javadoc)
* @drools.RuleEngine#refresh()
*/
@Override
public void refresh() {
ruleBa = RuleBa();
Package[] packages = Packages();
for(Package items :packages){
}
init();
}
/*
* (non-Javadoc)
* @drools.RuleEngine#drools.EntityRule) */
@Override
public void execute(final EntityRule entityRule) {
if(null == Packages() || 0 == Packages().length) { return;
}
StatefulSession statefulSession = wStatefulSession(); statefulSession.inrt(entityRule);
statefulSession.fireAllRules(new org.drools.spi.AgendaFilter() {
public boolean accept(Activation activation) {
return !Rule().getName().contains("_test");
}
});
statefulSession.dispo();
}
/**
* Read the rule files.
* @return
* @throws Exception
*/
private PackageBuilder getPackageBuilderFile()throws Exception { /** Get the rule files. */
List drlFilePath = getRuleFile();
/** Sets the file to 'readers'. */
List readers = loadRuleFile(drlFilePath);
/** To create the 'backageBuilder'. */
PackageBuilder backageBuilder = new PackageBuilder();
for (Reader r : readers) {
backageBuilder.addPackageFromDrl(r);
}
/
** Check if the script has a problem. */
if(backageBuilder.hasErrors()) {
throw new Errors().toString());
}
return backageBuilder;
}
氢怎么读
/**
* Load the script files.
* @param drlFilePath
* @return
* @throws FileNotFoundException
*/
private List loadRuleFile(List drlFilePath)
throws FileNotFoundException {
if (null == drlFilePath || 0 == drlFilePath.size()) {
return null;
}
List readers = new ArrayList();
for (String ruleFilePath : drlFilePath) {lfy
readers.add(new FileReader(new File(ruleFilePath)));
}

本文发布于:2023-05-22 05:29:26,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/728255.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:规则   业务   引擎   达到   得到   体现
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图