use of nl.knaw.huygens.timbuctoo.rml.dto.RdfUri in project timbuctoo by HuygensING.
the class RmlMapperTest method createsExactlyAsManySplitOffsAsNecessary.
@Test
public void createsExactlyAsManySplitOffsAsNecessary() {
/* given
x dep y
y dep a
y dep y
y dep z
z dep x
and unsortedList = [z, x, y, a]
we expect exactly one splitOff for y (y') such that
y dep a
y' dep y
y' dep z
in a sortedList as follows: [a, y, x, z, y']
before this test it was:
y dep a
y' dep y
y'' dep z
in a sortedList as follows: [a, y, x, z, y', y'']
*/
final String nameSpace = "http://example.org/";
final String mappingNameSpace = nameSpace + "mappings/";
final String theDependsOnPredicate = mappingNameSpace + "vocab#dependsOn";
final String theDepColumnKey = "dependsOn";
final String theIdColumnKey = "ID";
ErrorHandler errorHandler = mock(ErrorHandler.class);
AtomicInteger passesThroughDataSourceX = new AtomicInteger(0);
AtomicInteger passesThroughDataSourceY = new AtomicInteger(0);
AtomicInteger passesThroughDataSourceZ = new AtomicInteger(0);
AtomicInteger passesThroughDataSourceA = new AtomicInteger(0);
RmlMappingDocument rmlMappingDocument = rmlMappingDocument().withTripleMap(mappingNameSpace + "z", trip -> trip.withLogicalSource(rdf(nameSpace + "z")).withSubjectMap(sm -> sm.withTermMap(tm -> tm.withColumnTerm("rdfUri"))).withPredicateObjectMap(pom -> pom.withPredicate(theDependsOnPredicate).withReference(rm -> rm.withParentTriplesMap(mappingNameSpace + "x").withJoinCondition(theDepColumnKey, theIdColumnKey)))).withTripleMap(mappingNameSpace + "x", trip -> trip.withSubjectMap(sm -> sm.withTermMap(tm -> tm.withColumnTerm("rdfUri"))).withLogicalSource(rdf(nameSpace + "x")).withPredicateObjectMap(pom -> pom.withPredicate(theDependsOnPredicate).withReference(rm -> rm.withParentTriplesMap(mappingNameSpace + "y").withJoinCondition(theDepColumnKey, theIdColumnKey)))).withTripleMap(mappingNameSpace + "y", trip -> trip.withSubjectMap(sm -> sm.withTermMap(tm -> tm.withColumnTerm("rdfUri"))).withLogicalSource(rdf(nameSpace + "y")).withPredicateObjectMap(pom -> pom.withPredicate(theDependsOnPredicate).withReference(rm -> rm.withParentTriplesMap(mappingNameSpace + "y").withJoinCondition(theDepColumnKey + "Y", theIdColumnKey))).withPredicateObjectMap(pom -> pom.withPredicate(theDependsOnPredicate).withReference(rm -> rm.withParentTriplesMap(mappingNameSpace + "z").withJoinCondition(theDepColumnKey + "Z", theIdColumnKey))).withPredicateObjectMap(pom -> pom.withPredicate(theDependsOnPredicate).withReference(rm -> rm.withParentTriplesMap(mappingNameSpace + "a").withJoinCondition(theDepColumnKey + "A", theIdColumnKey)))).withTripleMap(mappingNameSpace + "a", trip -> trip.withLogicalSource(rdf(nameSpace + "a")).withSubjectMap(sm -> sm.withTermMap(tm -> tm.withColumnTerm("rdfUri"))).withPredicateObjectMap(pom -> pom.withPredicate(nameSpace + "vocab#name").withObjectMap(tm -> tm.withColumnTerm("naam")))).build(logicalSource -> {
if (logicalSource.asIri().get().equals(nameSpace + "z")) {
passesThroughDataSourceZ.incrementAndGet();
return Optional.of(new TestDataSource(Lists.newArrayList(ImmutableMap.of(theIdColumnKey, "z1", theDepColumnKey, "x1", "rdfUri", nameSpace + "z1")), errorHandler));
}
if (logicalSource.asIri().get().equals(nameSpace + "x")) {
passesThroughDataSourceX.incrementAndGet();
return Optional.of(new TestDataSource(Lists.newArrayList(ImmutableMap.of(theIdColumnKey, "x1", theDepColumnKey, "y1", "rdfUri", nameSpace + "x1")), errorHandler));
}
if (logicalSource.asIri().get().equals(nameSpace + "y")) {
passesThroughDataSourceY.incrementAndGet();
return Optional.of(new TestDataSource(Lists.newArrayList(ImmutableMap.of(theIdColumnKey, "y1", theDepColumnKey + "Y", "y2", "rdfUri", nameSpace + "y1"), ImmutableMap.of(theIdColumnKey, "y2", theDepColumnKey + "A", "a1", "rdfUri", nameSpace + "y2"), ImmutableMap.of(theIdColumnKey, "y3", theDepColumnKey + "Z", "z1", "rdfUri", nameSpace + "y3")), errorHandler));
}
if (logicalSource.asIri().get().equals(nameSpace + "a")) {
passesThroughDataSourceA.incrementAndGet();
return Optional.of(new TestDataSource(Lists.newArrayList(ImmutableMap.of(theIdColumnKey, "a1", "naam", "Naam van A", "rdfUri", nameSpace + "a1")), errorHandler));
}
return null;
});
final List<Quad> result = rmlMappingDocument.execute(errorHandler).collect(Collectors.toList());
// Verify that no unnecessary splitOffs have been generated by counting the amount of passes through each
// datasource.
assertThat(passesThroughDataSourceA.intValue(), equalTo(1));
assertThat(passesThroughDataSourceX.intValue(), equalTo(1));
assertThat(passesThroughDataSourceZ.intValue(), equalTo(1));
assertThat(passesThroughDataSourceY.intValue(), equalTo(2));
assertThat(result, containsInAnyOrder(likeTriple(uri(nameSpace + "a1"), uri(nameSpace + "vocab#name"), literal("Naam van A")), likeTriple(uri(nameSpace + "y2"), uri(theDependsOnPredicate), uri(nameSpace + "a1")), likeTriple(uri(nameSpace + "x1"), uri(theDependsOnPredicate), uri(nameSpace + "y1")), likeTriple(uri(nameSpace + "z1"), uri(theDependsOnPredicate), uri(nameSpace + "x1")), likeTriple(uri(nameSpace + "y3"), uri(theDependsOnPredicate), uri(nameSpace + "z1")), likeTriple(uri(nameSpace + "y1"), uri(theDependsOnPredicate), uri(nameSpace + "y2"))));
}
use of nl.knaw.huygens.timbuctoo.rml.dto.RdfUri in project timbuctoo by HuygensING.
the class RrTemplate method generateValue.
@Override
public Optional<QuadPart> generateValue(Row input) {
Matcher regexMatcher = pattern.matcher(template);
StringBuffer resultString = new StringBuffer();
while (regexMatcher.find()) {
String value = input.getRawValue(regexMatcher.group(1));
if (value == null) {
return Optional.empty();
} else {
if (termType == TermType.IRI) {
try {
value = URLEncoder.encode(value, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
LOG.error("Java is being really stoopid and throws an unsupportedEncodingException for UTF-8.");
throw new RuntimeException(e);
}
}
regexMatcher.appendReplacement(resultString, value);
}
}
if (StringUtils.isBlank(resultString.toString())) {
return Optional.empty();
}
regexMatcher.appendTail(resultString);
switch(termType) {
case IRI:
return Optional.of(new RdfUri(resultString.toString()));
case BlankNode:
return Optional.of(new RdfBlankNode(resultString.toString()));
case Literal:
return Optional.of(new RdfValue(resultString.toString(), dataType));
default:
throw new UnsupportedOperationException("Not all items in the Enumerable where handled");
}
}
use of nl.knaw.huygens.timbuctoo.rml.dto.RdfUri in project timbuctoo by HuygensING.
the class RrTriplesMap method getItems.
Stream<Quad> getItems(ErrorHandler defaultErrorHandler) {
final int[] numberOfItemsProcessed = new int[1];
Stream<Quad> quadStream = dataSource.getRows(defaultErrorHandler).peek(e -> {
numberOfItemsProcessed[0]++;
if (numberOfItemsProcessed[0] == 1) {
LoggerFactory.getLogger(RrTriplesMap.class).info("collection '{}' has at least one item", uri);
}
}).flatMap(row -> {
Optional<RdfUri> subjectOpt = subjectMap.generateValue(row);
if (subjectOpt.isPresent()) {
RdfUri subject = subjectOpt.get();
for (Tuple<RrRefObjectMap, String> subscription : subscriptions) {
subscription.getLeft().onNewSubject(row.getRawValue(subscription.getRight()), subject);
}
return predicateObjectMaps.stream().flatMap(predicateObjectMap -> predicateObjectMap.generateValue(subject, row));
} else {
defaultErrorHandler.subjectGenerationFailed(uri, row);
return Stream.empty();
}
});
return quadStream;
}
use of nl.knaw.huygens.timbuctoo.rml.dto.RdfUri in project timbuctoo by HuygensING.
the class RrTemplateTest method iriEncodesValueIfResultTypeIsIri.
@Test
public void iriEncodesValueIfResultTypeIsIri() throws Exception {
TestRow row = new TestRow(ImmutableMap.of("foo", "some s\"tring with wéird characters"), ImmutableMap.of(), new ThrowingErrorHandler());
RrTemplate rrTemplate = new RrTemplate("http://{foo}", TermType.IRI, null);
Optional<QuadPart> actual = rrTemplate.generateValue(row);
assertThat(actual, is(Optional.of(new RdfUri("http://some%20s%22tring%20with%20w%C3%A9ird%20characters"))));
}
Aggregations