漫話規則引擎(4): Java規則引擎規範:JSR94

1 概述

JSR-94是JCP(Java Community Process)制定的關於Java規則引擎API的規範,包括接口定義和示例代碼。於2004年8月發佈。 JSR-94定義了javax.rules和javax.rules.admin,前者包含了Java規則引擎運行時(Rumtime)API及異常 (Exception)定義,後者包含了規則管理相關的API和異常定義。 html

2 規則管理API

規則管理API在javax.rules.admin中定義,主要包括如下類/接口: java

 
類/接口 說明
Rule 規則實體
RuleExecutionSet 執行集,某個規則對應的動做
LocalRuleExecutionSetProvider 用於從本地建立執行集,如InputStream,Reader等
RuleExectuionSetProvider 用於從本地或遠程建立執行集,如xml Element,Serializable等
RuleAdministrator 用於獲取ExecutionSetProvider,並管理執行集的註冊/註銷

規則管理API實現的功能包括: 算法

  1. 裝載規則(Rule)和執行集(RuleExecutionSet)
  2. 執行集的註冊/註銷,只有註冊的執行集對應的規則才能被客戶訪問

3 運行時API

運行時API在javax.rules中定義,主要包括如下類/接口: less

 
類/接口 說明
RuleServiceProviderManager 經過URL註冊/註銷RuleServiceProvider
RuleServiceProvider 提供對RuleRuntime和RuleAdministrator的訪問
RuleRuntime 規則引擎運行時,能夠建立規則會話
RuleSession 規則會話,用於執行規則
RuleExecutionSetMetaData 執行集元數據,包括name,url,description等。執行集元數據會被RuleSession使用
StatelessRuleSession 無狀態規則會話
StatefulRuleSession 有狀態規則會話
Handle和ObjectFilter 有狀態會話維護會話狀態的幫助類

規則引擎運行時API實現的功能包括: ide

  1. 註冊/註銷規則引擎實例,只有註冊的規則引擎實例才能被使用
  2. 從註冊的規則引擎實例建立Runtime
  3. 從Runtime建立會話,包括有狀態和無狀態兩種會話
  4. 經過會話執行規則

4 異常定義

除了前面提到的主要類/接口外,JSR94還規定了規則引擎運行時及管理的一些異常,以下: 工具

5 代碼示例

下面是使用Drools做爲規則引擎實例的一個例子,規則文件使用了Drools的drl格式: 開發工具

JSR94Sample.java ui

複製代碼
package com.sample; import java.io.FileReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.rules.ConfigurationException; import javax.rules.RuleRuntime; import javax.rules.RuleServiceProvider; import javax.rules.RuleServiceProviderManager; import javax.rules.StatelessRuleSession; import javax.rules.admin.LocalRuleExecutionSetProvider; import javax.rules.admin.RuleAdministrator; import javax.rules.admin.RuleExecutionSet; import org.drools.jsr94.rules.RuleServiceProviderImpl; public class JSR94Sample { private static RuleServiceProvider ruleProvider; private static void initProvider(){ String uri = RuleServiceProviderImpl.RULE_SERVICE_PROVIDER; Class providerClass = RuleServiceProviderImpl.class; try{ //註冊ruleProvider  RuleServiceProviderManager.registerRuleServiceProvider(uri, providerClass); //從RuleServiceProviderManager獲取ruleProvider ruleProvider = RuleServiceProviderManager.getRuleServiceProvider(uri); }catch(ConfigurationException e){ e.printStackTrace(); } } private static void adminSample(){ try{ //獲取RuleAdministrator實例 RuleAdministrator admin = ruleProvider.getRuleAdministrator(); //獲取RuleExectuionSetProvider HashMap properties = new HashMap(); properties.put("name", "My Rules"); properties.put("description", "A trivial rulebase"); LocalRuleExecutionSetProvider ruleExecutionSetProvider = admin.getLocalRuleExecutionSetProvider(properties); //建立RuleExecutionSet FileReader reader = new FileReader("bin/sample.drl"); RuleExecutionSet reSet = ruleExecutionSetProvider.createRuleExecutionSet(reader, properties); //註冊RuleExecutionSet admin.registerRuleExecutionSet("mysample",reSet,properties); }catch(Exception e){ e.printStackTrace(); } } private static void runtimeSampe(){ try{ //獲取RuleRuntime, 建立會話 RuleRuntime runtime = ruleProvider.getRuleRuntime(); StatelessRuleSession ruleSession = (StatelessRuleSession)runtime.createRuleSession("mysample",null,RuleRuntime.STATELESS_SESSION_TYPE); //初始化輸入數據 Message message1 = new Message(); message1.setMessage("Hello World"); message1.setStatus(Message.HELLO); Message message2 = new Message(); message2.setMessage("Goodbye World"); message2.setStatus(Message.GOODBYE); List inputs = new ArrayList(); inputs.add(message1); inputs.add(message2); //執行規則 List<Message> results = ruleSession.executeRules(inputs); for(int i=0;i<results.size();i++){ Message msg = results.get(i); System.out.println(msg.message); } //釋放會話資源  ruleSession.release(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub  initProvider(); adminSample(); runtimeSampe(); } public static class Message { public static final int HELLO = 0; public static final int GOODBYE = 1; private String message; private int status; public String getMessage() { return this.message; } public void setMessage(String message) { this.message = message; } public int getStatus() { return this.status; } public void setStatus(int status) { this.status = status; } } }
複製代碼

 

規則文件使用的就是在這裏使用的例子。 this

6 實現JSR94的產品

主要的一些實現了JSR94的規則引擎產品以下: url

 
產品 商業/開源 規則描述語言 算法 規則開發工具 規則保存 部署方式
Drools 開源 DRL,xDRL,DSL,Decision Table ReteOO Eclipse,excel 文件系統 jar
Mandarax 開源 RuleML        
OpenRules 開源 Decision Table Rete excel   war
JLisa 開源          
Blaze 商業 SRL        
WebSphere ILOG JRules 商業          
JESS 商業          

7 小結

JSR94爲規則引擎提供了公用標準API,爲實現規則管理API和運行時API提供了指導規範, 目前已經得到不少開源/商業規則引擎產品的支持。 可是JSR94沒有對規則的描述語言進行規範,致使各規則引擎產品大多采用本身私有的描述語言。

Author: Holbrook Wong <wanghaikuo@tsinghua.org.cn>

Date: 2012-12-07 12:49:16 CST

相關文章
相關標籤/搜索