如何建立企業級應用的商業規則引擎
Upcoming SlideShare
Loading in...5
×
 

如何建立企業級應用的商業規則引擎

on

  • 1,292 views

 

Statistics

Views

Total Views
1,292
Views on SlideShare
847
Embed Views
445

Actions

Likes
2
Downloads
41
Comments
0

3 Embeds 445

http://www.codedata.com.tw 442
http://codedata.com.tw 2
http://www.mi2010.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    如何建立企業級應用的商業規則引擎 如何建立企業級應用的商業規則引擎 Presentation Transcript

    • 如何建立企業級應用的 商業規則引擎 李日貴 Jini Gary Lee 王文農 Steven Wang
    • 為何要使用規則引擎
    • Java Multi-tiers • Struts • JSF • SpringMVC • Tapestry • Wicket MVC Front Tier DAO Persistence Tier Buisiness Logic Middle Tier • Hibernate • MyBatis • JDO • JPA
    • RuleEngine 的好處 • 宣告式的開發 • 區隔商業邏輯與資料處理的程式 • 效能與擴充性 • 集中控管商業邏輯 • 簡易的工具 • 可閱讀的規則
    • Rule ? When (Conditions) THEN (Actions)
    • Rule Engine Working Memory Production Memory Match Rules Resolve Conflicts (Agenda) Fire RulesFact Rules
    • Forward Chaining Rule Base Working Memory Determine possible rules to fire Select Rules to Fire Conflict Resolution Strtegy Exit Fire Rule Conflict Set Rule Found No Rule Found Exit if specified by rule
    • Rete • Dr. Charles L. Forgy , 1974 • Data struture – List of elements – Tree-Strutured Sorting Network • Algorithm – Rule Compilation – Runtime Execution
    • http://en.wikipedia.org/wiki/File:Rete.svg
    • JSR-94 • Java Rule Engine API • JSR94 provides – Rule Administration – Rule Runtime APIs – Rule Language (RuleML)
    • JSR94 APIs • Register and unregister rules • Parse Rules • Inspect rule metadata • Execute Rules • Retrieve Results • Filter Results
    • Rule Engines • Commercial – Oracle Business Rules – IBM iLOG – Pega Rules – FICO Blaze Advisor – JESS ( JSR94 RI ) • Opensources – JBOSS Drools – OpenRules
    • JESS
    • JESS • A Rule Engine • A scripting environment in Java JESS  Java • Rete algorithm • LISP-like syntax • Can be licensed for commercial use, no cost for academic use
    • JESS language • *.clp ( in JessDE )
    • Basic Syntax • (a b c) – List of tokens • (1 2 3) – List of Integers • (+ 1 2) – Expression • (“Hello world”) – String • (foo ?x ?y) – Function
    • Define Functions (deffunction max(?a ?b) (if(> ?a ?b) then (return ?a) else (return ?b))) (printout t (max 3 5))
    • Define Advices (defadvice before + (bind $?argv ( create$ $?argv 1 ))) (printout t (+ 2 3) crlf) (undefadvice +) (printout t (+2 3) crlf)
    • Define Template (deftemplate automobile "A specific car." (slot make) (slot model) (slot year (type INTEGER)) (slot color (default white))) (assert (automobile (model CAMERY) (make TOYOTA) (year 2008))) (assert (automobile (model 520i) (make BMW) (year 2013))) (facts)
    • Define class (import domain.Account) (deftemplate Account (declare (from-class Account))) (bind ?a (new Account)) (add ?a) (facts) (modify 0 (id 1) (name jini)) (facts) (bind ?a (new Account)) (add ?a) (?a setId 2) (?a setName steven) (printout t (?a getId) "-" (?a getName) "." crlf) (facts) (update ?a) (facts)
    • Define rule (deftemplate person (slot name) (slot age)) (defrule vote "Can you vote" (person {age < 18}) => (printout t "No, you can't vote" crlf) ) (assert (person (age 17))) (run)
    • Use Java (bind ?map (new java.util.HashMap)) (call ?map put "A" "Apple") (call ?map put "B" "Banana") (call ?map put "C" "Cherry") (printout t (call ?map get "B"))
    • JessML <?xml version=‘1.0’ encoding=‘UTF-8’?> <rulebase xmlns=‘http://www.jessrules.com/JessML/1.0’> <template> <name>person</name><slot>name</slot> <value type=‘SYMBOL’>nil</value> </template> <facts> <name>course</name> <fact> <name>id</name><value type=‘INTEGER’>1</value> </fact> </facts> <rule> <lhs>..</lhs> <rhs>..</rhs> </rule> </rulebase>
    • JSR 94 public class JessRule { private static final String RULE_SERVICE_PROVIDER =“org.jcp.jsr94.jess”; public void run(){ Class.forName(“org.jcp.jsr94.jess.RuleServiceProviderImpl”); RuleServiceProvider serviceProvider = RuleServiceProviderManager .getRuleRuleServiceProvider(RULE_SERVICE_PROVIDER ); } }
    • Drools
    • Setup RuleIDE
    • Start Drools Project
    • simple Rule package javatwo.license rule “Check age < 18” when $a : Applicant(age < 18) then $a.setValid(false); end
    • Execute DRL KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("javatwo/ license/licenseApplication.drl"), ResourceType.DRL ); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackage s()); StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); Applicant applicant = new Applicant(1, "Kevin", 15); ksession.execute(applicant);
    • Nature Language kbuilder.add(ResourceFactory.newClassPathResource("nature.dsl"), ResourceType.DSL ); kbuilder.add(ResourceFactory.newClassPathResource("nature.dslr"), ResourceType.DSLR ); nature.dsl [condition][]Person is {name}=$person : Person(name=='{name}') [consequence][]SayHello=System.out.println("Hello "+$person.getName()); nature.dslr expander nature.dsl rule "Nature language" when Person is gary then SayHello end
    • Chinese ?! cnature.dsl [condition][]姓名是 {name}=$person : Person(name=='{name}') [consequence][]打招呼=System.out.println("Hello "+$person.getName()); cnature.dslr expander cnature.dsl rule "Nature language" when 姓名是 gary then 打招呼 end
    • JSR 94 public class DroolsRule { private static final String RULE_SERVICE_PROVIDER =“http://drools.org/”; public void run(){ Class.forName(“org.drools.jsr94.rules.RuleServiceProviderImpl ”); RuleServiceProvider serviceProvider = RuleServiceProviderManager .getRuleRuleServiceProvider(RULE_SERVICE_PROVIDER ); } }
    • Script
    • JSR-223 support • JSR-223 – javax.script • The scripting API consists of interfaces and classes that define Java Scripting Engines and provides a framework for their use in Java applications. – since Java 1.6 • Before JSR-223 – Use BeanShell or etc.. – API not standard
    • JSR-223 support
    • Scripting API component relationships
    • Eval Script // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create a Javascript engine ScriptEngine engine = factory.getEngineByName(“JavaScript”); // evaluate Javascript code from String engine.eval(“print(‘Hello world’)”);
    • Eval File // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create a Javascript engine ScriptEngine engine = factory.getEngineByName(“JavaScript”); // evaluate Javascript code from given file – specified by first argument engine.eval( new java.io.FileReader(args[0]));
    • Eval Variable ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName(“JavaScript”); File f = new File(“test.txt”); // expose File object as variable to script engine.put(“file”,f); // evaluate a script string. The script accesses “file” variable and calls method on it engine.eval(“print(file.getAbsolutePath())”);
    • Eval Script Function ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName(“JavaScript”); // Javascript code in a String String script = “function hello(name) { print(‘Hello’+name); }”; engine.eval(script); // javax.script.Invocable is an optional interface. Check whether your script engine implements or not! Note that the Javascript engine implements Invocable interface. Invocable inv = (Invocable) engine; inv.invokeFunction(“hello”, “Scripting!!”);
    • Drools
    • JESS
    • But their language/spec…
    • We want..
    • We are..
    • Case 1保費計算 • 不同險種會有不同的保費計算方式,公式 和因子也可能會不相同 • 複雜計算方式需要特殊的方式,最好能夠 在不須知道太多的情況下,Plug-In進來
    • 條件 公式 因子 因 子 公 式 畫 面 D B 其 他 公式:A+B/C-D 因子 A:畫面 B:Database C:另一個公式X+Y D:其他系統 X:常數 Y:Database PlugIn
    • 保費公式設定
    • 模擬保費公式設定
    • Case 2核保檢核 • 報價轉保單時,有許多企業邏輯需要被檢 核 • 這些邏輯,我們不希望hardcode在程式內
    • 條件 規則 結果 規則 結果 規則 結果 . . . 1.單一結果或多組結果 2.根據結果進行後續處理 - DataBase操作 -Java操作 -畫面相關操作 PlugIn Java Script SQL . . .
    • 核保檢核設定
    • 模擬核保檢核設定
    • 測試案例:保單內容
    • Rule & Engine
    • Find Match Rules
    • 計算保費處理
    • 核保檢核處理
    • Execute
    • 缺點 1. 不是使用宣告的方法,而是定義處理過程 2. 複雜的業務邏輯,可能會需要巢狀條件結 構,這會使得Rule難以閱讀且容易出錯 3. 可能需要定義代碼處理決定表,或建立更 好的Rule Engine 4. Multiple Script Language將會難以維護
    • 優點 1. 對開發者來說較直接 2. 物件可由Java或Script內產生並共用 3. Rule可以更加的動態