Guide to AI and Data Courses · Lecture Notes

Chapter 11: Expert Systems and Knowledge Representation

A service technician thinks "if the headlights are dim the battery is weak, and if the engine will not turn over as well the battery is flat", and tests that thought on hundreds of faults over the years. If we write this knowledge into a program as if-then rules, the program can find the fault without the technician and say why it thinks so. The course on expert systems and knowledge representation teaches how to write knowledge so that a computer can reason with it, and how to draw conclusions from it. These notes introduce the course and work through its three core ideas with small examples you can follow by hand.

Learning outcomes

Rule base
Write a domain's knowledge as facts and if-then rules; describe the working cycle of an inference engine.
Forward chaining
Run forward chaining step by step on a small rule base and apply conflict resolution.
Backward chaining
Prove a goal by backward chaining, say which questions will be asked of the user, and explain the result.
Certainty factors
Compute the confidence given by one rule and by two rules together with MYCIN-style certainty factors.
Place of the course
Separate an ontology and a knowledge graph from a rule base; state what sets expert systems apart from machine learning, fuzzy logic and probabilistic models.

11.1What question does the course ask?

How is an expert's knowledge written down and used for inference?

An expert system writes a domain expert's knowledge into a knowledge base of rules and facts and draws conclusions from it with an inference engine. Knowledge representation studies how to encode knowledge so that a computer can reason with it: logic, frames, semantic networks and ontologies.

In machine learning, knowledge is extracted from examples. In expert systems, knowledge is taken from a person and written down explicitly: "if these symptoms are present, this conclusion follows". Writing knowledge explicitly gives two things. First, the system can show which rules led to its conclusion; it explains itself. Second, the knowledge can be read and corrected independently of the system: changing a rule does not require retraining a model.

The question of the course therefore has two parts. Representation: in what form must knowledge be written so that it is meaningful to people and processable by a computer? Inference: by what method do we derive new knowledge from what has been written? Rules, logic, frames and ontologies are different representation languages; chaining is one of the inference methods that work on them.

11.1.1Why is writing knowledge down hard?

An expert cannot say most of what they know: an experienced technician says "that sound seems wrong to me" but cannot write the rule. Getting knowledge out of the expert and into rules is called knowledge acquisition, and it is known as the bottleneck of these systems. As the number of rules grows, rules shadow or contradict each other. So besides inference methods, the course also covers how to check and maintain a knowledge base. Nor is knowledge written only as rules: objects, classes and relations are represented with an ontology (the third core section).

11.1.2At a glance

CriterionExpert Systems and Knowledge Representation
Approachsymbolic logic and rules
Inputfacts and expert rules
Outputconclusion with justification
Success measureexpert agreement, explainability
Math loadlow: propositional and predicate logic
Data needslittle; a knowledge base

11.2A short history

YearMilestone
1965DENDRAL starts at Stanford: the first expert system, inferring molecular structure from mass spectra.
1972Colmerauer and Roussel develop Prolog: logic programming is born.
1970sMYCIN recommends antibiotics for blood infections, adding certainty factors to its rules.
1980R1 (XCON) starts configuring computer orders at DEC: the first big commercial success.
1984Lenat launches Cyc, aiming to hand-code common-sense knowledge.
2012Google announces the Knowledge Graph; knowledge graphs spread in search.

11.3Core concepts

The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.

ConceptMeaning
Knowledge baseDomain facts and IF-THEN rules.
Inference engineApplies rules to facts to derive new conclusions.
Forward chainingStart from facts and derive every reachable conclusion.
Backward chainingStart from a goal and look for facts that support it.
Explanation facilityShows which rules led to a conclusion.
Frames and networksRepresenting objects with slots and relations.
OntologyA domain's concepts, classes and relations: OWL.
Knowledge acquisitionEliciting knowledge from experts; the bottleneck.

11.4The rule base and forward chaining

A rule-based system has three parts: facts (what is currently known to be true), rules (IF conditions THEN conclusion) and the inference engine that applies the rules to the facts. Forward chaining starts from the facts: it finds a rule whose conditions are satisfied by the facts, fires it, and adds its conclusion as a new fact. The new fact may make other rules applicable; the loop continues until no rule is left.

Definition 11.1 (The inference engine cycle)

1. Match: find the rules whose conditions are satisfied by the facts and that have not fired yet (the conflict set). 2. Select: pick one by conflict resolution (for example the rule with the most conditions, i.e. the most specific). 3. Fire: add its conclusion to the facts. Stop when the set is empty.

Example 11.1: Tracing a battery fault

