package ic.app.se.simple.estimate;

import ic.app.se.simple.data.*;

import java.util.List;

import static ic.app.se.simple.common.Utils.readStringFromFile;
/**
 * Created by hjh on 15-10-9.
 */
public class PowerGrid {

    private String id;

    private BranchTable branchTable;

    private MeasurementTable measurementTable;

    private BusNumbers busNumbers;

    private MatrixY matrixYQ;

    private MatrixY matrixYP;

    private MatrixH matrixHQ;

    private MatrixH matrixHP;

    private MatrixHTH matrixHTHQ;

    private MatrixHTH matrixHTHP;

    private EstimatedState state;

    private Estimator estimator;

    private int KPQ;

    public PowerGrid(){

        KPQ=0;

        branchTable=new BranchTable();

        measurementTable=new MeasurementTable();

    }

    public void measure(){

        measurementTable.generateMeasurement(true);

    }

    public void estimate(){

//        state.reset();

        estimator.estimate();

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void initData(String filepath){

        List<String> content=readStringFromFile(filepath);

        branchTable.loadData(content);

        measurementTable.loadData(content);

        busNumbers=new BusNumbers(branchTable,measurementTable);

        matrixYP =new MatrixY(branchTable,busNumbers,0);

        matrixYQ =new MatrixY(branchTable,busNumbers,1);

        matrixHQ =new MatrixH(branchTable,busNumbers,measurementTable, matrixYQ);

        matrixHP =new MatrixH(branchTable,busNumbers,measurementTable, matrixYP);

        matrixHTHQ=new MatrixHTH(busNumbers,matrixHQ);

        matrixHTHP=new MatrixHTH(busNumbers,matrixHP);

        state=new EstimatedState(busNumbers.getNOB(),measurementTable.getNOM(),measurementTable.getV0(),0);

        state.setTIO(busNumbers.getTIO());

        estimator=new Estimator(this);

    }

    public MeasurementTable getMeasurementTable() {
        return measurementTable;
    }

    public EstimatedState getState() {
        return state;
    }

    public BusNumbers getBusNumbers() {
        return busNumbers;
    }

    public BranchTable getBranchTable() {
        return branchTable;
    }

    public MatrixY getMatrixY() {

        if (KPQ==1){

            return matrixYQ;

        }else {

            return matrixYP;

        }

    }

    public MatrixH getMatrixH() {

        if (KPQ==1){

            return matrixHQ;

        }else {

            return matrixHP;

        }

    }

    public MatrixHTH getMatrixHTH() {

        if (KPQ==1){

            return matrixHTHQ;

        }else {

            return matrixHTHP;

        }
    }

    public void setKPQ(int KPQ) {
        this.KPQ = KPQ;
    }

    public int getKPQ() {
        return KPQ;
    }
}

@@ -150,16 +150,17 @@
 omFile;%0A
+%0A
 /**%0A * C
@@ -668,16 +668,17 @@
 erGrid()
+ 
 %7B%0A%0A     
@@ -683,17 +683,19 @@
      KPQ
-=
+ = 
 0;%0A%0A    
@@ -705,25 +705,27 @@
  branchTable
-=
+ = 
 new BranchTa
@@ -756,17 +756,19 @@
 entTable
-=
+ = 
 new Meas
@@ -816,16 +816,17 @@
 easure()
+ 
 %7B%0A%0A     
@@ -906,16 +906,17 @@
 timate()
+ 
 %7B%0A%0A//   
@@ -1133,16 +1133,17 @@
 ilepath)
+ 
 %7B%0A%0A     
@@ -1165,17 +1165,19 @@
  content
-=
+ = 
 readStri
@@ -1302,17 +1302,19 @@
 sNumbers
-=
+ = 
 new BusN
@@ -1332,16 +1332,17 @@
 chTable,
+ 
 measurem
@@ -1371,16 +1371,17 @@
 trixYP =
+ 
 new Matr
@@ -1388,32 +1388,33 @@
 ixY(branchTable,
+ 
 busNumbers,0);%0A%0A
@@ -1408,16 +1408,17 @@
 Numbers,
+ 
 0);%0A%0A   
@@ -1432,16 +1432,17 @@
 trixYQ =
+ 
 new Matr
@@ -1449,32 +1449,33 @@
 ixY(branchTable,
+ 
 busNumbers,1);%0A%0A
@@ -1469,16 +1469,17 @@
 Numbers,
+ 
 1);%0A%0A   
@@ -1493,16 +1493,17 @@
 trixHQ =
+ 
 new Matr
@@ -1510,32 +1510,33 @@
 ixH(branchTable,
+ 
 busNumbers,measu
@@ -1522,32 +1522,33 @@
 ble, busNumbers,
+ 
 measurementTable
@@ -1579,16 +1579,17 @@
 trixHP =
+ 
 new Matr
@@ -1604,16 +1604,17 @@
 chTable,
+ 
 busNumbe
@@ -1616,16 +1616,17 @@
 Numbers,
+ 
 measurem
@@ -1665,17 +1665,19 @@
 trixHTHQ
-=
+ = 
 new Matr
@@ -1689,24 +1689,25 @@
 (busNumbers,
+ 
 matrixHQ);%0A%0A
@@ -1724,17 +1724,19 @@
 trixHTHP
-=
+ = 
 new Matr
@@ -1752,16 +1752,17 @@
 Numbers,
+ 
 matrixHP
@@ -1778,17 +1778,19 @@
    state
-=
+ = 
 new Esti
@@ -1820,16 +1820,17 @@
 etNOB(),
+ 
 measurem
@@ -1847,16 +1847,17 @@
 etNOM(),
+ 
 measurem
@@ -1873,16 +1873,17 @@
 getV0(),
+ 
 0);%0A%0A   
@@ -1940,17 +1940,19 @@
 stimator
-=
+ = 
 new Esti
@@ -2325,36 +2325,39 @@
 %0A        if (KPQ
+ 
 ==
+ 
 1)
+ 
 %7B%0A%0A            r
@@ -2374,32 +2374,33 @@
 ixYQ;%0A%0A        %7D
+ 
 else %7B%0A%0A        
@@ -2481,36 +2481,39 @@
 %0A        if (KPQ
+ 
 ==
+ 
 1)
+ 
 %7B%0A%0A            r
@@ -2530,32 +2530,33 @@
 ixHQ;%0A%0A        %7D
+ 
 else %7B%0A%0A        
@@ -2653,12 +2653,15 @@
 (KPQ
+ 
 ==
+ 
 1)
+ 
 %7B%0A%0A 
@@ -2700,16 +2700,17 @@
        %7D
+ 
 else %7B%0A%0A

correct program for estimator