2007.12.20更新修正部分文字
分析階段的Use Case(User Story)除了Business Flow描述外,另一個(gè)重要的部分就是業(yè)務(wù)規(guī)則。組織和實(shí)現(xiàn)這些業(yè)務(wù)規(guī)則有不同的手段(將帶來(lái)不同效益)。除傳統(tǒng)的開(kāi)發(fā)人員的代碼實(shí)現(xiàn)外,越來(lái)越多系統(tǒng)開(kāi)始使用規(guī)則系統(tǒng)來(lái)組織。 
 
對(duì)于應(yīng)用規(guī)則引擎網(wǎng)絡(luò)上著名的有兩篇文章:《Java規(guī)則引擎與其API應(yīng)用詳解》和《企業(yè)平臺(tái)中的業(yè)務(wù)規(guī)則引擎
此外,JBoss也基于Drools推出了JBPM。Oracle也推出了自己的規(guī)則產(chǎn)品。 
  
不過(guò)在應(yīng)用規(guī)則引擎前,需要了解四個(gè)問(wèn)題: 
第一個(gè)問(wèn)題是:什么算是業(yè)務(wù)規(guī)則?
我以為《Business Rules Applied》一書(shū)對(duì)Business Rule的分類比較合理: 
 

Constraint

A constraint can be a mandatory restriction or suggested restriction on the behavior of the business event. A mandatory constraint is a complete statement that expresses an unconditional circumstance that must be true or not true for the business event to complete with integrity.
Examples of mandatory constraints are:
A customer must not have more than 10 open orders at one time.
The total dollar amount of a customer order must not be greater than the customer’s single order credit limit amount.

Guideline

A guideline is a complete statement that expresses a warning about a circumstance that should be true or not true. A guideline does not force the circumstance to be true or not true, but merely warns about it, allowing the human to make the decision. Because a guideline only warns and does not reject, it provides a freedom of choice. An example of a guideline is:
A customer should not have more than 10 open orders at one time.

Action enabler

A complete statement that tests conditions and upon finding them true, initiates another business event, message, or other activity. That is, an action enabler initiates a new action external to the scope of the system or increment under study.
Examples of action enablers are:
If a customer order is valid, then initiate the Place Order process.
If a customer is high risk, then notify the customer services manager.
Action-enabler rules can be used in some commercial rules products to create an event-oriented sequence of workflow steps. It may be helpful to think of mandatory constraints and action enablers as opposites. Mandatory constraints stop an event from completing. Action enablers start an event.

Computation

A complete statement that provides an algorithm for arriving at the value of a term where such algorithms may include sum, difference, product, quotient, count, maximum, minimum, average.
An example of a computation rule is:
    The total-amount-due for an order is computed as the sum of the lineitem amount(s) for the order plus tax.

Inference

A complete statement that tests conditions and upon finding them true, establishes the truth of a new fact. Examples of inferences are:
If a customer has no outstanding invoices, then the customer is of preferred status.
    If a customer is of preferred status, then the customer’s order qualifies for a 20 percent discount.

 
需要強(qiáng)調(diào)一下的是(以下想法十分的不成熟):
規(guī)則中不應(yīng)該處理業(yè)務(wù)流程(任何具有實(shí)際意義的業(yè)務(wù)操作),只有規(guī)則本身。包括:
A. 不在規(guī)則中更新Domain Object的屬性;
B. 不訪問(wèn)Dao對(duì)象的創(chuàng)建,更新和刪除的持久化工作;
C. 不訪問(wèn)Domain Service中任何涉及Domain Object更新、網(wǎng)絡(luò)訪問(wèn)、文件操作和郵件操作等方法;
D. 只產(chǎn)生提供控制信息或者狀態(tài)信息等runtime數(shù)據(jù),由業(yè)務(wù)流程來(lái)完成A,B和C限制的行為。
 
