Commit f0202a70 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Support walking over tables with only a top grid.

Tables which have only a top grid are now walkable, with unit tests to prove
it.  These tables come out identical to tables with the grid in the left
position, and thus don't require further tests in generators.
parent b07899a2
Loading
Loading
Loading
Loading
+39 −9
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ public class Table {
        return MatlabParser.parseMatlabCode(m_variables, cell.contents());
    }

    private int HandleGrid(SubHierarchyFetcher grid, HierarchicalGridDepthFirstCheckerGenerator generator, int outputCellIndex) {
    private int HandleLeftGrid(SubHierarchyFetcher grid, HierarchicalGridDepthFirstCheckerGenerator generator, int outputCellXIndex, int outputCellYIndex) {
        List<HierarchicalCell> cells = grid.getSubHierarchy();
        for (int i = 0; i < cells.size(); ++i) {
            HierarchicalCell cell = cells.get(i);
@@ -94,26 +94,52 @@ public class Table {
            if (cell.getSubHierarchy().isEmpty()) {
                Map<Variable, Expression> outputCells = new LinkedHashMap<Variable, Expression>();
                for(Variable outputVariable : m_variables.getOutputVariables().values()) {
                    outputCells.put(outputVariable, parseMatlabCode(variableOutputs.get(outputVariable).get(0, outputCellIndex)));
                    outputCells.put(outputVariable, parseMatlabCode(variableOutputs.get(outputVariable).get(outputCellXIndex, outputCellYIndex)));
                }
                generator.handleLeafCell(parseMatlabCode(cell), outputCells);
                outputCellIndex++;
                outputCellYIndex++;
            } else {
                generator.handleEdgeCell(parseMatlabCode(cell));
                generator.descendIntoGridFromCell(parseMatlabCode(cell));
                outputCellIndex = HandleGrid(cell, generator, outputCellIndex);
                outputCellYIndex = HandleLeftGrid(cell, generator, outputCellXIndex, outputCellYIndex);
                generator.ascendFromGrid();
            }
        }
        return outputCellIndex;
        return outputCellYIndex;
    }

    private int HandleTopGrid(SubHierarchyFetcher grid, HierarchicalGridDepthFirstCheckerGenerator generator, int outputCellXIndex, int outputCellYIndex) {
        List<HierarchicalCell> cells = grid.getSubHierarchy();
        for (int i = 0; i < cells.size(); ++i) {
            HierarchicalCell cell = cells.get(i);

            if (cell.getSubHierarchy().isEmpty()) {
                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);
                outputCellXIndex++;
            } else {
                generator.handleEdgeCell(parseMatlabCode(cell));
                generator.descendIntoGridFromCell(parseMatlabCode(cell));
                outputCellXIndex = HandleTopGrid(cell, generator, outputCellXIndex, outputCellYIndex);
                generator.ascendFromGrid();
            }
        }
        return outputCellXIndex;
    }

    public String walk(HierarchicalGridDepthFirstCheckerGenerator generator) {
        HandleGrid(leftGrid, generator, 0);
        if (!leftGrid.getSubHierarchy().isEmpty()) {
            HandleLeftGrid(leftGrid, generator, 0, 0);
        } else {
            HandleTopGrid(topGrid, generator, 0, 0);
        }
        return generator.getFinalString();
    }

    private void HandleGrid(SubHierarchyFetcher grid, HierarchicalGridBreadthFirstCheckerGenerator generator) {
    private void HandleGrid(SubHierarchyFetcher grid, HierarchicalGridBreadthFirstCheckerGenerator generator, boolean checkTopGrid) {
        List<HierarchicalCell> cells = grid.getSubHierarchy();
        for (int i = 0; i < cells.size(); ++i) {
            HierarchicalCell cell = cells.get(i);
@@ -130,14 +156,18 @@ public class Table {

            if (!cell.getSubHierarchy().isEmpty()) {
                generator.descendIntoGridFromCell(parseMatlabCode(cell));
                HandleGrid(cell, generator);
                HandleGrid(cell, generator, checkTopGrid);
                generator.ascendFromGrid();
            }
        }
    }

    public String walk(HierarchicalGridBreadthFirstCheckerGenerator generator) {
        HandleGrid(leftGrid, generator);
        if (!leftGrid.getSubHierarchy().isEmpty()) {
            HandleGrid(leftGrid, generator, true);
        } else {
            HandleGrid(topGrid, generator, false);
        }
        return generator.getFinalString();
    }

+36 −8
Original line number Diff line number Diff line
@@ -98,15 +98,10 @@ public class TableWalkTest {
    public String SingleGridWalkScript;

    /**
     * Tests the ability for Table to walk a left grid only table.
     * <p>
     * This is similar to what all the generators walk for their tests.
     * Prepares the given grid for use by the single grid tests.
     * @param grid Grid to setup.
     */
    @Test
    public void SingleLeftGridWalkTest() {
        Table table = getInitialTable();

        List<HierarchicalCell> grid = table.getLeftGrid().getSubHierarchy();
    private void setupSingleGrid(List<HierarchicalCell> grid) {
        grid.add(new HierarchicalCell("x >= 0"));
        grid.add(new HierarchicalCell("x == 0"));
        grid.add(new HierarchicalCell("x <= 0"));
@@ -118,6 +113,18 @@ public class TableWalkTest {

        grid = grid.get(1).getSubHierarchy();
        grid.add(new HierarchicalCell("z == 0"));
    }

    /**
     * Tests the ability for Table to walk a left grid only table.
     * <p>
     * This is similar to what all the generators walk for their tests.
     */
    @Test
    public void SingleLeftGridWalkTest() {
        Table table = getInitialTable();

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

        final TwoDimensionalGrid outGrid = new TwoDimensionalGrid(1, 5);
        for(int i = 0; i < outGrid.sizeY(); i++) {
@@ -128,6 +135,27 @@ public class TableWalkTest {
        verifyWalk(table, SingleGridWalkScript);
    }

    /**
     * Tests the ability for Table to walk a top grid only table.
     * <p>
     * This generates the same script as the left only grid, and thus all the generators should just work with this
     * table.
     */
    @Test
    public void SingleTopGridWalkTest() {
        Table table = getInitialTable();

        setupSingleGrid(table.getTopGrid().getSubHierarchy());

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

        verifyWalk(table, SingleGridWalkScript);
    }

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