An Expert System is a specialized knowledge-based computer program designed to solve complex domain-specific problems at the level of a human human expert. Pioneered during the 1970s and 1980s (e.g., DENDRAL for chemical spectroscopy and MYCIN for infectious blood diseases), expert systems proved that commercial artificial intelligence could provide immense financial and operational value through automated symbolic reasoning.
Unlike standard software where domain knowledge and program control are tangled together in procedural code, an expert system strictly separates domain facts from reasoning logic:
| Dimension | Forward Chaining (Data-Driven) | Backward Chaining (Goal-Driven) |
|---|---|---|
| Starting Point | Starts with known input facts in working memory. | Starts with a proposed hypothesis or goal to be proven. |
| Direction | Bottom-up: Antecedent (IF) to Consequent (THEN). | Top-down: Consequent (THEN) back to Antecedent (IF). |
| Best Used When | Lots of initial data is available, and many potential conclusions are possible (e.g., real-time process control, industrial plant monitoring). | Specific hypothesis needs validation, or gathering new data is expensive (e.g., medical diagnostics, loan approvals, fault isolation). |
| Mechanism | Fires all matching rules, adds new derived facts to working memory, and repeats until a goal or fixpoint is reached. | Checks if goal is already known; if not, finds rules with goal in THEN-part, and sets their IF-conditions as subgoals recursively. |
When multiple production rules have their conditions satisfied simultaneously in working memory (the Conflict Set), the inference engine must select which rule to fire first:
In large systems with tens of thousands of rules, naively checking every rule against working memory at every cycle takes ( O(R cdot F^C) ) time — catastrophically slow. Charles Forgy invented the RETE Algorithm, which compiles production rules into a directed acyclic pattern-matching network:
The following script implements a working forward-chaining expert system with rule firing and working memory updates:
class Rule:
def __init__(self, name, conditions, conclusion):
self.name = name
self.conditions = set(conditions)
self.conclusion = conclusion
class ForwardChainingEngine:
def __init__(self, rules):
self.rules = rules
self.working_memory = set()
self.fired_rules = set()
def add_fact(self, fact):
self.working_memory.add(fact)
def run(self):
print("Starting Forward Chaining Inference...")
while True:
rule_fired = False
for rule in self.rules:
if rule.name in self.fired_rules:
continue
# If all rule conditions exist in working memory
if rule.conditions.issubset(self.working_memory):
print(f" -> [FIRE] Rule '{rule.name}' triggered! Added fact: {rule.conclusion}")
self.working_memory.add(rule.conclusion)
self.fired_rules.add(rule.name)
rule_fired = True
break # Cycle restart for conflict resolution
if not rule_fired:
break
print(f"Final Working Memory Facts: {sorted(list(self.working_memory))}")
# Define Medical Diagnostic Rules
rules = [
Rule("R1_Fever_Cough", ["fever", "cough"], "respiratory_infection"),
Rule("R2_Fatigue_LossSmell", ["respiratory_infection", "loss_of_smell"], "suspected_covid19"),
Rule("R3_Wheezing", ["cough", "shortness_of_breath"], "suspected_asthma"),
Rule("R4_Emergency", ["suspected_covid19", "low_oxygen"], "urgent_hospital_admission")
]
engine = ForwardChainingEngine(rules)
# Patient enters clinic with initial symptoms
engine.add_fact("fever")
engine.add_fact("cough")
engine.add_fact("loss_of_smell")
engine.add_fact("low_oxygen")
engine.run()
Broaden your foundational and advanced AI knowledge across our curriculum:
Rule representation, facts, and resolution refutation foundation.
Handling uncertainty when deterministic IF-THEN rules are insufficient.
From classical expert rule engines to modern autonomous LLM agents.
Forward chaining vs backward chaining interview questions and trade-offs.