package evolutionAlgorithm;

import org.junit.jupiter.api.Test;
import sk.shanki.dbsuite.mapper.data.ComplexCompanyDataFactory;
import sk.shanki.dbsuite.mapper.db.*;
import sk.shanki.dbsuite.mapper.evolutionAlgorithm.EvolutionAlgorithm;

public class EvolutionAlgorithmTimeOnBigData {
    @Test
    public void testEvolutionAlgorithmOneTableChange() {
        int wrongRows = 3;
        Database correctDb = ComplexCompanyDataFactory.instance().createModelDatabase();
        Database studentDb = ComplexCompanyDataFactory.instance().createModelDatabase();
        correctDb.buildReferencedRowsIndex();
        studentDb.buildReferencedRowsIndex();

        for(int tableIndex = 0 ; tableIndex < correctDb.size(); tableIndex++){
            Table studentTable = studentDb.get(tableIndex);
            System.out.println(studentTable.getName());
            for(int i = 0; i < wrongRows; i++){
                studentTable.remove();
            }
            for(int i = 0 ; i < correctDb.size(); i++){
                Table table = studentDb.get(i);
                Table correctTable = correctDb.get(i);
                int studentTableSize = table.size();
                for(int j = 0; j < correctTable.size(); j++){
                    Row correctRow = correctTable.get(j);
                    Row studentRow = null;
                    if(j < studentTableSize){
                        studentRow = table.get(j);
                        studentRow.mapTo(correctRow);
                    }
                    correctRow.mapTo(studentRow);

                }
            }

            double penalization = correctDb.computeDatabasePenalisation(AlphaFormulas.numberOfDataColumns(), RowDistanceFormulas.getCUSTOM());
            penalization += studentDb.computeDatabasePenalisation(AlphaFormulas.numberOfDataColumns(), RowDistanceFormulas.getCUSTOM());
            System.out.println(penalization);
            EvolutionAlgorithm evolutionAlgorithm = new EvolutionAlgorithm(correctDb, studentDb, 1000, 600);
            evolutionAlgorithm.solve(0);

        }

    }
    @Test
    public void testEvolutionAlgorithmEmployees() {
        Database correctDb = ComplexCompanyDataFactory.instance().createModelDatabase();
        Database studentDb = ComplexCompanyDataFactory.instance().createModelDatabase();

        Table studentTable = studentDb.get("employees");
//        System.out.println(studentTable.size());
        for(int wrongRows = 30 ; wrongRows < studentTable.size() / 2; wrongRows+=5){

            System.out.println(wrongRows);
            for(int i = 0; i < wrongRows; i++){
                studentTable.remove();
            }
            for(int i = 0 ; i < correctDb.size(); i++){
                Table table = studentDb.get(i);
                Table correctTable = correctDb.get(i);
                int studentTableSize = table.size();
                for(int j = 0; j < correctTable.size(); j++){
                    Row correctRow = correctTable.get(j);
                    Row studentRow = null;
                    if(j < studentTableSize){
                        studentRow = table.get(j);
                        studentRow.mapTo(correctRow);
                    }
                    correctRow.mapTo(studentRow);

                }
            }
            correctDb.buildReferencedRowsIndex();
            studentDb.buildReferencedRowsIndex();

            double penalization = correctDb.computeDatabasePenalisation(AlphaFormulas.numberOfDataColumns(), RowDistanceFormulas.getCUSTOM());
            penalization += studentDb.computeDatabasePenalisation(AlphaFormulas.numberOfDataColumns(), RowDistanceFormulas.getCUSTOM());
            System.out.println(penalization);
            EvolutionAlgorithm evolutionAlgorithm = new EvolutionAlgorithm(correctDb, studentDb, 1500, 600);
            evolutionAlgorithm.solve(penalization);
            System.out.println("end");


            correctDb = ComplexCompanyDataFactory.instance().createModelDatabase();
            studentDb = ComplexCompanyDataFactory.instance().createModelDatabase();

            studentTable = studentDb.get("employees");
            System.out.println(studentTable.size());

        }

    }
}