The rule base (a small piece of a fault diagnosis system):

  • R1: headlights dim → battery weak
  • R2: engine not cranking ∧ battery weak → battery flat
  • R3: battery flat ∧ battery old → replace battery
  • R4: battery flat ∧ battery new → charge battery
  • R5: fuel gauge empty → tank empty

Initial facts: headlights dim, engine not cranking, battery old.

  1. Round 1. The conflict set is {R1}: R2 lacks "battery weak", R3 and R4 lack "battery flat", R5 has no gauge fact. R1 fires; new fact battery weak.
  2. Round 2. The conflict set is {R2}. R2 fires; new fact battery flat.
  3. Round 3. The conflict set is {R3}: R4 has no "battery new" fact, so it does not match. R3 fires; new fact replace battery.
  4. Round 4. No unfired rule matches; the system stops.

Three of the five rules fired; R4 and R5 never matched. The chain derived is: headlights dim → battery weak → battery flat → replace battery. The same chain is the explanation the system would give.

11.4.1Conflict resolution

More than one rule can match in a round. A conflict resolution strategy decides which fires first. Common strategies are specificity (the rule with more conditions first, because it describes a more particular situation), priority (a number given to the rule by hand), recency (the rule that uses the most recently added fact first) and rule order. In large systems, comparing every rule with the facts from scratch in every round is slow; the Rete algorithm stores the matches and recomputes only those that depend on changed facts. Rule engines such as CLIPS and Drools use it.

Common mistake

Firing a rule on the same facts forever. If a rule whose conditions hold matches again in every round, the system never stops. Engines prevent this by letting a rule fire only once for the same set of facts and removing fired rules from the conflict set (the "not yet fired" condition above). When writing rules, one also avoids rules that add no fact or that recreate their own condition.

11.5Backward chaining, explanation and certainty factors

Backward chaining starts from the goal. A rule that gives the goal as its conclusion is found, and the rule's conditions become subgoals. A subgoal that is a fact is proved; if it is the conclusion of another rule, we descend into that rule's conditions; if it is neither a fact nor a conclusion, the user is asked. This is how Prolog works, and it is natural for diagnosis systems: it asks only the questions relevant to the goal.

Example 11.2: From the goal backwards

Using the rule base above, with no facts known at the start. The goal is charge battery. Conditions are tried left to right, depth first; the user's answers are in parentheses.

  1. Goal: charge battery. Only R4 gives it; subgoals: battery flat, battery new.
  2. Subgoal: battery flat. R2 gives it; subgoals: engine not cranking, battery weak.
  3. Question 1: is the engine not cranking? (yes).
  4. Subgoal: battery weak. R1 gives it; subgoal: headlights dim.
  5. Question 2: are the headlights dim? (yes). Battery weak, then battery flat, are proved.
  6. Question 3: is the battery new? (no). R4 fails; there is no other rule, so the goal cannot be proved.

Three questions were asked. Had the first answer been "no", one question (is the engine not cranking?) would have eliminated R2 and therefore R4, and the system would have stopped. With the goal replace battery the questions would be the same, the third being "is the battery old?", and a "yes" would prove the goal. Forward chaining does not ask these questions because the facts are given at the start; when it is not known in advance which facts to collect, backward chaining needs less data.

Forward chainingBackward chaining
Starts fromfactsa goal
Directionfrom data to conclusionsfrom a conclusion to data
Suited tomonitoring, configuration: many conclusionsdiagnosis, question answering: tests one hypothesis
Course toolsCLIPS, DroolsProlog

11.5.1The explanation facility

Explanation is what lets expert systems win trust. The user asks two questions. Why? When the system asks a question, the answer is "I need this to try this rule"; in the example, the reason for "is the engine not cranking?" is R2. How? When a conclusion appears, the answer is "I reached it with this rule and these facts": the chain of rules is the explanation. For learned models, such an explanation has to be sought afterwards (see Explainable AI and Ethics).

11.5.2Uncertainty: certainty factors

Real rules are not certain. MYCIN attaches a certainty factor (CF) to rules. The factor is a number between −1 and 1; here only positive values are used. The confidence in a rule's conclusion is the smallest of its conditions' confidences times the rule's own factor. If two rules support the same conclusion with confidences c₁ and c₂, the combined confidence is c₁ + c₂ · (1 − c₁).

Example 11.3: Two rules, one hypothesis

Hypothesis H has two rules. Rule a: A ∧ B → H with rule factor 0.9; the facts have CF(A) = 0.8 and CF(B) = 0.6. Rule b: C → H with rule factor 1.0 and CF(C) = 0.5 (the values are illustrative).

  1. Rule a. The smallest condition is min(0.8, 0.6) = 0.6; 0.6 × 0.9 = 0.54.
  2. Rule b. 0.5 × 1.0 = 0.50.
  3. Combine. 0.54 + 0.50 · (1 − 0.54) = 0.54 + 0.23 = 0.77.

