<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Drools_Overview</id>
	<title>Drools Overview - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Drools_Overview"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Drools_Overview&amp;action=history"/>
	<updated>2026-04-21T10:30:01Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=Drools_Overview&amp;diff=43037&amp;oldid=prev</id>
		<title>Bernard Szlachta at 06:05, 25 September 2016</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Drools_Overview&amp;diff=43037&amp;oldid=prev"/>
		<updated>2016-09-25T06:05:36Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Cat|Drools|001}}&lt;br /&gt;
{{Cat|OCEB}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;slideshow style=&amp;quot;nobleprog&amp;quot; headingmark=&amp;quot;⌘&amp;quot; incmark=&amp;quot;…&amp;quot; scaled=&amp;quot;true&amp;quot; font=&amp;quot;Trebuchet MS&amp;quot; headingmark2=&amp;quot;。&amp;quot; lang=&amp;quot;中文版&amp;quot;&amp;gt;&lt;br /&gt;
;title: Drools Overview&lt;br /&gt;
;author: 安博 Bernard Szlachta (NobleProg Ltd)&lt;br /&gt;
&amp;lt;/slideshow&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drools Overview Training Materials ==&lt;br /&gt;
{{Can I use your material}}&lt;br /&gt;
&lt;br /&gt;
== About Me。⌘ ==&lt;br /&gt;
* 安博 （Bernard Szlachta)&lt;br /&gt;
* Working with Drools/jBPM since 2007 (some projects for CERN, Philips, etc...)&lt;br /&gt;
* Obsessed with getting logic neat&lt;br /&gt;
* Wrote a couple of Rule Engines myself&lt;br /&gt;
&lt;br /&gt;
== Approach to Business Analysis and Design。⌘ ==&lt;br /&gt;
* https://en.wikipedia.org/wiki/AI_winter&lt;br /&gt;
* Data First (RDMS)关系数据库管理系统 - 1970s&lt;br /&gt;
* Object Orientation (OOP) 面向对象程序设计 - 1980s&lt;br /&gt;
* Processes First (BPMS) 业务流程管理系统 - 1990s&lt;br /&gt;
* Rules First (BRMS)业务规则管理系统 - first wave LISP/PROLOG - 1980 - practical use - 2000s&lt;br /&gt;
&lt;br /&gt;
== Data Approaches 。⌘ ==&lt;br /&gt;
* First approaches to analysis focused on data first&amp;lt;br /&amp;gt;最初，业务分析的方法关注的是“数据第一”。&lt;br /&gt;
** This lead to people storing a lot of data but really did not use it in efficient manner&amp;lt;br/&amp;gt;人们保存了许多数据，但是这些数据并没有很有效率地被利用。&lt;br /&gt;
** major problem at the time was ensuring that data is in the system rather than on paper (e.g. CIA Oracle project)&amp;lt;br /&amp;gt;那时候重要的问题是保证数据保存在系统里，而不是在纸面上（比如CIA Oracle project）&lt;br /&gt;
=== OOP 。⌘ ===&lt;br /&gt;
* business logic was mixed up with data &amp;lt;br /&amp;gt;后来，业务逻辑开始和数据混合在一起（比如object constructors, etc...）&lt;br /&gt;
&lt;br /&gt;
=== BPMs 。⌘ ===&lt;br /&gt;
* Process management tools allowed analyst to create executable process models&amp;lt;br /&amp;gt;过程管理工具允许分析师创建可执行的过程模型&lt;br /&gt;
** first true standard came along in 2004 BPMN 1.0 (execution through translation to BPEL)&amp;lt;br /&amp;gt;第一个真正的标准是2004年的BPMN 1.0（转译成BPEL后执行）&lt;br /&gt;
** 2008 - BPMN2 - direct execution of BPMN file&lt;br /&gt;
&lt;br /&gt;
=== BRMS 。⌘ ===&lt;br /&gt;
* Though Expert system existed from 70s, they started to be used in typical commercial setup in 2000s.&amp;lt;br /&amp;gt;虽然专家系统从70年代就已经存在，但是它们直到2000年以后，才真正应用到商业领域。&lt;br /&gt;
* Having rules and processes, a person can derive everything else (e.g. underlying data model, etc...)&amp;lt;br/&amp;gt;使用规则和过程，人们可以提取出所有其它的东西（比如底层的数据模型等等），&lt;br /&gt;
** So starting with analysing rules and processes is the easiest way&amp;lt;br /&amp;gt;所以从分析规则和过程来起步，才是最容易的方法。&lt;br /&gt;
* If analysis, for example, starts with UI or datamodel, there is no way to know what the logic should be&amp;lt;br /&amp;gt;如果分析师从用户界面或者数据模型起步，没有办法知道系统逻辑是什么&lt;br /&gt;
&lt;br /&gt;
=== Shift towards declarative languages standards | 声明式语言标准的演化。⌘===&lt;br /&gt;
[[File:ClipCapIt-150708-132606.PNG]]&lt;br /&gt;
&lt;br /&gt;
=== Shift declarative languages standards。⌘ ===&lt;br /&gt;
# Popular Standard languages are declarative &lt;br /&gt;
# SQL, HTML, BPMN populartiy reduced code needs to be written and allow non-technical people to contribute more into application development&lt;br /&gt;
# Rules do not have any real standards at the moment&lt;br /&gt;
## OMG SBVR - not used&lt;br /&gt;
## [http://www.omg.org/spec/PRR/1.0/PDF/ OMG Production Rule Representation (PRR)]&lt;br /&gt;
## MVEL - used by few engines, but may face legal issues&lt;br /&gt;
# Also no leading declarative integration standard &lt;br /&gt;
## Declarative Open Source solution exists (Apache CAMEL, ServiceMix, etc...)&lt;br /&gt;
&lt;br /&gt;
== Artificial Intelligence ⌘。==&lt;br /&gt;
Write down on your own answers to the question below:&lt;br /&gt;
* What is artificial intelligence? 人工智能是什么?  &lt;br /&gt;
* What is a difference between AI method and traditional programming? 人工智能和传统的程序有什么区别？&lt;br /&gt;
* What are the benefits of using artificial intelligence? 人工智能有什么好处？&lt;br /&gt;
* Why AI isn&amp;#039;t used everywhere? 为什么现在没有太多人用人工智能？&lt;br /&gt;
&lt;br /&gt;
== Artificial Intelligence ⌘。==&lt;br /&gt;
人工智能主要分为两个部分&lt;br /&gt;
* Rule Engines - More Deterministic - 规则引擎 - 确定性方法&lt;br /&gt;
** 一样的输入一样的输出&lt;br /&gt;
* Machine Learning - More Probabilistic - 机器学习，统计学习-概率方法&lt;br /&gt;
** 一样的输入输出可能不同&lt;br /&gt;
&lt;br /&gt;
== Problems ML vs RE ⌘。==&lt;br /&gt;
# What should be a pension for a specific person? 每个人的退休金应该是多少？&lt;br /&gt;
# Credit Card transaction is fraudulent? 信用卡交易是不是欺诈性的？&lt;br /&gt;
# What is the price of the insurance premium?保险的价格是多少？&lt;br /&gt;
# Machine Translation? 机器翻译&lt;br /&gt;
# What should be current price of a stock?  股票的实时价格是多少？&lt;br /&gt;
Write down your problems and think of the best solution?&lt;br /&gt;
&lt;br /&gt;
== Artificial Intelligence And Rules Engine ⌘。==&lt;br /&gt;
[[File:Drools-ai.jpg]]&lt;br /&gt;
* Neural Networks, Genetic Algorithms, Decision Trees, Frame Systems and Expert Systems&lt;br /&gt;
* Expert Systems are also known as Knowledge-based Systems and Knowledge-based Expert Systems &lt;br /&gt;
* Drools is a Rule Engine that uses the rule-based approach to implement an Expert System and is more correctly classified as a Production Rule System.&lt;br /&gt;
* 不仅仅是规则引擎，他还是Production Rule　System, or Business Rules Management System （BRMS）&lt;br /&gt;
&lt;br /&gt;
== Rule Engines ⌘==&lt;br /&gt;
* What is a rule?&lt;br /&gt;
* What is a Rule Engine?&lt;br /&gt;
* Why use a Rule Engine?&lt;br /&gt;
* Advantages of a Rule Engine&lt;br /&gt;
* When should you use a Rule Engine?&lt;br /&gt;
* When not to use a Rule Engine&lt;br /&gt;
* Scripting or Process Engines&lt;br /&gt;
* Strong and Loose Coupling&lt;br /&gt;
&lt;br /&gt;
== 规则引擎 。==&lt;br /&gt;
* 什么是规则？&lt;br /&gt;
* 什么是规则引擎?&lt;br /&gt;
* 为什么使用规则引擎?&lt;br /&gt;
* 规则引擎的优势&lt;br /&gt;
* 何时使用规则引擎?&lt;br /&gt;
* 何时不用规则引擎&lt;br /&gt;
* 脚本（Scripting）或过程引擎（Process Engines）&lt;br /&gt;
* 强耦合和弱耦合&lt;br /&gt;
&lt;br /&gt;
====业务规则是什么？ ⌘。====&lt;br /&gt;
 “规则基于facts，而facts基于terms.”&lt;br /&gt;
