
Forward and Backward Chaining: Part 1
By Charles Forgy, PhD
Since this is the first of these
columns, I decided to start with the most fundamental issue
in rules: The difference between forward and backward chaining.
In the rules literature, there are many articles that discuss
the difference between forward and backward chaining. Some
of these articles tell only a part of the story, and as
a result, while they may be technically correct, they can
be misleading. I hope to give a more complete explanation.
This is a twopart column. This months part gives
a detailed description of forward and backward chaining.
Next months part will explore why forward and backward
chaining are more alike than it may appear at first.
An Example
Before describing the two kinds of chaining,
lets define a few simple rules that we can use to
show what the two systems would do. The following rules
might be found in an order processing system. The rules
operate on the following variables:
 lastyearsorders: The total amount spent by the customer
in the last twelve months.
 currentorder: The dollar amount of the current order.
 discountlevel: An indication of how good a customer
this one is; the possible values are Platinum, Gold, Silver,
and Default.
 eligiblefordiscount: A logical variable; it is true
if the currentorder is large enough to be discounted.
 discountamount: How much to discount the current order.
The rules are:
R1: If lastyearsorders are >=
$10,000
Then discountlevel is Platinum.
R2: If lastyearsorders are < $10,000
And lastyearsorders are >= $5,000
Then discountlevel is Gold.
R3: If lastyearsorders are < $5,000
And lastyearsorders are >= $2,000
Then discountlevel is Silver.
R4: If lastyearsorders are < $2,000
Then discountlevel is Default.
R5: If currentorder is >= $100
Then eligiblefordiscount is true.
R6: If currentorder is < $100
Then eligiblefordiscount is false.
R7: If discountlevel is Platinum
And eligiblefordiscount is true
Then discountamount is 10%.
R8: If discountlevel is Gold
And eligiblefordiscount is true
Then discountamount is 7%.
R9: If discountlevel is Silver
And eligiblefordiscount is true
Then discountamount is 5%.
R10: If discountlevel is Default
Then discountamount is 0%.
R11: If eligiblefordiscount is false
Then discountamount is 0%.
Forward Chaining
A forwardchaining rule based system contains two basic components:
1. A collection of rules.
2. A collection of facts or assumptions that the rules operate
on. (This is sometimes called the rules Working
Memory.)
The standard definition of a forwardchaining system is that
the system operates by repeating the following sequence of
operations:
1. Examine the rules to find one whose
If part is satisfied by the current contents of Working
memory.
2. Fire the rule by adding to Working Memory the facts that
are specified in the rules Then part. (The Then part
may perform other actions as well, but we can ignore that
for now.)
This control cycle continues until no rules have satisfied
If parts.
For an example of how this works, suppose we were executing
the rules above, and suppose the system starts with the two
following facts in Working Memory:
lastyearsorders = $6300
currentorder = $260
With those facts, two rules, R2 and R5 can fire. If R2 fires
first, it will add the fact
Then rule R5 will fire and add the fact
eligiblefordiscount = true
After these two new facts have been added, rule R8 will be
satisfied. It will fire and add the fact
At this point, no other rules will have satisfied If parts,
so execution will terminate. Backward
Chaining
A backwardchaining rule based system contains three basic
components:
 A collection of rules.
 A collection of facts or assumptions
for the rules to operate on. (The Working Memory.)
 A stack of goals, where a goal
is simply a statement of something that the rules need
to determine.
The control flow of a backwardchaining system is more complex
than that of a forwardchaining system. Backwardchaining
systems try to satisfy the goals in the goal stack. They do
this by finding rules that can conclude the information needed
by the goal, and trying to make the If parts of those rules
satisfied. In more detail, the standard backwardchaining
control cycle is:
1. Check the conclusions
of the rules to find all rules that can satisfy the top
goal on the stack.
2. Process these rules one at a time:
a. Evaluate the
conditions in the rules If part one at a time:
i. If the condition
is currently unknown (that is, if there is not enough
information currently known to determine whether the
condition is satisfied) push a goal to make that condition
known, and recursively invoke the system.
ii. If the condition is known to be
unsatisfied, continue with the loop at Step 2.
iii. If it was not possible to determine
whether the condition was satisfied, continue with the
loop at Step 2.
b. If all the conditions in the selected rule
are satisfied, add to Working Memory the facts specified
in the Then part of the rule, pop the goal off the stack,
and return from this invocation of the system.
The system terminates with success when the goal stack is
empty. It terminates with failure if the system runs out of
rules to try in Step 2.
Using our same example, we will again suppose the system starts
with the two following facts:
lastyearsorders = $6300
currentorder = $260
In a backwardchaining system, we also need an initial goal.
Let us assume the stack contains one goal:
G1: Determine the value of discountamount
When the system begins evaluating goal G1, it will find that
R7, R8, R9, R10, and R11 might satisfy this goal. If it starts
with R7, it will determine that to satisfy R7s If part,
it need the value of discountlevel, so it will assert the
goal
G2: Determine the value of discountlevel
It will determine that rules R1, R2, R3, and R4 could satisfy
this goal. If it tries R1 first, it will determine that it
has all the information that it needs (since it already has
the value of lastyearsorders), but that R1s If part
is not satisfied. If it tries R2 next, it will determine that
R2s If part is satisfied, and conclude
When it adds this fact to Working Memory, it will pop goal
G2 from the stack, and return to G1 and R7 (where it was when
it pushed G2). It will determine that R7s If part cannot
be satisfied. If it selects R8 next, it will determine that
the first condition in R8 is satisfied, but that it does not
have the necessary information for the second condition. It
will push the goal
G3: Determine the value of eligiblefordiscount
It will find that rules R5 and R6 can determine this value.
If it tries R5 first, it will find that R5s single
condition is satisfied (because lastyearsorders are $6300).
It will conclude
eligiblefordiscount = true
It will pop G3, and return to G1 and R8. It will determine
that both of R8s conditions are satisfied. It will
conclude
It will pop goal G1, and terminate with success.
The backwardchaining system, then, concludes the same value
for discountamount as the forwardchaining system, though
it follows a completely different path to the conclusion.
Conclusion
The standard brief explanation of the difference between
forward and backward chaining usually focuses on two points:
 Forwardchaining systems simply
fire rules whenever the rules If parts are satisfied.
Backwardchaining systems attempt to fire rules only when
those rules can potentially satisfy a goal.
 Backwardchaining systems automatically
create new subgoals when more information is needed to
determine whether a given rule is satisfied. Forwardchaining
systems do not automatically create subgoals.
This is correct as far as it goes, but it does not give
an accurate picture of how rules are used in practice. Next
months column will attempt to show how rules are
actually used. It will also discuss the RulesPower systems
use of forward and backward chaining for business process
automation.
I welcome your questions, comments and suggestions for future
topics. Please email me at info@rulespower.com

