-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolishToClingo.py
executable file
·261 lines (193 loc) · 6.29 KB
/
polishToClingo.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import sys
PAIR = {"&","|",">","=","%"} # Constante, estas operaciones son BINARIAS
class Node:
def __init__(self, item):
self.item = item
self.left = None
self.right = None
# El recorrido en preorden devuelve la expresión en notación polaca
def preorden(node):
nodeList = []
if node is not None:
nodeList = nodeList + preorden(node.left)
nodeList.insert(0, node.item)
nodeList = nodeList + preorden(node.right)
return nodeList
def firstStep(node):
# replace a <-> b by (a /\ b) \/ (¬a /\ ¬b)
if node.item == "=": # damos por hecho que left y right not null
node.item = "|"
# Conjunción en la rama izquierda
aux_left = Node("&")
aux_left.left = node.left
aux_left.right = node.right
# Conjunción de negaciones en la rama derecha
aux_right = Node("&")
aux_right.left = Node("-")
aux_right.left.left = node.left
aux_right.right = Node("-")
aux_right.right.left = node.right
node.left = aux_left
node.right = aux_right
# replace a -> b by ¬a \/ b
elif node.item == ">":
node.item = "|"
aux = Node("-") # nodo auxiliar para añadir la negación
aux.left = node.left
node.left = aux
# replace a xor b by (a /\ ¬b) \/ (¬a /\ b)
elif node.item == "%":
node.item = "|"
# Conjunción en la rama izquierda
aux_left = Node("&")
aux_left.left = node.left # a
aux_left.right = Node("-")
aux_left.right.left = node.right # ¬b
# Conjunción de la rama derecha
aux_right = Node("&")
aux_right.left = Node("-")
aux_right.left.left = node.left
aux_right.right = node.right
node.left = aux_left
node.right = aux_right
elif node.item == "1":
node.item = "#true"
elif node.item == "0":
node.item = "#false"
if node.left is not None:
firstStep(node.left) # no importa el orden
if node.right is not None:
firstStep(node.right)
return node
def isNNF(node):
if node.item == "-" and (node.left.item == "|" or node.left.item == "&" or node.left.item == "-"):
return False
aux = True
if node.left is not None:
aux = aux and isNNF(node.left) # no importa el orden
if node.right is not None:
aux = aux and isNNF(node.right)
return aux
def toNNF(node):
if node.item == "-":
if node.left.item == "|" or node.left.item == "&": # DeMorgan
#node = node.left
if node.left.item == "|":
node.item = "&"
else:
node.item = "|"
# Negar hijo izquierdo
aux_left = Node("-")
aux_left.left = node.left.left
# Negar hijo derecho
aux_right = Node("-")
aux_right.left = node.left.right
node.left = aux_left
node.right = aux_right
elif node.left.item == "-": # ¬(¬a) = a (las que se creen con DeMorgan se deshacen aquí)
node = node.left.left
if node.left is not None:
node.left = toNNF(node.left) # no importa el orden
if node.right is not None:
node.right = toNNF(node.right)
return node
def cnf(node):
if node.item == "&":
aux_left = cnf(node.left)
aux_right = cnf(node.right)
aux_left.extend(aux_right) # se pegan las listas
return aux_left
elif node.item == "|":
aux_left = cnf(node.left)
aux_right = cnf(node.right)
l = []
for i in aux_left:
for j in aux_right:
aux = []
aux.extend(i)
aux.extend(j)
l.append(aux)
return l
elif node.item == "-":
return [["-" + node.left.item]]
else:
return [[node.item]]
def toClingo(l):
words = set()
result = ""
for x in l:
if x != "&":
aux = ":- "
for y in x:
if y.startswith("-"):
aux = aux + y[1:] + ", "
words.add(y[1:])
else:
aux = aux + "not " + y + ", "
words.add(y)
result = result + aux[:len(aux)-2] + ".\n" # quitamos la última ,
return (words,result + "\n")
def to_tree(words):
if len(words) == 1:
return Node(words[0])
word = words[0]
rest = words[1:]
if word in PAIR:
node = Node(word)
n = 1 # Para controlar que pertenece a la rama izquierda
aux = 0 # Offset para ver donde termina la rama izquierda
for var in rest:
aux = aux + 1
if var in PAIR:
n = n + 1
else:
if var == "-":
n = n
else:
n = n - 1
if n == 0:
break
node.left = to_tree(rest[:aux])
node.right = to_tree(rest[aux:])
return node
else:
node = Node(word)
node.left = to_tree(rest)
return node
def reductionToCNF(expresion):
word_splited = expresion.split()
tree = to_tree(word_splited[:len(word_splited)-1]) # quitamos el punto y construimos arbol
tree = firstStep(tree)
while not isNNF(tree):
tree = toNNF(tree)
l = cnf(tree)
return l
def main():
if len(sys.argv) != 2:
print("Usage: polishToClingo <input file>")
exit()
filaname = sys.argv[1]
f = open(filaname, "r")
words = set()
ins = ""
# inicia bucle infinito para leer línea a línea
while True:
# lee línea
linea = f.readline()
if not linea:
break # Si no hay más se rompe bucle
l = reductionToCNF(linea) # obtenemos la lista de listas
words_aux, result = toClingo(l)
words = words | words_aux
ins = ins + "% " + linea + "\n" + result
header = "{"
for x in words:
header = header + x + ";"
header = header[:len(header)-1] + "}.\n"
f.close() # Cierra archivo
filaname = filaname.split(".")
f = open(filaname[0] + ".lp" , "w")
f.write(header)
f.write(ins)
f.close()
main()