use of nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument in project timbuctoo by HuygensING.
the class RmlMapperTest method canHandleCircularReferenceToSelf.
@Test
public void canHandleCircularReferenceToSelf() {
final String theNamePredicate = "http://example.org/vocab#name";
final String theIsParentOfPredicate = "http://example.org/vocab#isParentOf";
final String theIsRelatedToPredicate = "http://example.org/vocab#isRelatedTo";
DataSource input = new TestDataSource(Lists.newArrayList(ImmutableMap.of("rdfUri", "http://example.com/persons/1", "naam", "Bill", "parentOf", "Joe", "relatedTo", ""), ImmutableMap.of("rdfUri", "http://example.com/persons/2", "naam", "Joe", "parentOf", "", "relatedTo", "Bill")));
RmlMappingDocument rmlMappingDocument = rmlMappingDocument().withTripleMap("http://example.org/personsMap", makePersonMap(theNamePredicate, theIsParentOfPredicate, theIsRelatedToPredicate)).build(x -> Optional.of(input));
List<Quad> result = rmlMappingDocument.execute(new LoggingErrorHandler()).collect(toList());
assertThat(result, containsInAnyOrder(likeTriple(uri("http://example.com/persons/1"), uri(theNamePredicate), literal("Bill")), likeTriple(uri("http://example.com/persons/2"), uri(theNamePredicate), literal("Joe")), likeTriple(uri("http://example.com/persons/1"), uri(theIsParentOfPredicate), uri("http://example.com/persons/2")), likeTriple(uri("http://example.com/persons/2"), uri(theIsRelatedToPredicate), uri("http://example.com/persons/1"))));
}
use of nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument 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.rmldata.RmlMappingDocument in project timbuctoo by HuygensING.
the class RmlRdfCreator method sendQuads.
@Override
public void sendQuads(RdfSerializer saver, DataSet dataSet, Consumer<String> status) throws LogStorageFailedException {
RdfDataSourceFactory dataSourceFactory = dataSet.getDataSource();
final Model model = ModelFactory.createDefaultModel();
try {
model.read(new ByteArrayInputStream(rdfData.getBytes(StandardCharsets.UTF_8)), null, "JSON-LD");
} catch (Exception e) {
throw new LogStorageFailedException(e);
}
final RmlMappingDocument rmlMappingDocument = rmlBuilder.fromRdf(model, dataSourceFactory::apply);
if (rmlMappingDocument.getErrors().size() > 0) {
throw new LogStorageFailedException("failure: " + String.join("\nfailure: ", rmlMappingDocument.getErrors()) + "\n");
}
// FIXME: trigger onprefix for all rml prefixes
// FIXME: store rml and retrieve it from tripleStore when mapping
Stream<Quad> triples = rmlMappingDocument.execute(new ReportingErrorHandler(status));
Iterator<Quad> iterator = triples.iterator();
while (iterator.hasNext()) {
Quad triple = iterator.next();
saver.onQuad(triple.getSubject().getUri().get(), triple.getPredicate().getUri().get(), triple.getObject().getContent(), triple.getObject().getLiteralType().orElse(null), triple.getObject().getLiteralLanguage().orElse(null), baseUri);
}
}
use of nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument in project timbuctoo by HuygensING.
the class RmlMapperTest method canHandleCircularReferenceWithIndirection.
@Test
public void canHandleCircularReferenceWithIndirection() {
final String theNamePredicate = "http://example.org/vocab#name";
final String theWrittenByPredicate = "http://example.org/vocab#writtenBy";
final String theCoAuthorOfPredicate = "http://example.org/vocab#coAuthorOf";
RmlMappingDocument rmlMappingDocument = rmlMappingDocument().withTripleMap("http://example.org/documentsMap", makeDocumentMap(theWrittenByPredicate)).withTripleMap("http://example.org/personsMap", makePersonMap(theNamePredicate, theCoAuthorOfPredicate)).build(makePersonDocumentSourceFactory());
List<Quad> result = rmlMappingDocument.execute(new ThrowingErrorHandler()).collect(toList());
assertThat(result, containsInAnyOrder(likeTriple(uri("http://www.example.org/persons/1"), uri(theNamePredicate), literal("Bill")), likeTriple(uri("http://www.example.org/persons/1"), uri(theCoAuthorOfPredicate), uri("http://www.example.org/documents/1")), likeTriple(uri("http://www.example.org/documents/1"), uri(theWrittenByPredicate), uri("http://www.example.org/persons/1"))));
}
use of nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument in project timbuctoo by HuygensING.
the class RmlMapperTest method canGenerateLinks.
@Test
public void canGenerateLinks() {
final String theNamePredicate = "http://example.org/vocab#name";
final String theWrittenByPredicate = "http://example.org/vocab#writtenBy";
RmlMappingDocument rmlMappingDocument = rmlMappingDocument().withTripleMap("http://example.org/personsMap", makePersonMap(theNamePredicate)).withTripleMap("http://example.org/documentsMap", makeDocumentMap(theWrittenByPredicate)).build(makePersonDocumentSourceFactory());
List<Quad> result = rmlMappingDocument.execute(new ThrowingErrorHandler()).collect(toList());
assertThat(result, contains(likeTriple(uri("http://www.example.org/persons/1"), uri(theNamePredicate), literal("Bill")), likeTriple(uri("http://www.example.org/documents/1"), uri(theWrittenByPredicate), uri("http://www.example.org/persons/1"))));
}
Aggregations