分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
规则引擎:Drools与JRuleEngine
译文:《规则引擎:Drools与JRuleEngine》
译者:chszs
作者:Vivek Tomar
原文:《Rule engine : Drools / JRuleEngine》
原文见http://www.naxos-software.de/blog/index.php?/archives/78-Rule-engine-Drools-JRuleEngine.html
一、规则引擎
规则引擎有助于基于存储在规则中的知识和推理来执行判断。这些规则基本上只有条件和动作,别无它物。
规则引擎的优点:
1、分隔应用程序的条件和控制流
(1) 规则都存储在单独的文件。
(2) 规则可以被技术人士和商业人士修改的。
(3) 规则改变后应用程序不必重新部署。
(4) 使用集中的规则使得应用程序更易于管理和维护。
2、规则替换了代码中的if else语句
(1) 规则脚本更容易提取。
(2) 即使是非技术人员也能轻易地遵循规则。
(3) 集中可以解决问题,而不是实现。
(4) 与实现代码相比,规则聚团更容易编写。
3、为什么作决定能很容易地概念化
总结:规则有助于消除代码中大量的if else语句,使代码更易于维护。
二、Drools介绍
Drools是一个开源实现。它是一个Java库,以Apache许可证发布,其二进制代码或源码下载均有效。
推理机的核心是一个规则引擎。它执行模式匹配,在执行动作中做出决策。RETE算法用于模式匹配。
知识被表述为规则。规则有两个主要部分:条件和动作。
例如:
如果用户(年龄>17),那么System.out.println("User is greater then 17");
在人工智能系统,主要有两种响应的方法。
1、正向链(Forward Chaining)
这是基于事实根据的。在工作区域检查中规则。当规则条件为真的规则不再有时,模式匹配结束。
2、反向链(Backward Chaining)
只检查规则的动作可以匹配目标的规则。如果满足条件,然后进行评估。
3、兼容JDK1.4,且需要下面的库。
(1) drools-all-jdk1.4.2.1.jar
(2) xercesImpl-2.6.2.jar
(3) antlr-2.7.5.jar
(4) janino-2.3.2.jar
4、代码示例(ApplyRule.java)
- /*
- /* $Header$
- */
- package com.vivek.drools.example;
- import java.io.IOException;
- import java.io.InputStream;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.drools.FactException;
- import org.drools.IntegrationException;
- import org.drools.RuleBase;
- import org.drools.WorkingMemory;
- import org.drools.io.RuleBaseLoader;
- import org.xml.sax.SAXException;
- /**
- * Demonstration of a sample rule using Java.
- *
- * @version <tt>$Revision: 1.0 $</tt>
- * @author <a href="mailto:{vivek.tomar@naxos-software.de}" mce_href="mailto:{vivek.tomar@naxos-software.de}">{Vivek Tomar}</a>.
- */
- public class ApplyRule {
- // Constants -----------------------------------------------------
- static Log log = LogFactory.getLog(ApplyRule.class.getName());
- private static final String RULE_FILE = "/rules/rules.drl";
- //private static final String RULE_IS_TURNOVER_INSURABLE = "turnoverInsurable";
- //private static final String RULE_IS_TURNOVER_NOT_INSURABLE = "turnoverNotInsurable";
- // Attributes ----------------------------------------------------
- private double turnover = 1000000;
- // Constructors --------------------------------------------------
- public ApplyRule () {
- try {
- InputStream in = this.getClass().getResourceAsStream(RULE_FILE);
- RuleBase ruleBase = RuleBaseLoader.loadFromInputStream(in);
- WorkingMemory workingMemory = ruleBase.newWorkingMemory();
- workingMemory.assertObject(this);
- // Fire specific rule
- //workingMemory.fireAllRules(new RuleNameEqualsAgendaFilter(RULE_IS_TURNOVER_INSURABLE));
- //Fire all rules
- workingMemory.fireAllRules();
- } catch (IntegrationException e) {
- e.printStackTrace();
- } catch (FactException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // Public --------------------------------------------------------
- public static void main (String args[]) {
- new ApplyRule();
- }
- public double getTurnover () {
- return turnover;
- }
- public void setTurnover (double turnover) {
- this.turnover = turnover;
- }
- public void printCompanyInsurable () {
- log.info("******************************");
- log.info("This company is Insurable.");
- log.info("******************************");
- }
- public void printCompanyNotInsurable () {
- log.info("==============================");
- log.info("This company is not Insurable.");
- log.info("==============================");
- }
- }
规则定义(rules.drl)
- <?xml version="1.0"?>
- <rule-set name="cheese rules"