-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.py
89 lines (70 loc) · 1.97 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import argparse
import logging
from decimal import Decimal
from sympy import to_cnf, SympifyError
from belief_base import BeliefBase
PROMPT = ">>> "
def print_help():
print(
f"""Available actions:
r: Belief revision
d: Calculate degree of belief
e: Empty belief base
p: Print belief base
h: Print this help dialog
q: Quit
"""
)
def handle_input(bb):
print('Select action:')
action = input(PROMPT)
action = action.lower()
if action == 'r':
print('--- Revision ---')
print('Enter a formula:')
frm = input(PROMPT)
try:
frm = to_cnf(frm)
print('Select order (real number from 0 to 1):')
order = input(PROMPT)
bb.revise(frm, Decimal(order))
except SympifyError:
print('Invalid formula')
except ValueError:
print('Order has to be a real number from 0 to 1')
print()
elif action == 'd':
print('--- Calculate degree ---')
print('Enter a formula:')
frm = input(PROMPT)
try:
frm = to_cnf(frm)
print(bb.degree(frm))
except SympifyError:
print('Invalid formula')
print()
elif action == 'e':
bb.clear()
print('--- Emptied belief base ---')
print()
elif action == 'p':
print('--- Printing belief base ---')
print(bb)
print()
elif action == 'h':
print_help()
elif action == 'q':
exit()
else:
print('Sorry, the command was not recognized. Type \'h\' for help.')
print()
handle_input(bb)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Belief base revision CLI tool.')
parser.add_argument('--debug', action='store_true', help='enable debugging')
args = parser.parse_args()
if args.debug:
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
bb = BeliefBase()
print_help()
handle_input(bb)