aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parsers/FunctionParser.java
blob: cc52ce9cb4092e6ca4bebce35663faff62ceaf84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 are 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));
				} else if (Integer.parseInt(splitString[0]) == 0 && functionSet.isEnabled(functionSet.getFunction(functionIndex))) {
					functionSet.disableFunction(functionIndex);
					resources.println("[Parser] Disabled function: " + functionSet.getFunction(functionIndex));
				}
			}
		}
		in.close();
		resources.println("[Parser] Finished parsing functions");
	}
}