Two weak pieces of evidence together gave a stronger conclusion (0.77), but the confidence never exceeds 1. The order of combination does not change the result: 0.50 + 0.54 · 0.50 = 0.77.

Common mistake

Taking a certainty factor for a probability. The factors come from the expert's degrees of belief in the rules; they need not obey the axioms of probability, and the conditions are assumed to be independent. To treat uncertainty consistently, Probabilistic Graphical Models are used; partial truth is the subject of Fuzzy Logic.

11.6Ontologies and knowledge graphs

Rules are good at writing "what to do", but writing down what a domain contains calls for a separate language. A dog is a mammal, a mammal is an animal: relations between classes like these are kept in an ontology. If knowledge is written as triples (subject, relation, object), the whole knowledge base becomes a graph: nodes are concepts or objects, edges are relations. This graph is the basic data model of RDF and of applications such as Google's knowledge graph. OWL adds meanings such as class, subclass and restriction to the triples.

Definition 11.2 (Triples and the subclass rule)

A triple has the form (subject, relation, object), for example (Rex, type, Dog). Two basic inferences are enough: (1) if (X, subClassOf, Y) and (Y, subClassOf, Z) then (X, subClassOf, Z); (2) if (a, type, X) and (X, subClassOf, Y) then (a, type, Y). This is also called inheritance: a subclass inherits the properties of its superclass.

Example 11.4: A small animal ontology

Seven triples are written down (asserted):

No.SubjectRelationObject
1DogsubClassOfMammal
2CatsubClassOfMammal
3MammalsubClassOfAnimal
4BirdsubClassOfAnimal
5RextypeDog
6TomtypeCat
7TweetytypeBird
  1. Apply rule 1. (1)+(3): Dog subClassOf Animal; (2)+(3): Cat subClassOf Animal. Two new triples.
  2. Apply rule 2. Rex: Mammal and Animal (2 triples). Tom: Mammal and Animal (2 triples). Tweety: Animal (1 triple). Five new triples.
  3. Total. 7 asserted + 2 + 5 = 14 triples; 7 of them were found by inference.
  4. Query. "All objects that are animals" (in SPARQL the pattern ?x type Animal) returns three results: Rex, Tom, Tweety. "Mammals" returns two: Rex and Tom.

Nobody wrote that Tweety is an "Animal"; the ontology derived it from the subclass chain. Without inference the query would find only what was written explicitly, and the animal query would return nothing.

11.6.1Frames and semantic networks

The same knowledge can also be written in an object-centered way. A frame holds an object's slots: the "type" slot of the Rex frame is Dog, its "owner" slot is a person. If a slot has no value, the default of the superclass is used; this is inheritance in a class hierarchy and resembles inheritance in Java. A semantic network draws the same knowledge as nodes and labeled edges; triples are the plain form of both. Description logics give these structures a consistent, decidable logical foundation.

Common mistake

Treating a fact that is not written in the ontology as false. OWL works under the open world assumption: if "Tweety is not a dog" is not written, the system does not know it, and does not count it as false either. Prolog and CLIPS, by contrast, work under the closed world assumption: what cannot be proved is taken as false. R4 above failing to match because "battery new" was never stated is an example. Mixing the two assumptions leads to unreasonable conclusions.

11.7How is the course taught?

11.7.1A typical workflow

InterviewexpertacquisitionEncoderules,ontologyCollectfactsuseranswersInferforward orbackwardExplainresult andreasons
Figure 11.1: A typical workflow in Expert Systems and Knowledge Representation

A fault diagnosis system turns technicians' experience into rules such as "if the engine won't start and battery voltage is low, replace the battery". It asks the user about symptoms, chains the rules to find the fault and shows step by step which rules it used.

11.7.2Topics over the semester

  1. The knowledge representation and reasoning problem
  2. Propositional and first-order logic; resolution
  3. Logic programming: Prolog
  4. Rule-based systems; forward and backward chaining
  5. The Rete algorithm and rule engines: CLIPS, Drools
  6. Frames, semantic networks and inheritance
  7. Description logics
  8. Ontologies: RDF, OWL and Protégé
  9. Knowledge graphs and SPARQL queries
  10. Uncertainty: certainty factors, default reasoning
  11. Knowledge acquisition and expert interviews
  12. Explanation, verification and knowledge base upkeep

This is a typical semester; the order and coverage vary between universities.

