diff --git a/src/main/java/de/neemann/digital/gui/components/karnaugh/KarnaughMap.java b/src/main/java/de/neemann/digital/gui/components/karnaugh/KarnaughMap.java index 489463842..c82ae9e2a 100644 --- a/src/main/java/de/neemann/digital/gui/components/karnaugh/KarnaughMap.java +++ b/src/main/java/de/neemann/digital/gui/components/karnaugh/KarnaughMap.java @@ -42,12 +42,9 @@ public class KarnaughMap implements Iterable { for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) cells.add(new Cell(row, col)); - addToRow(0, 2, 0, true); // add the variables to the cells - addToRow(1, 2, 0, false); - addToCol(0, 2, 1, true); - addToCol(1, 2, 1, false); - headerLeft = new Header(0, true, false); // define the headers needed - headerTop = new Header(1, true, false); + + headerLeft = new Header(0, true, false).toRows(2, this); + headerTop = new Header(1, true, false).toCols(2, this); headerRight = null; headerBottom = null; break; @@ -55,19 +52,10 @@ public class KarnaughMap implements Iterable { for (int row = 0; row < 2; row++) for (int col = 0; col < 4; col++) cells.add(new Cell(row, col)); - addToRow(0, 4, 0, true); - addToRow(1, 4, 0, false); - addToCol(0, 2, 1, true); - addToCol(1, 2, 1, true); - addToCol(2, 2, 1, false); - addToCol(3, 2, 1, false); - addToCol(0, 2, 2, false); - addToCol(1, 2, 2, true); - addToCol(2, 2, 2, true); - addToCol(3, 2, 2, false); - headerLeft = new Header(0, true, false); - headerTop = new Header(1, true, true, false, false); - headerBottom = new Header(2, false, true, true, false); + + headerLeft = new Header(0, true, false).toRows(4, this); + headerTop = new Header(1, true, true, false, false).toCols(2, this); + headerBottom = new Header(2, true, false, false, true).toCols(2, this); headerRight = null; break; case 4: @@ -75,26 +63,10 @@ public class KarnaughMap implements Iterable { for (int col = 0; col < 4; col++) cells.add(new Cell(row, col)); - addToRow(0, 4, 0, true); - addToRow(1, 4, 0, true); - addToRow(2, 4, 0, false); - addToRow(3, 4, 0, false); - addToRow(0, 4, 1, false); - addToRow(1, 4, 1, true); - addToRow(2, 4, 1, true); - addToRow(3, 4, 1, false); - addToCol(0, 4, 2, true); - addToCol(1, 4, 2, true); - addToCol(2, 4, 2, false); - addToCol(3, 4, 2, false); - addToCol(0, 4, 3, false); - addToCol(1, 4, 3, true); - addToCol(2, 4, 3, true); - addToCol(3, 4, 3, false); - headerLeft = new Header(0, true, true, false, false); - headerRight = new Header(1, false, true, true, false); - headerTop = new Header(2, true, true, false, false); - headerBottom = new Header(3, false, true, true, false); + headerLeft = new Header(0, true, true, false, false).toRows(4, this); + headerRight = new Header(1, true, false, false, true).toRows(4, this); + headerTop = new Header(2, true, true, false, false).toCols(4, this); + headerBottom = new Header(3, true, false, false, true).toCols(4, this); break; default: throw new KarnaughException(Lang.get("err_toManyVars")); @@ -105,16 +77,6 @@ public class KarnaughMap implements Iterable { addExpression(expr); // create the covers } - private void addToRow(int row, int cols, int var, boolean invert) { - for (int col = 0; col < cols; col++) - getCell(row, col).add(new VarState(var, invert)); - } - - private void addToCol(int col, int rows, int var, boolean invert) { - for (int row = 0; row < rows; row++) - getCell(row, col).add(new VarState(var, invert)); - } - /** * Returns the cell at the given position * @@ -533,5 +495,33 @@ public class KarnaughMap implements Iterable { return invert[i]; } + /** + * Initializes the table according to the selected header. + * + * @param cols the number columns in the table + * @param kmap the k-map to use + * @return this for chained calls + */ + public Header toRows(int cols, KarnaughMap kmap) { + for (int row = 0; row < invert.length; row++) + for (int col = 0; col < cols; col++) + kmap.getCell(row, col).add(new VarState(var, invert[row])); + return this; + } + + /** + * Initializes the table according to the selected header. + * + * @param rows the number rows in the table + * @param kmap the k-map to use + * @return this for chained calls + */ + public Header toCols(int rows, KarnaughMap kmap) { + for (int col = 0; col < invert.length; col++) + for (int row = 0; row < rows; row++) + kmap.getCell(row, col).add(new VarState(var, invert[col])); + return this; + } + } } diff --git a/src/test/java/de/neemann/digital/gui/components/karnaugh/KarnaughMapTest.java b/src/test/java/de/neemann/digital/gui/components/karnaugh/KarnaughMapTest.java index 73e9fdea4..7b7d25b2c 100644 --- a/src/test/java/de/neemann/digital/gui/components/karnaugh/KarnaughMapTest.java +++ b/src/test/java/de/neemann/digital/gui/components/karnaugh/KarnaughMapTest.java @@ -5,10 +5,7 @@ */ package de.neemann.digital.gui.components.karnaugh; -import de.neemann.digital.analyse.expression.Constant; -import de.neemann.digital.analyse.expression.Expression; -import de.neemann.digital.analyse.expression.ExpressionException; -import de.neemann.digital.analyse.expression.Variable; +import de.neemann.digital.analyse.expression.*; import de.neemann.digital.analyse.parser.ParseException; import de.neemann.digital.analyse.parser.Parser; import de.neemann.digital.analyse.quinemc.BoolTableBoolArray; @@ -86,7 +83,7 @@ public class KarnaughMapTest extends TestCase { // in 4x4 map a 8 cell block is drawn in wrong orientation public void testBUG_1() throws IOException, ParseException, KarnaughException { - Expression exp = new Variable("D"); + Expression exp = Not.not(new Variable("D")); KarnaughMap c = new KarnaughMap(Variable.vars(4), exp); assertEquals(1, c.size()); @@ -96,7 +93,7 @@ public class KarnaughMapTest extends TestCase { assertFalse(co.onlyEdges()); assertTrue(co.isVerticalDivided()); - exp = new Variable("B"); + exp = Not.not(new Variable("B")); c = new KarnaughMap(Variable.vars(4), exp); assertEquals(1, c.size()); diff --git a/src/test/java/de/neemann/digital/integration/TestInGUI.java b/src/test/java/de/neemann/digital/integration/TestInGUI.java index 2f04145cb..8d47e0c9f 100644 --- a/src/test/java/de/neemann/digital/integration/TestInGUI.java +++ b/src/test/java/de/neemann/digital/integration/TestInGUI.java @@ -245,14 +245,14 @@ public class TestInGUI extends TestCase { .add(new EnterTruthTable(0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1)) .press("F1") .delay(500) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 146, 110, new Color(215, 175, 175))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 109, new Color(187, 187, 221))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 86, 169, new Color(255, 187, 187))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 205, 169, new Color(127, 255, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 145, 228, new Color(127, 127, 255))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 230, new Color(255, 175, 255))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 86, 288, new Color(242, 242, 191))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 205, 289, new Color(127, 255, 255))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 197, 110, new Color(255, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 317, 108, new Color(127, 255, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 169, new Color(191, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 257, 170, new Color(127, 127, 191))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 197, 228, new Color(227, 227, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 316, 228, new Color(127, 255, 255))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 290, new Color(127, 127, 255))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 257, 290, new Color(255, 127, 255))) // .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class)) // .ask("Shows the k-map a checkerboard pattern?") .add(new GuiTester.CloseTopMost()) @@ -262,14 +262,14 @@ public class TestInGUI extends TestCase { public void testEdges() { new GuiTester() .use(createNew4VarTruthTable) - .add(new EnterTruthTable(0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1)) + .add(new EnterTruthTable(1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0)) .press("F1") .delay(500) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 136, 100, new Color(255, 127, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 100, new Color(255, 127, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 305, new Color(255, 127, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 136, 305, new Color(255, 127, 127))) -// .add(new GuiTester.ColorPickerCreator()) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 98, new Color(255, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 265, 95, new Color(255, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 265, 291, new Color(255, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 137, 296, new Color(255, 127, 127))) +// .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class)) // .ask("Are the edges covered in the k-map?") .add(new GuiTester.CloseTopMost()) .execute(); @@ -286,11 +286,10 @@ public class TestInGUI extends TestCase { .press("RIGHT", 3) .add(new EnterTruthTable(0, 0, 0, 1, 0, 1, 1, 1)) .press("F1") - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 136, 230, new Color(255, 127, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 209, 230, new Color(127, 255, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 311, 184, new Color(191, 127, 127))) - .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 266, 231, new Color(255, 127, 127))) -// .add(new GuiTester.ColorPickerCreator()) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 213, 179, new Color(191, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 144, 231, new Color(255, 127, 127))) + .add(new GuiTester.ColorPicker(KarnaughMapComponent.class, 315, 230, new Color(127, 255, 127))) +// .add(new GuiTester.ColorPickerCreator(KarnaughMapComponent.class)) // .ask("Shows the k-map a 'two out of three' pattern?") .add(new GuiTester.CloseTopMost()) .execute();