aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parsers/TestCaseParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/backend/parsers/TestCaseParser.java')
-rw-r--r--src/jcgp/backend/parsers/TestCaseParser.java90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/jcgp/backend/parsers/TestCaseParser.java b/src/jcgp/backend/parsers/TestCaseParser.java
new file mode 100644
index 0000000..cef018e
--- /dev/null
+++ b/src/jcgp/backend/parsers/TestCaseParser.java
@@ -0,0 +1,90 @@
+package jcgp.backend.parsers;
+
+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 abstract class TestCaseParser {
+
+ public static void parse(File file, TestCaseProblem<?> problem, ModifiableResources resources) {
+ FileReader fr;
+ try {
+ fr = new FileReader(file);
+ } catch (FileNotFoundException e) {
+ resources.println("[Parser] Error: could not find " + file.getAbsolutePath() + ".");
+ return;
+ }
+ resources.println("[Parser] Parsing file: " + file.getAbsolutePath() + "...");
+ Scanner in = new Scanner(fr);
+ boolean readingTestCases = false;
+ int inputs = 0, outputs = 0;
+ int cases = 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;
+ } 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);
+ cases++;
+ }
+ }
+ resources.println("[Parser] Finished, added " + cases + " test cases.");
+ in.close();
+ }
+
+ public static void parseParameters(File file, ModifiableResources resources) {
+
+ FileReader fr;
+ try {
+ fr = new FileReader(file);
+ } catch (FileNotFoundException e) {
+ resources.println("[Parser] Error: could not find " + file.getAbsolutePath() + ".");
+ return;
+ }
+
+ resources.println("[Parser] Parsing parameters...");
+ 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]));
+ resources.println("[Parser] Number of inputs set to " + resources.inputs());
+ } else if (nextLine.startsWith(".o")) {
+ String[] split = nextLine.split(" +");
+ resources.setOutputs(Integer.parseInt(split[1]));
+ resources.println("[Parser] Number of outputs set to " + resources.outputs());
+ }
+ }
+ in.close();
+
+ resources.println("[Parser] Finished parsing parameters.");
+ }
+}