第二個(gè)問(wèn)題是:要確定的是否所有的控制邏輯都要用可定制的業(yè)務(wù)規(guī)則來(lái)解決?
答案很明顯,并非所有的地方都需要。
很多控制邏輯細(xì)節(jié)都可以采用參數(shù)化配置來(lái)完成,采用參數(shù)配置對(duì)于提高性能也有好處。當(dāng)然參數(shù)化配置意味著,對(duì)應(yīng)的控制邏輯的業(yè)務(wù)邊界是確定的。一旦面對(duì)高度客戶化的業(yè)務(wù)邏輯,就需要采用規(guī)則系統(tǒng)了。
 
第三個(gè)問(wèn)題是:如果要,那么我們需要一個(gè)什么類型的規(guī)則系統(tǒng)?
規(guī)則系統(tǒng)的分類(以下是我的分類):
根據(jù)功能,規(guī)則系統(tǒng)可以分兩類:Formula系統(tǒng)和正統(tǒng)的rule系統(tǒng)。我把Formula系統(tǒng)看作是一個(gè)弱化的rule系統(tǒng),只做公式計(jì)算,Excel用的最多的功能也是這個(gè)。
根據(jù)語(yǔ)言,規(guī)則系統(tǒng)可以分兩類:編程語(yǔ)言系統(tǒng)和類DSL系統(tǒng)。類DSL更接近自然語(yǔ)言,更接近問(wèn)題描述語(yǔ)言。
根據(jù)引擎,又可以分為腳本系統(tǒng)和推理系統(tǒng)。如JRule就同時(shí)支持兩種技術(shù):checklist和forward chain,當(dāng)用checklist時(shí)就是一個(gè)腳本系統(tǒng)。BTW:Ajoo同學(xué)的JASKELL是一個(gè)不錯(cuò)的腳本系統(tǒng)。
使用什么的規(guī)則系統(tǒng),取決于系統(tǒng)的需要。
更多的時(shí)候系統(tǒng)不需要做推理(因?yàn)樵O(shè)計(jì)的時(shí)候人都做了),一個(gè)腳本系統(tǒng)就可以滿足需要,而一個(gè)弱化后的Formula系統(tǒng)更簡(jiǎn)單實(shí)用。
 
下面是我在javaeye上開(kāi)的一個(gè)和這個(gè)有關(guān)的話題:
規(guī)則系統(tǒng)or腳本系統(tǒng),自然語(yǔ)言,DSL or Java語(yǔ)言
 
第四個(gè)問(wèn)題是:一旦確定了使用規(guī)則系統(tǒng),該系統(tǒng)的受眾是誰(shuí),即誰(shuí)使用該系統(tǒng)?
受眾是開(kāi)發(fā)人員還是客戶將是一個(gè)問(wèn)題。
如果是開(kāi)發(fā)人員,所要做的工作就是把設(shè)計(jì)好的對(duì)象映射到規(guī)則系統(tǒng)中,不需要額外的工作。而如果是客戶,或許要多做些工作了。
問(wèn)題的所在是:通常系統(tǒng)設(shè)計(jì)采用OO原則,而OO追求的是細(xì)粒度的設(shè)計(jì),這點(diǎn)對(duì)于開(kāi)發(fā)人員來(lái)說(shuō)不是什么問(wèn)題。但對(duì)于客戶來(lái)說(shuō)則不一樣,客戶看到的或者說(shuō)說(shuō)理解的對(duì)象模型(如果他能理解的話)是一個(gè)粗粒度的概念。于是我們不得不額外做些工作——封裝不同對(duì)象(關(guān)聯(lián)的)到一個(gè)對(duì)象中。
 
一旦使用了類DSL的規(guī)則系統(tǒng),我們關(guān)心的就不是程序,而是一個(gè)問(wèn)題描述。雖然類DSL規(guī)則系統(tǒng)最終將生成程序代碼(利用代碼生成技術(shù)),但是很明顯,我們所關(guān)心的更接近于Use Case(User Story)。無(wú)論是開(kāi)發(fā)人員還是客戶都將更容易理解系統(tǒng)的行為。
 
 同樣,因?yàn)椴捎玫氖谴a生產(chǎn)技術(shù)——產(chǎn)生式編程(又拔了一個(gè)高度,hoho),綜合效益顯然(這個(gè)……廣告語(yǔ))。更多請(qǐng)參考《產(chǎn)生式編程》一書(shū)