-
Notifications
You must be signed in to change notification settings - Fork 0
/
expert.py
executable file
·123 lines (101 loc) · 3.07 KB
/
expert.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/local/bin/python3
import copy
import sys
import re
from KB_fact import *
KB_Tree = {}
FactBase = {}
def line_parser(file):
taskList = []
try:
for i, line in enumerate(file):
line = line.split('#')[0]
line = line.replace(' ', '').replace('\t', '').replace('\n', '')
newKB = list()
if (line == ''):
continue
elif (line.startswith('=')):
line = line.replace('=', '')
if (line.find(',') != -1):
line = list(filter(None, line.split(',')))
for elem in line:
FactBase[elem] = True
elif (line.find('?') != -1):
line = line.replace('?', '')
if (line.find(',') != -1):
line = list(filter(None, line.split(',')))
taskList = line
else:
newKB = KB_fact(line, FactBase, KB_Tree)
names = []
if (newKB.name.find('|') != -1 or newKB.name.find('^') != -1 or newKB.name.find('!') != -1):
raise Exception('Inclution has non-supported logical operator!')
if (newKB.name.find('+') != -1):
names = newKB.name.split('+')
else:
names = [newKB.name]
for n in names:
kb_copy = copy.deepcopy(newKB)
kb_copy.name = n
kb_copy.tree = KB_Tree
kb_copy.factBase = FactBase
if (n in KB_Tree):
KB_Tree[n].append(kb_copy)
else:
KB_Tree[n] = [kb_copy]
except Exception as err:
raise Exception(str(err) + ". At line #" + str(i + 1))
return taskList
def resolveTask(taskList):
for task in taskList:
if task in FactBase:
print("\033[1m\033[32m", task, " - ", True, "\033[0m", sep='')
elif task in KB_Tree:
equition = None
res = False
solve = None
for eq in KB_Tree[task]:
if (eq.implication == False):
if (equition == None):
equition = eq.resolve()
elif (equition != eq.resolve()):
raise Exception('Unresolved equtions in cause of different results of logical equality in var \'' + eq.name + '\'')
else:
solve = eq.resolve()
if solve:
res = solve
break
if (res != equition and equition != None):
raise Exception('Unresolved equtions in cause of different results of logical equality in var \'' + eq.name + '\'')
print("\033[1m\033[32m", task, ' - ', res, "\033[0m", sep='')
else:
print("\033[1m\033[32m", task, ' - ', False, "\033[0m", sep='')
if __name__ == "__main__":
taskList = str()
arg = str()
if (len(sys.argv) < 2):
print("Пиздец, ты серьезно??? Ты че, охуел не подавать мне файл на чтение?!")
exit()
try:
file = open(sys.argv[1], "r")
taskList = line_parser(file)
resolveTask(taskList)
except Exception as err:
print("\033[1m\033[41mError:", err, '\033[0m')
while True:
try:
arg = input(">> ")
if (arg.startswith('remove base')):
KB_Tree = {}
elif (arg.startswith('remove facts')):
FactBase = {}
else:
if (arg.startswith('open ')):
arg = open(arg.replace('open ', ''), 'r')
taskList = line_parser([arg] if (type(arg) is str) else arg)
if (taskList != []):
resolveTask(taskList)
except KeyboardInterrupt:
exit()
except Exception as err:
print("\033[1m\033[41mError:", err, '\033[0m')