&amp;lt;table border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;10&amp;quot; cellspacing=&amp;quot;0&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;tr&amp;gt;&lt;br /&gt;
			&amp;lt;td&amp;gt;[[File:ClipCapIt-140817-132803.PNG]]&amp;lt;/td&amp;gt;&lt;br /&gt;
			&amp;lt;td&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Terms&amp;#039;&amp;#039;&amp;#039;&amp;lt;br/&amp;gt;&lt;br /&gt;
*员工&lt;br /&gt;
*员工号&lt;br /&gt;
*车&lt;br /&gt;
*车牌号&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039;&amp;lt;br/&amp;gt;&lt;br /&gt;
*车有车牌&lt;br /&gt;
*员工开车&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;规则&amp;#039;&amp;#039;&amp;#039;&amp;lt;br/&amp;gt;&lt;br /&gt;
* 每个employee必须可被唯一识别 (unique ID)&lt;br /&gt;
* 每辆车必须只有一个车牌号&amp;lt;/td&amp;gt;&lt;br /&gt;
		&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Exercise ⌘。====&lt;br /&gt;
# Choose concrete business problem (if you cannot thing of any, use Pizza Shop)&lt;br /&gt;
# Write down few terms&lt;br /&gt;
# Write down a couple of facts related to these terms&lt;br /&gt;
# Write a couple of rules based on the facts&lt;br /&gt;
&lt;br /&gt;
== Rule vs Fact ⌘。==&lt;br /&gt;
; Method 1&lt;br /&gt;
 Term: Boss, Employee&lt;br /&gt;
 Fact: Employee has exactly one boss&lt;br /&gt;
 Rule: &lt;br /&gt;
       Employee reports to their boss&lt;br /&gt;
