https://github.com/thk2b/expert_system
https://github.com/thk2b/expert_system
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/thk2b/expert_system
- Owner: thk2b
- Created: 2018-11-21T23:27:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-08T22:24:39.000Z (about 7 years ago)
- Last Synced: 2025-08-20T22:54:40.219Z (10 months ago)
- Language: Python
- Size: 61.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# expert system
A propositional logic engine
## Usage
```
$ ./expert_system -h
usage: expert_system [-h] [--verbose] [filename [filename ...]]
positional arguments:
filename file to execute (default: stdin)
optional arguments:
-h, --help show this help message and exit
--verbose, -v verbose mode-
```
Alterntively, run with `$ python3 expert_system`
Run unit tests with `$ python3 test.py`
## Examples
```
$ ./expert_system -v
e> a => b # if a then b
e> =a # a is true
e> ?b # what about b
b is true because a is true
e> = # nothing is true
e> ?b
b is false
```
```
A => B + C
B => D | E
C => ! E
=A
?D # true
```
## Syntax
The parser implements the following grammar:
```
Grammar
Sessions = Session Sessions
| EOF
Session = Rules Statements Queries
Rules = Rule Rules
| NULL
Statements = Statement Statements
| NULL
Queries = Query Queries
| NULL
Rule = Expression ENTAILS Expression
Statement = ASSERT AtomList
Query = QUERY ExpressionList
Expr = XorExpr
XorExpr = Expr XOR Expr
| OrExpr
OrExpr = Expr OR Expr
| AndExpr
AndExpr = Expr AND Expr
| AtomExpr
NotExpr = NOT AtomExpr
| AtomExpr
AtomExpr = LPAREN Expr RPAREN
| Atom
Atom =
ExpressionList | Expression LIST_SEPARATOR ExpressionList
| AtomList
| NULL
AtomList = CompactAtomList
| SeparatedAtomList
CompactAtomList = CompactAtomList
| NULL
SeparatedAtomList = Atom LIST_SEPARATOR SeparatedAtomList
| NULL
terminals = {
"NEWLINE": "\n",
"ENTAILS": "=>",
"ASSERT": "=",
"QUERY": "?",
"AND": "+",
"OR": "|",
"XOR": "^",
"NOT": "!",
"LIST_SEPARATOR": ",",
"LPAREN": "(",
"RPAREN": ")",
}
```
## Algorithm and API
A backward-chaining algorithm (with caching) is used to respond to queries.
Inputed rules are transformed into a data-flow graph, and queries are lazily-executed.