Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท Rule-based Expert Systems

Forward Chaining: Data-Driven Reasoning

Build a forward-chaining inference engine: the match-select-act cycle, agenda, conflict resolution, refraction and when data-driven reasoning is the right choice. Runnable Python.

Forward chaining is reasoning that starts from what you know and pushes forward to what follows. You have facts in working memory; you look for rules whose conditions are satisfied; you fire them to add new facts; and you repeat until nothing new can be concluded. It is called data-driven because the data decides which rules fire.

Think of a monitoring system. Sensor readings arrive (data), rules turn readings into alarms, alarms into actions. Nobody asks a question; the facts push the reasoning along.

The recognize-act cycle

Every production system loops through three phases:

  1. Match: find all rules whose conditions hold in working memory. The set of matching rules is the conflict set, or agenda.
  2. Select: choose one rule from the agenda using a conflict resolution strategy.
  3. Act: fire the rule (add facts, remove facts, print something) and go back to step 1.

The loop stops when the agenda is empty or a goal fact appears.

flowchart LR
  M["Match<br/>find applicable rules"] --> S["Select<br/>conflict resolution"]
  S --> A["Act<br/>fire the rule"]
  A --> M
  M -->|"agenda empty"| E["Stop"]

Match, select, act. Working memory changes after each firing, so the next match sees the new facts.

Forward chaining in 3 words

Match Which rules apply?
Select Pick one
Act Fire, add new facts

Repeat until nothing is new

1 / 2

A basic forward chainer

The version below keeps a trace of each cycle so you can see the reasoning unfold. It also applies refraction: a rule instance may fire only once, otherwise a rule would fire forever on the same facts.

Python Forward chaining with a cycle trace

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.

Look at cycle 1: both R1 and R4 are on the agenda, but only the first is fired. Rule order is the simplest conflict resolution strategy. In cycle 2, the new fact overheating makes R2 applicable, and the chain continues.

Conflict resolution that matters

When the order of firing affects the result, the strategy is part of the design. Common choices:

  • Salience (priority): each rule carries a number; the highest runs first. Safety rules get high salience.
  • Specificity: the rule with the most conditions wins.
  • Recency: prefer rules that use the newest facts, so the system follows its current line of thought.
  • Refraction: do not fire the same rule on the same facts twice.
Python Salience and specificity together

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.

When smoke is present the emergency rule runs first, even though it was listed second. With only high temperature, the fan rule outranks the logging rule.

Facts that change: retraction

Real systems also remove facts. If a sensor reading changes, old conclusions based on it should go away. A simple engine can re-run from scratch each time the inputs change (clean and easy), while advanced engines keep dependency links to retract only what is affected (truth maintenance).

Python Re-deriving after the inputs change

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.

Each call starts from only the current inputs, so conclusions such as open_vent disappear when pressure_high is no longer reported.

Cost and the Rete idea

A naive engine re-checks every rule against all facts on each cycle: with R rules and F facts that is a lot of repeated work. The Rete algorithm (used by CLIPS, Drools and Jess) remembers partial matches in a network, so when one fact changes only the affected rules are re-evaluated. You do not need to implement Rete to use an expert system, but it is why industrial engines handle thousands of rules.

Forward versus backward chaining

Forward chaining Backward chaining
Starts from Known facts A goal to prove
Direction Data to conclusions Goal to needed data
Good for Monitoring, configuration, many outputs Diagnosis, a single question
Risk Derives many irrelevant facts May ask or search too deep

The next lesson builds the second kind.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the conflict set (agenda)?

    Match produces every rule whose conditions hold; select then picks one.
  2. 2. What problem does refraction prevent?

    Without refraction a rule whose condition stays true would fire forever.
  3. 3. Why is forward chaining called data-driven?

    Reasoning starts from the data in working memory, not from a goal.
  4. 4. Two rules match. One has salience 100, the other 10. With salience resolution, which fires first?

    Highest salience is chosen first.

Summary

  • Forward chaining repeats match, select, act until the agenda is empty.
  • Conflict resolution (order, salience, specificity, recency) decides which rule fires; refraction stops infinite repetition.
  • Re-deriving from current inputs is a simple way to handle changing facts; truth maintenance does it incrementally.
  • Rete avoids re-checking all rules every cycle.
  • Use forward chaining when data arrives and many conclusions are wanted.

Next: Backward Chaining