规则引擎 Drools与JRuleEngine

本文介绍了规则引擎的概念,重点讲解了Drools和JRuleEngine的使用,包括它们的优势、基本原理及代码示例,展示了如何在Java应用中实现规则引擎的功能。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!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)

  1. /* 
  2.   /* $Header$ 
  3.   */  
  4.   
  5.   package com.vivek.drools.example;  
  6.   
  7.   import java.io.IOException;  
  8.   import java.io.InputStream;  
  9.   import org.apache.commons.logging.Log;  
  10.   import org.apache.commons.logging.LogFactory;  
  11.   import org.drools.FactException;  
  12.   import org.drools.IntegrationException;  
  13.   import org.drools.RuleBase;  
  14.   import org.drools.WorkingMemory;  
  15.   import org.drools.io.RuleBaseLoader;  
  16.   import org.xml.sax.SAXException;  
  17.   
  18.   /** 
  19.   * Demonstration of a sample rule using Java. 
  20.   * 
  21.   * @version <tt>$Revision: 1.0 $</tt> 
  22.   * @author <a href="mailto:{vivek.tomar@naxos-software.de}" mce_href="mailto:{vivek.tomar@naxos-software.de}">{Vivek Tomar}</a>. 
  23.   */  
  24.   
  25.   public class ApplyRule {  
  26.   
  27.   // Constants -----------------------------------------------------  
  28.   
  29.   static Log log = LogFactory.getLog(ApplyRule.class.getName());  
  30.   private static final String RULE_FILE = "/rules/rules.drl";  
  31.   //private static final String RULE_IS_TURNOVER_INSURABLE = "turnoverInsurable";  
  32.   //private static final String RULE_IS_TURNOVER_NOT_INSURABLE = "turnoverNotInsurable";  
  33.   
  34.   // Attributes ----------------------------------------------------  
  35.   
  36.   private double turnover = 1000000;  
  37.   
  38.   // Constructors --------------------------------------------------  
  39.   
  40.   public ApplyRule () {  
  41.   try {  
  42.   InputStream in = this.getClass().getResourceAsStream(RULE_FILE);  
  43.   RuleBase ruleBase = RuleBaseLoader.loadFromInputStream(in);  
  44.   WorkingMemory workingMemory = ruleBase.newWorkingMemory();  
  45.   workingMemory.assertObject(this);  
  46.   // Fire specific rule  
  47.   //workingMemory.fireAllRules(new RuleNameEqualsAgendaFilter(RULE_IS_TURNOVER_INSURABLE));  
  48.   
  49.   //Fire all rules  
  50.   workingMemory.fireAllRules();  
  51.   } catch (IntegrationException e) {  
  52.   e.printStackTrace();  
  53.   } catch (FactException e) {  
  54.   e.printStackTrace();  
  55.   } catch (SAXException e) {  
  56.   e.printStackTrace();  
  57.   } catch (IOException e) {  
  58.   e.printStackTrace();  
  59.   }  
  60.   }  
  61.   
  62.   // Public --------------------------------------------------------  
  63.   
  64.   public static void main (String args[]) {  
  65.   new ApplyRule();  
  66.   }  
  67.   
  68.   public double getTurnover () {  
  69.   return turnover;  
  70.   }  
  71.   
  72.   public void setTurnover (double turnover) {  
  73.   this.turnover = turnover;  
  74.   }  
  75.   
  76.   public void printCompanyInsurable () {  
  77.   log.info("******************************");  
  78.   log.info("This company is Insurable.");  
  79.   log.info("******************************");  
  80.   }  
  81.   
  82.   public void printCompanyNotInsurable () {  
  83.   log.info("==============================");  
  84.   log.info("This company is not Insurable.");  
  85.   log.info("==============================");  
  86.   }  
  87.   }  
/*  /* $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)

  1.   <?xml version="1.0"?>  
  2. <rule-set name="cheese rules"  
  3.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值