Skip to content

Commit e119339

Browse files
committed
1466-instantiate-csv-data-as-rdf: fix parsing for various conditions, and added tests
1 parent 85ab018 commit e119339

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ private void updateMappings(AliasMap<Object> output) {
190190
newSubjectMap.put(TARGETS_KEY, TARGET_REF_KEY);
191191
}
192192
mappingValue.put(SUBJECT_KEY, newSubjectMap, SUBJECT_ALT_KEY, SUBJECT_ALT_TWO_KEY);
193-
194-
mappingValue.put(PRED_OBJ_KEY, updatePredObjMappings(mappingValue), PRED_OBJ_ALT_KEY);
193+
if (mappingValue.containsKey(PRED_OBJ_KEY, PRED_OBJ_ALT_KEY)) {
194+
mappingValue.put(PRED_OBJ_KEY, updatePredObjMappings(mappingValue), PRED_OBJ_ALT_KEY);
195+
}
195196
ruleMappings.put(field, mappingValue);
196197
});
197198
output.put(MAPPING_KEY, ruleMappings, MAPPING_ALT_KEY, MAPPING_ALT_TWO_KEY);
@@ -249,17 +250,35 @@ private List<Object> updatePredObjMappings(AliasMap<Object> mappingObj) {
249250
if (currentObjectsObj instanceof Map<?, ?>) {
250251
AliasMap<Object> currentObjectsMap = this.castToAliasMap(currentObjectsObj);
251252
this.updateFunctionMappingsIfPresent(currentObjectsMap);
253+
} else if (currentObjectsObj instanceof List) {
254+
List<Object> currentObjList = this.castToListObject(currentObjectsObj);
255+
for (int j = 0; j < currentObjList.size(); j++) {
256+
AliasMap<Object> objectNode = this.castToAliasMap(currentObjList.get(j));
257+
this.updateConditionMappings(objectNode);
258+
currentObjList.set(j, objectNode);
259+
}
260+
currentPOMap.put(OBJ_KEY, currentObjList, OBJ_ALT_KEY,
261+
OBJ_ALT_TWO_KEY);
252262
}
253-
// All conditions are functions
254-
if (currentPOMap.containsKey(CONDITION_KEY)) {
255-
AliasMap<Object> conditionMap = this.castToAliasMap(currentPOMap.get(CONDITION_KEY));
256-
this.updateFunctionMappingsIfPresent(conditionMap);
257-
}
263+
264+
this.updateConditionMappings(currentPOMap);
258265
}
259266
}
260267
return predObjList;
261268
}
262269

