ruleprunermt2.CoverageRemover.java Source code

Java tutorial

Introduction

Here is the source code for ruleprunermt2.CoverageRemover.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ruleprunermt2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author krzysztofzabinski
 */
public class CoverageRemover implements Runnable {
    private static List<List<String>> inputList = new ArrayList<>();
    private static List<List<String>> inputRules = new ArrayList<>();
    private static File[] inputFiles;
    private static File[] inputFilesRules;

    public static void readInput() {
        inputFiles = null;
        inputList.clear();
        File inputDirectory = new File("train/.");
        inputFiles = inputDirectory.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith("train.csv");
            }
        });

        for (File file : inputFiles) {
            List<String> tmpList = new ArrayList<>();

            BufferedReader reader = null;
            String line;
            try {

                reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
                while ((line = reader.readLine()) != null) {
                    tmpList.add(line);
                }

            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }
            }

            inputList.add(tmpList);
        }

    }

    public static void readRules() {
        inputFilesRules = null;
        inputRules.clear();
        File inputDirectory = new File("rules/.");
        inputFilesRules = inputDirectory.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".csv");
            }
        });
        for (File file : inputFilesRules) {
            List<String> tmpList = new ArrayList<>();

            BufferedReader reader = null;
            String line;
            try {

                reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
                while ((line = reader.readLine()) != null) {
                    tmpList.add(line);
                }

            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }

            }
            inputRules.add(tmpList);
        }
    }

    public static void removeRules() {
        //calculate min coverage
        int minCoverage = 0;
        for (int i = 0; i < inputRules.size(); i++) {
            for (int j = 0; j < inputRules.get(i).size(); j++) {
                String[] tmpRule = inputRules.get(i).get(j).split("->|,|\\(|\\)");
                List<String> tmpSetOfRules = new ArrayList<>();
                for (String tmpRule1 : tmpRule) {
                    if (tmpRule1.contains("=")) {
                        tmpSetOfRules.add(tmpRule1.split(" = ")[1]);
                    }
                }
                tmpSetOfRules.add(tmpRule[tmpRule.length - 1]);
                int coverageCounter = 0;
                for (int k = 0; k < inputList.get(i).size(); k++) {
                    int errorCounter = 0;
                    for (int kk = 0; kk < tmpSetOfRules.size(); kk++) {
                        if (!inputList.get(i).get(k).contains(tmpSetOfRules.get(kk))) {
                            errorCounter++;
                        } else if (errorCounter == 0 && kk >= (tmpSetOfRules.size() - 1)) {
                            coverageCounter++;
                        }
                    }
                }
                if (i == 0 && j == 0) {
                    minCoverage = coverageCounter;
                } else if (minCoverage > coverageCounter) {
                    minCoverage = coverageCounter;
                }
            }
        }

        //remove rules with min coverage
        for (int i = 0; i < inputRules.size(); i++) {
            for (int j = 0; j < inputRules.get(i).size(); j++) {
                String[] tmpRule = inputRules.get(i).get(j).split("->|,|\\(|\\)");
                List<String> tmpSetOfRules = new ArrayList<>();
                for (String tmpRule1 : tmpRule) {
                    if (tmpRule1.contains("=")) {
                        tmpSetOfRules.add(tmpRule1.split(" = ")[1]);
                    }
                }
                int coverageCounter = 0;
                for (int k = 0; k < inputList.get(i).size(); k++) {
                    int errorCounter = 0;
                    for (int kk = 0; kk < tmpSetOfRules.size(); kk++) {
                        if (!inputList.get(i).get(k).contains(tmpSetOfRules.get(kk))) {
                            errorCounter++;
                        } else if (errorCounter == 0 && kk >= (tmpSetOfRules.size() - 1)) {
                            coverageCounter++;
                        }
                    }
                }
                if (coverageCounter <= minCoverage) {
                    inputRules.get(i).remove(j);
                }
            }
        }
    }

    public static void writeResultToFile() throws IOException {
        FileUtils.cleanDirectory(new File("rules/"));
        for (int i = 0; i < inputRules.size(); i++) {
            try (FileWriter writeFile = new FileWriter("rules/" + inputFilesRules[i].getName())) {
                for (int j = 0; j < inputRules.get(i).size(); j++) {
                    writeFile.append(inputRules.get(i).get(j) + "\n");
                }
                writeFile.close();
            }
        }
    }

    @Override
    public void run() {
        readInput();
        readRules();
        removeRules();
        try {
            writeResultToFile();
        } catch (IOException ex) {
            Logger.getLogger(CoverageRemover.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}