diff options
Diffstat (limited to 'src/jcgp/backend/parsers/FunctionParser.java')
-rw-r--r-- | src/jcgp/backend/parsers/FunctionParser.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/jcgp/backend/parsers/FunctionParser.java b/src/jcgp/backend/parsers/FunctionParser.java new file mode 100644 index 0000000..6d6c73b --- /dev/null +++ b/src/jcgp/backend/parsers/FunctionParser.java @@ -0,0 +1,53 @@ +package jcgp.backend.parsers; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.Scanner; + +import jcgp.backend.function.FunctionSet; +import jcgp.backend.modules.problem.Problem; +import jcgp.backend.resources.ModifiableResources; + +/** + * Parses the functions from a .par file. + * Functions marked with a 1 will be enabled, + * and those marked with 0 are disabled. + * + * @author Eduardo Pedroni + * + */ +public abstract class FunctionParser { + + public static void parseFunctions(File file, Problem problem, ModifiableResources resources) { + + FileReader fr; + try { + fr = new FileReader(file); + } catch (FileNotFoundException e) { + resources.println("[Parser] Error: could not find " + file.getAbsolutePath() + "."); + return; + } + + Scanner in = new Scanner(fr); + FunctionSet functionSet = problem.getFunctionSet(); + resources.println("[Parser] Parsing file: " + file.getAbsolutePath() + "..."); + + while (in.hasNextLine()) { + String line = in.nextLine(); + if (line.substring(line.length() - 1).matches("[0-9]")) { + String[] splitString = line.split("[^0-9]+"); + int functionIndex = Integer.parseInt(splitString[splitString.length - 1]); + if (Integer.parseInt(splitString[0]) != 0 && !functionSet.isEnabled(functionSet.getFunction(functionIndex))) { + functionSet.enableFunction(functionIndex); + resources.println("[Parser] Enabled function: " + functionSet.getFunction(functionIndex).getName() + "."); + } else if (Integer.parseInt(splitString[0]) == 0 && functionSet.isEnabled(functionSet.getFunction(functionIndex))) { + functionSet.disableFunction(functionIndex); + resources.println("[Parser] Disabled function: " + functionSet.getFunction(functionIndex).getName() + "."); + } + } + } + in.close(); + resources.println("[Parser] Finished parsing functions."); + } +} |