270+
/**
271+
* Updates the condition mappings, which will contain a function if present.
272+
*
273+
* @param mapObj The mappings field object.
274+
*/
275+
private void updateConditionMappings(AliasMap<Object> mapObj) {
276+
if (mapObj.containsKey(CONDITION_KEY)) {
277+
AliasMap<Object> conditionMap = this.castToAliasMap(mapObj.get(CONDITION_KEY));
278+
this.updateFunctionMappingsIfPresent(conditionMap);
279+
}
280+
}
281+
263282
/**
264283
* Recursively updates function mappings within a YARRRML-compliant data
265284
* structure.
@@ -276,16 +295,16 @@ private void updateFunctionMappingsIfPresent(AliasMap<Object> mapObj) {
276295
if (mapObj.containsKey(PARAMS_KEY, PARAMS_ALT_KEY)) {
277296
List<Object> paramObjs = this
278297
.castToListObject(mapObj.get(PARAMS_KEY, PARAMS_ALT_KEY));
279-
for (int j = 0; j < paramObjs.size(); j++) {
280-
Object paramObj = paramObjs.get(j);
298+
for (int i = 0; i < paramObjs.size(); i++) {
299+
Object paramObj = paramObjs.get(i);
281300
// Detected use of shortcut, transforming into long form
282301
if (paramObj instanceof List) {
283302
List<Object> paramList = this.castToListObject(paramObj);
284303
if (paramList.size() == 2) {
285304
Map<String, Object> transformedParam = new HashMap<>();
286305
transformedParam.put("parameter", paramList.get(0).toString());
287306
transformedParam.put(VALUE_KEY, paramList.get(1).toString());
288-
paramObjs.set(j, transformedParam);
307+
paramObjs.set(i, transformedParam);
289308
}
290309
} else if (paramObj instanceof Map<?, ?>) {
291310
Object functionParamValue = this.castToAliasMap(paramObj).get(VALUE_KEY);

stack-clients/src/test/java/com/cmclinnovations/stack/clients/utils/YarrrmlFileTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class YarrrmlFileTest {
2323
private static final String EXPECTED_THREE_FILE_NAME = "yml/expected/rules_functions.yml";
2424
private static final String TEST_FOUR_FILE_NAME = "yml/test/rules2_functions.yml";
2525
private static final String EXPECTED_FOUR_FILE_NAME = "yml/expected/rules2_functions.yml";
26+
private static final String TEST_FIVE_FILE_NAME = "yml/test/rules_condition.yml";
27+
private static final String EXPECTED_FIVE_FILE_NAME = "yml/expected/rules_condition.yml";
2628

2729
@Test
2830
void testDefaultConstructor() {
@@ -85,6 +87,17 @@ void testAddRules_SuccessFunctionShorcutFormat() throws IOException, URISyntaxEx
8587
yarrrmlFile.getRules());
8688
}
8789

90+
@Test
91+
void testAddRules_SuccessConditionFormat() throws IOException, URISyntaxException {
92+
Path rulesFilePath = Paths.get(YarrrmlFileTest.class.getResource(TEST_FIVE_FILE_NAME).toURI());
93+
YarrrmlFile yarrrmlFile = new YarrrmlFile();
94+
yarrrmlFile.addRules(rulesFilePath, TEST_ENDPOINT);
95+
Assertions.assertEquals(this.genExpectedYarrrmlContents(
96+
YarrrmlFileTest.class.getResource(EXPECTED_FIVE_FILE_NAME),
97+
rulesFilePath, TEST_ENDPOINT),
98+
yarrrmlFile.getRules());
99+
}
100+
88101
private AliasMap<Object> genExpectedYarrrmlContents(URL expectedFilePath, Path expectedSourceLocation,
89102
String endpoint)
90103
throws IOException, URISyntaxException {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
prefixes:
2+
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
3+
base: "https://theworldavatar.io/kg/"
4+
grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#"
5+
6+
mappings:
7+
person-name:
8+
sources: source-ref
9+
po:
10+
- p: base:hasValue
11+
o: $(name)
12+
condition:
13+
function: idlab-fn:notEqual
14+
parameters:
15+
- parameter: grel:valueParameter
16+
value: $(name)
17+
- parameter: grel:valueParameter
18+
value: ''
19+
s:
20+
value: base:person/name/$(id)
21+
targets: target-ref
22+
person:
23+
sources: source-ref
24+
s:
25+
value: base:person/$(id)
26+
targets: target-ref
27+
po:
28+
- p: base:hasName
29+
o:
30+
- mapping: person-name
31+
condition:
32+
function: idlab-fn:notEqual
33+
parameters:
34+
- parameter: grel:valueParameter
35+
value: $(name)
36+
- parameter: grel:valueParameter
37+
value: ''
38+
sources:
39+
source-ref:
40+
referenceFormulation: csv
41+
access: [source]
42+
targets:
43+
target-ref:
44+
serialization: turtle
45+
type: sd
46+
access: [target]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
prefixes:
2+
rdfs: "http://www.w3.org/2000/01/rdf-schema#"
3+
base: "https://theworldavatar.io/kg/"
4+
grel: "http://users.ugent.be/~bjdmeest/function/grel.ttl#"
5+
6+
mappings:
7+
person:
8+
s: base:person/$(id)
9+
po:
10+
- p: base:hasName
11+
o:
12+
- mapping: person-name
13+
condition:
14+
function: idlab-fn:notEqual
15+
parameters:
16+
- [grel:valueParameter, $(name)]
17+
- [grel:valueParameter, ""]
18+
person-name:
19+
subject: base:person/name/$(id)
20+
po:
21+
- p: base:hasValue
22+
o: $(name)
23+
condition:
24+
function: idlab-fn:notEqual
25+
parameters:
26+
- [grel:valueParameter, $(name)]
27+
- [grel:valueParameter, ""]

0 commit comments

Comments
 (0)