Skip to content

Commit 86d6198

Browse files
committed
1466-instantiate-csv-data-as-rdf: Added alternate shortcut YARRRML keys for parsing
1 parent 1dc83bc commit 86d6198

File tree

3 files changed

+84
-82
lines changed

3 files changed

+84
-82
lines changed

stack-clients/src/main/java/com/cmclinnovations/stack/clients/utils/YarrrmlFile.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ public final class YarrrmlFile {
2828
private static final String TARGETS_KEY = "targets";
2929
private static final String TARGET_REF_KEY = "target-ref";
3030
private static final String MAPPING_KEY = "mappings";
31+
private static final String MAPPING_ALT_KEY = "mapping";
32+
private static final String MAPPING_ALT_TWO_KEY = "m";
3133
private static final String SUBJECT_KEY = "s";
34+
private static final String SUBJECT_ALT_KEY = "subject";
35+
private static final String SUBJECT_ALT_TWO_KEY = "subjects";
3236
private static final String PRED_OBJ_KEY = "po";
37+
private static final String PRED_OBJ_ALT_KEY = "predicateobjects";
3338
private static final String ACCESS_KEY = "access";
3439

3540
/**
@@ -78,6 +83,13 @@ public String getFileName() {
7883
return this.fileName;
7984
}
8085

86+
/**
87+
* Get the rules content.
88+
*/
89+
public Map<String, Object> getRules() {
90+
return this.rules;
91+
}
92+
8193
/**
8294
* Writes the file content into a byte array for further usage.
8395
*/
@@ -149,19 +161,23 @@ private void appendTargets(Map<String, Object> output, String endpoint) {
149161
* @param output The target YAML output.
150162
*/
151163
private void updateMappings(Map<String, Object> output) {
152-
Map<String, Object> ruleMappings = this.castToMapStringObject(output.get(MAPPING_KEY));
164+
Map<String, Object> ruleMappings = this.castToMapStringObject(
165+
this.getObjectWithOneOfManyKeys(output, MAPPING_KEY, MAPPING_ALT_KEY, MAPPING_ALT_TWO_KEY));
153166
ruleMappings.forEach((field, value) -> {
154167
// Addition of sources and their reference
155168
Map<String, Object> mappingValue = this.castToMapStringObject(value);
156169
mappingValue.put(SOURCES_KEY, SOURCE_REF_KEY);
157170

158171
// Addition of targets and their reference
159-
Object subjectVal = mappingValue.get(SUBJECT_KEY);
172+
Object subjectVal = this.getObjectWithOneOfManyKeys(mappingValue, SUBJECT_KEY, SUBJECT_ALT_KEY,
173+
SUBJECT_ALT_TWO_KEY);
160174
if (subjectVal instanceof String) {
161175
Map<String, Object> newSubjectMap = new HashMap<>();
162176
newSubjectMap.put("value", subjectVal);
163177
newSubjectMap.put(TARGETS_KEY, TARGET_REF_KEY);
164178
mappingValue.put(SUBJECT_KEY, newSubjectMap);
179+
mappingValue.remove(SUBJECT_ALT_KEY);
180+
mappingValue.remove(SUBJECT_ALT_TWO_KEY);
165181
} else if (subjectVal instanceof Map<?, ?>) {
166182
Map<String, Object> stringObjectMap = this.castToMapStringObject(subjectVal);
167183
stringObjectMap.put(TARGETS_KEY, TARGET_REF_KEY);
@@ -172,7 +188,8 @@ private void updateMappings(Map<String, Object> output) {
172188
// BUT YARRRML parser only accepts -[] as a shortcut and should be updated
173189
// accordingly
174190
List<Map<String, String>> transformedPo = new ArrayList<>();
175-
List<Object> originalPo = this.castToListObject(mappingValue.get(PRED_OBJ_KEY));
191+
List<Object> originalPo = this
192+
.castToListObject(this.getObjectWithOneOfManyKeys(mappingValue, PRED_OBJ_KEY, PRED_OBJ_ALT_KEY));
176193
for (Object predObj : originalPo) {
177194
if (predObj instanceof List) {
178195
List<Object> nestedPredObjList = this.castToListObject(predObj);
@@ -187,8 +204,27 @@ private void updateMappings(Map<String, Object> output) {
187204
}
188205
if (!transformedPo.isEmpty()) {
189206
mappingValue.put(PRED_OBJ_KEY, transformedPo);
207+
mappingValue.remove(PRED_OBJ_ALT_KEY);
190208
}
191209
});
210+
output.put(MAPPING_KEY, ruleMappings);
211+
output.remove(MAPPING_ALT_KEY);
212+
output.remove(MAPPING_ALT_TWO_KEY);
213+
}
214+
215+
/**
216+
* Gets the object from the mappings based on any of the shortcut key inputs.
217+
*
218+
* @param mappings The target mappings.
219+
* @param shortcutKeys The possible shortcut keys available in YARRRML.
220+
*/
221+
private Object getObjectWithOneOfManyKeys(Map<String, Object> mappings, String... shortcutKeys) {
222+
for (String shortcut : shortcutKeys) {
223+
if (mappings.containsKey(shortcut)) {
224+
return mappings.get(shortcut);
225+
}
226+
}
227+
throw new IllegalArgumentException("Invalid input. Object is not a Map.");
192228
}
193229

194230
private Map<String, Object> castToMapStringObject(Object obj) throws ClassCastException {
Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
package com.cmclinnovations.stack.clients.utils;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
5-
import java.io.OutputStreamWriter;
6-
import java.io.Writer;
7-
import java.net.URI;
84
import java.net.URISyntaxException;
9-
import java.nio.charset.StandardCharsets;
105
import java.nio.file.Path;
116
import java.nio.file.Paths;
127
import java.util.ArrayList;
13-
import java.util.LinkedHashMap;
8+
import java.util.HashMap;
149
import java.util.List;
1510
import java.util.Map;
1611

1712
import org.junit.jupiter.api.Assertions;
1813
import org.junit.jupiter.api.Test;
19-
import org.yaml.snakeyaml.DumperOptions;
20-
import org.yaml.snakeyaml.Yaml;
14+
15+
import com.google.common.base.Objects;
2116

2217
class YarrrmlFileTest {
2318
private static final String TEST_ENDPOINT = "https://example.org/kg/sparql";
2419
private static final String TEST_ONE_FILE_NAME = "rules.yml";
2520
private static final String TEST_TWO_FILE_NAME = "rules2.yml";
2621

2722
@Test
28-
void testDefaultConstructor() throws IOException {
23+
void testDefaultConstructor() {
2924
YarrrmlFile yarrrmlFile = new YarrrmlFile();
3025
String expected = "";
31-
checkRules(yarrrmlFile, expected, expected);
26+
checkRules(yarrrmlFile, expected, new HashMap<>());
3227
}
3328

3429
@Test
@@ -54,82 +49,53 @@ void testAddRules_SuccessShortcutFormat() throws IOException, URISyntaxException
5449
checkRules(yarrrmlFile, TEST_TWO_FILE_NAME, this.genExpectedYarrrmlContents(TEST_TWO_FILE_NAME));
5550
}
5651

57-
private void checkRules(YarrrmlFile yarrrmlFile, String expectedFileName, String expectedContents)
58-
throws IOException {
52+
private void checkRules(YarrrmlFile yarrrmlFile, String expectedFileName, Map<String, Object> expectedContents) {
5953
Assertions.assertEquals(expectedFileName, yarrrmlFile.getFileName());
60-
Assertions.assertEquals(expectedContents, new String(yarrrmlFile.write(), StandardCharsets.UTF_8));
54+
Assertions.assertTrue(Objects.equal(expectedContents, yarrrmlFile.getRules()));
6155
}
6256

63-
private String genExpectedYarrrmlContents(String fileName) throws IOException {
64-
DumperOptions options = new DumperOptions();
65-
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
66-
options.setPrettyFlow(true);
67-
Yaml yaml = new Yaml(options);
68-
Map<String, Object> yamlData = new LinkedHashMap<>();
69-
70-
Map<String, String> prefixes = new LinkedHashMap<>();
71-
prefixes.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
72-
prefixes.put("base", "https://theworldavatar.io/kg/");
73-
yamlData.put("prefixes", prefixes);
74-
75-
Map<String, Map<String, Object>> mappings = new LinkedHashMap<>();
76-
77-
Map<String, Object> personMapping = new LinkedHashMap<>();
78-
Map<String, Object> personS = new LinkedHashMap<>();
79-
personS.put("value", "base:person/$(id)");
80-
personS.put("targets", "target-ref");
81-
personMapping.put("s", personS);
82-
83-
List<Map<String, Object>> personPO = new ArrayList<>();
84-
this.genExpectedYarrrmlPredObj(personPO, "a", "base:Person");
85-
this.genExpectedYarrrmlPredObj(personPO, "base:hasName", "base:person/name/$(id)~iri");
86-
personMapping.put("po", personPO);
57+
private Map<String, Object> genExpectedYarrrmlContents(String fileName) {
58+
Map<String, Object> yamlData = new HashMap<>();
8759

88-
personMapping.put("sources", "source-ref");
89-
mappings.put("person", personMapping);
60+
yamlData.put("prefixes", Map.of(
61+
"rdfs", "http://www.w3.org/2000/01/rdf-schema#",
62+
"base", "https://theworldavatar.io/kg/"));
9063

91-
Map<String, Object> personNameMapping = new LinkedHashMap<>();
92-
Map<String, Object> personNameS = new LinkedHashMap<>();
93-
personNameS.put("value", "base:person/name/$(id)");
94-
personNameS.put("targets", "target-ref");
95-
personNameMapping.put("s", personNameS);
64+
Map<String, Map<String, Object>> mappings = new HashMap<>();
9665

97-
List<Map<String, Object>> personNamePO = new ArrayList<>();
98-
this.genExpectedYarrrmlPredObj(personNamePO, "a", "base:PersonName");
99-
this.genExpectedYarrrmlPredObj(personNamePO, "rdfs:label", "$(name)");
100-
personNameMapping.put("po", personNamePO);
66+
mappings.put("person", Map.of(
67+
"sources", "source-ref",
68+
"s", Map.of(
69+
"value", "base:person/$(id)",
70+
"targets", "target-ref"),
71+
"po", List.of(this.genExpectedYarrrmlPredObj("a", "base:Person"),
72+
this.genExpectedYarrrmlPredObj("base:hasName", "base:person/name/$(id)~iri"))));
10173

102-
personNameMapping.put("sources", "source-ref");
103-
mappings.put("person-name", personNameMapping);
74+
mappings.put("person-name", Map.of(
75+
"sources", "source-ref",
76+
"s", Map.of(
77+
"value", "base:person/name/$(id)",
78+
"targets", "target-ref"),
79+
"po", List.of(this.genExpectedYarrrmlPredObj("a", "base:PersonName"),
80+
this.genExpectedYarrrmlPredObj("rdfs:label", "$(name)"))));
10481

10582
yamlData.put("mappings", mappings);
10683

107-
Map<String, Map<String, Object>> sources = new LinkedHashMap<>();
108-
Map<String, Object> sourceRef = new LinkedHashMap<>();
109-
sourceRef.put("referenceFormulation", "csv");
110-
sourceRef.put("access", FileUtils.replaceExtension(fileName, "csv"));
111-
sources.put("source-ref", sourceRef);
112-
yamlData.put("sources", sources);
113-
114-
Map<String, Map<String, Object>> targets = new LinkedHashMap<>();
115-
Map<String, Object> targetRef = new LinkedHashMap<>();
116-
targetRef.put("serialization", "turtle");
117-
targetRef.put("access", "https://example.org/kg/sparql");
118-
targetRef.put("type", "sd");
119-
targets.put("target-ref", targetRef);
120-
yamlData.put("targets", targets);
121-
122-
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
123-
Writer writer = new OutputStreamWriter(outputStream)) {
124-
yaml.dump(yamlData, writer);
125-
return outputStream.toString();
126-
}
84+
yamlData.put("sources", Map.of(
85+
"source-ref", Map.of(
86+
"referenceFormulation", "csv",
87+
"access", FileUtils.replaceExtension(fileName, "csv"))));
88+
yamlData.put("targets", Map.of(
89+
"target-ref", Map.of(
90+
"serialization", "turtle",
91+
"access", "https://example.org/kg/sparql",
92+
"type", "sd")));
93+
return yamlData;
12794
}
12895

129-
private void genExpectedYarrrmlPredObj(List<Map<String, Object>> output, String predVal, String objVal) {
130-
Map<String, Object> predObjMap = new LinkedHashMap<>();
131-
predObjMap.put("p", predVal);
132-
predObjMap.put("o", objVal);
133-
output.add(predObjMap);
96+
private Map<String, Object> genExpectedYarrrmlPredObj(String predVal,
97+
String objVal) {
98+
return Map.of("p", predVal,
99+
"o", objVal);
134100
}
135101
}

stack-clients/src/test/resources/com/cmclinnovations/stack/clients/utils/rules2.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ prefixes:
22
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
33
base: "https://theworldavatar.io/kg/"
44

5-
mappings:
5+
mapping:
66
person:
7-
s: base:person/$(id)
8-
po:
7+
subjects: base:person/$(id)
8+
predicateobjects:
99
- [a, base:Person]
1010
- [base:hasName, base:person/name/$(id)~iri]
1111
person-name:
12-
s: base:person/name/$(id)
12+
subject: base:person/name/$(id)
1313
po:
1414
- [a, base:PersonName]
1515
- [rdfs:label, $(name)]

0 commit comments

Comments
 (0)