-
Notifications
You must be signed in to change notification settings - Fork 1
/
truthtable.py
558 lines (469 loc) · 20.4 KB
/
truthtable.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import wx
from wx import grid
from threading import Thread, Lock
import random
##include <cstdlib>
##include <ctime>
##include <math.h>
##include "truthtable.h"
##DEFINE_EVENT_TYPE(wxEVT_VALUE_CHANGE)
##
##BEGIN_EVENT_TABLE( TruthTable, wxGrid )
## EVT_GRID_CELL_RIGHT_CLICK( TruthTable::DisplayPopup )
## EVT_GRID_CELL_CHANGE(TruthTable::OnCellChange)
## EVT_MENU(MENU_SET1, TruthTable::OnMenuSet1)
## EVT_MENU(MENU_SET0, TruthTable::OnMenuSet0)
## EVT_MENU(MENU_SETDC, TruthTable::OnMenuSetDC)
## EVT_MENU(MENU_SETRAND, TruthTable::OnMenuSetRand)
##END_EVENT_TABLE()
[
MENU_SETDEC,
MENU_SET1,
MENU_SET0,
MENU_SETDC,
MENU_SETRAND
] = [wx.NewId() for i in range(5)]
class UpdateThread(Thread):
def __init__(self, parent, vars, outs, range_i):
Thread.__init__(self)
self.parent = parent
self.outs = outs
self.vars = vars
self.range_i = range_i
self.index = 0
def completion(self):
return int(100.0*(self.index-min(self.range_i))/len(self.range_i))
def run(self):
for i in self.range_i:
self.index = i
if self.outs >= 2:
self.parent.UpdateDCRow( i)
for j in range(0, self.vars):
self.parent.SetReadOnly(i, j, 1)
if(not(i%2)):
self.parent.SetCellBackgroundColour(i, j, wx.Colour(245, 245, 245))
else:
self.parent.SetCellBackgroundColour(i, j, wx.Colour(235, 235, 235))
if((i%(pow(2, self.vars-j)))<(pow(2, self.vars-j-1))):
self.parent.SetCellValue(i, j, ("0"))
else :
self.parent.SetCellValue(i, j, ("1"))
for j in range(0, self.outs):
if (j%2 ==0):
self.parent.SetCellBackgroundColour(i, self.vars+j, wx.Colour(215, 225, 255))
else:
self.parent.SetCellBackgroundColour(i, self.vars+j, wx.Colour(255, 255, 255))
class TruthTable(grid.Grid):
def __init__(self, parent, id=wx.NewId(), vars=4, outs=3, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name=""):
grid.Grid.__init__(self, parent, id, pos, size, style, name)
self.Lock = Lock()
self.showZeros=1
self.CreateGrid(pow(2, vars), vars+1)
self.numberOfVariables=vars
self.SetOuts(outs)
self.SetVars(vars)
self.SetSelectionMode(grid.Grid.wxGridSelectRows)
self.EnableDragGridSize(0)
self.Bind(grid.EVT_GRID_CELL_CHANGE, self.OnCellChange)
self.Bind(grid.EVT_GRID_CELL_RIGHT_CLICK, self.DisplayPopup )
self.Bind(grid.EVT_GRID_LABEL_LEFT_CLICK, self.OnLabelColClick)
self.stateMenu = wx.Menu()
self.stateMenu.Append(MENU_SETDEC, ("Set Decimale value"))
self.stateMenu.Append(MENU_SET1, ("Set to 1"))
self.stateMenu.Append(MENU_SET0, ("Set to 0"))
self.stateMenu.Append(MENU_SETDC, ("Set to \"don't care\""))
self.stateMenu.AppendSeparator()
self.stateMenu.Append(MENU_SETRAND, ("Set randomly"))
self.Bind(wx.EVT_MENU, self.OnMenuSetValue, id=MENU_SETDEC)
self.Bind(wx.EVT_MENU, self.OnMenuSet1, id=MENU_SET1)
self.Bind(wx.EVT_MENU, self.OnMenuSet0, id=MENU_SET0)
self.Bind(wx.EVT_MENU, self.OnMenuSetDC, id=MENU_SETDC)
self.Bind(wx.EVT_MENU, self.OnMenuSetRand, id=MENU_SETRAND)
self.SetDefaultCellAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
def SetCellValue(self, i, j, val):
self.Lock.acquire()
grid.Grid.SetCellValue(self, i, j, val)
self.Lock.release()
def SetReadOnly(self, i, j, Bool):
self.Lock.acquire()
grid.Grid.SetReadOnly(self, i, j, Bool)
self.Lock.release()
def SetCellBackgroundColour(self, i, j, color):
self.Lock.acquire()
grid.Grid.SetCellBackgroundColour(self, i, j, color)
self.Lock.release()
def DisplayPopup( self, event):
if(self.IsSelection() ): # && self.GetSelectedRows().Count()>1) <--- library bug
self.popup=event.GetPosition()
self.PopupMenu(self.stateMenu, event.GetPosition())
else:
menuRow=event.GetRow()
menuCol=event.GetCol()
self.SetGridCursor(menuRow, menuCol)
self.ClearSelection()
self.SelectRow(menuRow)
self.popup=event.GetPosition()
self.PopupMenu(self.stateMenu, event.GetPosition())
def OnCellChange( self, event=None, Row=0, Col=0):
if event:
Row = event.GetRow()
Col = event.GetCol()
if Col == self.numberOfVariables+self.numberOfOutputs:
# DEC column
current_dec_value = 0
for col in range( Col-1, self.numberOfVariables-1, -1):
if self.GetCellValue(Row, col) == '1':
current_dec_value += pow(2, (Col-1) -col)
if self.GetCellValue(Row, col) == '?':
current_dec_value =None
break
val = self.GetCellValue(Row, Col)
if current_dec_value != None and val !='?':
try:
val = int(self.GetCellValue(Row, Col))
except:
self.SetCellValue(Row, Col, str(current_dec_value))
val = current_dec_value
# Test limit values
if val < pow(2, self.numberOfOutputs) and val >=0:
# Do we need change
if (val != current_dec_value):
val_bin = val
for col in range( Col-1, self.numberOfVariables-1, -1):
if val_bin % 2 :
self.SetCellValueEvent(Row, col, '1')
else:
self.SetCellValueEvent(Row, col, '0')
val_bin = val_bin /2
else:
self.SetCellValue(Row, Col, str(current_dec_value))
else:
for col in range( Col-1, self.numberOfVariables-1, -1):
self.SetCellValueEvent(Row, col, '?')
else:
if( (self.GetCellValue(Row, Col)!= ("1")) &
(self.GetCellValue(Row, Col)!= ("")) &
(self.GetCellValue(Row, Col)!= ("0")) ):
self.SetCellValue(Row, Col, ("?"))
elif(self.GetCellValue(Row, Col)== ("")) & Col>self.numberOfVariables:
if(self.showZeros):
self.SetCellValue(Row, Col, ("0"))
elif(self.GetCellValue(Row, Col)== ("0"))& Col>self.numberOfVariables:
if(not self.showZeros):
self.SetCellValue(Row, Col, (""))
# Update DC column for row
self.UpdateDCRow( Row)
if event:
event.Skip(1)
def OnMenuSetValue( self, event):
outs = self.numberOfOutputs
if outs > 2:
#ColLabel = self.GetColLabelValue(Col)
dlg = wx.TextEntryDialog(
self, 'Set Decimale Value',
'', '')
#dlg.SetValue(ColLabel)
if dlg.ShowModal() == wx.ID_OK:
for i in range(0,(pow(2, self.numberOfVariables)) ):
if(self.IsInSelection(i, 0)) :
self.SetCellValueEvent(i, self.GetNumberCols()-1, str(dlg.GetValue()))
dlg.Destroy()
def OnMenuSet1( self, event):
for i in range(0,(pow(2, self.numberOfVariables)) ):
if(self.IsInSelection(i, 0)) :
for j in range(0, self.numberOfOutputs):
self.SetCellValueEvent(i, self.numberOfVariables+j, ("1"))
#evt = wx.grid.GridEvent(self.GetId(), wx.grid.wxEVT_GRID_CELL_CHANGE, self, i, self.numberOfVariables+j)
#self.GetEventHandler().ProcessEvent(evt)
#self.OnCellChange(None, i, self.numberOfVariables)
def OnMenuSet0( self, event):
for i in range(0,(pow(2, self.numberOfVariables)) ):
if(self.IsInSelection(i, 0)) :
for j in range(0, self.numberOfOutputs):
if(self.showZeros):
self.SetCellValueEvent(i, self.numberOfVariables+j, ("0"))
else:
self.SetCellValueEvent(i, self.numberOfVariables+j, (""))
#evt = wx.grid.GridEvent(self.GetId(), wx.grid.wxEVT_GRID_CELL_CHANGE, self, i, self.numberOfVariables+j)
#self.GetEventHandler().ProcessEvent(evt)
#self.OnCellChange(None, i, self.numberOfVariables)
def OnMenuSetDC( self, event):
for i in range(0,(pow(2, self.numberOfVariables)) ):
if(self.IsInSelection(i, 0)) :
for j in range(0, self.numberOfOutputs):
self.SetCellValueEvent(i, self.numberOfVariables+j, ("?"))
#evt = wx.grid.GridEvent(self.GetId(), wx.grid.wxEVT_GRID_CELL_CHANGE, self, i, self.numberOfVariables+j)
#self.GetEventHandler().ProcessEvent(evt)
#self.OnCellChange(None, i, self.numberOfVariables)
def OnMenuSetRand( self, event):
for i in range(0,(pow(2, self.numberOfVariables)) ):
if(self.IsInSelection(i, 0)) :
for j in range(0, self.numberOfOutputs):
if(random.random()>=0.5):
self.SetCellValueEvent(i, self.numberOfVariables+j, ("1"))
#self.OnCellChange(None, i, self.numberOfVariables)
#evt = wx.grid.GridEvent(self.GetId(), wx.grid.wxEVT_GRID_CELL_CHANGE, self, i, self.numberOfVariables+j)
#self.GetEventHandler().ProcessEvent(evt)
else:
if(self.showZeros):
self.SetCellValueEvent(i, self.numberOfVariables+j, '0')
else:
self.SetCellValueEvent(i, self.numberOfVariables+j, '')
#evt = wx.grid.GridEvent(self.GetId(), wx.grid.wxEVT_GRID_CELL_CHANGE, self, i, self.numberOfVariables+j)
#self.GetEventHandler().ProcessEvent(evt)
#self.OnCellChange(None, i, self.numberOfVariables)
def OnLabelColClick(self, event):
Row = event.GetRow()
Col = event.GetCol()
if Col > -1:
ColLabel = self.GetColLabelValue(Col)
dlg = wx.TextEntryDialog(
self, 'Change column name',
'', '')
dlg.SetValue(ColLabel)
if dlg.ShowModal() == wx.ID_OK:
self.SetColLabelValue(Col, dlg.GetValue())
self.SetColLabelSizefromLabel(Col)
#evt = wx.grid.GridEvent(self.GetId(), wx.grid.wxEVT_GRID_LABEL_RIGHT_CLICK, self, Row, Col)
#self.GetEventHandler().ProcessEvent(evt)
dlg.Destroy()
event.Skip(1)
def UpdateDCRow(self, Row):
# Update DC column
DC_Col = self.numberOfVariables+self.numberOfOutputs
if(self.GetNumberCols()<=DC_Col):
return
current_dec_value = 0
for col in range( DC_Col-1, self.numberOfVariables-1, -1):
if self.GetCellValue(Row, col) == '1':
current_dec_value += pow(2, (DC_Col-1) -col)
self.SetCellValue(Row, DC_Col, str(current_dec_value))
def SetCellValueEvent(self, row, col, value):
self.SetCellValue(row, col, value)
try:
EVT_GRID_CELL_CHANGE = wx.grid.wxEVT_GRID_CELL_CHANGE
except:
EVT_GRID_CELL_CHANGE = wx.grid.wxEVT_GRID_CELL_CHANGED
evt = wx.grid.GridEvent(self.GetId(),EVT_GRID_CELL_CHANGE, self, row, col)
self.GetEventHandler().ProcessEvent(evt)
def SetOuts(self, outs, UpdateGrid = True):
self.numberOfOutputs = outs
vars = self.numberOfVariables
if(self.GetNumberCols()<(vars+outs+1)):
if outs >= 2:
# Delete DEC col
self.DeleteCols(self.GetNumberCols()-1, 1)
InserNbCol = vars+outs-self.GetNumberCols()
self.InsertCols(vars,InserNbCol)
for i in range(0, InserNbCol):
self.SetColLabelValue(vars+i, 'f%d' % (outs-1-i))
if(self.GetNumberCols()>(vars+outs)):
if outs >= 1:
# Delete DEC col
self.DeleteCols(self.GetNumberCols()-1, 1)
ColLabel = []
for Col in range(self.GetNumberCols()-outs, (vars+outs+1) ):
ColLabel.append(self.GetColLabelValue(Col))
self.DeleteCols(vars, self.GetNumberCols()-(vars+outs))
for Col in range(self.GetNumberCols()-outs, (vars+outs) ):
if ColLabel:
self.SetColLabelValue(Col, ColLabel.pop(0))
if UpdateGrid:
self.DrawGrid(OverWrite = False)
def SetVars(self, vars, UpdateGrid = True):
#self.ClearGrid()
self.numberOfVariables=vars
outs = self.numberOfOutputs
if outs > 2:
self.DeleteCols(self.GetNumberCols()-1, 1)
ColLabel = []
for Col in range(0, self.GetNumberCols() ):
ColLabel.append(self.GetColLabelValue(Col))
if(self.GetNumberCols()<(vars+outs)):
InserNbCol = vars+outs-self.GetNumberCols()
self.InsertCols(0,InserNbCol)
## for i in range(0, InserNbCol):
## self.SetColLabelValue(i, chr(65+i))
## for Col in range(InserNbCol, vars+outs+1):
## self.SetColLabelValue(Col, ColLabel[Col-InserNbCol] )
#self.AppendCols(vars+outs-self.GetNumberCols())
if(self.GetNumberCols()>(vars+outs)):
NbDeleteCol = self.GetNumberCols()-(vars+outs)
self.DeleteCols(0, NbDeleteCol)
for Col in range(0, vars+outs):
self.SetColLabelValue(Col, ColLabel[Col+NbDeleteCol] )
if(self.GetNumberRows()<(pow(2, vars))):
self.AppendRows((pow(2, vars))-self.GetNumberRows())
if(self.GetNumberRows()>(pow(2, vars))):
self.DeleteRows(pow(2, vars), self.GetNumberRows()-(pow(2, vars)))
if UpdateGrid:
self.DrawGrid(OverWrite = False)
def DrawGrid(self, OverWrite = True):
vars = self.numberOfVariables
outs = self.numberOfOutputs
if outs >= 2:
self.AppendCols(1)
self.SetColLabelValue(self.GetNumberCols()-1, 'DEC')
if OverWrite:
for i in range(0, vars):
self.SetColLabelValue(i, chr(65+i))
for i in range(0, outs):
self.SetColLabelValue(vars+i, 'f%d' % (outs-1-i))
for i in range(0,(pow(2, vars)) ):
self.SetRowLabelValue(i, str(i))
if(self.showZeros):
self.SetCellValue(i, self.numberOfVariables, ("0"))
ThreadList = []
ref = vars-4
paquet = pow(2,ref)
if ref<=1 :
thread = UpdateThread(self, vars, outs, range(0,(pow(2, vars)) ))
thread.start()
ThreadList.append( thread )
else:
for paquet_index in range(0, pow(2, vars-ref)):
start = 0+paquet_index*pow(2, ref)
end = pow(2, ref)+paquet_index*pow(2, ref)
thread = UpdateThread(self, vars, outs, range(start,end))
thread.start()
ThreadList.append( thread )
while ThreadList:
for thread in ThreadList:
thread.join(10)
if not thread.is_alive():
ThreadList.remove(thread)
else:
completion = 100
for threadi in ThreadList:
completion = min(threadi.completion(), completion)
print '%s %s%% complete' % (thread.getName(), completion )
## for i in range(0,(pow(2, vars)) ):
## if outs >= 2:
## self.UpdateDCRow( i)
##
## for j in range(0, vars):
## self.SetReadOnly(i, j, 1)
## if(not(i%2)):
## self.SetCellBackgroundColour(i, j, wx.Colour(245, 245, 245))
## else:
## self.SetCellBackgroundColour(i, j, wx.Colour(235, 235, 235))
## if((i%(pow(2, vars-j)))<(pow(2, vars-j-1))):
## self.SetCellValue(i, j, ("0"))
## else :
## self.SetCellValue(i, j, ("1"))
## for j in range(0, outs):
## if (j%2 ==0):
## self.SetCellBackgroundColour(i, vars+j, wx.Colour(215, 225, 255))
## else:
## self.SetCellBackgroundColour(i, vars+j, wx.Colour(255, 255, 255))
dc = wx.ClientDC(self)
dc.SetFont(self.GetLabelFont())
[w,h] = dc.GetTextExtent( str( pow(2, vars) ))
self.SetRowLabelSize(w+15)
self.AutoSizeColumns(1)
for i in range(0, outs):
self.SetColSize(vars+i, (1.5*self.GetColSize(0)))
self.ForceRefresh()
self.AdjustScrollbars()
def SetColLabelSizefromLabel(self, col):
#dc = wx.PaintDC(self)
dc = wx.ClientDC(self)
dc.SetFont(self.GetLabelFont())
[w,h] = dc.GetTextExtent( self.GetColLabelValue(col) )
if(w<20):
w=20
#self.SetColLabelSize(w+15)
self.SetColSize(col, w+15)
#self.AutoSizeColumns(1)
self.ForceRefresh()
self.AdjustScrollbars()
def SetShowZeros(self, s):
self.showZeros=s;
for j in range(0, self.numberOfOutputs):
for i in range(0, self.GetNumberRows() ):
if(self.showZeros & (self.GetCellValue(i, self.numberOfVariables+j)== (""))) :
self.SetCellValue(i, self.numberOfVariables+j, ("0"))
else:
if((not self.showZeros) & (self.GetCellValue(i, self.numberOfVariables+j)== ("0"))) :
self.SetCellValue(i, self.numberOfVariables+j, (""))
self.ForceRefresh()
def GetShowZeros(self):
return self.showZeros
def Write(self, filePath):
"""
Genere un fichier a partir d'une table
"""
fh = open(filePath, "w")
fh.write("%Generated by Kmapreducer\n\n")
# @TODO Rajouter la date et l'heure
fh.write("\n")
#sweep variables
fh.write("% sweep_variables " )
vars = self.numberOfVariables
for var in range(0, vars):
varName = self.GetColLabelValue(var)
fh.write( varName+" ")
fh.write("\n\n" )
#performances
fh.write("% Performance_expressions_table:\n")
outs = self.numberOfOutputs
for out in range(0, outs):
outName = self.GetColLabelValue(vars + out)
fh.write("%performance "+outName+"= \"not defined\" type = int\n")
fh.write("\n")
# Ligne des variables
fh.write("% ")
for col in range(0, vars + outs) :
name = self.GetColLabelValue(col)
fh.write(name+" ")
fh.write("\n")
# Datas
for row in range(0, self.GetNumberRows()) :
for col in range(0, vars + outs):
data = self.GetCellValue(row, col)
if (data == '') | (data == ' '):
data = '0'
if data == '?':
data = '2'
fh.write(data + ' ')
fh.write('\n')
fh.close()
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Simple Grid Demo", size=(640,480))
self.grid = TruthTable(self)
class BoaApp(wx.App):
def OnInit(self):
frame = TestFrame(None)
self.main = frame
self.SetTopWindow(self.main)
self.main.Show(True)
return True
def OnOpenWidgetInspector(self):
# Activate the widget inspection tool
if __debug__:
#from wx.lib.inspection import InspectionTool
#if not InspectionTool().initialized:
# InspectionTool().Init()
# Find a widget to be selected in the tree. Use either the
# one under the cursor, if any, or this frame.
#wnd = wx.FindWindowAtPointer()
#if not wnd:
# wnd = self
#InspectionTool().Show(wnd, True)
None
def main():
application = BoaApp(0)
if __debug__:
BoaApp.OnOpenWidgetInspector(application)
#from wx.lib.mixins.inspection import InspectableApp
#app = InspectableApp(False)
#frame = TestFrame(None)
#frame.Show(True)
#import wx.lib.inspection
#wx.lib.inspection.InspectionTool().Show()
application.MainLoop()
if __name__ == '__main__':
## import wx.lib.inspection
## wx.lib.inspection.InspectionTool().Show()
main()