Skip to content

Commit d94728b

Browse files
committed
Add optional parameter "select"
- enable integration test - add test See #415.
1 parent 219f2bf commit d94728b

File tree

5 files changed

+110
-14
lines changed

5 files changed

+110
-14
lines changed

metafix/src/main/java/org/metafacture/metafix/FixMethod.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.metafacture.metamorph.functions.Timestamp;
2424
import org.metafacture.metamorph.maps.FileMap;
2525

26+
import java.io.File;
2627
import java.io.IOException;
2728
import java.util.Arrays;
2829
import java.util.Collections;
@@ -102,10 +103,15 @@ public void apply(final Metafix metafix, final Record record, final List<String>
102103
.map(str -> str.replaceAll(replaceTargets + "$", ""))
103104
.orElse(params.get(0));
104105
final RdfMap rdfMap = new RdfMap();
105-
106-
rdfMap.setResource(metafix.resolvePath(resourceName));
106+
if (resourceName.startsWith("http")) {
107+
rdfMap.setResource(resourceName);
108+
}
109+
else {
110+
rdfMap.setResource(metafix.resolvePath(resourceName));
111+
}
107112
withOption(options, RdfMap.TARGET, rdfMap::setTarget);
108113
withOption(options, RdfMap.TARGET_LANGUAGE, rdfMap::setTargetLanguage);
114+
withOption(options, RdfMap.SELECT, rdfMap::setSelect);
109115
withOption(options, Maps.DEFAULT_MAP_KEY, rdfMap::setDefault);
110116

111117
metafix.putMap(rdfMapName, rdfMap);
@@ -489,7 +495,6 @@ public void apply(final Metafix metafix, final Record record, final List<String>
489495

490496
@Override
491497
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
492-
<<<<<<< HEAD
493498
final Map<String, String> map;
494499

495500
if (params.size() <= 1) {

metafix/src/main/java/org/metafacture/metafix/maps/RdfMap.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
public final class RdfMap extends AbstractReadOnlyMap<String, String> {
5858
public static final String TARGET = "target";
5959
public static final String TARGET_LANGUAGE = "target_language";
60+
public static final String SELECT = "select";
6061
private static final int MAX_REDIRECTIONS = 10;
6162
private static final int MIN_HTTP_STATUS_CODE = 299;
6263
private static final int MAX_HTTP_STATUS_CODE = 400;
@@ -66,6 +67,7 @@ public final class RdfMap extends AbstractReadOnlyMap<String, String> {
6667
private final Map<String, String> map = new HashMap<>();
6768
private String targetLanguage;
6869
private String target;
70+
private Select select = Select.DEFAULT;
6971

7072
/**
7173
* Creates an instance of {@link RdfMap}.
@@ -145,7 +147,7 @@ private void loadFile(final String file) {
145147
*/
146148
@Override
147149
public String get(final Object key) {
148-
String ret;
150+
String ret = null;
149151
if (map.containsKey(key.toString())) {
150152
ret = map.get(key.toString());
151153
}
@@ -156,20 +158,29 @@ public String get(final Object key) {
156158
final Resource resource = ResourceFactory.createResource(key.toString());
157159
final Property targetProperty = ResourceFactory.createProperty(target);
158160
try {
159-
//first try to get LITERAL using SUBJECT and PROPERTY
160-
if (!targetLanguage.isEmpty()) {
161-
ret = model.getRequiredProperty(resource, targetProperty, targetLanguage).getString();
161+
if (select.equals(Select.SUBJECT)) {
162+
ret = getSubjectUsingPropertyAndLiteral(key, targetProperty);
162163
}
163164
else {
164-
ret = model.getRequiredProperty(resource, targetProperty).getString();
165+
//first try to get LITERAL using SUBJECT and PROPERTY
166+
if (!targetLanguage.isEmpty()) {
167+
ret = model.getRequiredProperty(resource, targetProperty, targetLanguage).getString();
168+
}
169+
else {
170+
ret = model.getRequiredProperty(resource, targetProperty).getString();
171+
}
165172
}
166173
}
167174
catch (final PropertyNotFoundException | NullPointerException | NoSuchElementException e) {
168175
//second try to get SUBJECT using PROPERTY and LITERAL
169-
ret = getSubjectUsingPropertyAndLiteral(key, targetProperty);
176+
if (select.equals(Select.DEFAULT)) {
177+
ret = getSubjectUsingPropertyAndLiteral(key, targetProperty);
178+
}
170179
//third try: get LITERAL of PREDICATE A using PREDICATE B
171-
if (ret == null) {
172-
ret = getLiteralOfPredicateUsingOtherPredicate(key, targetProperty);
180+
if (!select.equals(Select.SUBJECT)) {
181+
if (ret == null) {
182+
ret = getLiteralOfPredicateUsingOtherPredicate(key, targetProperty);
183+
}
173184
}
174185
}
175186
map.put(key.toString(), ret);
@@ -270,6 +281,37 @@ public void setTarget(final String target) {
270281
this.target = target;
271282
}
272283

284+
/**
285+
* Gets whether the Subject or the Object or a mixture of both should be retrieved in the RDF.
286+
* <br>
287+
* Setting "select" is optional.
288+
*
289+
* @return the selected position to be retrieved
290+
**/
291+
public String getSelect() {
292+
return select.toString();
293+
}
294+
295+
/**
296+
* Sets whether the Subject or the Object or a mixture of both should be retrieved in the RDF.
297+
* <br>
298+
* Setting "select" is optional.
299+
* <strong>Defaults to retrieve both: tries to get "objects" and as a fallback "subjects".</strong>
300+
*
301+
* @param position the position to be retrieved. Can be "subject" or "object".
302+
*/
303+
public void setSelect(final String position) {
304+
if ("subject".equalsIgnoreCase(position)) {
305+
select = Select.SUBJECT;
306+
}
307+
else if ("object".equalsIgnoreCase(position)) {
308+
select = Select.OBJECT;
309+
}
310+
else {
311+
throw new FixExecutionException("Couldn't set parameter - use 'subject' or 'object' as value");
312+
}
313+
}
314+
273315
/**
274316
* Sets the default value returned if the key couldn't be found.
275317
* <br>
@@ -283,8 +325,9 @@ public void setDefault(final String defaultValue) {
283325

284326
/**
285327
* Gets a redirected URL, if any redirection takes place. Adapted predated code from org.apache.jena.rdfxml.xmlinput.JenaReader.
286-
*
328+
* <p>
287329
* Note: Using newer jena version (needs java 11) this method would be obsolete.
330+
*
288331
* @param url the URL to resolve
289332
* @return the (redirected) URL
290333
* @throws IOException if any IO error occurs
@@ -324,4 +367,9 @@ private String read(final String url) throws IOException {
324367
}
325368
return connectionURL;
326369
}
370+
371+
private enum Select {
372+
SUBJECT, OBJECT, DEFAULT
373+
}
374+
327375
}

metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,50 @@ public void shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecific
11571157
shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecificLanguage("http://www.w3.org/2004/02/skos/core#prefLabel");
11581158
}
11591159

1160+
@Test
1161+
public void shouldLookupRdfDefinedPropertyToSubject() {
1162+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1163+
"lookup_rdf('a', '" + HCRT_RDF_MAP + "', target: 'skos:prefLabel', target_language: 'de', select: 'subject')"
1164+
),
1165+
i -> {
1166+
i.startRecord("1");
1167+
i.literal("name", "Jake");
1168+
i.literal("a", "Softwareanwendung");
1169+
i.endRecord();
1170+
i.startRecord("2");
1171+
i.literal("name", "Blacky");
1172+
i.literal("a", "Nachschlagewerk");
1173+
i.endRecord();
1174+
i.startRecord("3");
1175+
i.literal("name", "Noone");
1176+
i.literal("a", "cat");
1177+
i.endRecord();
1178+
i.startRecord("4");
1179+
i.literal("name", "Noone_2");
1180+
i.literal("a", "Assessment");
1181+
i.endRecord();
1182+
},
1183+
o -> {
1184+
o.get().startRecord("1");
1185+
o.get().literal("name", "Jake");
1186+
o.get().literal("a", "https://w3id.org/kim/hcrt/application");
1187+
o.get().endRecord();
1188+
o.get().startRecord("2");
1189+
o.get().literal("name", "Blacky");
1190+
o.get().literal("a", "https://w3id.org/kim/hcrt/index");
1191+
o.get().endRecord();
1192+
o.get().startRecord("3");
1193+
o.get().literal("name", "Noone");
1194+
o.get().literal("a", "cat");
1195+
o.get().endRecord();
1196+
o.get().startRecord("4");
1197+
o.get().literal("name", "Noone_2");
1198+
o.get().literal("a", "Assessment");
1199+
o.get().endRecord();
1200+
}
1201+
);
1202+
}
1203+
11601204
private void shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecificLanguage(final String target) {
11611205
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
11621206
"set_array('prefLabel', 'Mathematics, Natural Sciences')",
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lookup_rdf('a', '../../../../../maps/hcrt.ttl', target: 'http://www.w3.org/2004/02/skos/core#prefLabel', target_language: 'de')
1+
lookup_rdf('a', '../../../../../maps/hcrt.ttl', target: 'http://www.w3.org/2004/02/skos/core#prefLabel', target_language: 'de', select: 'subject')
22
# Cant define specific lookup-match fields, would expect something like this:
33
# lookup_rdf("a", "./hcrt.ttl", match="http://www.w3.org/2004/02/skos/core#prefLabel", match_language: "de")
44

metafix/src/test/resources/org/metafacture/metafix/integration/lookup/fromJson/toJson/lookupRdfDefinedPropertyToSubject/todo.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)