规则引擎简介
Java规则引擎是推理引擎的一种,它起源于基于规则的专家系统。 Java规则引擎将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策。Java规则引擎接受数据输入,解释业务规则,并根据规则作出业务决策。从这个意义上来说,它是软件方法学在"关注点分离"上的一个重要的进展。 JSR-94规范定义了独立于厂商的标准API,开发人员可以通过这个标准的API使用Java规则引擎规范的不同产品实现。但值得注意的是,这个规范并没有强制统一规则定义的语法,因此,当需要将应用移植到其他的Java规则引擎实现时,可能需要变换规则定义。
基于规则的专家系统(RBES)
专家系统是人工智能的一个分支,它模仿人类的推理方式,使用试探性的方法进行推理,并使用人类能理解的术语解释和证明它的推理结论。专家系统有很多分类:神经网络、基于案例推理和基于规则系统等。 规则引擎则是基于规则的专家系统的一部分。为了更深入的了解Java规则引擎,下面简要地介绍基于规则的专家系统(RBES)。
RBES的技术架构
RBES包括三部分:Rule Base(knowledge base)、Working Memory(fact base)和Rule Engine(推理引擎)。它们的结构如下所示:
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMyMDE3LmNuYmxvZ3MuY29tL2Jsb2cvNDMxNzQ4LzIwMTcwOS80MzE3NDgtMjAxNzA5MDUwMDE1NTEwMDctMjA3Njg1ODA3Mi5qcGc%3D.jpg?w=700)
RBES的推理(规则)引擎
和人类的思维相对应,规则引擎存在两者推理方式:演绎法(Forward-Chaining)和归纳法(Backward-Chaining)。演绎法从一个初始的事实出发,不断地应用规则得出结论(或执行指定的动作)。而归纳法则是从假设出发,不断地寻找符合假设的事实。 Rete算法是目前效率最高的一个Forward-Chaining推理算法,Drools项目是Rete算法的一个面向对象的Java实现。 规则引擎的推理步骤如下: 1. 将初始数据(fact)输入Working Memory。 2. 使用Pattern Matcher比较规则(rule)和数据(fact)。 3. 如果执行规则存在冲突(conflict),即同时激活了多个规则,将冲突的规则放入冲突集合。 4. 解决冲突,将激活的规则按顺序放入Agenda。 5. 使用规则引擎执行Agenda中的规则。重复步骤2至5,直到执行完毕所有Agenda中的规则。
String implName = "org.jcp.jsr94.ri.RuleServiceProvider";
Class.forName(implName);
RuleServiceProvider serviceProvider = RuleServiceProviderManager.getRuleServiceProvider(implName);
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
RuleAdministrator admin = serviceProvider.getRuleAdministrator();
HashMap properties = new HashMap();
properties.put("name", "My Rules");
properties.put("description", "A trivial rulebase");
FileReader reader = new FileReader("rules.xml");
RuleExecutionSet ruleSet = null;
try {
LocalRuleExecutionSetProvider lresp =
admin.getLocalRuleExecutionSetProvider(properties);
ruleSet = lresp.createRuleExecutionSet(reader, properties);
} finally {
reader.close();
}
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
JSR-94的产品实现 Java规则引擎商业产品有: l. ILOG公司的JRules 2. BlazeSoft公司的Blaze 3. Rules4J 4. Java Expert System Shell (JESS) 开源项目的实现包括: l. Drools项目
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
public class RuleEngineFacade {
private RuleAdministrator ruleAdministrator;
private RuleServiceProvider ruleServiceProvider;
private LocalRuleExecutionSetProvider ruleSetProvider;
private RuleRuntime ruleRuntime;
// configuration parameters
private String ruleServiceProviderUrl;
private Class ruleServiceProviderImpl; public void setRuleServiceProviderUrl(String url) {
this.ruleServiceProviderUrl = url;
}
public void setRuleServiceProviderImpl(Class impl) {
this.ruleServiceProviderImpl = impl;
}
public void init() throws Exception {
RuleServiceProviderManager.registerRuleServiceProvider(
ruleServiceProviderUrl, ruleServiceProviderImpl);
ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider(ruleServiceProviderUrl);
ruleAdministrator = ruleServiceProvider.getRuleAdministrator();
ruleSetProvider = ruleAdministrator.getLocalRuleExecutionSetProvider(null);
}
public void addRuleExecutionSet(String bindUri,InputStream resourceAsStream)
throws Exception {
Reader ruleReader = new InputStreamReader(resourceAsStream);
RuleExecutionSet ruleExecutionSet =
ruleSetProvider.createRuleExecutionSet(ruleReader, null);
ruleAdministrator.registerRuleExecutionSet(bindUri,ruleExecutionSet,null);
}
public StatelessRuleSession getStatelessRuleSession(String key)
throws Exception {
ruleRuntime = ruleServiceProvider.getRuleRuntime();
return (StatelessRuleSession) ruleRuntime.createRuleSession(key, null, RuleRuntime.STATELESS_SESSION_TYPE);
}
public StatefulRuleSession getStatefulRuleSession(String key)
throws Exception {
ruleRuntime = ruleServiceProvider.getRuleRuntime();
return (StatefulRuleSession) ruleRuntime.createRuleSession(
key, null, RuleRuntime.STATEFUL_SESSION_TYPE);
}
public RuleServiceProvider getRuleServiceProvider() {
return this.ruleServiceProvider;
}
}
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
public class Rule {
private String ruleName;
private RuleEngineFacade engineFacade; public void init() throws Exception {
InputStream is = Rule.class.getResourceAsStream(ruleName);
engineFacade.addRuleExecutionSet(ruleName, is);
is.close();
} public void setRuleName(String name) {
this.ruleName = name;
} public void setEngineFacade(RuleEngineFacade engine) {
this.engineFacade = engine;
} public StatelessRuleSession getStatelessRuleSession()
throws Exception {
return engineFacade.getStatelessRuleSession(ruleName);
} public StatefulRuleSession getStatefuleRuleSession()
throws Exception {
return engineFacade.getStatefulRuleSession(ruleName);
}
}
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
<bean id="ruleEngine" class="spring.RuleEngineFacade" init-method="init" singleton="false">
<property name="ruleServiceProviderUrl">
<value>http://drools.org/</value>
</property>
<property name="ruleServiceProviderImpl">
<value>org.drools.jsr94.rules.RuleServiceProviderImpl</value>
</property>
</bean>
<bean id="fibonacci" class="spring.Rule" init-method="init">
<property name="ruleName">
<value>/test/fibonacci.drl</value>
</property>
<property name="engineFacade">
<ref local="ruleEngine"/>
</property>
</bean>
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)
public class JSRTest extends TestCase {
ApplicationContext ctx = null;
protected void setUp() throws Exception {
super.setUp();
ctx = new FileSystemXmlApplicationContext("testrule.xml");
}
public void testGetRuleSession() throws Exception {
Rule rule = (Rule)ctx.getBean("fibonacci");
assertNotNull(rule.getStatefuleRuleSession());
assertNotNull(rule.getStatelessRuleSession());
}
public void testStatelessRule() throws Exception {
Rule rule = (Rule)ctx.getBean("fibonacci");
Fibonacci fibonacci = new Fibonacci(50);
List list = new ArrayList();
list.add(fibonacci);
StatelessRuleSession session = rule.getStatelessRuleSession();
session.executeRules(list);
session.release();
}
public void testStatefulRule() throws Exception {
Rule rule = (Rule)ctx.getBean("fibonacci");
Fibonacci fibonacci = new Fibonacci(50);
StatefulRuleSession session = rule.getStatefuleRuleSession();
session.addObject(fibonacci);
session.executeRules();
session.release();
}
}
![Java规则引擎及JSR-94[转] Java规则引擎及JSR-94[转]](https://image.shishitao.com:8440/aHR0cHM6Ly9jb21tb24uY25ibG9ncy5jb20vaW1hZ2VzL2NvcHljb2RlLmdpZg%3D%3D.gif?w=700)