;Method 2&lt;br /&gt;
 Term: Boss, Employee&lt;br /&gt;
 Fact: Employee has Boss&lt;br /&gt;
 Rule:&lt;br /&gt;
       Employee has exactly one boss&lt;br /&gt;
       Employee reports to their boss&lt;br /&gt;
== Fact or Rule? ⌘。==&lt;br /&gt;
* VAT is 6% &lt;br /&gt;
:增值税税率为6%&lt;br /&gt;
* Square area formula is X^2&lt;br /&gt;
:平方面积公式为x 2&lt;br /&gt;
* Buy one get one free&lt;br /&gt;
:买一送一&lt;br /&gt;
* Employee must not work longer than 8 hours a day&lt;br /&gt;
:员工工作时间每天不能超过8小时&lt;br /&gt;
&lt;br /&gt;
== Rule Engine ⌘==&lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;Rules&amp;#039;&amp;#039;&amp;#039; are stored in the Production Memory&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039; that the &amp;#039;&amp;#039;&amp;#039;Inference Engine&amp;#039;&amp;#039;&amp;#039; matches against are kept in the &amp;#039;&amp;#039;&amp;#039;Working Memory&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* Facts are asserted into the Working Memory where they may then be &amp;#039;&amp;#039;&amp;#039;modified&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;retracted&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* A system with a large number of rules and facts may result in many rules being true for the same fact assertion; these rules are said &amp;#039;&amp;#039;&amp;#039;to be in conflict&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* The &amp;#039;&amp;#039;&amp;#039;Agenda&amp;#039;&amp;#039;&amp;#039; manages the execution order of these conflicting rules using a &amp;#039;&amp;#039;&amp;#039;Conflict Resolution&amp;#039;&amp;#039;&amp;#039; strategy.&lt;br /&gt;
&lt;br /&gt;
[[File:Drools-ruleengine.PNG|300px]]&lt;br /&gt;
&lt;br /&gt;
== 规则引擎 。==&lt;br /&gt;
* 这些 &amp;#039;&amp;#039;&amp;#039;规则&amp;#039;&amp;#039;&amp;#039; 保存在Production Memory里&lt;br /&gt;
* 推理机（&amp;#039;&amp;#039;&amp;#039;Inference Engine&amp;#039;&amp;#039;&amp;#039;）针对&amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039;进行匹配, &amp;#039;&amp;#039;&amp;#039;Facts&amp;#039;&amp;#039;&amp;#039;存在于“工作内存”（&amp;#039;&amp;#039;&amp;#039;Working Memory&amp;#039;&amp;#039;&amp;#039;）里. &lt;br /&gt;
* Facts 在工作内存中经过断言(asserted)后，可以被 &amp;#039;&amp;#039;&amp;#039;modified 修改&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;retracted 撤回&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* 如果一个事实和多个规则同时匹配&lt;br /&gt;
** 那么这时这些规则叫做&amp;#039;&amp;#039;&amp;#039;发生冲突（to be in conflict）&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
* The Agenda是一个列表， 通过使用Conflict Resolution策略，来管理发生冲突的规则的激活顺序&lt;br /&gt;
&lt;br /&gt;
[[File:Drools-ruleengine.PNG|300px]]&lt;br /&gt;
&lt;br /&gt;
== Rules in Conflict 发生冲突的规则。 ⌘==&lt;br /&gt;
;Facts&lt;br /&gt;
:Customer is a not-for-profit that also is a returning customer&lt;br /&gt;
:客户是非盈利组织，同时是一个回头客&lt;br /&gt;
;Rule 1&lt;br /&gt;
:Non-for-profit customer receive 20% discount&lt;br /&gt;
:非盈利组织客户打8折&lt;br /&gt;
;Rule 2&lt;br /&gt;
:Returning customer receive 10% discount&lt;br /&gt;
:回头客打9折&lt;br /&gt;
&lt;br /&gt;
== The simplest rule Engine ⌘。==&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 while HAS RULE TO FIRE&lt;br /&gt;
  for each FACT&lt;br /&gt;
      for each RULE&lt;br /&gt;
           if FACT matches RULE than FIRE RULE&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 当有规则可以激活的时候&lt;br /&gt;
  遍历事实&lt;br /&gt;
      遍历规则&lt;br /&gt;
           如果事实匹配规则，那么激活规则&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 执行方法 Methods of Execution ⌘。==&lt;br /&gt;
* 前向链接（Forward Chaining）&lt;br /&gt;
* 反向链接（Backward Chaining）&lt;br /&gt;
* 混合规则系统（Hybrid Rule Systems）&lt;br /&gt;
&lt;br /&gt;
== Forward vs Back Chaining ⌘。==&lt;br /&gt;
Goal: determine which things can be transported together (human is a thing) &amp;lt;br/&amp;gt;&lt;br /&gt;
目标：这写东西可不可以在一起运输&lt;br /&gt;
;Facts:&lt;br /&gt;
   WangHao (type human)&lt;br /&gt;
   Bernard (type human)&lt;br /&gt;
   Water (type element)&lt;br /&gt;
   Computer (type element)&lt;br /&gt;
   Uranium (type element, radioactive == true)&lt;br /&gt;
