Skip to content

Commit

Permalink
Fixed bug where configurations with namespace IRIs used in the restri…
Browse files Browse the repository at this point in the history
…ctions, properties and absoluteProperties caused errors.

Now namespace IRIs get replaced by the prefix that was defined for them using the prefix tags. If a namespace IRI is not defined in the prefix section an error is thrown indicating which IRI needs a prefix.
  • Loading branch information
becker-al committed Jun 8, 2024
1 parent db44bff commit ad12b40
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/user_manual/configuration_file/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Optional properties can be set to segment the requested dataset.
* The graph of the endpoint can be specified directly ofter the `ENDPOINT` tag using the `GRAPH` tag.
* The limits of the query can be set with the `MINOFFSET` and `MAXOFFSET` tags directly after the `PAGESIZE` tag. The resulting query will ask about the statements in the interval [`MINOFFSET`, `MAXOFFSET`]. Note that `MINOFFSET` must be smaller than `MAXOFFSET`! If both `SOURCE` and `TARGET` are restricted, a warning is generated.

Please note that LIMES does not allow namespace IRIs to be used in the `PROPERTY`, `RESTRICTION`, and `OPTIONAL_PROPERTY` tag. Please use namespace prefixes and reference the namespace IRI using a prefix.

This comment has been minimized.

Copy link
@MSherif

MSherif Jun 10, 2024

Contributor

Please add an example PROPERTY tag

### Preprocessing Functions
#### Simple

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.aksw.limes.core.io.config.reader;

import org.aksw.limes.core.io.config.Configuration;
import org.aksw.limes.core.io.config.KBInfo;

import java.util.*;

/**
* @author Mohamed Sherif ([email protected])
Expand Down Expand Up @@ -48,5 +51,78 @@ public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}

/**
* This method replaces any URIs used in the kbInfo with their prefixes
* @param info
*/
public static void replaceURIsWithPrefixes(KBInfo info) {
Map<String, String> prefixes = info.getPrefixes();
HashMap<String, String> rev = new HashMap<>();
for(Map.Entry<String, String> entry : prefixes.entrySet()) {
rev.put(entry.getValue(), entry.getKey());
}
info.setProperties(replaceURIsWithPrefixes(info.getProperties(), rev));
info.setOptionalProperties(replaceURIsWithPrefixes(info.getOptionalProperties(), rev));
info.setRestrictions(replaceURIsWithPrefixes(info.getRestrictions(), rev));
info.setFunctions(replaceURIsWithPrefixes(info.getFunctions(), rev));
}

private static ArrayList<String> replaceURIsWithPrefixes(Collection<String> props, HashMap<String, String> rev) {
ArrayList<String> replacements = new ArrayList<>();
for (String property : props) {
String originalProp = property;
for (Map.Entry<String, String> prefixEntry : rev.entrySet()) {
if(property.contains(prefixEntry.getKey())){
property = property.replace(prefixEntry.getKey(), prefixEntry.getValue() + ":");
}
}
replacements.add(property);

if(property.contains("://")){
throw new IllegalArgumentException("LIMES does not support using URIs in the properties, optionalProperties, restrictions, or functions in the configuration file. " +
"Please define a prefix and use the prefix for the following URI: " + originalProp);
}
}
return replacements;
}

private static LinkedHashMap<String, Map<String, String>> replaceURIsWithPrefixes(Map<String, Map<String, String>> funcs, HashMap<String, String> rev) {
LinkedHashMap<String, Map<String, String>> replacements = new LinkedHashMap<>();
for (Map.Entry<String, Map<String, String>> entry : funcs.entrySet()) {
String property = entry.getKey();
String originalProp = property;

//Replace key of function
for (Map.Entry<String, String> prefixEntry : rev.entrySet()) {
if(property.contains(prefixEntry.getKey())){
property = property.replace(prefixEntry.getKey(), prefixEntry.getValue() + ":");
}
}
if(property.contains("://")){
throw new IllegalArgumentException("LIMES does not support using URIs in the properties, optionalProperties, restrictions, or functions in the configuration file. " +
"Please define a prefix and use the prefix for the following URI: " + originalProp);
}

//Replace value map
Map<String, String> intermediateReplacement = new HashMap<>();
for (Map.Entry<String, String> stringEntry : entry.getValue().entrySet()) {
String subKey = stringEntry.getKey();
String origSubKey = subKey;
for (Map.Entry<String, String> prefixEntry : rev.entrySet()) {
subKey = subKey.replace(prefixEntry.getKey(), prefixEntry.getValue() + ":");
}
intermediateReplacement.put(subKey, stringEntry.getValue());
if(subKey.contains("://")){
throw new IllegalArgumentException("LIMES does not support using URIs in the properties, optionalProperties, restrictions, or functions in the configuration file. " +
"Please define a prefix and use the prefix for the following URI: " + origSubKey);
}

}

replacements.put(property, intermediateReplacement);
}
return replacements;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public Configuration read(Model configurationModel) {
configuration.setOutputFormat(output.toString());
}

// 10. Check for invalid config because of URIs without prefixes
if(configuration.getSourceInfo() != null){
replaceURIsWithPrefixes(configuration.getSourceInfo());
}
if(configuration.getTargetInfo() != null){
replaceURIsWithPrefixes(configuration.getTargetInfo());
}


return configuration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,15 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
children = list.item(0).getChildNodes();
configuration.setOutputFormat(getText(list.item(0)));
}

