package ic.app.se.simple.data;

import ic.app.se.simple.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static ic.app.se.simple.common.Utils.loadSectionData;

/**
 * Created by hjh on 15-10-9.
 *
 * Page 227 ELE
 */
public class BranchTable {

    public static Logger logger= LoggerFactory.getLogger(BranchTable.class);

    private int NOE;

    private int[] i;

    private int[] j;

    private double[] rij;

    private double[] xij;

    //  if yk is below zero, it means susceptance which is -y0/2 otherwise yk is transformer ratio kij
    private double[] yk;

    public double[] getRij() {
        return rij;
    }

    public double[] getXij() {
        return xij;
    }

    public double[] getYk() {
        return yk;
    }

    public int getNOE() {
        return NOE;
    }

    public int[] getI() {
        return i;
    }

    public int[] getJ() {
        return j;
    }

    public void setI(int[] i) {
        this.i = i;
    }

    public void setJ(int[] j) {
        this.j = j;
    }

    public void setNOE(int NOE) {
        this.NOE = NOE;
    }

    public void setRij(double[] rij) {
        this.rij = rij;
    }

    public void setXij(double[] xij) {
        this.xij = xij;
    }

    public void setYk(double[] yk) {
        this.yk = yk;
    }

    public void loadData(List<String> data){

        List<String> branchData=loadSectionData(data, Constants.CDF.BRANCH_SECTION_START_KEY);

        int[] itmp=new int[branchData.size()];

        int[] jtmp=new int[branchData.size()];

        double[] rijtmp=new double[branchData.size()];

        double[] xijtmp=new double[branchData.size()];

        double[] yktmp=new double[branchData.size()];

        for (int k = 0; k < branchData.size(); k++){

            String entry=branchData.get(k);

            String col[]=entry.trim().split(" +");

            if (col.length!=Constants.CDF.BRANCH_SECTION_NUMBER_OF_COLUMN) {

                logger.error("Input data error: {}", entry);

                return;

            }

            itmp[k]=Integer.parseInt(col[0]);

            jtmp[k]=Integer.parseInt(col[1]);

            rijtmp[k]=Double.parseDouble(col[6]);

            xijtmp[k]=Double.parseDouble(col[7]);

            double y=Double.parseDouble(col[8]);

            double tk=Double.parseDouble(col[14]);

            if (tk>0){

                yktmp[k]=tk;

            }else if (y>0){

                yktmp[k]=-y;

            }else {

                yktmp[k]=0.0;

            }

        }

//        these are numbers
        setI(itmp);

        setJ(jtmp);

        setRij(rijtmp);

        setXij(xijtmp);

        setYk(yktmp);

        setNOE(branchData.size());

    }

}

@@ -247,16 +247,20 @@
 10-9.%0A *
+ %3Cp%3E
 %0A * Page
@@ -331,16 +331,17 @@
 r logger
+ 
 = Logger
@@ -1387,16 +1387,17 @@
 g%3E data)
+ 
 %7B%0A%0A     
@@ -1422,17 +1422,19 @@
 anchData
-=
+ = 
 loadSect
@@ -1507,17 +1507,19 @@
 t%5B%5D itmp
-=
+ = 
 new int%5B
@@ -1553,25 +1553,27 @@
   int%5B%5D jtmp
-=
+ = 
 new int%5Bbran
@@ -1608,25 +1608,27 @@
 ble%5B%5D rijtmp
-=
+ = 
 new double%5Bb
@@ -1670,17 +1670,19 @@
 %5D xijtmp
-=
+ = 
 new doub
@@ -1727,17 +1727,19 @@
 %5B%5D yktmp
-=
+ = 
 new doub
@@ -1813,16 +1813,17 @@
 (); k++)
+ 
 %7B%0A%0A     
@@ -1841,17 +1841,19 @@
 ng entry
-=
+ = 
 branchDa
@@ -1888,17 +1888,19 @@
 ng col%5B%5D
-=
+ = 
 entry.tr
@@ -1948,10 +1948,12 @@
 ngth
+ 
 !=
+ 
 Cons
@@ -2115,25 +2115,27 @@
      itmp%5Bk%5D
-=
+ = 
 Integer.pars
@@ -2168,17 +2168,19 @@
  jtmp%5Bk%5D
-=
+ = 
 Integer.
@@ -2211,33 +2211,35 @@
        rijtmp%5Bk%5D
-=
+ = 
 Double.parseDoub
@@ -2272,17 +2272,19 @@
 ijtmp%5Bk%5D
-=
+ = 
 Double.p
@@ -2324,17 +2324,19 @@
 double y
-=
+ = 
 Double.p
@@ -2377,17 +2377,19 @@
 ouble tk
-=
+ = 
 Double.p
@@ -2428,19 +2428,22 @@
   if (tk
-%3E
+ %3E 
 0)
+ 
 %7B%0A%0A     
@@ -2461,17 +2461,19 @@
 yktmp%5Bk%5D
-=
+ = 
 tk;%0A%0A   
@@ -2478,24 +2478,25 @@
            %7D
+ 
 else if (y%3E0
@@ -2497,11 +2497,14 @@
 f (y
-%3E
+ %3E 
 0)
+ 
 %7B%0A%0A 
@@ -2526,17 +2526,19 @@
 yktmp%5Bk%5D
-=
+ = 
 -y;%0A%0A   
@@ -2547,16 +2547,17 @@
        %7D
+ 
 else %7B%0A%0A
@@ -2580,17 +2580,19 @@
 yktmp%5Bk%5D
-=
+ = 
 0.0;%0A%0A  

correct program for estimator