aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parser/TestCaseParser.java
blob: d47d6631ac6bafc22db910bce68d29849ab6e7ef (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package jcgp.backend.parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import jcgp.backend.modules.problem.TestCaseProblem;
import jcgp.backend.resources.ModifiableResources;

public class TestCaseParser {

	private TestCaseProblem<?> problem;
	
	public TestCaseParser(TestCaseProblem<?> problem) {
		this.problem = problem;
	}
	
	public void parse(File file) {
		FileReader fr;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		
		Scanner in = new Scanner(fr);
		boolean readingTestCases = false;
		int inputs = 0, outputs = 0;
		
		problem.clearTestCases();
		
		while (in.hasNextLine()) {
			String nextLine = in.nextLine();
			
			if (nextLine.startsWith(".i")) {
				String[] split = nextLine.split(" +");
				inputs = Integer.parseInt(split[1]);
			} else if (nextLine.startsWith(".o")) {
				String[] split = nextLine.split(" +");
				outputs = Integer.parseInt(split[1]);
			} else if (nextLine.startsWith(".p") || nextLine.startsWith(".t")) {
				readingTestCases = true;
			} else if (nextLine.startsWith(".e")) {
				readingTestCases = false;
				// set test cases? not safe probably
			} else if (readingTestCases) {
				String[] split = nextLine.split("( |\t)+");
				String[] inputCases = new String[inputs];
				String[] outputCases = new String[outputs];
				for (int i = 0; i < inputs; i++) {
					inputCases[i] = split[i];
				}
				for (int o = 0; o < outputs; o++) {
					outputCases[o] = split[o + inputs];
				}
				
				problem.addTestCase(inputCases, outputCases);
			}
		}
		
		in.close();
	}

	public static void parseParameters(File file, ModifiableResources resources) {
		
		FileReader fr;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}
		
		Scanner in = new Scanner(fr);

		while (in.hasNextLine()) {
			String nextLine = in.nextLine();
			if (nextLine.startsWith(".i")) {
				String[] split = nextLine.split(" +");
				resources.setInputs(Integer.parseInt(split[1]));
			} else if (nextLine.startsWith(".o")) {
				String[] split = nextLine.split(" +");
				resources.setOutputs(Integer.parseInt(split[1]));
			}
		}
		in.close();
	}
}