Search in sources :

Example 1 with RdfUri

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"))));
}
Also used : ANY(nl.knaw.huygens.timbuctoo.rml.TripleMatcher.ANY) RmlMappingDocument(nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument) TripleMatcher.likeTriple(nl.knaw.huygens.timbuctoo.rml.TripleMatcher.likeTriple) TriplesMapBuilder(nl.knaw.huygens.timbuctoo.rml.rmldata.builders.TriplesMapBuilder) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) RdfUri(nl.knaw.huygens.timbuctoo.rml.dto.RdfUri) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Test(org.junit.Test) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RmlMappingDocument.rmlMappingDocument(nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument.rmlMappingDocument) RdfResource(nl.knaw.huygens.timbuctoo.rml.rdfshim.RdfResource) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RdfValue(nl.knaw.huygens.timbuctoo.rml.dto.RdfValue) Optional(java.util.Optional) RdfLiteral(nl.knaw.huygens.timbuctoo.rml.rdfshim.RdfLiteral) Mockito.mock(org.mockito.Mockito.mock) Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RmlMappingDocument(nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument) Test(org.junit.Test)

Example 2 with RdfUri

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");
    }
}
Also used : Matcher(java.util.regex.Matcher) RdfUri(nl.knaw.huygens.timbuctoo.rml.dto.RdfUri) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RdfValue(nl.knaw.huygens.timbuctoo.rml.dto.RdfValue) RdfBlankNode(nl.knaw.huygens.timbuctoo.rml.dto.RdfBlankNode)

Example 3 with RdfUri

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;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) ErrorHandler(nl.knaw.huygens.timbuctoo.rml.ErrorHandler) Collectors(java.util.stream.Collectors) Row(nl.knaw.huygens.timbuctoo.rml.Row) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) RdfUri(nl.knaw.huygens.timbuctoo.rml.dto.RdfUri) Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) Optional(java.util.Optional) RrRefObjectMap(nl.knaw.huygens.timbuctoo.rml.rmldata.termmaps.RrRefObjectMap) DataSource(nl.knaw.huygens.timbuctoo.rml.DataSource) Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) RdfUri(nl.knaw.huygens.timbuctoo.rml.dto.RdfUri) RrRefObjectMap(nl.knaw.huygens.timbuctoo.rml.rmldata.termmaps.RrRefObjectMap)

Example 4 with RdfUri

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"))));
}
Also used : RdfUri(nl.knaw.huygens.timbuctoo.rml.dto.RdfUri) TestRow(nl.knaw.huygens.timbuctoo.rml.TestRow) ThrowingErrorHandler(nl.knaw.huygens.timbuctoo.rml.ThrowingErrorHandler) QuadPart(nl.knaw.huygens.timbuctoo.rml.dto.QuadPart) Test(org.junit.Test)

Aggregations

RdfUri (nl.knaw.huygens.timbuctoo.rml.dto.RdfUri)4 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Quad (nl.knaw.huygens.timbuctoo.rml.dto.Quad)2 RdfValue (nl.knaw.huygens.timbuctoo.rml.dto.RdfValue)2 Test (org.junit.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Matcher (java.util.regex.Matcher)1