-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
151 lines (129 loc) · 4.52 KB
/
manager.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from node import Node
from wffparser import Parser
from syntax import Syntax
from evaluator import Evaluator
class Manager():
config = {}
eval_config = {}
def __init__(self, input_phrase:str):
self.input_phrase = input_phrase
self.root = Node()
self.load_config()
self.syntax = Syntax(self.config)
self.parser = Parser(input_phrase, self.config)
self.evaluator = Evaluator(self.eval_config, self.config)
self.output = ""
def update_evaluator(self, config:dict):
for k in config:
v = config[k]
if(v == "True"):
config[k] = True
elif(v == "False"):
config[k] = False
else:
from utils import fail
fail("config", (k,v))
self.evaluator.eval_config = config
def load_config(self):
with open("config.json") as f:
from json import load
self.config = load(f)
def parse(self):
self.syntax.validate(self.input_phrase)
self.parser.parse()
self.syntax.set_root(self.parser.root)
self.root = self.parser.root
out = "Parser: ✔️"
self.output += out + "\n"
print(out)
def validate(self):
ret = self.syntax.validate(self.input_phrase)
self.evaluator.eval_config = {i : False for i in self.syntax.atom_list}
out = "Symbols: ✔️"
self.output += out + "\n"
print(out)
return ret
def check_syntax(self):
if(self.syntax.root == None):
self.parse()
rez = self.syntax.check_syntax()
out = "Syntax: ✔️"
self.output += out + "\n"
print(out)
return rez
def reconstruct(self,):
if(self.syntax.root == None):
self.parse()
return self.syntax.reconstruct()
def evaluate(self, standalone = False):
if(self.syntax.root == None):
self.parse()
if(self.evaluator.was_set == False):
self.evaluator.setValues(self.syntax.root)
rez = self.evaluator.evaluate(self.syntax.root)
if standalone == True:
self.validate()
self.check_syntax()
from prettytable import PrettyTable
self.reconstruct()
table = PrettyTable([k for k in self.evaluator.eval_config.keys()] + self.syntax.operations)
table.add_row([k for k in self.evaluator.eval_config.values()] + self.evaluator.results)
out = "Evaluation: ✔️" +"\n"
out += str(table)
self.output += out
print(out)
return rez
def evaluate_all_interpretations(self, pr = True):
if(len(self.evaluator.eval_config) == 0):
self.validate()
p = 2 ** len(self.evaluator.eval_config.keys()) - 1 # number of bits we need
lbits = len(bin(p).split("b")[1])
from prettytable import PrettyTable
self.reconstruct()
self.check_syntax()
rez = PrettyTable([k for k in self.evaluator.eval_config.keys()] + self.syntax.operations)
while p != -1:
bits = [int(c) for c in bin(p).split("b")[1]]
while( len(bits) < lbits):
bits.insert(0, 0)
i = 0
l = []
for k in self.evaluator.eval_config.keys():
self.evaluator.eval_config[k] = bool(bits[i])
l.append(bool(bits[i]))
i += 1
self.evaluator.was_set = False
self.evaluate()
rez.add_row(l + self.evaluator.results)
self.evaluator.results = []
p -= 1
out = "Evaluation: ✔️" +"\n"
if pr:
out += str(rez)
self.output += out
print(out)
return rez
def sat_not_sat(self, pr= False):
rez = self.evaluate_all_interpretations(pr)
r = []
for row in rez._rows:
r.append(row[-1]) # get the last item
ok = False
out = "Is "
if(all(r)):
out += "Valid"
else:
for i in r:
if i == True:
ok = True
if ok:
out += "Satisfiable"
else:
out += "Not satisfiable"
print(out)
self.output += out
def print_tree(self):
if(self.syntax.root == None):
self.parse()
from pptree import print_tree
print_tree(self.syntax.root, childattr='childs', nameattr='info')