Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 46 ยท Logic and Knowledge Representation

Rule-Based and Expert Systems

Build a production system and an expert system in Python: working memory, forward and backward chaining, conflict resolution, explanations and certainty factors.

An expert system is a program that copies the decision-making of a human specialist in a narrow field: diagnosing a car fault, choosing a loan product, configuring a computer order. Instead of a clever algorithm, it holds the specialist's knowledge as hundreds of explicit if-then rules and a general-purpose engine that applies them. In the 1980s these systems were the commercial face of AI. Their descendants live on as business-rule engines, compliance checkers, tax software and the "decision tables" inside many large applications.

This lesson shows the whole architecture in working code. You will build a rule-based system (also called a production system) with working memory, run it in two directions (forward and backward chaining), add an explanation facility, and handle uncertain rules with certainty factors. The logic behind chaining is covered in Inference and Resolution; here the emphasis is on the engineering of a rule-based program.

The architecture of an expert system

flowchart LR
    U["User"] <--> UI["User interface"]
    UI <--> IE["Inference engine"]
    IE <--> WM["Working memory<br/>(facts about this case)"]
    IE <--> KB["Knowledge base<br/>(if-then rules)"]
    IE --> EX["Explanation facility<br/>(why / how)"]
    KA["Knowledge engineer"] --> KB
    EX --> UI

The knowledge base holds general rules, working memory holds the facts of the current case, and the inference engine connects them.

Component Job
Knowledge base The rules written with the expert; changes rarely
Working memory Facts known about the current case; changes constantly
Inference engine Picks rules and fires them; the same engine works for any knowledge base
Explanation facility Answers "why did you ask that?" and "how did you conclude that?"
Knowledge acquisition module Tools for adding and testing rules
User interface Questions and answers in plain language

The separation is the point: you improve the system by editing rules, not by rewriting code. A knowledge engineer interviews the expert and turns the answers into rules.

Rules and working memory

A production rule has a condition part and an action part: IF conditions THEN conclusion. The conditions are checked against working memory, a set of facts. When all conditions match, the rule is applicable, and firing it adds its conclusion to memory.

TEXT
IF  has_fever AND has_cough       THEN suspect_flu
IF  suspect_flu AND short_of_breath THEN see_doctor

Two ways of using the same rules give the two classic strategies.

Forward chaining: from facts to conclusions

Forward chaining is data-driven. Start with the known facts, fire any rule whose conditions hold, add what it concludes, and repeat until nothing new appears. Use it when you have data and want to see everything that follows (monitoring, configuration, alarm systems).

The engine below keeps a recognise-act cycle: find all applicable rules (the conflict set), pick one, fire it, repeat. The rule it picks is chosen by a conflict resolution policy. Here it is simple: the rule with the most conditions fires first (a "specificity" strategy), and the file order breaks ties. It also records, for every new fact, which rule produced it, which is what the explanation facility will use later.

Python A forward-chaining engine with conflict resolution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the first cycle two rules are applicable: R1 (two conditions) and R6 (three conditions). R6 is more specific, so the specificity strategy fires it first. R5 only becomes applicable after R1 has added suspect_flu. Other common strategies are recency (prefer rules that match the newest facts), priority (the author ranks rules) and refraction (never fire the same rule on the same facts twice, which is what the r[2] not in memory test provides here).

Backward chaining: from a goal to the facts it needs

Backward chaining is goal-driven. Start from a hypothesis ("is it flu?") and look for rules that conclude it. Each condition of such a rule becomes a subgoal. A subgoal is satisfied if it is a known fact, or if another rule can prove it, or, in an interactive system, if the user confirms it when asked. This asks only the questions that matter, which is why diagnosis systems such as MYCIN used it.

Python Backward chaining that asks the user

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trace shows the goal-driven order of work. Only facts that appear in rules for see_doctor are ever asked about, and has_fever is asked twice because two branches need it (a real system would remember the answer). Both branches fail here, because there is no shortness of breath and no stiff neck, so the goal is not proved. Change the answers and run it again to see the other paths. A forward engine would have needed every fact up front.

Forward chaining Backward chaining
Driven by Data (facts) Goal (hypothesis)
Works from Conditions to conclusions Conclusion to conditions
Good for Monitoring, configuration, "what follows?" Diagnosis, "is X true?", asking few questions
Risk Fires rules that are irrelevant to the goal May explore many dead-end subgoals

