modeling and design of rule-based systems yonglei tao
TRANSCRIPT
Modeling and Design of Rule-Based Systems
Yonglei Tao
Key Takeaway Points Business rules are decision-making rules
each consists of conditions and actions to perform when the conditions are met.
A decision table is used to specify business rules check complete, consistent, and non-redundant generate code support test-case generation and test-driven
development enable rules to be updated dynamically
2
Business Rules of an Office Worker
10% Pay Raise
Promotion
Own Office
Stay
Quit
1 2 3 4
Y
x
Y
Y
x
Y-
-
x
N N
N
4 1 1
N
N
-
x
2Rule Count
Decision Table
10% Pay Raise
Promotion Own Office
Stay
Quit
Y
N
Y
Y Stay
N
N
Quit
Decision Tree
3
Decision Table Terminology
10% Pay Raise
Promotion
Own Office
Stay
Quit
1 2 3 4
Y
x
Y
Y
x
Y-
-
x
N N
N
4 1 1
N
N
-
x
2Rule Count
Decision Table
conditionstubs
conditionentry
actionstubs
For checking completeness 4+1+1+2= 23=8
rule number
action entry
indifference or don’t care
4
Limited and Extended Entry
customer
rules covered
request pay
OK order
Rule# 1
>2m
-
3
X
2
<=2m
3
X
reg.
1 2
due payment
X
4
X
new
1
no
5
X
2
no<=2m
new/NR
notnew
Extended entry decision table: non-binary.conditions extend into condition entries.
10% Pay Raise
Promotion
Own Office
Stay
Quit
1 2 3 4
Y
x
Y
Y
x
Y-
-
x
N N
N
4 1 1
N
N
-
x
2Rule Count
Limited entry decision table
5
Systematic Decision Table Construction1. Identify conditions that affect actions2. Identify actions that are affected by conditions3. Draw a table with appropriate rows and
columns4. Enter the conditions and actions into the
condition stubs and action stubs5. Systematically enter the condition values in
the condition entries6. For each condition combination, check the
corresponding action(s). If the actions must be performed in a certain order, use serial numbers to indicate the order
6
Systematic Decision Table Construction
10% Pay Raise
Promotion
Own Office
Stay
Quit
1 2 3 4
Y
x
Y
N
x
NY
Y
Y Y
Y
1 1 1
Y
N
N
1Rule Count
5 6 7 8
N
x
Y
N
x
NY
Y
x
N N
Y
1 1 1
N
N
N
x
1
x x
7
Progressive Decision Table Construction Enter the conditions one at a time and
completes a rule as early as possible
promotedwork
interest
10% raise
ownoffice
stay
quite
Y
N
NNN
YYY
8
Progressive Decision Table Construction1) Draw a table of required rows and columns 2) Enter the actions into the action stubs.
Stay
Quit
Rule Count
Rule #
9
Progressive Decision Table Construction3) Enter one of the conditions into condition stub
and one of the condition value into the condition entry. Save the remaining values.
Promotion
1
Y
Remaining Values
Stay
Quit
Rule Count
Rule #
N
10
Progressive Decision Table Construction4) If the rule can lead to an action then
enter “-” in the remaining conditions check the appropriate action entries begin a new rule: copy all the condition entries of the previous rule
except the last condition enter one of the remaining values of the last condition
Promotion
1
Y
Remaining Values
Stay
Quit
Rule Count
Rule #
N
11
Progressive Decision Table Construction else
enter another condition and one of its values save the remaining values
>=10% Raise Y N
Promotion
1
Y
Remaining Values
Stay
Quit
Rule Count
Rule #
N
12
Progressive Decision Table Construction5) Repeat the above step
promotedwork
interest
10% raise
ownoffice
stay
quite
Y
N
NNN
YYY
>=10% Raise Y N
Promotion
1
Y
Remaining Values
Stay
Quit
Rule Count
Rule #
N
X
--
--
2
Y
N
--
--
X
3
N
Y N
Work Interest Y N
--
X
4
N
Y
N
Own Office Y N
X
5
N
Y
N
N
X
13
Quality Aspect of Decision Table Completeness
are there missing rules? check by the rule count
are there missing conditions or actions? application dependent
Consistency is there more than one rule with the same
condition combination but different actions? does a rule have conflicting actions?
Non-redundancy can/should some rules be eliminated or merged?
14
Rule#
Rule Count
Stay
Quit
10% raise Y
X
promoted Y1
Y
N
X
2N
Y
- - Y
X
3
work interest
N
Y
N
X
Y
4
- - -own office
4 2 14 1 2 1 1
N
Y
N
X
N
5
N
N
Y
X
-
6
N
N
N
X
Y
7
N
N
N
X
N
8
Are there redundancies in the decision table?What problems can be caused by redundancy?Is there a systematic way to detect and remove redundancies?
Decision Table Redundancy
15
Decision Table Redundancy Problems
can cause inconsistency if one place is modified and the other is not, or
modified differently the code is more tedious
and more difficult to understand and maintain
Benefits to removal shorter and more
efficient code code is easier to
comprehend will not cause
redundancy related inconsistency
Removing decision table redundancies is called “decision table consolidation.”
16
Decision Table Consolidation
Rule#
Rule Count
Stay
Quit
10% raise Y
X
promoted Y1
Y
N
X
2N
Y
- - Y
X
3
work interest
N
Y
N
X
Y
4
- - -own office
4 2 14 1 2 1 1
N
Y
N
X
N
5
N
N
Y
X
-
6
N
N
N
X
Y
7
N
N
N
X
N
8
rule 4: !promoted & 10% raise & !work interest & own office ==> stay
rule 7: !promoted & !10% raise & !work interest & own office ==> stay
17
Decision Table Consolidation
rules 4+7: (!promoted & !work interest & own office) ==> stay
Rule#
Rule Count
Stay
Quit
10% raise Y
X
promoted Y
1
Y
N
X
2
N
Y
- - Y
X
3
work interest
N
Y
N
X
Y
4
- - -own office
4 2 14 1 2 1 1
N
Y
N
X
N
5
N
N
Y
X
-
6
N
N
N
X
Y
7
N
N
N
X
N
8 Rule#
Rule Count
Stay
Quit
10% raise Y
X
promoted Y
1
Y
N
X
2
N
Y
- - Y
X
3
work interest
N
--
N
X
Y
4
- - -own office
4 2 24 1 2 1
N
Y
N
X
N
5
N
N
Y
X
-
6
N
N
N
X
N
8
Rule 7 deleted.Rule 4 changed.
18
Decision Table Consolidation
Rule#
Rule Count
Stay
Quit
10% raise Y
X
promoted Y
1
Y
N
X
2
N
Y
- - Y
X
3
work interest
N
--
N
X
Y
4
- - -own office
4 2 24 1 2 1
N
Y
N
X
N
5
N
N
Y
X
-
6
N
N
N
X
N
8
Rules 5 and 8 differ on only one condition. So they can be merged.
Rule#
Rule Count
Stay
Quit
10% raise Y
X
promoted Y
1
Y
N
X
2
N
Y
- - Y
X
3
work interest
N
--
N
X
Y
4
- - -own office
4 2 24 2 2
N
--
N
X
N
5
N
N
Y
X
-
6
Rule 8 deleted and rule 5 changed.
19
Consolidation Algorithm
2 or more unmarked rules differ on only 1
condition?
1 of the rules has “-” for that condition?
delete the otherrules
Halt
Do therules include all
cases for thatcondition?
replace the rules with 1 that has “-” for that condition
Y
YY
N
N
N
mark the rules as visited
20
Consolidation Algorithm Decision Table
>2 or more unmarked rules differ on 1 condition?
1 of the rules has “-” for condition?
the rules include all cases for condition?
Rule Count
delete the other rules
replace rules with one with “-” for condition
go to this decision table
halt
Rule # 1 2 3 4
Y YY
Y
Y
N
N
N
N --
-- --
X
X
X
X X
X
2 1 1 4
21
Code Generation from Decision Table First, arrange the rows and columns so that the
condition rows have increasing frequencies of alternating condition values
Draw a tree to highlight the alternating columns
>1 rule differ on 1 condition?
1 of the rules has “-” for condition?
the rules include all cases for condition?
Rule Count
delete the other rulesreplace rules with one with “-” for conditiongo to this decision table
halt
Rule # 1 2 34
Y YY
Y
Y
N
N
N
N--
----
X
X
X
X X
X
2 1 14
22
Code Generation>1 rule differ on 1 condition?
1 of the rules has “-” for condition?
the rules include all cases for condition?
Rule Count
delete the other rulesreplace rules with one with “-” for conditiongo to this decision table
halt
Rule # 1 2 34
Y YY
Y
Y
N
N
N
N--
----
X
X
X
X X
X
2 1 14
while (true) doif (not >1 rule differ on 1 condition) then breakelse if (1 of the rules has “--” for the condition) then delete the other rules else if (the rules include all cases) then replace rules with one with “--” for that condition
end23
Test Case Generation Decision tables are commonly used to
generate test cases for cause-effect testing Each rule is a test case
identify the component under test initialize the input with appropriate condition
values check the output of the actions
Support unit testing and system testing
24
Decision Tree
>1 rule differ on 1 condition?
Y
N Halt
1 of the rules has “-” for condition?
Y
N
the rules include all cases for condition?
Y replace rules with one with “-” for condition;go to this table
delete the other rulesgo to this table
N go to this table
Decision tree is another method to specify process logic.
25
Class Exercise Using the consolidation algorithm decision
table, consolidate the following decision table.
More than $1M business/yr?
Good payment history?
With us more than 10 years?
Priority treatment
Normal treatment
1 2 3 4
Y
x
Y
N
x
NY
Y
Y Y
Y
1 1 1
Y
N
N
1Rule Count
5 6 7 8
N
x
Y
N
x
NY
Y
x
N N
Y
1 1 1
N
N
N
x
1
x
x
26
Competition of Telephone Companies In the 1990s, U.S. telephone companies (AT&T,
MCI, GTE) competed for customers intensively They offered discount plans to attract customers Each new plan required modification of conditional
statements of the computer program It required a lot of time to change, compile, test
and debug the computer program One company adopted a technology that gave it a
competitive advantage The technology is an application of the
interpreter pattern
2727