;Rule:&lt;br /&gt;
   human is a mammal 人类是哺乳动物&lt;br /&gt;
   mammal is a living organism   哺乳动物是有生命的生物体&lt;br /&gt;
   living organism shall not be transported with radioactive elements&lt;br /&gt;
   有生命的生物体不能和放射性物质一起运输&lt;br /&gt;
;Forward Chaining&lt;br /&gt;
 Execution (Inference):&lt;br /&gt;
  human is mammal&lt;br /&gt;
  mammal is living organism&lt;br /&gt;
  computer is not living organism&lt;br /&gt;
  water is not living organism&lt;br /&gt;
 Derived Fact: human is living organism&lt;br /&gt;
 Conclusion: Wanghao (human) cannot be transported with Uranium (element, radioactive == true)&lt;br /&gt;
 &lt;br /&gt;
;Backward Chaining&lt;br /&gt;
 Conclusion: Wanghao (human) cannot be transported with Uranium (element, radioactive == true)&lt;br /&gt;
 Explanation:&lt;br /&gt;
 goal    human is living organism&lt;br /&gt;
 subgoal       human is mammal&lt;br /&gt;
 subgoal           mammal is living organism&lt;br /&gt;
&lt;br /&gt;
== Forward Chaining ⌘==&lt;br /&gt;
* &amp;quot;data-driven&amp;quot; and thus reactionary,  &lt;br /&gt;
* Facts are asserted into working memory  &lt;br /&gt;
* One or more rules being concurrently true and scheduled for execution by the Agenda.  starts with a fact, it propagates and end in a conclusion.&lt;br /&gt;
&lt;br /&gt;
== 前向链接 。==&lt;br /&gt;
* &amp;quot;数据驱动&amp;quot;从而reactionary,  &lt;br /&gt;
* Facts经过断言进入工作内存&lt;br /&gt;
* 一或多个规则同时为真并由the Agenda计划执行。从一个fact开始, 不断传播下去，直到得出结论&lt;br /&gt;
&lt;br /&gt;
== Backward Chaining ⌘==&lt;br /&gt;
* &amp;quot;Goal-driven&amp;quot;&lt;br /&gt;
* Starts with a conclusion which the engine tries to satisfy. &lt;br /&gt;
* If it can&amp;#039;t it then searches for conclusions that it can satisfy (subgoals), &lt;br /&gt;
* Subgoals will help satisfy some unknown part of the current goal&lt;br /&gt;
* It continues this process until either the initial conclusion is proven or there are no more subgoals. &lt;br /&gt;
* Prolog is an example of a Backward Chaining engine&lt;br /&gt;
&lt;br /&gt;
== 反向链接 。==&lt;br /&gt;
* &amp;quot;目标驱动&amp;quot;&lt;br /&gt;
* 从结论开始，结论即引擎要证实的结论&lt;br /&gt;
* 如果不能证实（得到结论），那么将搜索可以满足的结论（子目标）&lt;br /&gt;
* 子目标可以帮助满足当前目标的一些未知部分&lt;br /&gt;
* 引擎不断重复这个过程直到最初结论得到证实或者不再有子目标可以证实&lt;br /&gt;
* 比如Prolog就是一个反向链接引擎&lt;br /&gt;
&lt;br /&gt;
== Why use a Rule Engine ⌘==&lt;br /&gt;
* When should you use a rule engine?&lt;br /&gt;
* What advantage does a rule engine have over hand coded &amp;quot;if...then&amp;quot; approaches?&lt;br /&gt;
* Why should you use a rule engine instead of a scripting framework, like BeanShell?&lt;br /&gt;
&lt;br /&gt;
== 为何使用规则引擎 。==&lt;br /&gt;
* 何时应该使用规则引擎?&lt;br /&gt;
* 规则引擎比手写代码&amp;quot;if...then&amp;quot;的办法有什么优势?&lt;br /&gt;
* 为什么用规则引擎，而不用脚本（scripting）, 例如BeanShell?&lt;br /&gt;
&lt;br /&gt;
== Advantages of Rule Engine ⌘==&lt;br /&gt;
* Declarative Programming (&amp;quot;What&amp;quot;, not &amp;quot;How&amp;quot;)&lt;br /&gt;
* We can see &amp;quot;how&amp;quot; engine solved the problem  &lt;br /&gt;
* Logic and Data Separation (breaking OO coupling of data and logic)&lt;br /&gt;
* Speed and Scalability (Rete, Leaps, PHREAK)&lt;br /&gt;
* Centralization of Knowledge (Documentation)&lt;br /&gt;
* Tool Integration&lt;br /&gt;
* Explanation Facility&lt;br /&gt;
* Understandable Rules (DSL)&lt;br /&gt;
&lt;br /&gt;
== 规则引擎的优势 。==&lt;br /&gt;
* 声明式(declarative)编程 (用户想的是“什么”，而不是“怎么做”，&amp;quot;What&amp;quot;, not &amp;quot;How&amp;quot; as in 命令式(imperative) &lt;br /&gt;
** Can use on multiple platforms (Java, .NET, etc...)&lt;br /&gt;
** Upgrading Engine can speed up rules (e.g. Drools 5.0 -&amp;gt; 6.4) - you can optimise it yourself&lt;br /&gt;
** Engine chooses method of solving he problem (RETE, PHREAK, etc...)&lt;br /&gt;
* 可以看见引擎如何解决问题&lt;br /&gt;
* 逻辑数据分离 (打破了面向对象的逻辑数据耦合)&lt;br /&gt;
* 速度和扩展性 (Rete, Leaps, PHREAK)&lt;br /&gt;
* 知识集中化 (文档化)&lt;br /&gt;
* 集成工具&lt;br /&gt;
* Explanation Facility&lt;br /&gt;
* 规则易于理解 （DSL领域特定语言）&lt;br /&gt;
&lt;br /&gt;
== When should you use RE? ⌘==&lt;br /&gt;
* The problem may be complex  (no non-fragile way of building a solution for it)&lt;br /&gt;
* The problem is beyond any obvious algorithmic solution&lt;br /&gt;
* The logic changes often&lt;br /&gt;
* Domain experts (or business analysts) are readily available, but are nontechnical&lt;br /&gt;
* There are too many rules to manage them in the code&lt;br /&gt;
* We need rules to be explicit and documented&lt;br /&gt;
&lt;br /&gt;
== 何时该使用规则引擎? 。==&lt;br /&gt;
* 问题复杂 (一时找不到稳妥的解决方法)&lt;br /&gt;
* 问题超出一般明显的解决算法&lt;br /&gt;
* 逻辑经常变动&lt;br /&gt;
* 领域专家(或者业务分析员) 在身边, 但不懂技术&lt;br /&gt;
* 代码中有太多规则&lt;br /&gt;
* 需要规则显式说明并有文档记录&lt;br /&gt;
&lt;br /&gt;
== When shouldn&amp;#039;t you use RE? 何时不该用规则引擎? 。⌘==&lt;br /&gt;
* Maintaining the infrastructure/integration is more expensive than the gains&lt;br /&gt;
: 维护和集成框架成本过高 (安装，维护，安全补丁，升级，etc)&lt;br /&gt;
* With system which hardly ever change &lt;br /&gt;
: 系统几乎不会改动&lt;br /&gt;
* There is no person understanding RE&lt;br /&gt;
: 没有人懂规则引擎&lt;br /&gt;
Also see https://en.wikipedia.org/wiki/AI_winter&lt;br /&gt;
&lt;br /&gt;
== Alternatives to Rule Engine 其他选择 。⌘==&lt;br /&gt;
* Scripting   写脚本&lt;br /&gt;
* Decision Tables (Lookup tables)  决策表 (see OMG DMN)&lt;br /&gt;
* Decisions Tree 决策树&lt;br /&gt;
* Other data-driven solutions 其他数据驱动（data-driven)解决方案&lt;br /&gt;
&lt;br /&gt;
== Strong and Loose Coupling ⌘==&lt;br /&gt;
How to determine rules may not be most suitable?&lt;br /&gt;
;Strong coupling&lt;br /&gt;
* &amp;quot;firing&amp;quot; one rule will clearly result in another rule firing, and so on; &lt;br /&gt;
* there is a clear chain of logic&lt;br /&gt;
* Decision Tree or process should be used &lt;br /&gt;
;Loosely coupling&lt;br /&gt;
* rules to be changed, removed and added without requiring changes to other rules&lt;br /&gt;
&lt;br /&gt;
== 强耦合和弱耦合 。==&lt;br /&gt;
如何知道使用规则引擎是否是合适？&lt;br /&gt;
;强耦合&lt;br /&gt;
* 当一个规则激活（&amp;quot;firing&amp;quot;）将明显的导致另一个规则激活&lt;br /&gt;
* 有一个很清晰的逻辑链&lt;br /&gt;
* 可以用手写代码，&amp;#039;&amp;#039;&amp;#039;决策树&amp;#039;&amp;#039;&amp;#039;，过程&lt;br /&gt;
;弱耦合 &lt;br /&gt;
*规则允许被改动，删除和添加，而且这么做不牵扯改动其它规则&lt;br /&gt;
&lt;br /&gt;
== Rules Examples ⌘==&lt;br /&gt;
;Rules are the guidelines which instruct people &amp;#039;&amp;#039;&amp;#039;what&amp;#039;&amp;#039;&amp;#039; to do &amp;#039;&amp;#039;&amp;#039;when&amp;#039;&amp;#039;&amp;#039; certain conditions happen&lt;br /&gt;
* When the booking form arrives, then we send the confirmation letter&lt;br /&gt;
* When there are no bookings 14 days before the course commences, then we cancel the course&lt;br /&gt;
&lt;br /&gt;
== 规则举例 。==&lt;br /&gt;
规则就是一些&amp;#039;&amp;#039;&amp;#039;准则&amp;#039;&amp;#039;&amp;#039;，用来决定在&amp;#039;&amp;#039;&amp;#039;一定条件下做什么&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* 当收到预定表时，发送确认信&lt;br /&gt;
* 当课程开始前14天仍然没有任何预定，取消课程&lt;br /&gt;
&lt;br /&gt;
== Rules Syntax 规则语法 。⌘==&lt;br /&gt;
&amp;lt;source&amp;gt; &lt;br /&gt;
 When (if)            Then&lt;br /&gt;
 条件 (condition)     行动 (action)&lt;br /&gt;
 Left side            Right side&lt;br /&gt;
 LHS (Left Hand Side) RHS (Right Hand Side)&lt;br /&gt;
