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

Add an initial table description.

Add an initial table description to hold what a table is.  Currently I'm not
sure if it is best, as it limits tables to 2 dimensions, and allows for multiple
variable 2 dimensional tables.  But for now it fully represents MATLAB's TET
tables (though TET can't represent all tables represented here ...).
parent d158fbc4
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the distribution
 *     * Neither the name of the McMaster Centre for Software Certification nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ca.mcscert.jtet.tablularexpression;

/**
 * @author matthew
 */
final public class IllegalTableSetup extends Exception {
    public IllegalTableSetup(String message) {
        super(message);
    }
}
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the distribution
 *     * Neither the name of the McMaster Centre for Software Certification nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ca.mcscert.jtet.tablularexpression;

import ca.mcscert.jtet.expression.Variable;
import ca.mcscert.jtet.expression.VariableCollection;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
public class Table {
    public Table(VariableCollection variables) {
        this.variables = variables;
    }

    public void verifyTable() throws IllegalTableSetup {
        if (variableNames.size() != variableOutputs.size()) {
            throw new IllegalTableSetup("The number of output variables and the number of outputs disagree!");
        }

        int leftCount = leftGrid.countTotalCases();
        int topCount = topGrid.countTotalCases();
        for (int i = 0; i < variableOutputs.size(); ++i) {
            TwoDimensionalGrid output = variableOutputs.get(i);
            if (output.sizeX() != topCount || output.sizeY() != leftCount) {
                throw new IllegalTableSetup("There are a different number of outputs then conditions for variable " + variableNames.get(i) + "!");
            }
        }
    }

    public VariableCollection getVariables() {
        return variables;
    }

    public HierarchicalGrid getLeftGrid() {
        return leftGrid;
    }

    public HierarchicalGrid getTopGrid() {
        return topGrid;
    }

    public List<TwoDimensionalGrid> getVariableOutputs() {
        return variableOutputs;
    }

    public List<Variable> getVariableNames() {
        return variableNames;
    }

    private VariableCollection variables;
    private final HierarchicalGrid leftGrid = new HierarchicalGrid();
    private final HierarchicalGrid topGrid = new HierarchicalGrid();
    private final List<TwoDimensionalGrid> variableOutputs = new ArrayList<TwoDimensionalGrid>();
    private final List<Variable> variableNames = new ArrayList<Variable>();

}
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the distribution
 *     * Neither the name of the McMaster Centre for Software Certification nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package ca.mcscert.jtet.tablularexpression.test;

import ca.mcscert.jtet.expression.Variable;
import ca.mcscert.jtet.tablularexpression.HierarchicalCell;
import ca.mcscert.jtet.tablularexpression.IllegalTableSetup;
import ca.mcscert.jtet.tablularexpression.Table;
import ca.mcscert.jtet.tablularexpression.TwoDimensionalGrid;
import org.junit.Test;

public class TableTest {

    @Test(expected = IllegalTableSetup.class)
    public void testVerifyTableWithTooManyVariables() throws IllegalTableSetup {
        Table t = new Table(null);
        t.getVariableNames().add(new Variable("a", null));

        t.verifyTable();
    }

    @Test(expected = IllegalTableSetup.class)
    public void testVerifyTableWithTooManyVariableOutputs() throws IllegalTableSetup {
        Table t = new Table(null);
        t.getVariableOutputs().add(new TwoDimensionalGrid());

        t.verifyTable();
    }

    @Test(expected = IllegalTableSetup.class)
    public void testVerifyTableWithWrongTopSize() throws IllegalTableSetup {
        Table t = new Table(null);
        t.getVariableNames().add(new Variable("a", null));
        t.getVariableOutputs().add(new TwoDimensionalGrid());

        t.getLeftGrid().getSubHiearchy().add(new HierarchicalCell());
        t.getTopGrid().getSubHiearchy().add(new HierarchicalCell());
        t.getTopGrid().getSubHiearchy().add(new HierarchicalCell());

        t.verifyTable();
    }

    @Test(expected = IllegalTableSetup.class)
    public void testVerifyTableWithWrongLeftSize() throws IllegalTableSetup {
        Table t = new Table(null);
        t.getVariableNames().add(new Variable("a", null));
        t.getVariableOutputs().add(new TwoDimensionalGrid());
        t.getVariableOutputs().get(0).resize(2, 5);

        t.getLeftGrid().getSubHiearchy().add(new HierarchicalCell());
        t.getLeftGrid().getSubHiearchy().get(0).getSubHiearchy().add(new HierarchicalCell());
        t.getLeftGrid().getSubHiearchy().get(0).getSubHiearchy().add(new HierarchicalCell());
        t.getLeftGrid().getSubHiearchy().add(new HierarchicalCell());
        t.getLeftGrid().getSubHiearchy().add(new HierarchicalCell());
        t.getTopGrid().getSubHiearchy().add(new HierarchicalCell());
        t.getTopGrid().getSubHiearchy().add(new HierarchicalCell());

        t.verifyTable();
    }
}