Skip to content

Commit b382b3c

Browse files
committed
Compute Karnaugh map conditionally
1 parent 06e3002 commit b382b3c

File tree

1 file changed

+64
-37
lines changed

1 file changed

+64
-37
lines changed

index.html

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
<body data-bs-theme='dark' class='bg-body text-body'>
111111
<div class='container my-4 p-4 rounded-3 bg-body-tertiary'>
112112
<div class='row g-3 align-items-stretch'>
113-
<div class='col-12 col-lg-6'>
113+
<div class='col-12 col-lg-5'>
114114
<label for='oracle-code' class='form-label display-6 fw-light text-body-emphasis'
115115
style='font-size: 1.2rem;'>Arbitrary Oracle Function</label>
116116
<div class='btn-group rounded-top w-100' style='overflow-x: auto'>
@@ -196,7 +196,7 @@
196196
<span style='font-size: 1.5rem;'>&larr;</span>
197197
</button>
198198
</div>
199-
<div class='col-12 col-lg-5'>
199+
<div class='col-12 col-lg-6'>
200200
<label for='output-code' class='form-label display-6 fw-light text-body-emphasis'
201201
style='font-size: 1.2rem;'>Minimal Logic (Quine-McCluskey)</label>
202202
<div class='btn-group rounded-top w-100' style='overflow-x: auto'>
@@ -217,9 +217,9 @@
217217
<label class='btn btn-sm btn-secondary' for='outputTruthTable'
218218
onclick='showOutputTab("table")'>Truth Table</label>
219219

220-
<input type='radio' class='btn-check' name='outputOptions' id='outputSATInstance'
220+
<input type='radio' class='btn-check' name='outputOptions' id='outputSAT'
221221
autocomplete='off'>
222-
<label class='btn btn-sm btn-secondary' for='outputSATInstance' onclick='showOutputTab("sat")'>SAT
222+
<label class='btn btn-sm btn-secondary' for='outputSAT' onclick='showOutputTab("sat")'>SAT
223223
Instance</label>
224224
</div>
225225
<div class='my-2 d-flex align-items-center' style='gap: 0.5rem;'>
@@ -388,56 +388,80 @@
388388

389389
// CNF is true, DNF is false
390390
const isCNF = document.getElementById('formCnfCheckbox').checked;
391-
let expression, satExpression, kmapObject;
391+
let output, kmapObject, outputs = ['expr', 'sat'];
392+
393+
if (document.getElementById('outputKMap').checked) outputs.push('kmap');
392394

393395
if (isCNF) {
394-
({ expr: expression, sat: satExpression, kmap: kmapObject } = QuineMcCluskey.solveCNF(variableNames, window.oracle, ['expr', 'sat', 'kmap']));
396+
output = QuineMcCluskey.solveCNF(variableNames, window.oracle, outputs);
395397
} else {
396-
({ expr: expression, sat: satExpression, kmap: kmapObject } = QuineMcCluskey.solveDNF(variableNames, window.oracle, ['expr', 'sat', 'kmap']));
398+
output = QuineMcCluskey.solveDNF(variableNames, window.oracle, outputs);
397399
}
398400

