Skip to content

Commit

Permalink
Add optional parameter "select"
Browse files Browse the repository at this point in the history
- enable integration test
- add test

See metafacture/metafacture-core#415.
  • Loading branch information
dr0i committed Dec 13, 2022
1 parent 219f2bf commit d94728b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 14 deletions.
11 changes: 8 additions & 3 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.metafacture.metamorph.functions.Timestamp;
import org.metafacture.metamorph.maps.FileMap;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -102,10 +103,15 @@ public void apply(final Metafix metafix, final Record record, final List<String>
.map(str -> str.replaceAll(replaceTargets + "$", ""))
.orElse(params.get(0));
final RdfMap rdfMap = new RdfMap();

rdfMap.setResource(metafix.resolvePath(resourceName));
if (resourceName.startsWith("http")) {
rdfMap.setResource(resourceName);
}
else {
rdfMap.setResource(metafix.resolvePath(resourceName));
}
withOption(options, RdfMap.TARGET, rdfMap::setTarget);
withOption(options, RdfMap.TARGET_LANGUAGE, rdfMap::setTargetLanguage);
withOption(options, RdfMap.SELECT, rdfMap::setSelect);
withOption(options, Maps.DEFAULT_MAP_KEY, rdfMap::setDefault);

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

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

if (params.size() <= 1) {
Expand Down
66 changes: 57 additions & 9 deletions metafix/src/main/java/org/metafacture/metafix/maps/RdfMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
public final class RdfMap extends AbstractReadOnlyMap<String, String> {
public static final String TARGET = "target";
public static final String TARGET_LANGUAGE = "target_language";
public static final String SELECT = "select";
private static final int MAX_REDIRECTIONS = 10;
private static final int MIN_HTTP_STATUS_CODE = 299;
private static final int MAX_HTTP_STATUS_CODE = 400;
Expand All @@ -66,6 +67,7 @@ public final class RdfMap extends AbstractReadOnlyMap<String, String> {
private final Map<String, String> map = new HashMap<>();
private String targetLanguage;
private String target;
private Select select = Select.DEFAULT;

/**
* Creates an instance of {@link RdfMap}.
Expand Down Expand Up @@ -145,7 +147,7 @@ private void loadFile(final String file) {
*/
@Override
public String get(final Object key) {
String ret;
String ret = null;
if (map.containsKey(key.toString())) {
ret = map.get(key.toString());
}
Expand All @@ -156,20 +158,29 @@ public String get(final Object key) {
final Resource resource = ResourceFactory.createResource(key.toString());
final Property targetProperty = ResourceFactory.createProperty(target);
try {
//first try to get LITERAL using SUBJECT and PROPERTY
if (!targetLanguage.isEmpty()) {
ret = model.getRequiredProperty(resource, targetProperty, targetLanguage).getString();
if (select.equals(Select.SUBJECT)) {
ret = getSubjectUsingPropertyAndLiteral(key, targetProperty);
}
else {
ret = model.getRequiredProperty(resource, targetProperty).getString();
//first try to get LITERAL using SUBJECT and PROPERTY
if (!targetLanguage.isEmpty()) {
ret = model.getRequiredProperty(resource, targetProperty, targetLanguage).getString();
}
else {
ret = model.getRequiredProperty(resource, targetProperty).getString();
}
}
}
catch (final PropertyNotFoundException | NullPointerException | NoSuchElementException e) {
//second try to get SUBJECT using PROPERTY and LITERAL
ret = getSubjectUsingPropertyAndLiteral(key, targetProperty);
if (select.equals(Select.DEFAULT)) {
ret = getSubjectUsingPropertyAndLiteral(key, targetProperty);
}
//third try: get LITERAL of PREDICATE A using PREDICATE B
if (ret == null) {
ret = getLiteralOfPredicateUsingOtherPredicate(key, targetProperty);
if (!select.equals(Select.SUBJECT)) {
if (ret == null) {
ret = getLiteralOfPredicateUsingOtherPredicate(key, targetProperty);
}
}
}
map.put(key.toString(), ret);
Expand Down Expand Up @@ -270,6 +281,37 @@ public void setTarget(final String target) {
this.target = target;
}

/**
* Gets whether the Subject or the Object or a mixture of both should be retrieved in the RDF.
* <br>
* Setting "select" is optional.
*
* @return the selected position to be retrieved
**/
public String getSelect() {
return select.toString();
}

/**
* Sets whether the Subject or the Object or a mixture of both should be retrieved in the RDF.
* <br>
* Setting "select" is optional.
* <strong>Defaults to retrieve both: tries to get "objects" and as a fallback "subjects".</strong>
*
* @param position the position to be retrieved. Can be "subject" or "object".
*/
public void setSelect(final String position) {
if ("subject".equalsIgnoreCase(position)) {
select = Select.SUBJECT;
}
else if ("object".equalsIgnoreCase(position)) {
select = Select.OBJECT;
}
else {
throw new FixExecutionException("Couldn't set parameter - use 'subject' or 'object' as value");
}
}

/**
* Sets the default value returned if the key couldn't be found.
* <br>
Expand All @@ -283,8 +325,9 @@ public void setDefault(final String defaultValue) {

/**
* Gets a redirected URL, if any redirection takes place. Adapted predated code from org.apache.jena.rdfxml.xmlinput.JenaReader.
*
* <p>
* Note: Using newer jena version (needs java 11) this method would be obsolete.
*
* @param url the URL to resolve
* @return the (redirected) URL
* @throws IOException if any IO error occurs
Expand Down Expand Up @@ -324,4 +367,9 @@ private String read(final String url) throws IOException {
}
return connectionURL;
}

private enum Select {
SUBJECT, OBJECT, DEFAULT
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,50 @@ public void shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecific
shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecificLanguage("http://www.w3.org/2004/02/skos/core#prefLabel");
}

@Test
public void shouldLookupRdfDefinedPropertyToSubject() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"lookup_rdf('a', '" + HCRT_RDF_MAP + "', target: 'skos:prefLabel', target_language: 'de', select: 'subject')"
),
i -> {
i.startRecord("1");
i.literal("name", "Jake");
i.literal("a", "Softwareanwendung");
i.endRecord();
i.startRecord("2");
i.literal("name", "Blacky");
i.literal("a", "Nachschlagewerk");
i.endRecord();
i.startRecord("3");
i.literal("name", "Noone");
i.literal("a", "cat");
i.endRecord();
i.startRecord("4");
i.literal("name", "Noone_2");
i.literal("a", "Assessment");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("name", "Jake");
o.get().literal("a", "https://w3id.org/kim/hcrt/application");
o.get().endRecord();
o.get().startRecord("2");
o.get().literal("name", "Blacky");
o.get().literal("a", "https://w3id.org/kim/hcrt/index");
o.get().endRecord();
o.get().startRecord("3");
o.get().literal("name", "Noone");
o.get().literal("a", "cat");
o.get().endRecord();
o.get().startRecord("4");
o.get().literal("name", "Noone_2");
o.get().literal("a", "Assessment");
o.get().endRecord();
}
);
}

private void shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecificLanguage(final String target) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('prefLabel', 'Mathematics, Natural Sciences')",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lookup_rdf('a', '../../../../../maps/hcrt.ttl', target: 'http://www.w3.org/2004/02/skos/core#prefLabel', target_language: 'de')
lookup_rdf('a', '../../../../../maps/hcrt.ttl', target: 'http://www.w3.org/2004/02/skos/core#prefLabel', target_language: 'de', select: 'subject')
# Cant define specific lookup-match fields, would expect something like this:
# lookup_rdf("a", "./hcrt.ttl", match="http://www.w3.org/2004/02/skos/core#prefLabel", match_language: "de")

This file was deleted.

0 comments on commit d94728b

Please sign in to comment.