Many real engines combine both. Prolog uses backward chaining; the OPS5 and CLIPS family use forward chaining.

The explanation facility

Experts and users will not trust a recommendation they cannot question. Expert systems answer two questions:

  • Why? (while asking): "I ask about shortness of breath because I am trying to prove see_doctor through rule R2."
  • How? (after concluding): "I concluded see_doctor because R2 fired, and its conditions suspect_flu and short_of_breath were established by R1 and your answer."

The why dictionary built in the forward-chaining engine is exactly this record. Walking it recursively prints a proof tree. This transparency was one of the great advantages of rule-based AI over later black-box models.

Certainty factors: rules that are not 100% sure

Real expert knowledge is rarely certain ("a fever and a cough make flu likely"). The medical system MYCIN attached a certainty factor (CF) from -1 (certainly false) to +1 (certainly true) to each rule and each fact. Two formulas drive the calculation:

  • A rule's conclusion gets CF = CF(rule) x min(CF of its conditions).
  • Two rules supporting the same conclusion combine: if both are positive, CF = a + b x (1 - a).
Python Combining certainty factors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first rule gives 0.7 x 0.8 = 0.56. The second gives 0.4 x 0.6 = 0.24. Together: 0.56 + 0.24 x 0.44 = 0.666. More evidence raises belief but never past 1. Certainty factors are ad hoc compared with probability theory (see Bayesian Networks for the principled way), but they were simple, fast and good enough to match specialists on narrow tasks. Fuzzy logic and Dempster-Shafer theory are two other answers to the same problem; see Fuzzy Logic and Belief Functions.

Strengths and weaknesses

Advantages

  • Knowledge is explicit, readable and easy to audit.
  • Expertise is preserved even after the expert leaves, and available at any time.
  • Consistent decisions, with no fatigue or mood.
  • Rules can be added one at a time; the engine never changes.
  • Explanations come almost for free.

Disadvantages

  • Knowledge acquisition bottleneck: experts find it hard to put intuition into rules, and extracting them is slow.
  • Brittleness: outside its narrow domain the system fails abruptly, with no common sense to fall back on.
  • Maintenance: thousands of rules interact in ways nobody predicted, so one new rule can break an old one.
  • No learning: it does not improve from experience unless people edit it.
  • Uncertainty is clumsy: certainty factors only approximate probability.

These problems, together with the cost of maintaining huge rule bases, helped cause the second AI winter described in What Is Classical AI?. Rules are still the right tool where the logic is stable, legal or must be explained: eligibility checks, tax and insurance calculations, safety interlocks.

Note: Modern rule engines (Drools, CLIPS, business-rule management systems) use an efficient matching algorithm called Rete, which remembers partial matches between cycles instead of re-checking every rule against every fact each time.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which strategy is data-driven: it starts from known facts and applies rules until nothing new follows?

    Forward chaining begins with the facts in working memory and fires applicable rules; backward chaining starts from a goal.
  2. 2. What does conflict resolution decide?

    When more than one rule can fire, a strategy such as specificity, recency or priority chooses one.
  3. 3. Why is backward chaining a good fit for diagnosis dialogues?

    The goal creates subgoals, and only unknown facts in those subgoals need to be asked.
  4. 4. A rule with certainty 0.5 has a condition with certainty 0.8. What is the conclusion's certainty factor?

    CF = rule CF times the minimum condition CF = 0.5 x 0.8 = 0.4.
  5. 5. Which is a classic weakness of expert systems?

    Rule bases are hard to keep consistent as they grow, and they have no common sense outside the rules.

Summary

  • An expert system separates a knowledge base of rules, working memory of case facts and a general inference engine.
  • Forward chaining is data-driven; backward chaining is goal-driven and asks only relevant questions.
  • Conflict resolution (specificity, recency, priority, refraction) chooses among applicable rules.
  • Explanation facilities answer "why?" and "how?" by replaying the rule trail.
  • Certainty factors attach graded belief to rules and combine evidence, as an approximate alternative to probability.
  • Strengths are transparency and consistency; weaknesses are the knowledge bottleneck, brittleness and maintenance.

Next: Types of Reasoning.