399-
renderExpressionTextbox(expression);
400-
circuitExpression = expression.replace(/&&/g, '&').replace(/\|\|/g, '|');
401-
kmapData = kmapObject;
402-
outputSat.setValue(satExpression);
403-
404-
// Collect used vars from expression
405-
const identifierRegex = /\b[A-Za-z_][A-Za-z0-9_]*\b/g;
406-
const used = new Set();
407-
let match;
408-
while ((match = identifierRegex.exec(expression)) !== null) {
409-
const name = match[0];
410-
// Filter to original vars (skip literals)
411-
if (variableNames.includes(name) && name !== 'true' && name !== 'false') {
412-
used.add(name);
413-
}
401+
// Assign Karnaugh map and SAT output
402+
if (output && output.kmap) kmapData = output.kmap;
403+
if (output && output.sat) {
404+
if (typeof outputSat !== 'undefined' && outputSat) outputSat.setValue(output.sat);
414405
}
415-
// Only keep used variable names
416-
const maintainVars = document.getElementById('maintainVarsCheckbox').checked;
417-
if (maintainVars) {
418-
usedVariableNames = variableNames.slice();
406+
407+
if (output && output.expr) {
408+
const expression = output.expr;
409+
renderExpressionTextbox(expression);
410+
circuitExpression = expression.replace(/&&/g, '&').replace(/\|\|/g, '|');
411+
412+
// Fetch used variables from expression
413+
const identifierRegex = /\b[A-Za-z_][A-Za-z0-9_]*\b/g;
414+
const used = new Set();
415+
let match;
416+
while ((match = identifierRegex.exec(expression)) !== null) {
417+
const name = match[0];
418+
// Filter to original vars (skip literals)
419+
if (variableNames.includes(name) && name !== 'true' && name !== 'false') {
420+
used.add(name);
421+
}
422+
}
423+
// Only keep used variable names
424+
const maintainVars = document.getElementById('maintainVarsCheckbox').checked;
425+
if (maintainVars) {
426+
usedVariableNames = variableNames.slice();
427+
} else {
428+
usedVariableNames = variableNames.filter(v => used.has(v));
429+
}
430+
const funcArgs = usedVariableNames.join(',');
431+
const funcBody = 'return ' + expression + ';';
432+
const finalFunction = 'function oracle(' + funcArgs + ') {\n ' + funcBody + '\n}';
433+
if (typeof outputCode !== 'undefined' && outputCode) {
434+
outputCode.setValue(finalFunction);
435+
}
436+
console.log(finalFunction);
419437
} else {
420-
usedVariableNames = variableNames.filter(v => used.has(v));
438+
// No expression generated
439+
circuitExpression = '';
440+
usedVariableNames = [];
441+
if (typeof outputCode !== 'undefined' && outputCode) {
442+
outputCode.setValue('// No expression generated');
443+
}
421444
}
422-
const funcArgs = usedVariableNames.join(',');
423-
const funcBody = 'return ' + expression + ';';
424-
const finalFunction = 'function oracle(' + funcArgs + ') {\n ' + funcBody + '\n}';
425-
outputCode.setValue(finalFunction);
426-
console.log(finalFunction);
427445

428446
outputTableNeedsUpdate = true;
429447
if (document.getElementById('outputTruthTable').checked) {
430-
setupOutputTable();
448+
if (output && output.expr) {
449+
setupOutputTable();
450+
}
431451
}
432452
if (document.getElementById('outputKMap').checked) {
433-
renderKarnaughMapTable();
453+
if (kmapData) {
454+
renderKarnaughMapTable();
455+
}
434456
}
435457
if (document.getElementById('outputCircuit').checked) {
436-
renderLogicCircuit();
458+
if (circuitExpression && circuitExpression.length) {
459+
renderLogicCircuit();
460+
}
437461
}
438462

439463
document.getElementById('runButton').disabled = document.getElementById('autoRunCheckbox').checked;
440-
document.getElementById('copyButton').disabled = false;
464+
document.getElementById('copyButton').disabled = !(output && output.expr);
441465

442466
const errorIndicator = document.getElementById('errorIndicator');
443467
if (errorIndicator) {
@@ -503,7 +527,9 @@
503527
}
504528
if (tab === 'kmap') {
505529
document.getElementById('outputTab-kmap').style.display = '';
506-
renderKarnaughMapTable();
530+
if (document.getElementById('autoRunCheckbox').checked) {
531+
run();
532+
}
507533
} else {
508534
document.getElementById('outputTab-kmap').style.display = 'none';
509535
}
@@ -856,6 +882,7 @@
856882
function renderKarnaughMapTable() {
857883
const table = document.getElementById('karnaughMapTable');
858884
if (!table) return;
885+
if (!kmapData) return;
859886
const { vars, kmap, groups = [] } = kmapData;
860887
const { layout: { rowVars, colVars, rowGrayLabels, colGrayLabels }, values } = kmap;
861888

0 commit comments

Comments
 (0)