aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parsers/TestCaseParser.java
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-04-26 19:56:24 +0100
committerEduardo Pedroni <ep625@york.ac.uk>2014-04-26 19:56:24 +0100
commit4c8de2402f2878cde7587c7f3bbf4ffaea86efd4 (patch)
tree29156510f648a2d9f8de4df3b2617d4a056e1d90 /src/jcgp/backend/parsers/TestCaseParser.java
parentb0c0698e5503c2506217117bf144fde31e6f6601 (diff)
Moved files around to different folders, and commented some more packages. Aiming to have the entire backend fully commented by the end of today
Diffstat (limited to 'src/jcgp/backend/parsers/TestCaseParser.java')
-rw-r--r--src/jcgp/backend/parsers/TestCaseParser.java74
1 files changed, 40 insertions, 34 deletions
diff --git a/src/jcgp/backend/parsers/TestCaseParser.java b/src/jcgp/backend/parsers/TestCaseParser.java
index cef018e..bcfa8a9 100644
--- a/src/jcgp/backend/parsers/TestCaseParser.java
+++ b/src/jcgp/backend/parsers/TestCaseParser.java
@@ -8,14 +8,34 @@ import java.util.Scanner;
import jcgp.backend.modules.problem.TestCaseProblem;
import jcgp.backend.resources.ModifiableResources;
+/**
+ * Contains a static method for parsing values from a
+ * CGP problem data file. The actual file extension
+ * varies from problem to problem, and is therefore
+ * defined in the experiment's Problem instance.
+ *
+ *
+ * @author Eduardo Pedroni
+ *
+ */
public abstract class TestCaseParser {
+ /**
+ * Sets the number of inputs and outputs in the resources
+ * to match the given file, and parses each test case
+ * from the file into the specified problem.
+ *
+ * @param file the problem file to parse.
+ * @param problem the problem into which to parse the problem data.
+ * @param resources a modifiable reference to the experiment's resources
+ */
public static void parse(File file, TestCaseProblem<?> problem, ModifiableResources resources) {
+ // create reader and scanner, print error message if file is missing
FileReader fr;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
- resources.println("[Parser] Error: could not find " + file.getAbsolutePath() + ".");
+ resources.println("[Parser] Error: could not find " + file.getAbsolutePath());
return;
}
resources.println("[Parser] Parsing file: " + file.getAbsolutePath() + "...");
@@ -24,28 +44,44 @@ public abstract class TestCaseParser {
int inputs = 0, outputs = 0;
int cases = 0;
+ // this overwrites any previously added test cases
problem.clearTestCases();
while (in.hasNextLine()) {
String nextLine = in.nextLine();
-
+ // set resources input count to parsed value
if (nextLine.startsWith(".i")) {
String[] split = nextLine.split(" +");
inputs = Integer.parseInt(split[1]);
- } else if (nextLine.startsWith(".o")) {
+ resources.setInputs(inputs);
+ resources.println("[Parser] Number of inputs set to " + resources.inputs());
+ }
+ // set resources output count to parsed value
+ else if (nextLine.startsWith(".o")) {
String[] split = nextLine.split(" +");
outputs = Integer.parseInt(split[1]);
+ resources.setOutputs(outputs);
+ resources.println("[Parser] Number of outputs set to " + resources.outputs());
+
} else if (nextLine.startsWith(".p") || nextLine.startsWith(".t")) {
readingTestCases = true;
+
} else if (nextLine.startsWith(".e")) {
readingTestCases = false;
+
+ /*
+ * Split every line at one or more spaces or tabs, and
+ * parse the two sides into inputs and outputs.
+ */
} 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];
}
@@ -54,37 +90,7 @@ public abstract class TestCaseParser {
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());
- }
- }
+ resources.println("[Parser] Finished, added " + cases + " test cases");
in.close();
-
- resources.println("[Parser] Finished parsing parameters.");
}
}