// 10. Check for invalid config because of URIs without prefixes
if(configuration.getSourceInfo() != null){
replaceURIsWithPrefixes(configuration.getSourceInfo());
}
if(configuration.getTargetInfo() != null){
replaceURIsWithPrefixes(configuration.getTargetInfo());
}

}
} catch (Exception e) {
logger.warn(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ else if (q[ql].contains("^")) {
if (kb.getRestrictions().size() > 0) {
query = query + "}";
}

logger.info("Query issued is \n" + query);
return query;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.aksw.limes.core.io.config.reader.xml;

import org.aksw.limes.core.io.config.Configuration;
import org.aksw.limes.core.io.config.KBInfo;
import org.aksw.limes.core.io.config.reader.AConfigurationReader;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;

import java.util.*;

public class AConfigurationReaderTest {
Map<String, String> prefixes;
LinkedHashMap<String, Map<String, String>> functions;
KBInfo sourceInfo, targetInfo;
Configuration testConf;

@Before
public void init() {
prefixes = new HashMap<>();
prefixes.put("geos", "http://www.opengis.net/ont/geosparql#");
prefixes.put("lgdo", "http://linkedgeodata.org/ontology/");
prefixes.put("geom", "http://geovocab.org/geometry#");
prefixes.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
prefixes.put("limes", "http://limes.sf.net/ontology/");

functions = new LinkedHashMap<>();
Map<String, String> f = new LinkedHashMap<>();
f.put("polygon", null);
functions.put("geom:geometry/geos:asWKT", f);

sourceInfo = new KBInfo(
"linkedgeodata", //String id
"http://linkedgeodata.org/sparql", //String endpoint
null, //String graph
"?x", //String var
new ArrayList<>(Arrays.asList("geom:geometry/geos:asWKT")), //List<String> properties
new ArrayList<>(), //List<String> optionalProperties
new ArrayList<>(Arrays.asList("?x a lgdo:RelayBox")), //ArrayList<String> restrictions
functions, //LinkedHashMap<String, Map<String, String>> functions
prefixes, //Map<String, String> prefixes
2000, //int pageSize
"sparql", //String type
-1, //int minOffset
-1 //int maxoffset
);

targetInfo = new KBInfo(
"linkedgeodata", //String id
"http://linkedgeodata.org/sparql", //String endpoint
null, //String graph
"?y", //String var
new ArrayList<>(Arrays.asList("geom:geometry/geos:asWKT")), //List<String> properties
new ArrayList<>(), //List<String> optionalProperties
new ArrayList<>(Arrays.asList("?y a lgdo:RelayBox")), //ArrayList<String> restrictions
functions, //LinkedHashMap<String, Map<String, String>> functions
prefixes, //Map<String, String> prefixes
2000, //int pageSize
"sparql", //String type
-1, //int minOffset
-1 //int maxoffset
);

testConf = new Configuration();
testConf.setPrefixes(prefixes);
testConf.setSourceInfo(sourceInfo);
testConf.setTargetInfo(targetInfo);

}

@Test
public void testReplaceWithPrefixProp() {
testConf.getSourceInfo().addProperty("http://www.opengis.net/ont/geosparql#test");
AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo());
assertTrue(testConf.getSourceInfo().getProperties().contains("geos:test"));
}

@Test
public void testReplaceWithPrefixOptionalProp() {
testConf.getSourceInfo().addOptionalProperty("http://www.opengis.net/ont/geosparql#test");
AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo());
assertTrue(testConf.getSourceInfo().getOptionalProperties().contains("geos:test"));
}

@Test
public void testReplaceWithPrefixRestriction() {
testConf.getSourceInfo().addRestriction("http://www.opengis.net/ont/geosparql#test");
AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo());
assertTrue(testConf.getSourceInfo().getRestrictions().contains("geos:test"));
}

@Test
public void testReplaceWithPrefixFunc() {
LinkedHashMap<String, Map<String, String>> x = testConf.getSourceInfo().getFunctions();
HashMap<String, String> y = new HashMap<>();
y.put("http://www.opengis.net/ont/geosparql#test", "toLower");
x.put("http://www.opengis.net/ont/geosparql#test", y);
testConf.getSourceInfo().setFunctions(x);
AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo());
assertTrue(testConf.getSourceInfo().getFunctions().containsKey("geos:test"));
}

@Test
public void testReplaceWithPrefixPropError() {
testConf.getPrefixes().remove("geos");
testConf.getSourceInfo().addProperty("http://www.opengis.net/ont/geosparql#test");
assertThrows(IllegalArgumentException.class, () -> AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo()));
}

@Test
public void testReplaceWithPrefixOptionalPropError() {
testConf.getPrefixes().remove("geos");
testConf.getSourceInfo().addOptionalProperty("http://www.opengis.net/ont/geosparql#test");
assertThrows(IllegalArgumentException.class, () -> AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo()));
}

@Test
public void testReplaceWithPrefixRestrictionError() {
testConf.getPrefixes().remove("geos");
testConf.getSourceInfo().addRestriction("http://www.opengis.net/ont/geosparql#test");
assertThrows(IllegalArgumentException.class, () -> AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo()));
}

@Test
public void testReplaceWithPrefixFuncError() {
testConf.getPrefixes().remove("geos");
LinkedHashMap<String, Map<String, String>> x = testConf.getSourceInfo().getFunctions();
HashMap<String, String> y = new HashMap<>();
y.put("http://www.opengis.net/ont/geosparql#test", "toLower");
x.put("http://www.opengis.net/ont/geosparql#test", y);
testConf.getSourceInfo().setFunctions(x);
assertThrows(IllegalArgumentException.class, () -> AConfigurationReader.replaceURIsWithPrefixes(testConf.getSourceInfo()));
}

}

0 comments on commit ad12b40

Please sign in to comment.