-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
63 lines (50 loc) 路 2.21 KB
/
main.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
import sys
from expert_system import Prompt, Tree, Print
from expert_system.parser.Parser import ESParser
from expert_system.config.Env import Env
from expert_system.config.Cmd import Cmd
from expert_system.util.Color import Color
def resolve_lines(parser):
tree = Tree.NPITree(parser.structured_rules, parser.facts, parser.queries)
results = {}
for query in parser.queries:
results[query] = tree.resolve_query(query)
color = Color.GREEN if results[query] is True else Color.FAIL
print(f"{ query } resolved as { color }{ results[query] }{ Color.END }")
return results
def save_history(results):
exp_sys = Print.ESPrinter(parser.structured_rules, parser.facts, parser.queries).create_array_rules_facts_queries()
with open(Env.LOG_PATH, 'a') as f:
for query, val in results.items():
f.write(query + '=' + str(val) + ',')
f.write('\n')
for x in exp_sys:
f.write(x + '\n')
f.write(';' + '\n')
Print.ESPrinter(parser.structured_rules, parser.facts, parser.queries).parser_file_history()
if __name__ == "__main__":
args = Cmd.args
try:
with open(args.input) as f:
lines = f.readlines()
if args.mode == "interactive":
Prompt.ESPrompt(lines).cmdloop()
else:
parser = ESParser(lines)
if args and args.graph:
print(f"{Color.PURPLE}[Tree representation 馃尦]{Color.END}")
Print.ESPrinter(parser.structured_rules, parser.facts, parser.queries).display_tree_in_shell()
print("")
if args and args.rules:
print(f"{Color.PURPLE}[List of rules 馃摎]{Color.END} ")
Print.ESPrinter(parser.structured_rules, parser.facts, parser.queries).display_rules()
print("")
res = resolve_lines(parser)
if args and args.image:
Print.ESPrinter(parser.structured_rules, parser.facts, parser.queries).create_image()
print(f"{Color.PURPLE}Image { Env.IMG_PATH } create{Color.END} ")
if args.history:
save_history(res)
except (Exception, BaseException) as e:
print(e)
sys.exit(1)