&lt;br /&gt;
 举例  Example        &lt;br /&gt;
 如果下雨              拿雨伞&lt;br /&gt;
 if it rains          take an umbrella&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Action 。⌘ ==&lt;br /&gt;
Action can be&lt;br /&gt;
* Pure calculation or proper function, no IO involved | 简单的函数,（没有IO，没有太重的消耗资源的应用）&lt;br /&gt;
* INSERT (ASSERT) new fact(s) - 插入新的Facts&lt;br /&gt;
* UPDATE (MODIFY) existing fact(s)  更新存在的Facts&lt;br /&gt;
* DELETE (REVERT) existing fact(s)  删除存在的Facts&lt;br /&gt;
;Action shall not use external system&lt;br /&gt;
;行动不应该用外部的系统&lt;br /&gt;
&lt;br /&gt;
== Bad and Good Actions 。⌘==&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 WHEN &lt;br /&gt;
   WRONG PASSWORD 3 TIMES&lt;br /&gt;
 THAN&lt;br /&gt;
   SEND EMAIL&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 WHEN &lt;br /&gt;
   WRONG PASSWORD 3 TIMES&lt;br /&gt;
 THAN&lt;br /&gt;
   INSERT ACTION_SEND_EMAIL_ACTION_FACT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sample PHP code ⌘。==&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 function _course_date_calculate_prices($course_date_node){&lt;br /&gt;
    for($i=1; $i &amp;lt;= PUBLIC_COURSE_MAX_USERS; $i++){&lt;br /&gt;
        $prices[$i][&amp;#039;suffix&amp;#039;] = &amp;quot; &amp;quot; . variable_get(&amp;#039;nobleprog_currency&amp;#039;,&amp;#039;GBP&amp;#039;) .&amp;#039; &amp;#039; . t(&amp;quot;per person&amp;quot;) . &amp;quot;. &amp;quot; ;&lt;br /&gt;
        $prices[$i][&amp;#039;value&amp;#039;]= course_date_course_price($course_date_node,$i);&lt;br /&gt;
    }&lt;br /&gt;
    course_date_prices_apply_discounts($prices, $course_date_node);&lt;br /&gt;
    return $prices;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 function course_date_course_price(&amp;amp;$node, $number_of_delagets = 1){&lt;br /&gt;
    $node_training = node_load($node-&amp;gt;field_public_training[0][&amp;#039;nid&amp;#039;]);&lt;br /&gt;
&lt;br /&gt;
    //If course doesn&amp;#039;t have price, get it form the outline&lt;br /&gt;
    if(is_numeric($node-&amp;gt;field_course_price[0][&amp;#039;value&amp;#039;])){&lt;br /&gt;
        $price = $node-&amp;gt;field_course_price[0][&amp;#039;value&amp;#039;];&lt;br /&gt;
    } else {&lt;br /&gt;
        $price = $node_training-&amp;gt;field_public[0][&amp;#039;value&amp;#039;];&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    if($number_of_delagets &amp;gt;= 2){&lt;br /&gt;
        $discount_per_next_delegate = variable_get(&amp;#039;discount_per_next_delegate&amp;#039;,DISCOUT_PER_NEXT_DELEGATE);&lt;br /&gt;
        $price = $price - ($price * $discount_per_next_delegate * ($number_of_delagets - 1) );&lt;br /&gt;
        $price = round($price,-1);&lt;br /&gt;
    }&lt;br /&gt;
    return $price;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
    //apply a discount once the first booking has been made&lt;br /&gt;
    //this stuff should work only&lt;br /&gt;
    if(&amp;#039;booking_form&amp;#039; == $node-&amp;gt;type and $op == &amp;#039;insert&amp;#039;) {&lt;br /&gt;
        $date_node_id = $node-&amp;gt;field_course_date_id[0][&amp;#039;nid&amp;#039;];&lt;br /&gt;
        if(is_numeric($date_node_id)){&lt;br /&gt;
            $course_date_node = node_load($date_node_id);&lt;br /&gt;
            if($course_date_node-&amp;gt;type == &amp;#039;course_date&amp;#039;){&lt;br /&gt;
                //If the manual discount has been applied don&amp;#039;t touch it&lt;br /&gt;
                //this may be done manualy of by this very function&lt;br /&gt;
                $now = date(&amp;#039;Y-m-d&amp;#039;);&lt;br /&gt;
                $course_start_date = $course_date_node-&amp;gt;field_course_date[0][&amp;#039;value&amp;#039;];&lt;br /&gt;
                $month_before_the_course = date(&amp;#039;Y-m-d&amp;#039;,strtotime( $course_start_date . &amp;quot;- 1 month&amp;quot;));&lt;br /&gt;
 &lt;br /&gt;
                if(! $course_date_node-&amp;gt;field_course_price[0][&amp;#039;value&amp;#039;] &amp;gt; 0 ){&lt;br /&gt;
 &lt;br /&gt;
                    $course_outline_node_id = $course_date_node-&amp;gt;field_public_training[0][&amp;#039;nid&amp;#039;];&lt;br /&gt;
                    $course_outline_node = node_load($course_outline_node_id);&lt;br /&gt;
                    $course_outline_price = $course_outline_node-&amp;gt;field_public[0][&amp;#039;value&amp;#039;];&lt;br /&gt;
                    $early_booking_discount = variable_get(&amp;#039;nobleprog_early_booking_discount&amp;#039;,EARLY_BOOKING_DISCOUNT);&lt;br /&gt;
                    $course_date_node-&amp;gt;field_course_price[0][&amp;#039;value&amp;#039;] = $course_outline_price * (1-($early_booking_discount+0.02));&lt;br /&gt;
                    node_save($course_date_node);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules written in English 。⌘==&lt;br /&gt;
* If you book the course one month before it commences, apply a 10% discount&lt;br /&gt;
:如果你在课程开始的1个月前预定课程，将享受&amp;#039;&amp;#039;&amp;#039;减10%&amp;#039;&amp;#039;&amp;#039;的优惠&lt;br /&gt;
* If the course is booked (be the same company), the next person booking will have a 12% discount (no matter what the time is when they book)&lt;br /&gt;
:如果课程已经预定了，那么下一个人预定将享受&amp;#039;&amp;#039;&amp;#039;减12%&amp;#039;&amp;#039;&amp;#039;的优惠&lt;br /&gt;
&lt;br /&gt;
== Fact Type vs Facts | 事实类型vs事实（Facts） 。⌘==&lt;br /&gt;
* Facts are the data needed to check the condition &lt;br /&gt;
: Facts是用来检验条件满足与否的数据&lt;br /&gt;
* Facts can be modified by the rule Engine &lt;br /&gt;
: 规则引擎可以更改事实&lt;br /&gt;
* Fact Type is the structure of the Fact&lt;br /&gt;
* Fact Type ~ Class/Field；&lt;br /&gt;
: Fact ~ Object/Value&lt;br /&gt;
; For the rules above:&lt;br /&gt;
* original price 原价 (e.g. fact: Course 1 price is 100 RMB)&lt;br /&gt;
* current date 当前日期&lt;br /&gt;
* course has been booked 课程已预定了&lt;br /&gt;
* commencement date 开始日期&lt;br /&gt;
* number of participants 报名数量&lt;br /&gt;
* price 价格&lt;br /&gt;
&lt;br /&gt;
== Rule Language  ⌘。==&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
 Rule &amp;quot;Is Early Booking&amp;quot;&lt;br /&gt;
 When&lt;br /&gt;
    $CommencmentDate   &amp;gt; $CurrentDate  + 1  month&lt;br /&gt;
    $course has been booked == false&lt;br /&gt;
 Then&lt;br /&gt;
   $Price = $Price - $Price *10%&lt;br /&gt;
 End&lt;br /&gt;
&lt;br /&gt;
 Rule &amp;quot;Is Booked &amp;quot;&lt;br /&gt;
  When&lt;br /&gt;
   $Course has been booked == true&lt;br /&gt;
  Then&lt;br /&gt;
   $Price =$Price - $Price*12%&lt;br /&gt;
 End&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Decision Table (Rules as Data) ⌘==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! EarlyBooking !! Booked !! Discount&lt;br /&gt;
|-&lt;br /&gt;
| TRUE || TRUE || 12%&lt;br /&gt;
|-&lt;br /&gt;
| TRUE || FALSE || 10%&lt;br /&gt;
|-&lt;br /&gt;
| FALSE || TRUE || 12%&lt;br /&gt;
|-&lt;br /&gt;
| FALSE || FALSE || 0%&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Facts as Objects - Java Beans ⌘。==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  public class BookingRequest {&lt;br /&gt;
      private Boolean booked = true;&lt;br /&gt;
      private Boolean earlyBooking = true;&lt;br /&gt;
      private Float discount = new Float(0);&lt;br /&gt;
  //getters and setters here&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Business Rule (mvel) 。⌘==&lt;br /&gt;
 rule &amp;quot;Already Booked&amp;quot;&lt;br /&gt;
     when&lt;br /&gt;
         booking : BookingRequest( booked == &amp;quot;true&amp;quot; , discountValue : discount )&lt;br /&gt;
     then&lt;br /&gt;
         System.out.println(&amp;quot;booked&amp;quot;);&lt;br /&gt;
         booking.setDiscount( discountValue + 0.12 );&lt;br /&gt;
         update( booking );&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 rule &amp;quot;Early Booking&amp;quot;&lt;br /&gt;
     when&lt;br /&gt;
         booking : BookingRequest( earlyBooked == &amp;quot;true&amp;quot; , discountValue : discount )&lt;br /&gt;
     then&lt;br /&gt;
         System.out.println(&amp;quot;early&amp;quot;);&lt;br /&gt;
         booking.setDiscount( discountValue + 0.1 );&lt;br /&gt;
         update( booking );&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== Guided Editor ⌘。==&lt;br /&gt;
[[File:ApplyDiscountRule.png|400px]]&lt;br /&gt;
&lt;br /&gt;
== Domain Specific Language  领域特定语言（DSL） 。⌘==&lt;br /&gt;
* Rule language (mvel or java dialects) may not  be easy to understand by doctors or managers&lt;br /&gt;
: 规则语言 (mvel 或者 java dialects) 对于医生和经理可能不太容易理解&lt;br /&gt;
* Certain phrases like $now &amp;lt;= $start can be expressed much clear in English (event has started)&lt;br /&gt;
:一些意思的表达比如 $now &amp;lt;= $start 用英语表达更加清楚 (event has started)&lt;br /&gt;
&lt;br /&gt;
== DSL Example。⌘==&lt;br /&gt;
 expander sample.dsl &lt;br /&gt;
&lt;br /&gt;
 rule &amp;quot;Already Booked&amp;quot;&lt;br /&gt;
     when&lt;br /&gt;
         booking : BookingRequest( booked == &amp;quot;true&amp;quot;  )&lt;br /&gt;
     then&lt;br /&gt;
         booking.setDiscount( 0.12 );&lt;br /&gt;
         update( booking );&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 rule &amp;quot;Already Booked&amp;quot;&lt;br /&gt;
     when&lt;br /&gt;
         Booking request:&lt;br /&gt;
             - has been booked &lt;br /&gt;
     then&lt;br /&gt;
            set discount to 0.12&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
== Changing Rules Into DSL ⌘==&lt;br /&gt;
# Unit test the rule &lt;br /&gt;
# Create a DSL file&lt;br /&gt;
# Changed the extension from .drl to .dslr&lt;br /&gt;
# Add expander clause into dslr file ( expander DiscountDSL.dsl)&lt;br /&gt;
# Prefix all lines with &amp;quot;&amp;gt;&amp;quot; charater&lt;br /&gt;
# Change your rules write DSL rules line by line&lt;br /&gt;
&lt;br /&gt;
== 将规则改为领域特定语言(DSL) 。==&lt;br /&gt;
# 单元测试规则 &lt;br /&gt;
# 建立DSL文件&lt;br /&gt;
# 更改扩展名 .drl 为 .dslr&lt;br /&gt;
# 添加 expander 语句到 dslr 文件 (expander DiscountDSL.dsl)&lt;br /&gt;
# 在所有行行首添加 &amp;quot;&amp;gt;&amp;quot; 符号&lt;br /&gt;
# 一个一个地改变你的规则&lt;br /&gt;
&lt;br /&gt;
== RuleFlow/Process ⌘。==&lt;br /&gt;
* Rules are fired without defined order&lt;br /&gt;
:规则的启动没有启动顺序&lt;br /&gt;
* If too many rules depend on the result of the other, complexity may become hard to maintainable&lt;br /&gt;
:如果太多规则依赖另一个规则的结果，可能变得难以维护&lt;br /&gt;
* If rule should be invoked only after certain stage is reached then RuleFlow/BPMN is useful&lt;br /&gt;
:如果规则只在过程执行的某个阶段才会激活，那么RuleFlow/BPMN很有用&lt;br /&gt;
:[[FILE:ClipCapIt-160826-185821.PNG]]&lt;br /&gt;
* 几乎所有的项目，两者都用&lt;br /&gt;
* 把逻辑用显式的方式写出来&lt;br /&gt;
&lt;br /&gt;
== Fusion  ⌘。==&lt;br /&gt;
* Already part of KIE, don&amp;#039;t need to install anything&lt;br /&gt;
* Enabled by declaring a fact as an event @role(event)&lt;br /&gt;
* Event has &amp;#039;&amp;#039;&amp;#039;start time&amp;#039;&amp;#039;&amp;#039; and can have &amp;#039;&amp;#039;&amp;#039;interval&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
: 事件有&amp;#039;&amp;#039;&amp;#039;起始时间&amp;#039;&amp;#039;&amp;#039;还有时间&amp;#039;&amp;#039;&amp;#039;间隔&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* three most features&lt;br /&gt;
** Temporal Operators (before, after, ends....)&lt;br /&gt;
** Aggregation using Time Window&lt;br /&gt;
** Stream Mode&lt;br /&gt;
&lt;br /&gt;
== Fusion Use Cases | Fusion 案例  ⌘。==&lt;br /&gt;
; Using operators &lt;br /&gt;
: Trainer should not be assigned to teach two courses at the same time&lt;br /&gt;
: 培训师不可以同时培训两门课&lt;br /&gt;
 Course1 &amp;#039;&amp;#039;&amp;#039;overlaps&amp;#039;&amp;#039;&amp;#039; Course2&lt;br /&gt;
; Aggregations (Time Window) 聚合&lt;br /&gt;
: Trainer shall not teach more than 40 hours in any 30 day window &lt;br /&gt;
: 培训师在三十天内总课时不能超过40小时&lt;br /&gt;
  accumulate(&lt;br /&gt;
    $c: Courses(trainer_name=&amp;quot;WangHao&amp;quot;,$d : duration) &amp;#039;&amp;#039;&amp;#039;over window:time( 30d )&amp;#039;&amp;#039;&amp;#039;,&lt;br /&gt;
    $duration :sum（$d）)&lt;br /&gt;
 eval($duration &amp;gt; 40h)&lt;br /&gt;
; Streaming Mode with Moving Window&lt;br /&gt;
* When sales exceeds 3 bookings per coordinator per day, send warning to managers&lt;br /&gt;
: 如果一个培训专员每天收到的订单超过3个，那么给经理发出警告&lt;br /&gt;
* If share price falls 10% in less than an hour, sell of all shares &lt;br /&gt;
:如果股票价格在一小时之内跌过10%，那么卖出全部的股票&lt;br /&gt;
&lt;br /&gt;
== Next Module  ⌘。==&lt;br /&gt;
[[Drools Kie - Overview]]&lt;/div&gt;</summary>
		<author><name>Bernard Szlachta</name></author>
	</entry>
</feed>