Search in sources :

Example 1 with Quad

use of nl.knaw.huygens.timbuctoo.rml.dto.Quad 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"))));
}
Also used : Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) RmlMappingDocument(nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument) Test(org.junit.Test)

Example 2 with Quad

use of nl.knaw.huygens.timbuctoo.rml.dto.Quad 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 3 with Quad

use of nl.knaw.huygens.timbuctoo.rml.dto.Quad 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);
    }
}
Also used : Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) ByteArrayInputStream(java.io.ByteArrayInputStream) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) Model(org.apache.jena.rdf.model.Model) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) RmlMappingDocument(nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument)

Example 4 with Quad

use of nl.knaw.huygens.timbuctoo.rml.dto.Quad 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 5 with Quad

use of nl.knaw.huygens.timbuctoo.rml.dto.Quad 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"))));
}
Also used : Quad(nl.knaw.huygens.timbuctoo.rml.dto.Quad) RmlMappingDocument(nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument) Test(org.junit.Test)

Aggregations

Quad (nl.knaw.huygens.timbuctoo.rml.dto.Quad)7 RmlMappingDocument (nl.knaw.huygens.timbuctoo.rml.rmldata.RmlMappingDocument)6 Test (org.junit.Test)5 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 RdfUri (nl.knaw.huygens.timbuctoo.rml.dto.RdfUri)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ByteArrayInputStream (java.io.ByteArrayInputStream)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 Collectors.toList (java.util.stream.Collectors.toList)1