/
logic.py
314 lines (228 loc) · 9.13 KB
/
logic.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python
#
# Copyright 2012 Knowledge Economy Developments Ltd
#
# Henry Gomersall
# heng@kedevelopments.co.uk
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import pyparsing
import qm
import sys
ParseError = pyparsing.ParseException
class BoolOperand(object):
def __init__(self,t):
self.args = t[0][0::2]
def __unicode__(self):
sep = u' %s ' % self.symbol
return u'(' + sep.join([unicode(arg) for arg in self.args]) + u')'
def __str__(self):
return unicode(self).encode(
sys.stdout.encoding or DEFAULT_ENCODING, 'replace')
class BoolAnd(BoolOperand):
symbol = u'.'
def __call__(self, bool_dict):
return_bool = True
for each_arg in self.args:
if each_arg not in bool_dict:
# We assume this must be an BoolOperand
arg_bool = each_arg(bool_dict)
else:
arg_bool = bool(bool_dict[each_arg])
if not arg_bool:
# The AND of the args must be False
return_bool = False
break
return return_bool
class BoolOr(BoolOperand):
symbol = u'+'
def __call__(self, bool_dict):
return_bool = False
for each_arg in self.args:
if each_arg not in bool_dict:
# We assume this must be an BoolOperand
arg_bool = each_arg(bool_dict)
else:
arg_bool = bool(bool_dict[each_arg])
if arg_bool:
# The OR of the args must be True
return_bool = True
break
return return_bool
class BoolXOr(BoolOperand):
symbol = u'\u2295'
def __call__(self, bool_dict):
return_bool = False
for each_arg in self.args:
if each_arg not in bool_dict:
# We assume this must be an BoolOperand
arg_bool = each_arg(bool_dict)
else:
arg_bool = bool(bool_dict[each_arg])
if arg_bool and not return_bool:
# The exclusive case
return_bool = True
elif arg_bool and return_bool:
# More than one input is True
return_bool = False
break
return return_bool
class BoolNot(BoolOperand):
def __init__(self,t):
self.arg = t[0][1]
def __call__(self, bool_dict):
if self.arg not in bool_dict:
# We assume this must be an BoolOperand
arg_bool = self.arg(bool_dict)
else:
arg_bool = bool(bool_dict[self.arg])
return not arg_bool
def __unicode__(self):
return u'~' + str(self.arg)
def parse_boolean_function_string(boolean_string, boolean_variables):
'''Parses the string using the passed iterable of boolean variables
and constructs a callable that implements the boolean function that
the string describes.
The four logical operands that are allowed, in order of precedence,
are:
NOT: 'NOT', '~', '!'
AND: 'AND', '.', '&'
OR: 'OR', '+', '|'
XOR: 'XOR'
The word form of each of the above is not case dependent.
Parantheses can be used to explicitly denote precedence.
The callable takes a dictionary of variables to their logical values
and returns the result of applying the boolean function to those
variables.
>>> func = parse_boolean_function_string('A XOR B', ['A', 'B'])
>>> func({'A':True, 'B':False})
True
>>> func({'A':True, 'B':True})
False
>>> func = parse_boolean_function_string('not (A and B)', ['A', 'B'])
>>> func({'A':False, 'B':False})
True
>>> func({'A':True, 'B':True})
False
>>> func({'A':False, 'B':True})
True
>>> func = parse_boolean_function_string('A.B+~B', ['A', 'B'])
>>> print(func)
((A . B) + ~B)
>>> func({'A':True, 'B':False})
True
>>> func({'A':False, 'B':False})
True
>>> func({'A':False, 'B':True})
False
'''
not_symbol = pyparsing.Or(
[pyparsing.CaselessLiteral(sym) for sym in ('NOT', '~', '!')])
and_symbol = pyparsing.Or(
[pyparsing.CaselessLiteral(sym) for sym in ('AND', '.', '&')])
or_symbol = pyparsing.Or(
[pyparsing.CaselessLiteral(sym) for sym in ('OR', '+', '|')])
xor_symbol = pyparsing.Or(
[pyparsing.CaselessLiteral(sym) for sym in ('XOR',)])
precedence_list = [
(not_symbol, 1, pyparsing.opAssoc.RIGHT, BoolNot),
(and_symbol, 2, pyparsing.opAssoc.LEFT, BoolAnd),
(or_symbol, 2, pyparsing.opAssoc.LEFT, BoolOr),
(xor_symbol, 2, pyparsing.opAssoc.LEFT, BoolXOr),]
bool_tokens = pyparsing.Or(
[pyparsing.Keyword(var) for var in boolean_variables])
bool_parser = pyparsing.operatorPrecedence(
bool_tokens, precedence_list)
func = bool_parser.parseString(boolean_string, parseAll=True)[0]
if type(func) is str:
var_str = func
func = lambda bool_var_dict: bool_var_dict[var_str]
return func
def get_minterms_from_string(boolean_string, boolean_variables):
if boolean_string == '':
return []
bool_func = parse_boolean_function_string(boolean_string,
boolean_variables)
bool_var_dict = dict.fromkeys(boolean_variables, False)
def get_minterms(min_term_list, depth=0, offset=0):
depth_contribution = 2**depth
for val in [False, True]:
if val:
offset += depth_contribution
bool_var_dict[boolean_variables[depth]] = val
if depth+1 == len(boolean_variables):
if bool_func(bool_var_dict):
min_term_list.append(offset)
else:
get_minterms(min_term_list, depth+1, offset)
min_term_list = []
get_minterms(min_term_list)
min_term_list.sort()
return min_term_list
def get_canonical_minimal_sop_from_string(
boolean_string, boolean_variables):
'''Returns the full minimal canonical form for all the variables
passed, given the string, or simply '0' if the string always evaluates
to False.
>>> get_canonical_minimal_sop_from_string('A AND (B OR C)', ['A', 'B', 'C'])
[(3, 4), (5, 2)]
'''
min_terms = get_minterms_from_string(boolean_string, boolean_variables)
_qm = qm.QM(boolean_variables)
minimal_sop = _qm.solve(min_terms, [])
return minimal_sop[1]
def get_minimal_sop_from_string(boolean_string, boolean_variables):
'''Get the minimal sum of products as a list of lists which
correspond to products and sums respectively.
Each entry in the inner list corresponds to a boolean variable
given in the boolean_variables argument, and in the same order.
The value of each entry in the inner list is either True, False
or None, according to whether that variable should be True,
False or "Don't care" in generating a True output.
An empty string will return all "Don't care".
>>> get_minimal_sop_from_string('A AND B', ['A', 'B', 'C'])
[[True, True, None]]
>>> get_minimal_sop_from_string('A OR C', ['A', 'B', 'C'])
[[True, None, None], [None, None, True]]
>>> get_minimal_sop_from_string('', ['A', 'B', 'C'])
[[None, None, None]]
'''
canonical_sop = get_canonical_minimal_sop_from_string(
boolean_string, boolean_variables)
minimal_sop = []
if canonical_sop == '0':
canonical_sop = [(0, -1)]
for each_term in canonical_sop:
and_terms = []
for j in xrange(len(boolean_variables)):
if each_term[0] & 1<<j:
and_terms.append(True)
elif not each_term[1] & 1<<j:
and_terms.append(False)
else:
and_terms.append(None)
minimal_sop.append(and_terms)
return minimal_sop
if __name__ == "__main__":
import doctest
doctest.testmod()