|
@@ -0,0 +1,123 @@
|
|
|
+package csv;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.io.FileWriter;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import org.apache.commons.csv.CSVFormat;
|
|
|
+import org.apache.commons.csv.CSVParser;
|
|
|
+import org.apache.commons.csv.CSVRecord;
|
|
|
+
|
|
|
+public class CSV2OML {
|
|
|
+
|
|
|
+ public static void generateOML(String tableName, String csvPath, String outputPath, String version, String iri) throws FileNotFoundException, IOException {
|
|
|
+ CSVParser parser;
|
|
|
+
|
|
|
+ String header = generateHeader(iri);
|
|
|
+
|
|
|
+ parser = new CSVParser(new FileReader(csvPath), CSVFormat.DEFAULT);
|
|
|
+ List<CSVRecord> list = parser.getRecords();
|
|
|
+ String table = "";
|
|
|
+ String headerRow = "";
|
|
|
+ StringBuilder rows = new StringBuilder();
|
|
|
+ StringBuilder cells = new StringBuilder();
|
|
|
+
|
|
|
+ table = generateTable(tableName, list, version, csvPath);
|
|
|
+
|
|
|
+ for (CSVRecord record : list) {
|
|
|
+
|
|
|
+ if (record.getRecordNumber() == 1) {
|
|
|
+ headerRow = generateRow(record, tableName, true);
|
|
|
+ } else {
|
|
|
+ rows.append(generateRow(record, tableName, false));
|
|
|
+ }
|
|
|
+
|
|
|
+ cells.append(generateCells(record, tableName));
|
|
|
+
|
|
|
+ }
|
|
|
+ parser.close();
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
+ content.append(header);
|
|
|
+ content.append(table);
|
|
|
+ content.append(headerRow);
|
|
|
+ content.append(rows);
|
|
|
+ content.append(cells);
|
|
|
+ content.append("\n}");
|
|
|
+ FileWriter fw = new FileWriter(new File(outputPath));
|
|
|
+ fw.write(content.toString());
|
|
|
+ fw.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String generateHeader(String iri) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("description " + iri + " as csv {\n");
|
|
|
+ sb.append("\tuses <http://ua.com.be/sdo2l/vocabulary/processtraces#> as traces\n");
|
|
|
+ sb.append("\tuses <http://ua.com.be/sdo2l/vocabulary/base/file#> as file\n");
|
|
|
+ sb.append("\tuses <http://ua.com.be/sdo2l/vocabulary/base/tabular#> as tabular\n\n");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String generateTable(String tableName, List<CSVRecord> list, String version, String cvsPath) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ StringBuilder rowList = new StringBuilder();
|
|
|
+ StringBuilder cellList = new StringBuilder();
|
|
|
+ sb.append("\tci " + tableName + " : tabular:TabularData [\n");
|
|
|
+ sb.append("\t\ttraces:hasVersion \"" + version + "\"\n");
|
|
|
+ sb.append("\t\tfile:isLocatedAt \"" + cvsPath + "\"\n");
|
|
|
+
|
|
|
+ for (CSVRecord record : list) {
|
|
|
+ rowList.append("\t\ttabular:hasRows " + tableName + "-row-" + (record.getRecordNumber() - 1) + "\n");
|
|
|
+ for (int i = 0; i < record.size(); i++) {
|
|
|
+ cellList.append("\t\ttabular:hasCells " + tableName + "-cell-" + (record.getRecordNumber() - 1) + "" + i
|
|
|
+ + "\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append(cellList);
|
|
|
+ sb.append(rowList);
|
|
|
+ sb.append("\t]\n");
|
|
|
+ sb.append("\n");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String generateCells(CSVRecord record, String tableName) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < record.size(); i++) {
|
|
|
+ sb.append("\tci " + tableName + "-cell-" + (record.getRecordNumber() - 1) + "" + i + " : tabular:Cell [\n");
|
|
|
+ sb.append("\t\ttabular:hasRowPosition " + (record.getRecordNumber() - 1) + "\n");
|
|
|
+ sb.append("\t\ttabular:hasColumnPosition " + i + "\n");
|
|
|
+ sb.append("\t\ttabular:holdsContent \"" + record.get(i) + "\"\n");
|
|
|
+ sb.append("\t\ttabular:isCellOfTabularData " + tableName + "\n");
|
|
|
+ sb.append("\t\ttabular:isInCollection " + tableName + "-row-" + (record.getRecordNumber() - 1) + "\n");
|
|
|
+ sb.append("\t]\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String generateRow(CSVRecord record, String tableName, boolean headerRow) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("\tci " + tableName + "-row-" + (record.getRecordNumber() - 1) + " : tabular:"
|
|
|
+ + (headerRow ? "HeaderRow" : "Row") + " [\n");
|
|
|
+ sb.append("\t\ttabular:hasRowId " + (record.getRecordNumber() - 1) + "\n");
|
|
|
+ sb.append("\t\ttabular:isRowOfTabularData " + tableName + "\n");
|
|
|
+
|
|
|
+ for (int i = 0; i < record.size(); i++) {
|
|
|
+ sb.append("\t\ttabular:hasCell " + tableName + "-cell-" + (record.getRecordNumber() - 1) + i + "\n");
|
|
|
+ }
|
|
|
+ sb.append("\t]\n");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws FileNotFoundException, IOException {
|
|
|
+ generateOML("drivetrain-sensor-data", "src/main/resources/csv/sensor.csv", "target/sensor.oml", "0.0.1",
|
|
|
+ "<http://ua.com.be/sdo2l/description/drivetrain/artifacts#>");
|
|
|
+ generateOML("torque-profile", "src/main/resources/csv/profile.csv", "target/profile.oml", "0.0.1",
|
|
|
+ "<http://ua.com.be/sdo2l/description/drivetrain/artifacts#>");
|
|
|
+ generateOML("coupling-sensor-data", "src/main/resources/csv/coupling.csv", "target/coupling.oml", "0.0.1",
|
|
|
+ "<http://ua.com.be/sdo2l/description/drivetrain/artifacts#>");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|