Commit 3c12ea25 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Handle walking multiple grids.

Multiple grid tables (with both top and left) now parse correctly.  Unit
tests to help prove this, with the underlying assumption that since no
interface changes happened, all generators are happy.

Note that the breadth first generator doesn't get a notice on grid switch,
which is still planned.
parent f0202a70
Loading
Loading
Loading
Loading
+16 −5
Original line number Diff line number Diff line
@@ -92,11 +92,18 @@ public class Table {
            HierarchicalCell cell = cells.get(i);

            if (cell.getSubHierarchy().isEmpty()) {
                if (!topGrid.getSubHierarchy().isEmpty()) {
                    generator.handleEdgeCell(parseMatlabCode(cell));
                    generator.descendIntoGridFromCell(parseMatlabCode(cell));
                    HandleTopGrid(topGrid, generator, outputCellXIndex, outputCellYIndex);
                    generator.ascendFromGrid();
                } else {
                    Map<Variable, Expression> outputCells = new LinkedHashMap<Variable, Expression>();
                    for (Variable outputVariable : m_variables.getOutputVariables().values()) {
                        outputCells.put(outputVariable, parseMatlabCode(variableOutputs.get(outputVariable).get(outputCellXIndex, outputCellYIndex)));
                    }
                    generator.handleLeafCell(parseMatlabCode(cell), outputCells);
                }
                outputCellYIndex++;
            } else {
                generator.handleEdgeCell(parseMatlabCode(cell));
@@ -144,7 +151,7 @@ public class Table {
        for (int i = 0; i < cells.size(); ++i) {
            HierarchicalCell cell = cells.get(i);

            if (cell.getSubHierarchy().isEmpty()) {
            if (cell.getSubHierarchy().isEmpty() && !(checkTopGrid && !topGrid.getSubHierarchy().isEmpty())) {
                generator.handleLeafCell(parseMatlabCode(cell));
            } else {
                generator.handleEdgeCell(parseMatlabCode(cell));
@@ -158,6 +165,10 @@ public class Table {
                generator.descendIntoGridFromCell(parseMatlabCode(cell));
                HandleGrid(cell, generator, checkTopGrid);
                generator.ascendFromGrid();
            } else if (checkTopGrid && !topGrid.getSubHierarchy().isEmpty()) {
                generator.descendIntoGridFromCell(parseMatlabCode(cell));
                HandleGrid(topGrid, generator, false);
                generator.ascendFromGrid();
            }
        }
    }
+103 −0
Original line number Diff line number Diff line
@@ -68,6 +68,37 @@ public class TableWalkTest {
                                "  L(z Equals 0)\n" +
                                " A\n" +
                                "A\n",
                        "E(x GreaterThenEqual 0)\n" +
                                "E(x Equals 0)\n" +
                                "E(x LessThenEqual 0)\n" +
                                "D(x GreaterThenEqual 0)\n" +
                                " L(y GreaterThenEqual 0)\n" +
                                " L(y LessThen 0)\n" +
                                "A\n" +
                                "D(x Equals 0)\n" +
                                " E(z LessThenEqual 0)\n" +
                                " E(z Equals 0)\n" +
                                " E(z GreaterThenEqual 0)\n" +
                                " D(z LessThenEqual 0)\n" +
                                "  L(y GreaterThenEqual 0)\n" +
                                "  L(y LessThen 0)\n" +
                                " A\n" +
                                " D(z Equals 0)\n" +
                                "  E(z Equals 0)\n" +
                                "  D(z Equals 0)\n" +
                                "   L(y GreaterThenEqual 0)\n" +
                                "   L(y LessThen 0)\n" +
                                "  A\n" +
                                " A\n" +
                                " D(z GreaterThenEqual 0)\n" +
                                "  L(y GreaterThenEqual 0)\n" +
                                "  L(y LessThen 0)\n" +
                                " A\n" +
                                "A\n" +
                                "D(x LessThenEqual 0)\n" +
                                " L(y GreaterThenEqual 0)\n" +
                                " L(y LessThen 0)\n" +
                                "A\n",
                },
                {
                        new HierarchicalGridDepthFirstScriptGenerator(),
@@ -87,6 +118,47 @@ public class TableWalkTest {
                                "A\n" +
                                "L(x LessThenEqual 0)\n" +
                                "OUT output -> 4\n",
                        "E(x GreaterThenEqual 0)\n" +
                                "D(x GreaterThenEqual 0)\n" +
                                " L(y GreaterThenEqual 0)\n" +
                                " OUT output -> (0 Addition 0)\n" +
                                " L(y LessThen 0)\n" +
                                " OUT output -> (1 Addition 0)\n" +
                                "A\n" +
                                "E(x Equals 0)\n" +
                                "D(x Equals 0)\n" +
                                " E(z LessThenEqual 0)\n" +
                                " D(z LessThenEqual 0)\n" +
                                "  L(y GreaterThenEqual 0)\n" +
                                "  OUT output -> (0 Addition 1)\n" +
                                "  L(y LessThen 0)\n" +
                                "  OUT output -> (1 Addition 1)\n" +
                                " A\n" +
                                " E(z Equals 0)\n" +
                                " D(z Equals 0)\n" +
                                "  E(z Equals 0)\n" +
                                "  D(z Equals 0)\n" +
                                "   L(y GreaterThenEqual 0)\n" +
                                "   OUT output -> (0 Addition 2)\n" +
                                "   L(y LessThen 0)\n" +
                                "   OUT output -> (1 Addition 2)\n" +
                                "  A\n" +
                                " A\n" +
                                " E(z GreaterThenEqual 0)\n" +
                                " D(z GreaterThenEqual 0)\n" +
                                "  L(y GreaterThenEqual 0)\n" +
                                "  OUT output -> (0 Addition 3)\n" +
                                "  L(y LessThen 0)\n" +
                                "  OUT output -> (1 Addition 3)\n" +
                                " A\n" +
                                "A\n" +
                                "E(x LessThenEqual 0)\n" +
                                "D(x LessThenEqual 0)\n" +
                                " L(y GreaterThenEqual 0)\n" +
                                " OUT output -> (0 Addition 4)\n" +
                                " L(y LessThen 0)\n" +
                                " OUT output -> (1 Addition 4)\n" +
                                "A\n",
                },
        });
    }
@@ -97,6 +169,9 @@ public class TableWalkTest {
    @Parameter(value = 1)
    public String SingleGridWalkScript;

    @Parameter(value = 2)
    public String MultipleGridWalkScript;

    /**
     * Prepares the given grid for use by the single grid tests.
     * @param grid Grid to setup.
@@ -156,6 +231,34 @@ public class TableWalkTest {
        verifyWalk(table, SingleGridWalkScript);
    }

    /**
     * Tests the ability for Table to walk a table with both top and left grids defined.
     * <p>
     * This tests the ability for Table to walk more complicated tables, verifying that the generated script is equivalent
     * to a table with a single grid with an equivalent mathematical description.  This uses the same left grid as the
     * single left grid test, but a different top grid to avoid an explosion in output cells, simplifying the script.
     */
    @Test
    public void MultipleGridWalkTest() {
        Table table = getInitialTable();

        setupSingleGrid(table.getLeftGrid().getSubHierarchy());

        List<HierarchicalCell> grid = table.getTopGrid().getSubHierarchy();
        grid.add(new HierarchicalCell("y >= 0"));
        grid.add(new HierarchicalCell("y < 0"));

        final TwoDimensionalGrid outGrid = new TwoDimensionalGrid(3, 5);
        for(int i = 0; i < outGrid.sizeX(); i++) {
            for(int j = 0; j < outGrid.sizeY(); j++) {
                outGrid.get(i, j).setContents(String.valueOf(i) + "+" + String.valueOf(j));
            }
        }
        table.getVariableOutputs().put(table.getVariables().getOutputVariables().get("output"), outGrid);

        verifyWalk(table, MultipleGridWalkScript);
    }

    /**
     * This has the table walk itself with the walker, verifying its output against the given script.
     * @param table Table to walk.