天天看點

Drools 規則引擎-學習筆記(一)

Knowledge representation :人工智能領域的有關知識是如何表達與操作的。

Expert System : 專家系統也被稱作 Knowledge-based System, 然而早期的專家系統是把邏輯寫死的。

Drools是一個Rule Engine, 它使用rule-based的方法實作專家系統, 可以更正确的歸類為Production Rule System-産生式規則系統.

Production Rule System 是一種Rule Engine, 也是一種Expert System.

一個Production Rule System是Turing complete(圖靈完全)的。The brain of a Production Rule System 是一個推理引擎(Inference Engine)。 推理引擎是用來比對facts and data與Production Rules的, 這個過程可以叫做模式比對(Pattern Matching). 用于推理引擎的模式比對算法包括: Linear, Rete, Treat, Leaps.

Drools 實作并繼承了Rete算法,Leaps過去也實作過但現在已經retired了,由于其變的unmaintained了。Drools的Rete實作稱作ReteOO, 表示: the Rete algorithm for object oriented systems.

Rules 存儲在Production Memory中,而對應的facts存儲在Working Memory中。由于系統中可能包含很多規則與事實, 可能比對的時候出現多個規則比對成功,這樣會出現執行沖突(Be in conflict). 這裡就出現了Agenda 了, 這個用來保證沖突規則的執行順序的, 當然要通過一些沖突解決政策(Conflict Resolution strategy)。

Drools 規則引擎-學習筆記(一)

對于一個Rule System來說, 有兩種執行方法:Forward Chaining 和 Backward Chaining。同時運用兩種方法的可以稱作混合規則系統(Hybrid Rule Systems).

Forward chaining 是“data-driven”的,簡單地講,就是以fact開始的,事實傳播然後以一個conclusion結束。Drools就是一個forward chaining engine.

Drools 規則引擎-學習筆記(一)

Backward Chaining是一個“goal-driven”的,簡單地講,就是一個以conclusion開始的, 然後引擎去嘗試去satisfy這個結論。如果不能滿足的話, 就又去查詢其他能被satisfy的conclusions.Drools計劃在未來的版本中提供對Backward Chaining的支援。

Drools 規則引擎-學習筆記(一)

規則引擎的Advantages:

Declarative Programming: tell the Rule engines What to do , not  How todo.

Logic and Data Separation(邏輯與資料分離):data is in your domain objects, and the logic is in the rules. 這個從根本上break了資料與邏輯的面向對象耦合。

Drools的規則引擎使用步驟:

用KnowledgeBuilder的執行個體去編譯規則.drl檔案;

然後獲得一個KnowledgeBase 執行個體,把上步驟中builder執行個體擷取的KnowledgePackage添加到 KnowledgeBase執行個體中;

建立一個Knowledge session,接着insert facts,  再fireAllRules.

繼續閱讀