-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverifyTree.py
More file actions
214 lines (193 loc) · 7.39 KB
/
Copy pathverifyTree.py
File metadata and controls
214 lines (193 loc) · 7.39 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from sympy import *
from sympy.logic.inference import *
import sys
CONVERSIONS = [ (' ^ ', ' & '), (' -> ', ' >> '), (' --> ', ' >> '),
(' v ', ' | '), (' <- ', ' << '), (' <-- ', ' << '),
(' ! ', ' ~ '), (' $ ', ' >> '), (' % ', ' <--> ') ]
KEYS = [ 'Identifier', 'Statement', 'Rule', 'Parent', 'Children' ]
SPLIT_LINE = '\n'
SPLIT_VALUE = ';'
SPLIT_LIST = ' '
ROOT_NAME = '1'
CLOSED_SYMBOL = 'x'
OPEN_SYMBOL = 'o'
def createTree(text):
'''
Creates a recursive dictionary to represent a truth tree using the
global variables KEYS, SPLIT_LINE, SPLIT_VALUE, and SPLIT_LIST to
parse the text input
'''
Nodes = {}
lines = text.strip(SPLIT_LINE).split(SPLIT_LINE)
for line in lines:
values = line.split(SPLIT_VALUE)
node = dict(zip(KEYS, values))
print(line)
node['Children'] = node['Children'].strip().split(SPLIT_LIST)
#node['Rule'] = node['Rule'].split(SPLIT_LIST)
rule = node['Rule'].strip().split(SPLIT_LIST)
print(rule)
node['Rule'] = {'RuleName': rule[0], 'Variables': rule[1:]}
Nodes[node['Identifier']] = node
return Nodes
def createLogicStatement(string):
'''
Creates a logic statement from a given string using the global variable
CONVERSIONS to convert to a sympy acceptable format.
'''
for old, new in CONVERSIONS:
string = string.replace(old, new)
return string.lower().strip()
def createConjunction(list_strings):
'''
Conjuncts a list of strings into a single string
'''
return '(' + ') & ('.join(list_strings) + ')'
def createDisjunction(list_strings):
'''
Disjuncts a list of strings into a single string
'''
return '(' + ') | ('.join(list_strings) + ')'
def checkDecomp(parent, children):
'''
Returns True if the parent statement is logically equivalent to the
conjunction of the children statements. Raises an exception error if a
statement is not correctly formated
'''
try:
parent = to_cnf(createLogicStatement(parent))
children = to_cnf(createLogicStatement(createConjunction(children)))
return Equivalent(parent, children) == True
except:
raise Exception("Could not understand statement")
return False
def checkBranch(parent, children):
'''
Returns True if the parent statement is logically equivalent to the
disjuntion of the children statements. Raises an exception error if a
statement is not correctly formated
'''
try:
parent = to_cnf(createLogicStatement(parent))
children = to_cnf(createLogicStatement(createDisjunction(children)))
return Equivalent(parent, children) == True
except:
raise Exception("Could not understand statement")
return False
def checkContradiction(statements):
'''
Returns True if the conjuction of statements is a contradiction.
'''
try:
conjunction = to_cnf(createConjunction(statements))
return Equivalent(conjunction, False) == True
except:
raise Exception("Could not understand statement")
return False
def validateDecomp(Nodes, parent, children):
'''
Returns True if decomposition of parent node into children nodes
was valid.
'''
parent_statement = Nodes[parent]["Statement"]
children_statements = [Nodes[s]['Statement'] for s in children ]
return checkDecomp(parent_statement, children_statements)
def validateBranch(Nodes, parent, children):
'''
Returns True if branch of parent statement into child staments is valid
and all child statements in branch have same parent
'''
parent_statement = Nodes[parent]["Statement"]
children_statements = [Nodes[s]['Statement'] for s in children ]
valid_statement = checkBranch(parent_statement, children_statements)
parents = [ Nodes[n]['Rule']['Variables'] for n in children ]
parents = set(parents)
return valid_statement and len(parents)==1
def validateClosed(Nodes, parents, child):
'''
Returns True if conjunction of parents is a contradiction and child statement
is the contradiction symbol
'''
parent_statements = [Nodes[s]['Statement'] for s in parents ]
valid_statement = checkContradiction(parent_statements)
return (valid_statement) and (Nodes[child]['Statement'] == CLOSED_SYMBOL)
def validateOpen(Nodes, current, statements):
'''
Returns True if every statement is a literal or a negation of a literal
'''
ans = [ is_literal(s) for s in statements ]
return all(ans) and (Nodes[current]['Statement'] == OPEN_SYMBOL)
def validateTruthTree(Nodes, root=ROOT_NAME, statements=[]):
'''
Returns True if the truth tree is fully decomposed and every rule passed.
Performs a DFS on Nodes starting at root, where statements is the set of
unprocessed statements.
'''
rule = Nodes[root]['Rule']['RuleName']
print(Nodes[root]['Rule'])
if rule == 'Premise':
new_statements = statements + list(Nodes[root]['Statement'])
for child in Nodes[root]['Children']:
ans = validateTruthTree(Nodes, root=child, statements=new_statements)
if (ans == False):
return False
return True
elif rule == 'Branch':
new_statements = statements + list(Nodes[root]['Statement'])
parent_rule_node = Nodes[root]['Rule']['Variables'][0]
parent_node = Nodes[root]['Parent']
branch_nodes = Nodes[parent_node.strip()]['Children']
if (validateBranch(Nodes, parent_rule_node, branch_nodes) == False):
return False
for child in Nodes[root]['Children']:
ans = validateTruthTree(Nodes, root=child, statements=new_statements)
if (ans == False):
return False
return True
elif rule == 'Decomp':
parent_rule_node = Nodes[root]['Rule']['Variables'][0]
decomp_rule = Nodes[root]['Rule']
decomp_nodes = [root]
new_statements = statements + list(Nodes[root]['Statement'])
while (len(Nodes[root]['Children']) == 1) and \
(Nodes[root]['Rule'] == decomp_rule):
root = Nodes[root]['Children'][0]
decomp_nodes.append(root)
new_statements += list(Nodes[root]['Statement'])
if (validateDecomp(Nodes, parent_rule_node, decomp_nodes) == False):
return False
for child in Nodes[root]['Children']:
ans = validateTruthTree(Nodes, root=child, statements=new_statements)
if (ans == False):
return False
return True
elif rule == 'Closed':
parent_rule_nodes = Nodes[root]['Rule']['Variables']
return validateClosed(Nodes, parent_rule_nodes, root) == False
elif rule == 'Open':
return validateOpen(Nodes, root, statements)
else:
raise Exception("Could not interpret rule")
return False
def validateFromFile(text):
'''
Returns true if the truth tree associated with the text is valid
'''
Nodes = createTree(text)
return validateTruthTree(Nodes)
if __name__ == "__main__":
'''
f = open('Tree1.txt')
f = f.read()
print(validateFromFile(f))
f = open('Tree2.txt')
f = f.read()
print(validateFromFile(f))
'''
print(sys.argv)
f = open(sys.argv[1])
f = f.read()
if not validateFromFile(f):
print(False)
sys.exit(1)
print(True)