11.7.3Skills you will gain

  • writing rules in CLIPS or Drools
  • logic programming in Prolog
  • building ontologies in Protégé
  • querying knowledge graphs with SPARQL
  • eliciting expert knowledge as rules

11.7.4Try it yourself

The Try It: Why Won't the Car Start? slide in the presentation lets you solve a typical problem of this course yourself: A mechanic's knowledge was written as nine IF–THEN rules. Click the symptoms on the left to mark them; the system fires rules one by one by forward chaining, reaches a diagnosis and explains why.

11.8Are you ready for this course?

11.8.1Prerequisites

CourseWhy it is needed
Artificial Intelligencesymbolic AI, logic and rules
Discrete Mathematicspropositional and predicate logic, inference rules
Data Structurestrees, graphs and hash tables: storing rules and facts
Object Oriented Programming with Javaclasses and inheritance: a bridge to frames and Drools

11.8.2Ask yourself

For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.

QuestionIf not, start with
Can you write a sentence such as "all humans are mortal" in first-order logic?Propositional and predicate logic from discrete mathematics
Can you run depth-first and breadth-first search on an example?Artificial Intelligence
Can you use a hash map and a graph in a program?Hash tables and graphs from a data structures course
Can you explain classes, objects and inheritance with an example?An intro to object-oriented programming in Java

11.9Neighboring courses

Fuzzy LogicArtificial IntelligenceGraphical ModelsExplainable AI & EthicsExpert Systems
subfieldbuilds onshares methodsapplication
Figure 11.2: Neighbors of Expert Systems and Knowledge Representation and the kind of relation

11.9.1Where it overlaps

RelationCourseShared ground
subfieldThis course is a subfield of Artificial Intelligencesymbolic AI, logic and rules
shares methodsShares methods with Fuzzy Logic and Fuzzy SystemsIF-THEN rule base, inference engine
shares methodsShares methods with Probabilistic Graphical Modelsinference under uncertainty
shares methodsShares methods with Explainable AI and AI Ethicsexplainable decisions through readable rules

11.9.2Where it differs

  • Machine Learning: ML learns knowledge from examples and often gives no reasons; an expert system takes knowledge from experts and explains its result with rules.
  • Fuzzy Logic and Fuzzy Systems: Fuzzy logic lets rules be partly true; this course teaches representing knowledge and chaining rules in general.
  • Probabilistic Graphical Models: PGMs model uncertainty consistently with probability distributions; expert systems mostly write knowledge as crisp rules.
  • Explainable AI and AI Ethics: XAI seeks explanations for learned models after the fact; an expert system explains itself because it is built from readable rules.

11.10In practice

11.10.1Applications

AreaWhat is done
Clinical decision supportDiagnosis and treatment advice from symptoms and tests.
Fault diagnosisFinding machine, vehicle and network problems.
Product configurationBuilding orders from compatible parts.
Business rulesLoan approval, insurance and compliance checks.
Knowledge graphsEntity and relation data in search engines.
Semantic webLinking data through ontologies.

11.10.2Risks and ethics

RiskExplanation
Bias written into rulesAn expert's habits and an institution's old practices become rules; credit or insurance rules can quietly exclude some groups.
Rule conflicts and upkeepThousands of rules can shadow and contradict each other; in large systems such as XCON, maintaining the rules became a burden of its own.
Unclear responsibilityAfter a wrong diagnosis or a rejected application, it is unclear whether the fault lies with the expert, the knowledge engineer or the user.
Errors in knowledge basesWrong or outdated facts in open knowledge bases spread to the search and chat systems built on them.

These issues are discussed in depth in Explainable AI and AI Ethics.

11.10.3Tools and datasets

Tools: CLIPS, Drools, SWI-Prolog, Protégé, Apache Jena, Neo4j.

Well-known knowledge bases: Wikidata, DBpedia, WordNet, Gene Ontology, SNOMED CT, Protégé Pizza ontology.

11.11Common misconceptions

Common mistake

Expert systems were a 1980s fad.

Under new names, business rule engines and knowledge graphs are used today in banking, healthcare and search.

Common mistake

An expert system knows as much as the expert.

It knows only what was encoded; it fails quickly outside its domain or when common sense is needed.

Common mistake

Writing the rules is easy.

Much expert knowledge is tacit; turning it into rules (knowledge acquisition) is the hardest and costliest step.

Common mistake

Large language models made knowledge representation obsolete.

Language models can produce false facts; they are paired with knowledge graphs for verifiable results.

