Skip to content

A propositional calculus expert system.

License

Notifications You must be signed in to change notification settings

fedefloris/Expert-system

Repository files navigation

Expert-system - 42born2code

Build Status Python 3.6

Challenge

A propositional calculus expert system.

The goal of this project is to implement a backward-chaining inference engine.

The program can read rules and facts from a given knowledge base contained in a text file. The examples folder contains many samples of possible text files.

The format of the file is explained in Knowledge base.

For more details look at the subject.

Install dependencies

pip install -r requirements.txt

Using the project

Run with no arguments to see all the options:

$> ./expert_system.py
usage: expert_system.py [-h] [-c file] [-v] [-o] file

A propositional calculus expert system.

positional arguments:
  file                  file with rules and facts

optional arguments:
  -h, --help            show this help message and exit
  -c file, --config file
                        file with symbols values
  -v, --verbose         displays investigation steps of the inference engine
  -o, --output          displays original input but prints facts in correct
                        colour

Run with no options to show only the result of the queries:

$> ./expert_system.py examples/and.txt
C is true
F is false

Run with -c and specify a valid config file. The format of the config file is explained in Config File.

$> ./expert_system.py -c test/examples/config/change_op_and test/examples/config/and_op_changed
C is true
F is false

Knowledge base

The knowledge base contains three types of statements:

Anything preceded by # is treated as a comment.

An example of a text file could be:

# if A or B is true, C will be true
A | B => C

# if D and E are true, F will be true
D + E => F  

# List of facts that will be initially true, every other fact will be false by default
=ABDH

# Ask the expert system the state (true/false) of the following facts:
?CF

Rules

A rule statement describes the status of facts based on logical conditions.

A fact can be represented by a single uppercase letter, its status can be either true or false.

The syntax is better described with an example: A + B => C says that the status of C is true if both A and B are true.

To learn more about the rule's syntax, check the files in the examples folder.

Initializations

An initialization statement sets to true a list of specified facts.

Any fact not contained inside this kind of statement is false by default.

The syntax is =list_of_facts. For example, =ABC will initialize A, B and C to true.

Queries

A query statement asks the inference engine the status (true or false) of specified facts.

The syntax is ?list_of_facts. For example, ?DE will ask the status of D and E.

Config file

The config file allows the changing of symbols values inside text files.

The syntax is set symbol_identifier = "new_character".

Anything preceded by # is treated as a comment.

For example, here we're setting the and symbol to &:

set op_and = "&"

Running the tests

pytest -v

License

This project is licensed under the MIT License - see the LICENSE.md file for details