Commit 674f46c1 authored by Yanjun Jiang's avatar Yanjun Jiang
Browse files

EventB Uses Table class.

Combine the several parameters in the constructor to one table object
parameter to make the interface more clean.
parent 83da113b
Loading
Loading
Loading
Loading
+6 −17
Original line number Diff line number Diff line
@@ -29,23 +29,16 @@
package ca.mcscert.jtet.eventbgenerator;

import ca.mcscert.jtet.expression.EventBGenerator;
import ca.mcscert.jtet.expression.VariableCollection;
import ca.mcscert.jtet.tablularexpression.*;

import java.util.List;

/**
 *
 * @author Yanjun Jiang <jiangy76@mcmaster.ca>
 */
final public class EventBTableGenerator {

    public EventBTableGenerator(VariableCollection variableDefinitions, String tableName, HierarchicalGrid leftGrid,  List<String> outputVariables, TwoDimensionalGrid outputGrid) {
        m_variableDefinitions = variableDefinitions;
        m_tableName = tableName;
        m_leftGrid  = leftGrid;
        m_outputVariables =  outputVariables;
        m_outputGrid = outputGrid;
    public EventBTableGenerator(Table table) {
        m_table = table;
    }

    public String generateEventBXml(){
@@ -58,11 +51,11 @@ final public class EventBTableGenerator {
        m_output += machineFileCfgPreXml;

        //Step 2: Generate the Variables & Invariants Xml
        m_output += m_eventBGenerator.GenerateVariablesDeclaration(m_variableDefinitions);
        m_output += m_eventBGenerator.GenerateVariablesDeclaration(m_table.getInputVariables());

        //Step 3: Generate the Events Xml
        m_hierarchcialGridCheckerGenerator = new HierarchicalGridEventBGenerator(m_variableDefinitions, m_tableName, m_outputVariables, m_outputGrid);
        m_output  += HierarchcialGridEventBCheckerWalkerGenerator.GenerateCheckerFromGrid(m_leftGrid, m_hierarchcialGridCheckerGenerator);
        m_hierarchcialGridCheckerGenerator = new HierarchicalGridEventBGenerator(m_table);
        m_output  += HierarchcialGridEventBCheckerWalkerGenerator.GenerateCheckerFromGrid(m_table.getLeftGrid(), m_hierarchcialGridCheckerGenerator);

        //Step 3: Generate the Post Xml
        m_output += machineFileCfgPostXml;
@@ -70,12 +63,8 @@ final public class EventBTableGenerator {
        return m_output;
    }

    Table m_table;
    String m_output = "";
    String m_tableName = "";
    HierarchicalGrid m_leftGrid;
    List<String> m_outputVariables;
    TwoDimensionalGrid m_outputGrid;
    VariableCollection m_variableDefinitions;
    EventBGenerator m_eventBGenerator = new EventBGenerator();
    HierarchcialGridCheckerGenerator m_hierarchcialGridCheckerGenerator;
}
 No newline at end of file
+8 −6
Original line number Diff line number Diff line
@@ -30,10 +30,12 @@ package ca.mcscert.jtet.eventbgenerator;

import ca.mcscert.jtet.expression.BooleanVariableType;
import ca.mcscert.jtet.expression.EventBGenerator;
import ca.mcscert.jtet.expression.Variable;
import ca.mcscert.jtet.expression.VariableCollection;
import ca.mcscert.jtet.parsers.MatlabParser;
import ca.mcscert.jtet.tablularexpression.Cell;
import ca.mcscert.jtet.tablularexpression.HierarchcialGridCheckerGenerator;
import ca.mcscert.jtet.tablularexpression.Table;
import ca.mcscert.jtet.tablularexpression.TwoDimensionalGrid;

import java.util.ArrayDeque;
@@ -46,11 +48,11 @@ import java.util.List;
 */
final public class HierarchicalGridEventBGenerator implements HierarchcialGridCheckerGenerator {

    public HierarchicalGridEventBGenerator(VariableCollection variableDefinitions, String tableName, List<String> outputVariables, TwoDimensionalGrid outputGrid) {
        m_variableDefinitions = variableDefinitions;
        m_tableName = tableName;
        m_outputGrid = outputGrid;
        m_outputVariables = outputVariables;
    public HierarchicalGridEventBGenerator(Table table) {
        m_variableDefinitions = table.getInputVariables();
        m_tableName = table.getTableName();
        m_outputGrid = table.getVariableOutputs().get(0);
        m_outputVariables = table.getOutputVariables();
    }

    private String getCurrentEventName(){
@@ -132,7 +134,7 @@ final public class HierarchicalGridEventBGenerator implements HierarchcialGridCh
    int m_currentEventNo = 0;
    String m_eventsXml = "";
    String m_tableName = "";
    List<String> m_outputVariables;
    List<Variable> m_outputVariables;
    TwoDimensionalGrid m_outputGrid;
    VariableCollection m_variableDefinitions;
    Deque<String> m_parentCellsEventBCode = new ArrayDeque<String>();
+29 −39
Original line number Diff line number Diff line
@@ -28,17 +28,17 @@
 */
package ca.mcscert.jtet.eventbgenerator;

import ca.mcscert.jtet.expression.VariableCollection;
import ca.mcscert.jtet.expression.Variable;
import ca.mcscert.jtet.parsers.VariableParser;
import ca.mcscert.jtet.tablularexpression.HierarchicalCell;
import ca.mcscert.jtet.tablularexpression.HierarchicalGrid;
import ca.mcscert.jtet.tablularexpression.Table;
import ca.mcscert.jtet.tablularexpression.TwoDimensionalGrid;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

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

import static org.junit.Assert.assertEquals;
@@ -53,12 +53,10 @@ public class EventBTableGeneratorTest {
    @Test
    public void testOneLayerTableEventBGenerator() {

        //construct table name
        String tableName = "oneLayerTable";

        Table table = new Table("oneLayerTable", variableParser.parseVariables("x,output"));

        /*********construct left conditional Grid***********/
        HierarchicalGrid leftGrid = new HierarchicalGrid();
        HierarchicalGrid leftGrid = table.getLeftGrid();
        List<HierarchicalCell> cells = leftGrid.getSubHiearchy();

        cells.add(new HierarchicalCell("x >= 0"));
@@ -66,18 +64,18 @@ public class EventBTableGeneratorTest {


        //construct Output Variables(Acquired from TopGrid in Matlab GUI)
        List<String> outputVariables = new ArrayList<String>();
        outputVariables.add("output");
        List<Variable> outputVariables = table.getOutputVariables();
        outputVariables.add(new Variable("output",null));


        //construct output Grid
        TwoDimensionalGrid outputGrid = new TwoDimensionalGrid(1, 2);
        outputGrid.get(0,0).setContents("1");
        outputGrid.get(0,1).setContents("2");
        table.getVariableOutputs().add(outputGrid);


                                                                                                                                                                                                                                                                                                                    //        nextGrid.get(1).getSubHiearchy().add(new HierarchicalCell("z == 0"));
        VariableCollection variableCollection = variableParser.parseVariables("x,output");
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(variableCollection, tableName,leftGrid, outputVariables, outputGrid);
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(table);
        String out = eventBTableGenerator.generateEventBXml();

        String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
@@ -105,11 +103,10 @@ public class EventBTableGeneratorTest {

    @Test
    public void testTwoLayerTableEventBGenerator() {
        //construct table name
        String tableName = "twoLayerTable";
        Table table = new Table("twoLayerTable", variableParser.parseVariables("x,z,output"));

        //construct left condition Grid
        HierarchicalGrid leftGrid = new HierarchicalGrid();
        HierarchicalGrid leftGrid = table.getLeftGrid();
        List<HierarchicalCell> cells = leftGrid.getSubHiearchy();

        cells.add(new HierarchicalCell("x > 0"));
@@ -123,8 +120,8 @@ public class EventBTableGeneratorTest {


        //construct Output Variables(Acquired from TopGrid in Matlab GUI)
        List<String> outputVariables = new ArrayList<String>();
        outputVariables.add("output");
        List<Variable> outputVariables = table.getOutputVariables();
        outputVariables.add(new Variable("output",null));

        //construct output Grid
        TwoDimensionalGrid outputGrid = new TwoDimensionalGrid(1, 5);
@@ -133,9 +130,9 @@ public class EventBTableGeneratorTest {
        outputGrid.get(0,2).setContents("3");
        outputGrid.get(0,3).setContents("4");
        outputGrid.get(0,4).setContents("5");
        table.getVariableOutputs().add(outputGrid);

        VariableCollection variableCollection = variableParser.parseVariables("x,z,output");
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(variableCollection, tableName,leftGrid, outputVariables, outputGrid);
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(table);
        String out = eventBTableGenerator.generateEventBXml();

        String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
@@ -179,14 +176,12 @@ public class EventBTableGeneratorTest {

    }


    @Test
    public void testThreeLayerTableEventBGenerator() {
        //construct table name
        String tableName = "threeLayerTable";
        Table table = new Table("threeLayerTable", variableParser.parseVariables("x,y,z,w,output"));

        //construct left condition Grid
        HierarchicalGrid leftGrid = new HierarchicalGrid();
        HierarchicalGrid leftGrid = table.getLeftGrid();
        List<HierarchicalCell> cells = leftGrid.getSubHiearchy();

        cells.add(new HierarchicalCell("x > 0"));
@@ -207,8 +202,9 @@ public class EventBTableGeneratorTest {


        //construct Output Variables(Acquired from TopGrid in Matlab GUI)
        List<String> outputVariables = new ArrayList<String>();
        outputVariables.add("output");
        //construct Output Variables(Acquired from TopGrid in Matlab GUI)
        List<Variable> outputVariables = table.getOutputVariables();
        outputVariables.add(new Variable("output",null));


        //construct output Grid
@@ -221,9 +217,9 @@ public class EventBTableGeneratorTest {
        outputGrid.get(0,5).setContents("6");
        outputGrid.get(0,6).setContents("7");
        outputGrid.get(0,7).setContents("8");
        table.getVariableOutputs().add(outputGrid);

        VariableCollection variableCollection = variableParser.parseVariables("x,y,z,w,output");
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(variableCollection, tableName,leftGrid, outputVariables, outputGrid);
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(table);
        String out = eventBTableGenerator.generateEventBXml();

        String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
@@ -300,32 +296,27 @@ public class EventBTableGeneratorTest {
        //C_pwrConfirm is treated as enumerate here. It can choose values from
        //False:0 True:1

        //construct table name
        String tableName = "IIP";
        Table table = new Table("IIP", variableParser.parseVariables("cPwrStatus,cPwrConfirm"));

        //construct left Conditional Grid
        HierarchicalGrid leftGrid = new HierarchicalGrid();
        //construct left condition Grid
        HierarchicalGrid leftGrid = table.getLeftGrid();
        List<HierarchicalCell> cells = leftGrid.getSubHiearchy();

        //if I use c_pwrStatus, error would occur
        cells.add(new HierarchicalCell("cPwrStatus == 4"));
        cells.add(new HierarchicalCell("cPwrStatus ~= 4"));



        //construct Output Variables(Acquired from TopGrid in Matlab GUI)
        List<String> outputVariables = new ArrayList<String>();
        outputVariables.add("cPwrConfirm");

        List<Variable> outputVariables = table.getOutputVariables();
        outputVariables.add(new Variable("cPwrConfirm",null));

        //construct outputGrid
        TwoDimensionalGrid outputGrid = new TwoDimensionalGrid(1, 2);
        outputGrid.get(0,0).setContents("0");
        outputGrid.get(0,1).setContents("1");
        table.getVariableOutputs().add(outputGrid);


        VariableCollection variableCollection = variableParser.parseVariables("cPwrStatus,cPwrConfirm");
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(variableCollection, tableName,leftGrid, outputVariables, outputGrid);
        EventBTableGenerator eventBTableGenerator = new EventBTableGenerator(table);
        String out = eventBTableGenerator.generateEventBXml();

        String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
@@ -351,6 +342,5 @@ public class EventBTableGeneratorTest {

    }


    VariableParser variableParser = new VariableParser();
}