11.12Summary

  • An expert system writes an expert's knowledge as facts and if-then rules in a knowledge base; the inference engine matches, selects and fires rules.
  • Forward chaining starts from facts and derives every reachable conclusion; conflict resolution (specificity, priority, recency) decides which of the rules matching in the same round fires first.
  • Backward chaining starts from the goal, descends into subgoals and asks only for the facts it needs; the chain of rules is the explanation of the result (the why and how questions).
  • With certainty factors, a rule's conclusion is min(conditions) × rule factor; two rules giving the same conclusion combine as c₁ + c₂(1 − c₁).
  • An ontology writes a domain's classes and relations as triples; subclass inference derives knowledge that was not written. The open world (OWL) and closed world (Prolog, CLIPS) assumptions differ.

11.13Test Yourself

  1. Rules: R1: A ∧ B → C; R2: C → D; R3: D ∧ E → F; R4: B → E. Initial facts A and B. Forward chaining tries the rules in the listed order in every round (a fact added within a round can be used in the same round). Which facts are added in which round, and how many rounds are needed?

    Answer

    Round 1: R1 adds C, R2 adds D, R3 fails because E is missing, R4 adds E. Round 2: R3 adds F. Round 3: no new fact is added, the system stops. The facts added are C, D, E, F; three rounds are needed (the third is only the stopping check).

  2. In the rule base of the text, the goal is "charge battery" and the user answers the first question ("is the engine not cranking?") with "no". How many questions does backward chaining ask, and what is the result?

    Answer

    The goal descends to R4 and its first subgoal "battery flat" to R2; the first condition of R2, "engine not cranking", is asked, and since the answer is no, R2 fails. "Battery flat" cannot be obtained by any other rule, so R4 fails. Only 1 question is asked; the goal cannot be proved.

  3. A hypothesis has two rules: rule a has factor 0.8 and its conditions have confidences 0.7 and 0.9; rule b has factor 0.6 and a single condition with confidence 0.5. What is the combined confidence?

    Answer

    Rule a: min(0.7, 0.9) × 0.8 = 0.7 × 0.8 = 0.56. Rule b: 0.5 × 0.6 = 0.30. Combined: 0.56 + 0.30 × (1 − 0.56) = 0.56 + 0.132 = 0.692.

  4. An ontology has the triples Eagle subClassOf Bird, Bird subClassOf Animal and Zeus type Eagle. Which new triples are found by applying the subclass and type inferences?

    Answer

    Eagle subClassOf Animal (transitivity of subclass). Zeus type Bird and Zeus type Animal (type inference). 3 new triples in all.

  5. Rules: R1: A → X; R2: A ∧ B → Y; R3: B → Z. Facts A and B. What is the firing order under specificity conflict resolution? If every rule fires only once for the fact set, how many firings are there in all?

    Answer

    All three rules match. R2 has two conditions, is the most specific, and fires first. R1 and R3 have one condition each; if the tie is broken by rule order, the order is R2, R1, R3. Since each rule fires once, there are 3 firings in all, and then the system stops.

  6. An ontology states "Ali type Patient" and does not state "Ali type Healthy". How do OWL and Prolog answer the question: is Ali healthy?

    Answer

    OWL, under the open world assumption, says "unknown": a fact that is not written is not taken as false. Prolog, under the closed world assumption, says "no" (cannot be proved). The same knowledge base gives different query results in the two systems; which assumption is needed depends on the application.

11.14After this course

This course is the gateway to symbolic AI. Next you can take Probabilistic Graphical Models to handle uncertainty consistently, Fuzzy Logic for partial truth, or Explainable AI and AI Ethics for explaining decisions and accountability.

Related careers: Knowledge engineer, Ontology specialist, Decision support systems developer, Business rules analyst.

11.15Further reading

  • P. Jackson, Introduction to Expert Systems (3rd ed., 1999): covers rule-based systems, chaining and knowledge acquisition at textbook level.
  • R. J. Brachman and H. J. Levesque, Knowledge Representation and Reasoning (2004): looks at logic, rules, frames and description logics from the point of view of representation and inference.
  • B. G. Buchanan and E. H. Shortliffe (eds.), Rule-Based Expert Systems (1984): lets you read the MYCIN experiments and certainty factors at their source.

11.16References

  1. Jackson, Introduction to Expert Systems, 3rd ed., Addison-Wesley, 1999.
  2. Brachman and Levesque, Knowledge Representation and Reasoning, Morgan Kaufmann, 2004.
  3. Giarratano and Riley, Expert Systems: Principles and Programming, 4th ed., Thomson Course Technology, 2005.
  4. Buchanan and Shortliffe (eds.), Rule-Based Expert Systems: The MYCIN Experiments of the Stanford Heuristic Programming Project, Addison-Wesley, 1984.

Related pages: Slides · Slides (PDF) · Course page