forked from cruzeneko/Boole-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmc.js
1243 lines (1073 loc) · 37.2 KB
/
qmc.js
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) Thorsten Thormaehlen, Marburg, 2013, All rights reserved
// Contact: www.thormae.de
// This software is written for educational (non-commercial) purpose.
// There is no warranty or other guarantee of fitness for this software,
// it is provided solely "as is".
function PetrickMethod()
{
this.problem;
this.maxProblemSize = 100;
this.solution;
this.log = "";
var that = this;
this.test = function() {
var andArray = new Array();
var orArray;
var monomA;
var monomB;
orArray = new Array();
monomA = new Object(); // using objects ensures that (x and x) = x
monomA[1] = 1;
orArray.push(monomA);
monomB = new Object();
monomB[2] = 2;
orArray.push(monomB);
andArray.push(orArray);
orArray = new Array();
monomA = new Object();
monomA[3] = 3;
orArray.push(monomA);
monomB = new Object();
monomB[4] = 4;
orArray.push(monomB);
andArray.push(orArray);
orArray = new Array();
monomA = new Object();
monomA[1] = 1;
orArray.push(monomA);
monomB = new Object();
monomB[3] = 3;
orArray.push(monomB);
andArray.push(orArray);
orArray = new Array();
monomA = new Object();
monomA[5] = 5;
orArray.push(monomA);
monomB = new Object();
monomB[6] = 6;
orArray.push(monomB);
andArray.push(orArray);
orArray = new Array();
monomA = new Object();
monomA[2] = 2;
orArray.push(monomA);
monomB = new Object();
monomB[5] = 5;
orArray.push(monomB);
andArray.push(orArray);
orArray = new Array();
monomA = new Object();
monomA[4] = 4;
orArray.push(monomA);
monomB = new Object();
monomB[6] = 6;
orArray.push(monomB);
andArray.push(orArray);
/*orArray = new Array();
monomA = new Object();
monomA[4] = 4;
orArray.push(monomA);
monomB = new Object();
monomB[4] = 4;
orArray.push(monomB);
andArray.push(orArray);*/
this.solve(andArray);
};
this.solve = function(eq) {
this.problem = eq;
this.log = "";
//printEqnArray(eq);
printEqnArrayFancy(eq);
// multiply out
var andArray = eq;
var loopCounter = 0;
while (andArray.length > 1) {
var newAndArray = new Array();
for (var i = 1; i < andArray.length; i += 2) {
var orTermA = andArray[i - 1];
var orTermB = andArray[i];
var newOrArray = new Array();
for (var a = 0; a < orTermA.length; a++) {
for (var b = 0; b < orTermB.length; b++) {
var monom1 = orTermA[a];
var monom2 = orTermB[b];
var resultingMonom = new Object();
for (var m in monom1) {
resultingMonom[monom1[m]] = monom1[m];
}
for (var n in monom2) {
resultingMonom[monom2[n]] = monom2[n];
}
newOrArray.push(resultingMonom);
}
}
newAndArray.push(newOrArray);
}
// if uneven copy last and-term
if (andArray.length % 2 === 1) {
newAndArray.push(andArray[andArray.length - 1]);
}
//printEqnArray(newAndArray);
printEqnArrayFancy(newAndArray);
andArray.length = 0;
// simplify or-term
for (var i = 0; i < newAndArray.length; i++) {
var orTerm = newAndArray[i];
var newOrTerm = simplifyOrTerm(orTerm);
if (newOrTerm.length > 0) {
andArray.push(newOrTerm);
}
}
var problemSize = eqnArrayProblemSize(andArray);
if (problemSize > this.maxProblemSize) {
console.log("Error: The cyclic covering problem is too large to be solved with Petrick's method (increase maxProblemSize). Size=" + problemSize);
return false;
}
//printEqnArray(andArray);
printEqnArrayFancy(andArray);
loopCounter++;
}
this.solution = andArray;
return true;
};
function simplifyOrTerm(orTerm) {
// find a monom that is the same or simpler than another one
var newOrTerm = new Array();
var markedForDeletion = new Object();
for (var a = 0; a < orTerm.length; a++) {
var keepA = true;
var monomA = orTerm[a];
for (var b = a + 1; b < orTerm.length && keepA; b++) {
var monomB = orTerm[b];
var overlapBoverA = 0;
var lengthA = 0;
for (var m in monomA) {
if (monomB[m] in monomA) {
overlapBoverA++;
}
lengthA++;
}
var overlapAoverB = 0;
var lengthB = 0;
for (var m in monomB) {
if (monomA[m] in monomB) {
overlapAoverB++;
}
lengthB++;
}
if (overlapBoverA === lengthB) {
keepA = false;
}
if (lengthA < lengthB && overlapAoverB === lengthA) {
markedForDeletion[b] = b;
}
}
if (keepA) {
if (a in markedForDeletion) {
// do nothing
} else
newOrTerm.push(orTerm[a]);
}
}
return newOrTerm;
}
function printEqnArrayFancy(andArray) {
var str = "";
for (var i = 0; i < andArray.length; i++) {
var first = true;
str += "(";
var orArray = andArray[i];
for (var j = 0; j < orArray.length; j++) {
if (!first)
str += " + ";
var monom = orArray[j];
for (var k in monom) {
str += "<i>p</i><sub><small>"+ monom[k] + "</small></sub>";
}
first = false;
}
str += ")";
}
if(that.log.length > 0) {
that.log += "<p>⇔ " + str + "</p>";
}else{
that.log += "<p>"+ str + "</p>";
}
}
function eqnArrayProblemSize(andArray) {
var monomCounter = 0;
for (var i = 0; i < andArray.length; i++) {
var orArray = andArray[i];
monomCounter += orArray.length;
}
return monomCounter;
}
function printEqnArray(andArray) {
var str = "";
for (var i = 0; i < andArray.length; i++) {
var first = true;
str += "(";
var orArray = andArray[i];
for (var j = 0; j < orArray.length; j++) {
if (!first)
str += " or ";
var monom = orArray[j];
for (var k in monom) {
str += monom[k];
}
first = false;
}
str += ")";
}
console.log(str);
}
}
function PrimTerm() {
this.implicant = -1;
this.termString = "";
this.color = [0, 0, 0];
this.coloredTermString = "";
this.used = false;
this.neededByVar = new Object;
}
function Implicant() {
this.imp = new Object();
this.isPrim = false;
this.isOnlyDontCare = false;
this.bitMask = 0;
}
function ImplicantGroup() {
this.group = new Array;
this.order = -1;
}
function PrimTermTable(ord) {
this.essentialPrimTerms = new Array();
this.order = ord;
this.remainingVars = new Array();;
this.remainingPrimTerms = new Array();
this.supersededPrimTerms = new Array();
}
function hsvToRgb(h, s, v) {
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
r = v, g = t, b = p;
break;
case 1:
r = q, g = v, b = p;
break;
case 2:
r = p, g = v, b = t;
break;
case 3:
r = p, g = q, b = v;
break;
case 4:
r = t, g = p, b = v;
break;
case 5:
r = v, g = p, b = q;
break;
}
return [ Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255) ];
}
function QuineMcCluskeyDataCtrl() {
this.noOfVars = -1;
this.funcdata = new Array;
this.primTerms = new Array;
this.implicantGroups = new Array;
this.minimalTerm = "";
this.coloredMinimalTerm = "";
this.minimalTermPrims = new Array;
this.primTermTables = new Array;
this.petrickSolver = new PetrickMethod();
this.petrickTermPrims = new Array;
this.allowDontCare = false;
this.varNames = [];
this.lastKnownSolution = "";
this.init = function(no) {
this.noOfVars = no;
this.funcdata.length = 0;
this.primTerms.length = 0;
this.implicantGroups.length = 0;
this.minimalTerm = "0";
this.coloredMinimalTerm = "0";
this.minimalTermPrims.length = 0;
this.primTermTables.length = 0;
this.petrickTermPrims.length = 0;
var noOfFuncData = Math.pow(2, this.noOfVars);
for (var i = 0; i < noOfFuncData; i++) {
this.funcdata.push(0);
}
//this.petrickSolver.test();
};
this.setFuncData = function(i, val) {
if (i < 0 || i >= this.funcdata.length)
return;
this.funcdata[i] = val;
};
this.activated = function(i) {
if (i < 0 || i >= this.funcdata.length)
return;
this.funcdata[i] += 1;
if(this.allowDontCare) {
if (this.funcdata[i] > 2) this.funcdata[i] = 0;
}else{
if (this.funcdata[i] > 1) this.funcdata[i] = 0;
}
this.compute();
};
this.recompute = function() {
this.compute();
}
this.random = function() {
for (var i = 0; i < this.funcdata.length; i++) {
if(this.allowDontCare) {
this.funcdata[i] = Math.floor(Math.random() * 3);
}else{
this.funcdata[i] = Math.floor(Math.random() * 2);
}
}
this.compute();
};
this.clear = function() {
for (var i = 0; i < this.funcdata.length; i++) {
this.funcdata[i] = 0;
}
this.compute();
};
function bitCount(value) {
var counter = 0;
while (value > 0) {
if ((value & 1) === 1) counter++;
value >>= 1;
}
return counter;
}
this.isSolutionKnown = function() {
//debugger;
console.log("Queried if solution is known: "+this.lastKnownSolution);
if (this.lastKnownSolution != "") {
return true;
}
else return false;
};
this.getLastSolution = function() {
console.log("Queried for last known solution");
return this.lastKnownSolution;
};
this.compute = function() {
this.primTerms.length = 0;
this.implicantGroups.length = 0;
this.minimalTerm = "0";
this.coloredMinimalTerm = "0";
this.minimalTermPrims.length = 0;
this.primTermTables.length = 0;
this.petrickTermPrims.length = 0;
var counter = 0;
var lastIg = -1;
var continueLoop = true;
while(continueLoop) {
continueLoop = false;
var ig = new ImplicantGroup();
if(counter === 0) {
for (var i = 0; i < this.funcdata.length; i++) {
if(this.funcdata[i] > 0) {
var impl = new Implicant();
impl.imp[i] = i;
impl.isPrim = true;
ig.group.push(impl);
continueLoop = true;
}
}
}else{
for (var i = 0; i < lastIg.group.length; i++) {
for (var j = i+1; j < lastIg.group.length; j++) {
var imp1 = lastIg.group[i];
var imp2 = lastIg.group[j];
if (imp1.bitMask === imp2.bitMask) {
var found = false;
var xor = -1;
for (var m in imp1.imp) {
for (var n in imp2.imp) {
var i1 = imp1.imp[m];
var i2 = imp2.imp[n];
//console.log(i1 + "<->" + i2);
xor = (i1 ^ i2) & (~imp1.bitMask);
if (bitCount(xor) === 1) {
//console.log("found merge candidate" + i1 + "<->" + i2);
found = true;
}
break;
}
break;
}
if (found) {
imp1.isPrim = false;
imp2.isPrim = false;
var impl = new Implicant();
impl.isPrim = true;
impl.bitMask = imp1.bitMask | xor;
for (var m in imp1.imp)
impl.imp[m] = parseInt(m);
for (var n in imp2.imp)
impl.imp[n] = parseInt(n);
var foundMatch = false; // determine if this combination is already there
for(var k=0; k < ig.group.length; k++) {
var exist = ig.group[k];
var isTheSame = true;
for(var m in impl.imp) {
var found = false;
for (var n in exist.imp) {
if(parseInt(m) === parseInt(n)) {
found = true;
}
}
if(!found) {
isTheSame = false;
break;
}
}
if(isTheSame) {
foundMatch = true;
break;
}
}
if(!foundMatch) {
ig.group.push(impl);
continueLoop = true;
}
}
}
}
}
}
if(continueLoop) this.implicantGroups.push(ig);
lastIg = ig;
counter++;
}
// collect primterms
this.primTerms.length = 0;
this.minimalTermPrims.length = 0;
var color = 0.0;
for(var i= this.implicantGroups.length-1; i >=0; i--) {
var g = this.implicantGroups[i].group;
for(var j=0; j < g.length; j++) {
if(g[j].isPrim) {
// prim terms introduced by don't cares
// must have at least one 1
var containsOne = false;
var allFuncPrimTerm = g[j].imp;
for(var kk in allFuncPrimTerm) {
var k = allFuncPrimTerm[kk];
if(this.funcdata[k] === 1) {
containsOne = true;
}
}
if(!containsOne){
g[j].isOnlyDontCare = true;
} else {
var primTerm = new PrimTerm();
primTerm.implicant = g[j];
// extract minTerm as string
for (var thisVal in primTerm.implicant.imp) {
var minTerm = "";
var one = 1;
var append = "·";
var needed = (~primTerm.implicant.bitMask);
for (var v = 0; v < this.noOfVars; v++) {
if ((needed & one) === one) {
if ((thisVal & one) === one) {
//minTerm = "<i>x</i><sub><small>" + this.varNames[v] + "</small></sub>" + minTerm;
minTerm = "<i>" + this.varNames[v] + "</i>" + append + minTerm;
} else {
//minTerm = "<i>x̄</i><sub><small>" + this.varNames[v] + "</small></sub>" + minTerm;
minTerm = "<i style=\"text-decoration: overline;\">" + "‌" + this.varNames[v] + "</i>" + append + minTerm;
}
}
one = one << 1;
}
if(minTerm.endsWith(append)) {
minTerm = minTerm.substring(0,minTerm.length-append.length);
}
minTerm = "(" + minTerm + ")";
if (primTerm.implicant.bitMask === Math.pow(2, this.noOfVars) - 1)
minTerm = "1";
primTerm.color = hsvToRgb(color, 1.0, 0.5);
color += 0.22;
color = color % 1.0;
primTerm.termString = minTerm;
var colorStr = "rgb(" + primTerm.color[0] + "," + primTerm.color[1] + "," + primTerm.color[2] + ")";
primTerm.coloredTermString = "<span style='color:" + colorStr + "'>" + minTerm + "</span>";
break;
}
this.primTerms.push(primTerm);
}
}
}
}
// looking for essential prime implicants
var remaining = new Object();
for (var i = 0; i < this.funcdata.length; i++) {
if(this.funcdata[i] === 1) {
remaining[i] = i;
}
}
this.primTermTables.length = 0;
var primTableLoop = 0;
var primTableFound = (this.primTerms.length > 0);
var cyclicCoveringFound = false;
var primTermTable;
while (primTableFound) {
primTableFound = false;
primTermTable = new PrimTermTable(primTableLoop);
for (var r in remaining) {
primTermTable.remainingVars.push(remaining[r]);
}
if (primTableLoop === 0) {
for (var j = 0; j < this.primTerms.length; j++) {
primTermTable.remainingPrimTerms.push(this.primTerms[j]);
}
} else {
// remove rows
var prevTable = this.primTermTables[primTableLoop-1];
for(var k=0; k <prevTable.remainingPrimTerms.length; k++) {
if(!prevTable.remainingPrimTerms[k].used){
var superseded = false;
var impA = prevTable.remainingPrimTerms[k].implicant.imp;
var varCover = new Object;
var countA = 0;
for(var r in remaining) {
var v = remaining[r];
if (v in impA) {
varCover[v] = v;
countA++;
}
}
for (var l = 0; l < prevTable.remainingPrimTerms.length && !superseded; l++) {
if (!prevTable.remainingPrimTerms[l].used && k !== l) {
var impB = prevTable.remainingPrimTerms[l].implicant.imp;
var countB = 0;
for (var r in varCover) {
var v = varCover[r];
if (v in impB) {
countB++;
}
}
if(countA === countB) {
var countBInRemaining = 0;
for (var r in remaining) {
var v = remaining[r];
if (v in impB) {
countBInRemaining++;
}
}
if(countBInRemaining > countA) {
superseded = true;
}else{
if(k > l) {
superseded = true;
}
}
}
}
}
if(!superseded) {
primTermTable.remainingPrimTerms.push(prevTable.remainingPrimTerms[k]);
}else{
prevTable.supersededPrimTerms.push(prevTable.remainingPrimTerms[k]);
}
}
}
}
if (primTermTable.remainingPrimTerms.length > 0) {
this.primTermTables.push(primTermTable);
var currentTerms = primTermTable.remainingPrimTerms;
var toBeRemoved = new Object();
for (var r in remaining) {
var i = remaining[r];
var count = 0;
var term = -1;
for (var j = 0; j < currentTerms.length && count < 2; j++) {
if (i in currentTerms[j].implicant.imp) {
term = j;
count++;
}
}
if (count === 1) {
currentTerms[term].neededByVar[i] = primTableLoop;
if(!currentTerms[term].used) {
this.minimalTermPrims.push(currentTerms[term]);
currentTerms[term].used = true;
primTermTable.essentialPrimTerms.push(currentTerms[term]);
primTableFound = true;
for (var r in remaining) {
var ii = remaining[r];
if (ii in currentTerms[term].implicant.imp) {
toBeRemoved[ii] = ii;
}
}
}
}
}
// remove columns
var tmpRemaining = new Object();
for (var e in remaining){
var ee = remaining[e];
tmpRemaining[ee] = ee;
delete remaining[e];
}
var remainingCount = 0;
for (var r in tmpRemaining) {
var t = tmpRemaining[r];
if(!(t in toBeRemoved)) {
remaining [t] = t;
remainingCount++;
}
}
}
if( remainingCount === 0 ) {
primTableFound = false; // break loop
}else{
if(!primTableFound) {
cyclicCoveringFound = true;
}
}
primTableLoop++;
}
var solutionFound = true;
// Petrick's Method
if (cyclicCoveringFound) {
//console.log("Cyclic covering found");
var andArray = new Array();
for (var r in remaining) {
var ii = remaining[r];
var orArray = new Array();
for (var k = 0; k < primTermTable.remainingPrimTerms.length; k++) {
var imp = primTermTable.remainingPrimTerms[k].implicant.imp;
if(ii in imp){
var monom = new Object();
monom[k] = k;
orArray.push(monom);
}
}
andArray.push(orArray);
}
solutionFound = this.petrickSolver.solve(andArray);
if (solutionFound) {
var solutions = this.petrickSolver.solution[0];
var bestSolution = -1;
var bestCount = 10000000;
var bestVarCount = 10000000;
for (var i = 0; i < solutions.length; i++) {
var count = 0;
for (var j in solutions[i]) {
count++;
}
if (count <= bestCount) { // first sort accoring to monom length
var foundBest = true;
if (count === bestCount) {
var bestVarCountNew = 0;
for (var j in solutions[i]) {
for (var v in primTermTable.remainingPrimTerms[j].implicant.imp) {
bestVarCountNew++;
}
}
if (bestVarCountNew >= bestVarCount)
foundBest = false;
}
if (foundBest) {
bestCount = count;
bestSolution = i;
bestVarCount = 0;
for (var j in solutions[bestSolution]) {
for (var v in primTermTable.remainingPrimTerms[j].implicant.imp) {
bestVarCount++;
}
}
}
}
}
//console.log("Best solution " + bestSolution);
var best = solutions[bestSolution];
for (var b in best) {
var addPrimTerm = primTermTable.remainingPrimTerms[best[b]];
this.minimalTermPrims.push(addPrimTerm);
this.petrickTermPrims.push(addPrimTerm);
}
}
}
if (solutionFound) {
this.minimalTerm = "";
this.coloredMinimalTerm = "";
var firstL = true;
for (var i = 0; i < this.minimalTermPrims.length; i++) {
if (!firstL) {
this.minimalTerm += " + ";
this.coloredMinimalTerm += " + ";
}
this.minimalTerm += this.minimalTermPrims[i].termString;
this.coloredMinimalTerm += this.minimalTermPrims[i].coloredTermString;
firstL = false;
}
if (this.minimalTermPrims.length === 0) {
this.minimalTerm = "0";
this.coloredMinimalTerm = "0";
}
}else{
this.minimalTerm = 'Error: The cyclic covering problem is too large (increase the "maxProblemSize" parameter)';
this.coloredMinimalTerm = 'Error: The cyclic covering problem is too large (increase the "maxProblemSize" parameter)';
}
var formula = this.minimalTerm.replace(/‌/g,"~");
console.log("Final solution is " + strip(formula));
this.lastKnownSolution = strip(formula);
//debugger;
};
}
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
function QuineMcCluskey(parentDivId, columns, language) {
var myDiv = -1;
var divId = parentDivId;
this.cols = columns + 1;
this.rows = Math.pow(2, columns);
this.data = new QuineMcCluskeyDataCtrl();
var that = this;
//var varNames = [];
var labels;
if(language === 0) {
labels = {ttable:"Truth table",
minExp:"Minimal boolean expression",
impli:"Implicants",
order:"Order",
primChart:"Prime implicant chart",
primChartReduced:"Reduced prime implicant chart (Iteration",
extractedPrims:"Extracted essential prime implicants",
extractedMPrims:"Extracted prime implicants",
petricksM:"Petrick's method"};
}else{
labels = {ttable:"Wahrheitstafel",
minExp:"Minimaler boolescher Ausdruck",
impli:"Implikanten",
order:"Ordnung",
primChart:"Primimplikantentafel",
primChartReduced:"Reduzierte Primimplikantentafel (Iteration",
extractedPrims:"Extrahierte essentielle Primimplikanten",
extractedMPrims:"Extrahierte Primimplikanten",
petricksM:"Verfahren von Petrick"};
}
this.init = function() {
this.data.init(columns);
myDiv = document.createElement('div');
if (!myDiv) {
console.log("QuineMcCluskey error: can not create a canvas element");
myDiv = -1;
} else {
var parent = document.getElementById(divId);
if (!parent) {
if(divId !== "fakeDivId") {
console.log("QuineMcCluskey error: can not find an element with the given name: " + divId);
}
myDiv = -1;
} else {
document.body.appendChild(myDiv);
parent.appendChild(myDiv);
}
}
this.update();
};
this.setInputVarNames = function(names) {
console.log(names);
this.data.varNames = names;
}
this.setNoOfVars = function(vars) {
var c = parseInt(vars);
if (c < 1 && c > 6)
return;
this.cols = c + 1;
this.rows = Math.pow(2, c);
this.data.init(c);
this.update();
};
this.genRandom = function() {
this.data.random();
this.update();
};
this.allowDontCares = function(type) {
if(type > 0) {
this.data.allowDontCare = true;
}else{
this.data.allowDontCare = false;
}
this.data.clear();
this.update();
};
this.drawImplicantGroup = function(g, parent, primFlag, t, drawPetrickVars) {
var primTermTable = this.data.primTermTables[t];
var myTable = document.createElement('table');
myTable.setAttribute('class', 'qmcTableClass');
var myRow = document.createElement('tr');
var cell1h = document.createElement('td');
cell1h.setAttribute('class', 'qmcTdNoBorder');
cell1h.innerHTML = "";
myRow.appendChild(cell1h);
for (var j = 0; j < this.data.noOfVars; j++) {
var myCell = document.createElement('th');
myCell.innerHTML = "<i>x</i><sub><small>" + (this.data.noOfVars-1-j) + "</small></sub>";
myCell.setAttribute('class', 'qmcHeaderX qmcBit');
myRow.appendChild(myCell);
}
if (primFlag) {
for (var i = 0; i < primTermTable.remainingVars.length; i++) {
var cellImph = document.createElement('td');
cellImph.setAttribute('class', 'qmcTdNoBorder');
cellImph.innerHTML = primTermTable.remainingVars[i].toString(10);
myRow.appendChild(cellImph);
}
}
var cellImph = document.createElement('td');
cellImph.setAttribute('class', 'qmcTdNoBorder');
cellImph.innerHTML = "";
myRow.appendChild(cellImph);
myTable.appendChild(myRow);
var iMax = 0;
if(!primFlag) iMax = g.group.length; else iMax = primTermTable.remainingPrimTerms.length;
for (var i = 0; i < iMax; i++) {
var impl = -1;
if(!primFlag) impl = g.group[i]; else impl = primTermTable.remainingPrimTerms[i].implicant;
var bits = 0;
var mask = impl.bitMask;
for(var m in impl.imp) {
bits = impl.imp[m];
break;
}
myRow = document.createElement('tr');
var cell1 = document.createElement('td');
var cell1Str = "";
var first = true;
for(var m in impl.imp) {
if(!first) cell1Str += ", ";
cell1Str += impl.imp[m].toString(10);
first = false;
}
cell1.innerHTML = cell1Str + ":";
cell1.setAttribute('class', 'qmcTdNoBorder');
myRow.appendChild(cell1);
var res = bits.toString(2);
for (var j = 0; j < this.data.noOfVars; j++) {
var myCell = document.createElement('td');
myCell.setAttribute('class', 'qmcBit');
var str;
var currentBit = Math.pow(2, (this.data.noOfVars - 1)-j);
if ((currentBit & mask) === currentBit) {
str = "-";
myCell.innerHTML = str;
} else {
if (j >= (this.data.noOfVars) - res.length) {