diff --git a/tools/compile-results/build.gradle b/tools/compile-results/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..aaf434a860f4f820ac9b5241ebcc3d0cbbed6d87 --- /dev/null +++ b/tools/compile-results/build.gradle @@ -0,0 +1,18 @@ +plugins { + id 'java' + id 'application' +} + +application { + mainClass = 'moobench.tools.compile.results.CompileResultsMain' +} + +dependencies { + // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core + implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.3' + // https://mvnrepository.com/artifact/org.apache.commons/commons-csv + implementation 'org.apache.commons:commons-csv:1.8' + + implementation 'org.slf4j:slf4j-api:1.7.+' + implementation 'ch.qos.logback:logback-classic:1.2.3' +} diff --git a/tools/compile-results/src/main/java/moobench/tools/compile/results/CompileResultsMain.java b/tools/compile-results/src/main/java/moobench/tools/compile/results/CompileResultsMain.java new file mode 100644 index 0000000000000000000000000000000000000000..6b40cdeeaf7f66bb47dfe1f73beed3516b6bd5ef --- /dev/null +++ b/tools/compile-results/src/main/java/moobench/tools/compile/results/CompileResultsMain.java @@ -0,0 +1,88 @@ +/** + * + */ +package moobench.tools.compile.results; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.DoubleNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Read the CSV output of the R script and the existing JSON file and append a + * record to the JSON file based on the CSV dataset. + * + * @author Reiner Jung + * + */ +public class CompileResultsMain { + + public static void main(String[] args) { + try { + final JsonNode rootNode; + if (Paths.get(args[1]).toFile().exists()) { + /** Read JSON file. */ + rootNode = readJsonFile(Paths.get(args[1]).toFile()); + } else { + rootNode = readJsonString(); + } + + JsonNode resultsNode = rootNode.get("results"); + + if (!(resultsNode instanceof ArrayNode)) { + System.exit(1); + } + + ArrayNode arrayResultsNode = (ArrayNode)resultsNode; + + /** Read CSV file. */ + final CSVParser csvParser = new CSVParser(Files.newBufferedReader(Paths.get(args[0])), + CSVFormat.DEFAULT.withHeader()); + List<String> header = csvParser.getHeaderNames(); + + JsonNodeFactory factory = JsonNodeFactory.instance; + + /** Put CSV in JSON. */ + for (CSVRecord record : csvParser.getRecords()) { + Map<String, JsonNode> recordMap = new HashMap<>(); + for (int i=0; i < record.size(); i++) { + recordMap.put(header.get(i), new DoubleNode(Double.parseDouble(record.get(i)))); + } + arrayResultsNode.add(new ObjectNode(factory, recordMap)); + } + + /** Write JSON file. */ + ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(Paths.get(args[1]).toFile(), rootNode); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static JsonNode readJsonString() throws JsonMappingException, JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + String value = "{ \"results\" : [] }"; + return mapper.readTree(value); + } + + private static JsonNode readJsonFile(File file) throws JsonProcessingException, IOException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.readTree(file); + } +}