Commit 5ca0a995 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Move SAL generator over to the new method of getting output cells.

The SAL generator now uses the passed in output variables to get the output
expression for the given variable.  Due to limitiations with SAL syntax,
the generator needs to be run once per variable, so the generator takes in
the variable to output for.
parent 89af17e2
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -43,10 +43,10 @@ import java.util.Map;
 */
public class HierarchicalGridSALGenerator implements HierarchicalGridDepthFirstCheckerGenerator {

    public HierarchicalGridSALGenerator(VariableCollection variableDefinitions, TwoDimensionalGrid outputGrid)
    public HierarchicalGridSALGenerator(VariableCollection variableDefinitions, Variable outputVariable)
    {
        this.variableDefinitions = variableDefinitions;
        this.outputGrid = outputGrid;
        this.m_outputVariable = outputVariable;
    }

    @Override
@@ -80,8 +80,11 @@ public class HierarchicalGridSALGenerator implements HierarchicalGridDepthFirstC
    @Override
    public void handleLeafCell(Cell cell, Map<Variable, Cell> outputCells)
    {
        if (m_firstOutputCell == null) {
            m_firstOutputCell = outputCells.get(m_outputVariable);
        }
        outputCellPrelude(cell);
        result.append(getOutputCellAsSALCode(m_rightMostGridIndex++));
        result.append(getOutputCellAsSALCode(outputCells.get(m_outputVariable)));
        result.append(" ");
    }

@@ -102,22 +105,22 @@ public class HierarchicalGridSALGenerator implements HierarchicalGridDepthFirstC

    private void handleEndIf() {
        result.append("ELSE ")
                .append(getOutputCellAsSALCode(0))
                .append(getOutputCellAsSALCode(m_firstOutputCell))
                .append(" ENDIF");
    }

    private String getOutputCellAsSALCode(int yIndex) {
        return MatlabParser.parseMatlabCode(variableDefinitions, outputGrid.get(0, yIndex).contents()).getCheckerOutput(m_generator, RealVariableType.Type);
    private String getOutputCellAsSALCode(Cell outputCell) {
        return MatlabParser.parseMatlabCode(variableDefinitions, outputCell.contents()).getCheckerOutput(m_generator, m_outputVariable.type());
    }

    final private StringBuilder result = new StringBuilder();

    private boolean m_currentlyRunning = false;
    private boolean m_firstCell = true;
    private int m_rightMostGridIndex = 0;
    private final Variable m_outputVariable;
    private Cell m_firstOutputCell;

    final private VariableCollection variableDefinitions;
    final private TwoDimensionalGrid outputGrid;

    final private SALExpressionGenerator m_generator = new SALExpressionGenerator();
}
+13 −4
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@
 */
package ca.mcscert.jtet.salgenerator;

import ca.mcscert.jtet.expression.PartialVariableCollection;
import ca.mcscert.jtet.expression.RealVariableType;
import ca.mcscert.jtet.expression.Variable;
import ca.mcscert.jtet.expression.VariableCollection;
import ca.mcscert.jtet.parsers.VariableParser;
import ca.mcscert.jtet.tablularexpression.*;
@@ -35,7 +38,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

@@ -49,7 +54,10 @@ public class HierarchicalGridSALGeneratorTest {
    // Exercise the cvc3 generator using a table designed in matlab.
    @Test
    public void exerciseCVC3Generator() {
        Table table = new Table(null, new VariableCollection(variableParser.parseVariables("x,z")));
        Variable outputVar = new Variable("output", new RealVariableType());
        Map<String, Variable> variableMap = new HashMap<String, Variable>(1);
        variableMap.put(outputVar.name(), outputVar);
        Table table = new Table(null, new VariableCollection(variableParser.parseVariables("x,z"), new PartialVariableCollection(variableMap)));
        List<HierarchicalCell> topCells = table.getLeftGrid().getSubHierarchy();

        topCells.add(new HierarchicalCell("x > 0"));
@@ -63,12 +71,14 @@ public class HierarchicalGridSALGeneratorTest {

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

        TwoDimensionalGrid outputGrid = new TwoDimensionalGrid();
        outputGrid.resize(1, 5);
        for (int i = 0; i < outputGrid.sizeY(); ++i) {
            outputGrid.get(0, i).setContents(String.valueOf(i + 1));
        }
        table.getVariableOutputs().put(outputVar, outputGrid);

        String out = table.walk(new HierarchicalGridSALGenerator(table.getVariables(), outputGrid));
        String out = table.walk(new HierarchicalGridSALGenerator(table.getVariables(), outputVar));

        String expected = "IF (x > 0) THEN 1 ELSIF (x = 0) THEN IF (z > 0) THEN 2 ELSIF (z = 0) THEN IF (z = 0) THEN 3 ELSE 1 ENDIF ELSIF (0 > z) THEN 4 ELSE 1 ENDIF ELSIF (0 > x) THEN 5 ELSE 1 ENDIF";
        assertEquals(expected, out);
@@ -79,7 +89,7 @@ public class HierarchicalGridSALGeneratorTest {
    public void testEmptyTable() {
        Table table = new Table(null, new VariableCollection(variableParser.parseVariables("")));

        String out = table.walk(new HierarchicalGridSALGenerator(new VariableCollection(variableParser.parseVariables("x,z")), outputGrid));
        String out = table.walk(new HierarchicalGridSALGenerator(new VariableCollection(variableParser.parseVariables("x,z")), null));

        String expected = "";

@@ -87,5 +97,4 @@ public class HierarchicalGridSALGeneratorTest {
    }

    VariableParser variableParser = new VariableParser();
    TwoDimensionalGrid outputGrid = new TwoDimensionalGrid();
}