Skip to content
Snippets Groups Projects
Commit 9da0aead authored by Reiner Jung's avatar Reiner Jung
Browse files

Added files.

parent 0850ae4d
No related branches found
No related tags found
No related merge requests found
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'
}
/**
*
*/
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment