Search in sources :

Example 11 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceServiceTest method testPutDeleteLdpC.

@Test
void testPutDeleteLdpC() {
    final TriplestoreResourceService svc = new TriplestoreResourceService();
    svc.idService = idService;
    svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
    svc.initialize();
    final Dataset dataset = rdf.createDataset();
    dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("resource title"));
    dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("resource description"));
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant later = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.Container).container(root).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(resource).thenAccept(checkResource(later, LDP.Container, 2L, 1L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
    // Now add a child resource
    dataset.clear();
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    dataset.add(Trellis.PreferUserManaged, child, DC.title, rdf.createLiteral("child"));
    dataset.add(Trellis.PreferUserManaged, child, DC.description, rdf.createLiteral("nested resource"));
    final Instant evenLater = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(child).interactionModel(LDP.RDFSource).container(resource).build(), dataset).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(child).thenAccept(checkChild(evenLater, 2L, 1L)).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(evenLater, LDP.Container, 2L, 1L, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(evenLater)).toCompletableFuture()).join();
    // Now delete the child resource
    final BlankNode bnode = rdf.createBlankNode();
    dataset.clear();
    dataset.add(Trellis.PreferAudit, bnode, type, AS.Delete);
    dataset.add(Trellis.PreferAudit, bnode, type, PROV.Activity);
    dataset.add(Trellis.PreferServerManaged, child, type, LDP.Resource);
    final Instant preDelete = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.delete(builder(child).interactionModel(LDP.RDFSource).container(resource).build()).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful delete operation!");
    allOf(svc.get(child).thenAccept(res -> assertEquals(DELETED_RESOURCE, res, "Incorrect resource object!")).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(preDelete, LDP.Container, 2L, 1L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(preDelete)).toCompletableFuture()).join();
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) XSD(org.trellisldp.vocabulary.XSD) Optional.of(java.util.Optional.of) RDF.type(org.trellisldp.vocabulary.RDF.type) Resource(org.trellisldp.api.Resource) SKOS(org.trellisldp.vocabulary.SKOS) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DC(org.trellisldp.vocabulary.DC) Literal(org.apache.commons.rdf.api.Literal) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) Awaitility.setDefaultPollInterval(org.awaitility.Awaitility.setDefaultPollInterval) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) RDFConnectionAdapter(org.apache.jena.rdflink.RDFConnectionAdapter) RDFLinkHTTP(org.apache.jena.rdflink.RDFLinkHTTP) RDFS(org.trellisldp.vocabulary.RDFS) Instant.now(java.time.Instant.now) Mock(org.mockito.Mock) Metadata.builder(org.trellisldp.api.Metadata.builder) RDF(org.apache.commons.rdf.api.RDF) AS(org.trellisldp.vocabulary.AS) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) BinaryMetadata(org.trellisldp.api.BinaryMetadata) DefaultIdentifierService(org.trellisldp.api.DefaultIdentifierService) RDFFactory(org.trellisldp.api.RDFFactory) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) BlankNode(org.apache.commons.rdf.api.BlankNode) Predicate.isEqual(java.util.function.Predicate.isEqual) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) IRI(org.apache.commons.rdf.api.IRI) PROV(org.trellisldp.vocabulary.PROV) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) UpdateRequest(org.apache.jena.update.UpdateRequest) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) Dataset(org.apache.commons.rdf.api.Dataset) Instant(java.time.Instant) BlankNode(org.apache.commons.rdf.api.BlankNode) Test(org.junit.jupiter.api.Test)

Example 12 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceServiceTest method testPutLdpDc.

@Test
void testPutLdpDc() {
    final TriplestoreResourceService svc = new TriplestoreResourceService();
    svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
    svc.idService = idService;
    svc.initialize();
    final Dataset dataset = rdf.createDataset();
    dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("direct container"));
    dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("LDP-DC test"));
    dataset.add(Trellis.PreferUserManaged, resource, LDP.membershipResource, members);
    dataset.add(Trellis.PreferUserManaged, resource, LDP.hasMemberRelation, DC.relation);
    final Instant later = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.DirectContainer).container(root).memberRelation(DC.relation).membershipResource(members).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(resource).thenAccept(checkResource(later, LDP.DirectContainer, 4L, 0L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
    // Now add a membership resource
    dataset.clear();
    dataset.add(Trellis.PreferUserManaged, members, DC.title, rdf.createLiteral("member resource"));
    final Instant evenLater = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(members).interactionModel(LDP.RDFSource).container(root).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(members).thenAccept(checkMember(evenLater, 1L, 0L, 0L)).toCompletableFuture(), svc.get(members).thenAccept(res -> assertFalse(res.getBinaryMetadata().isPresent(), "Unexpected binary metadata!")).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(later, LDP.DirectContainer, 4L, 0L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(checkPredates(evenLater)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater, 2L)).toCompletableFuture()).join();
    // Now add the child resources to the ldp-dc
    dataset.clear();
    dataset.add(Trellis.PreferUserManaged, child, DC.title, rdf.createLiteral("ldp-dc child resource"));
    final Instant evenLater2 = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(child).interactionModel(LDP.RDFSource).container(resource).build(), dataset).toCompletableFuture(), svc.touch(members).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful create operation!");
    final IRI container = rdf.createIRI(resource.getIRIString() + "/");
    allOf(svc.get(child).thenAccept(checkChild(evenLater2, 1L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(evenLater2, LDP.DirectContainer, 4L, 0L, 1L)).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertTrue(res.stream(LDP.PreferContainment).anyMatch(isEqual(rdf.createQuad(LDP.PreferContainment, container, LDP.contains, child))), "Missing contains triple!")).toCompletableFuture(), svc.get(members).thenAccept(checkMember(evenLater2, 1L, 0L, 1L)).toCompletableFuture(), svc.get(members).thenAccept(res -> assertTrue(res.stream(LDP.PreferMembership).anyMatch(isEqual(rdf.createQuad(LDP.PreferMembership, members, DC.relation, child))), "Missing membership triple!")).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater, 2L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(evenLater2)).toCompletableFuture()).join();
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) XSD(org.trellisldp.vocabulary.XSD) Optional.of(java.util.Optional.of) RDF.type(org.trellisldp.vocabulary.RDF.type) Resource(org.trellisldp.api.Resource) SKOS(org.trellisldp.vocabulary.SKOS) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DC(org.trellisldp.vocabulary.DC) Literal(org.apache.commons.rdf.api.Literal) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) Awaitility.setDefaultPollInterval(org.awaitility.Awaitility.setDefaultPollInterval) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) RDFConnectionAdapter(org.apache.jena.rdflink.RDFConnectionAdapter) RDFLinkHTTP(org.apache.jena.rdflink.RDFLinkHTTP) RDFS(org.trellisldp.vocabulary.RDFS) Instant.now(java.time.Instant.now) Mock(org.mockito.Mock) Metadata.builder(org.trellisldp.api.Metadata.builder) RDF(org.apache.commons.rdf.api.RDF) AS(org.trellisldp.vocabulary.AS) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) BinaryMetadata(org.trellisldp.api.BinaryMetadata) DefaultIdentifierService(org.trellisldp.api.DefaultIdentifierService) RDFFactory(org.trellisldp.api.RDFFactory) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) BlankNode(org.apache.commons.rdf.api.BlankNode) Predicate.isEqual(java.util.function.Predicate.isEqual) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) IRI(org.apache.commons.rdf.api.IRI) PROV(org.trellisldp.vocabulary.PROV) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) UpdateRequest(org.apache.jena.update.UpdateRequest) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) IRI(org.apache.commons.rdf.api.IRI) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) Dataset(org.apache.commons.rdf.api.Dataset) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 13 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceServiceTest method testPutLdpDcMultipleInverse.

@Test
void testPutLdpDcMultipleInverse() {
    final TriplestoreResourceService svc = new TriplestoreResourceService();
    svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
    svc.idService = idService;
    svc.initialize();
    final IRI container = rdf.createIRI(resource.getIRIString() + "/");
    final IRI container2 = rdf.createIRI(resource2.getIRIString() + "/");
    final Dataset dataset = rdf.createDataset();
    dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("direct container inverse"));
    dataset.add(Trellis.PreferUserManaged, resource, RDFS.label, rdf.createLiteral("LDP-DC test"));
    dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("LDP-DC inverse test"));
    dataset.add(Trellis.PreferUserManaged, resource, LDP.membershipResource, members);
    dataset.add(Trellis.PreferUserManaged, resource, LDP.isMemberOfRelation, DC.relation);
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant later = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.DirectContainer).container(root).membershipResource(members).memberOfRelation(DC.relation).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(resource).thenAccept(checkResource(later, LDP.DirectContainer, 5L, 1L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
    dataset.clear();
    dataset.add(Trellis.PreferUserManaged, resource2, DC.title, rdf.createLiteral("Second LDP-DC"));
    dataset.add(Trellis.PreferUserManaged, resource2, LDP.membershipResource, members);
    dataset.add(Trellis.PreferUserManaged, resource2, LDP.isMemberOfRelation, DC.subject);
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant evenLater = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(resource2).interactionModel(LDP.DirectContainer).container(root).membershipResource(members).memberOfRelation(DC.subject).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(resource2).thenAccept(res -> assertAll("Check resource", () -> checkResource(res, resource2, LDP.DirectContainer, evenLater), () -> checkResourceStream(res, 3L, 0L, 1L, 0L, 0L))).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater, 2L)).toCompletableFuture()).join();
    // Now add a membership resource
    dataset.clear();
    dataset.add(Trellis.PreferUserManaged, members, DC.title, rdf.createLiteral("Membership resource"));
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant evenLater2 = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(members).interactionModel(LDP.RDFSource).container(root).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful membership resource create operation!");
    allOf(svc.get(members).thenAccept(checkMember(evenLater2, 1L, 1L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertAll("Check resource", () -> checkResource(later, LDP.DirectContainer, 5L, 1L, 0L).accept(res), () -> checkPredates(evenLater2).accept(res))).toCompletableFuture(), svc.get(resource2).thenAccept(res -> assertAll("Check second resource", () -> checkPredates(evenLater2).accept(res), () -> checkResourceStream(res, 3L, 0L, 1L, 0L, 0L))).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater2, 3L)).toCompletableFuture()).join();
    // Now add the child resources to the ldp-dc
    dataset.clear();
    dataset.add(Trellis.PreferUserManaged, child, DC.title, rdf.createLiteral("Child resource"));
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    final Instant evenLater3 = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(child).interactionModel(LDP.RDFSource).container(resource).build(), dataset).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(child).thenAccept(res -> assertAll("Check resource", () -> checkResource(res, child, LDP.RDFSource, evenLater3), () -> checkResourceStream(res, 1L, 0L, 1L, 1L, 0L))).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertAll("Check resource", () -> checkResource(evenLater3, LDP.DirectContainer, 5L, 1L, 1L).accept(res), () -> assertTrue(res.stream(LDP.PreferContainment).anyMatch(isEqual(rdf.createQuad(LDP.PreferContainment, container, LDP.contains, child))), "Missing contains triple!"))).toCompletableFuture(), svc.get(members).thenAccept(res -> assertAll("Check member resource", () -> checkMember(evenLater2, 1L, 1L, 0L).accept(res), () -> checkPredates(evenLater3).accept(res))).toCompletableFuture(), svc.get(root).thenAccept(res -> assertAll("Check root resource", () -> checkRoot(evenLater2, 3L).accept(res), () -> checkPredates(evenLater3).accept(res))).toCompletableFuture()).join();
    // Now add a child resources to the other ldp-dc
    dataset.clear();
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
    dataset.add(Trellis.PreferUserManaged, child2, DC.title, rdf.createLiteral("Second child resource"));
    final Instant evenLater4 = meanwhile();
    assertDoesNotThrow(() -> allOf(svc.create(builder(child2).interactionModel(LDP.RDFSource).container(resource2).build(), dataset).toCompletableFuture(), svc.touch(resource2).toCompletableFuture()).join(), "Unsuccessful create operation!");
    allOf(svc.get(child2).thenAccept(res -> assertAll("Check child resource", () -> checkResource(res, child2, LDP.RDFSource, evenLater4), () -> checkResourceStream(res, 1L, 0L, 1L, 1L, 0L))).toCompletableFuture(), svc.get(resource2).thenAccept(res -> assertAll("Check resource", () -> checkResource(res, resource2, LDP.DirectContainer, evenLater4), () -> checkResourceStream(res, 3L, 0L, 1L, 0L, 1L), () -> assertTrue(res.stream(LDP.PreferContainment).anyMatch(isEqual(rdf.createQuad(LDP.PreferContainment, container2, LDP.contains, child2))), "Missing contains triple!"))).toCompletableFuture(), svc.get(members).thenAccept(res -> assertAll("Check member resource", () -> checkMember(evenLater2, 1L, 1L, 0L).accept(res), () -> checkPredates(evenLater4).accept(res))).toCompletableFuture(), svc.get(root).thenAccept(res -> assertAll("Check root resource", () -> checkRoot(evenLater2, 3L).accept(res), () -> checkPredates(evenLater4).accept(res))).toCompletableFuture()).join();
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) XSD(org.trellisldp.vocabulary.XSD) Optional.of(java.util.Optional.of) RDF.type(org.trellisldp.vocabulary.RDF.type) Resource(org.trellisldp.api.Resource) SKOS(org.trellisldp.vocabulary.SKOS) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DC(org.trellisldp.vocabulary.DC) Literal(org.apache.commons.rdf.api.Literal) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) Awaitility.setDefaultPollInterval(org.awaitility.Awaitility.setDefaultPollInterval) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) RDFConnectionAdapter(org.apache.jena.rdflink.RDFConnectionAdapter) RDFLinkHTTP(org.apache.jena.rdflink.RDFLinkHTTP) RDFS(org.trellisldp.vocabulary.RDFS) Instant.now(java.time.Instant.now) Mock(org.mockito.Mock) Metadata.builder(org.trellisldp.api.Metadata.builder) RDF(org.apache.commons.rdf.api.RDF) AS(org.trellisldp.vocabulary.AS) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) Trellis(org.trellisldp.vocabulary.Trellis) BinaryMetadata(org.trellisldp.api.BinaryMetadata) DefaultIdentifierService(org.trellisldp.api.DefaultIdentifierService) RDFFactory(org.trellisldp.api.RDFFactory) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) BlankNode(org.apache.commons.rdf.api.BlankNode) Predicate.isEqual(java.util.function.Predicate.isEqual) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) IRI(org.apache.commons.rdf.api.IRI) PROV(org.trellisldp.vocabulary.PROV) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) UpdateRequest(org.apache.jena.update.UpdateRequest) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) IRI(org.apache.commons.rdf.api.IRI) RDFLinkDataset(org.apache.jena.rdflink.RDFLinkDataset) Dataset(org.apache.commons.rdf.api.Dataset) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 14 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceService method delete.

@Override
public CompletionStage<Void> delete(final Metadata metadata) {
    LOGGER.debug("Deleting: {}", metadata.getIdentifier());
    return runAsync(() -> {
        try (final Dataset dataset = rdf.createDataset()) {
            final Instant eventTime = now();
            dataset.add(PreferServerManaged, metadata.getIdentifier(), DC.type, DeletedResource);
            dataset.add(PreferServerManaged, metadata.getIdentifier(), type, LDP.Resource);
            storeResource(metadata.getIdentifier(), dataset, eventTime, OperationType.DELETE);
        } catch (final Exception ex) {
            throw new TrellisRuntimeException("Error deleting resource: " + metadata.getIdentifier(), ex);
        }
    });
}
Also used : TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Dataset(org.apache.commons.rdf.api.Dataset) Instant(java.time.Instant) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException)

Example 15 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceService method buildUpdateRequest.

/**
 * This is equivalent to the SPARQL below.
 *
 * <p><pre><code>
 * DELETE WHERE { GRAPH IDENTIFIER { ?s ?p ?o } };
 * DELETE WHERE { GRAPH IDENTIFIER?ext=acl { ?s ?p ?o } };
 * DELETE WHERE { GRAPH trellis:PreferServerManaged {
 *   IDENTIFIER a ldp:NonRDFSource .
 *   IDENTIFIER dc:hasPart ?s .
 *   ?s ?p ?o .
 * };
 * DELETE WHERE { GRAPH trellis:PreferServerManaged { IDENTIFIER ?p ?o } };
 * INSERT DATA {
 *   GRAPH IDENTIFIER { ... }
 *   GRAPH IDENTIFIER?ext=acl { ... }
 *   GRAPH trellis:PreferServerManaged { ... }
 *   GRAPH IDENTIFIER?ext=audit { ... }
 * }
 * </code></pre></p>
 */
private UpdateRequest buildUpdateRequest(final IRI identifier, final Literal time, final Dataset dataset, final OperationType operation) {
    // Set the time
    dataset.add(PreferServerManaged, identifier, DC.modified, time);
    final UpdateRequest req = new UpdateRequest();
    req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(identifier), SUBJECT, PREDICATE, OBJECT)))));
    extensions.forEach((ext, graph) -> req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(getExtIRI(identifier, ext), SUBJECT, PREDICATE, OBJECT))))));
    req.add(new UpdateDeleteWhere(new QuadAcc(asList(new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(type), toJena(LDP.NonRDFSource)), new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(DC.hasPart), SUBJECT), new Quad(toJena(PreferServerManaged), SUBJECT, PREDICATE, OBJECT)))));
    req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(PreferServerManaged), toJena(identifier), PREDICATE, OBJECT)))));
    final QuadDataAcc sink = new QuadDataAcc(synchronizedList(new ArrayList<>()));
    if (operation == OperationType.DELETE) {
        dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
    } else {
        dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
        dataset.getGraph(PreferUserManaged).ifPresent(g -> g.stream().map(t -> new Quad(toJena(identifier), toJena(t))).forEach(sink::addQuad));
        dataset.getGraph(PreferAudit).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, "audit"), toJena(t))).forEach(sink::addQuad));
        extensions.forEach((ext, graph) -> dataset.getGraph(graph).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, ext), toJena(t))).forEach(sink::addQuad)));
    }
    req.add(new UpdateDataInsert(sink));
    return req;
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) XSD(org.trellisldp.vocabulary.XSD) DeletedResource(org.trellisldp.vocabulary.Trellis.DeletedResource) DatabaseMgr.connectDatasetGraph(org.apache.jena.tdb2.DatabaseMgr.connectDatasetGraph) RDF.type(org.trellisldp.vocabulary.RDF.type) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc) Resource(org.trellisldp.api.Resource) Collections.singletonList(java.util.Collections.singletonList) SUBJECT(org.trellisldp.triplestore.TriplestoreUtils.SUBJECT) Txn.executeWrite(org.apache.jena.system.Txn.executeWrite) TrellisUtils.normalizeIdentifier(org.trellisldp.api.TrellisUtils.normalizeIdentifier) PreferAudit(org.trellisldp.vocabulary.Trellis.PreferAudit) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) Collections.synchronizedList(java.util.Collections.synchronizedList) DC(org.trellisldp.vocabulary.DC) Set(java.util.Set) Literal(org.apache.commons.rdf.api.Literal) Instant(java.time.Instant) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Var(org.apache.jena.sparql.core.Var) Stream.builder(java.util.stream.Stream.builder) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Query(org.apache.jena.query.Query) FOAF(org.trellisldp.vocabulary.FOAF) Arrays.stream(java.util.Arrays.stream) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Instant.now(java.time.Instant.now) NodeFactory.createURI(org.apache.jena.graph.NodeFactory.createURI) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) UpdateDeleteInsert(org.apache.jena.sparql.modify.request.UpdateDeleteInsert) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Supplier(java.util.function.Supplier) RDF(org.apache.commons.rdf.api.RDF) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) Quad(org.apache.jena.sparql.core.Quad) ResourceService(org.trellisldp.api.ResourceService) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) Logger(org.slf4j.Logger) PREDICATE(org.trellisldp.triplestore.TriplestoreUtils.PREDICATE) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) JenaCommonsRDF(org.apache.jena.commonsrdf.JenaCommonsRDF) TriplestoreUtils.getObject(org.trellisldp.triplestore.TriplestoreUtils.getObject) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) IRI(org.apache.commons.rdf.api.IRI) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) Node(org.apache.jena.graph.Node) DatasetFactory.createTxnMem(org.apache.jena.query.DatasetFactory.createTxnMem) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) UpdateRequest(org.apache.jena.update.UpdateRequest) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) OBJECT(org.trellisldp.triplestore.TriplestoreUtils.OBJECT) Quad(org.apache.jena.sparql.core.Quad) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) ArrayList(java.util.ArrayList) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc)

Aggregations

Dataset (org.apache.commons.rdf.api.Dataset)104 IRI (org.apache.commons.rdf.api.IRI)70 Test (org.junit.jupiter.api.Test)59 Resource (org.trellisldp.api.Resource)47 Instant (java.time.Instant)45 RDF (org.apache.commons.rdf.api.RDF)38 LDP (org.trellisldp.vocabulary.LDP)33 Stream (java.util.stream.Stream)30 Map (java.util.Map)28 BinaryMetadata (org.trellisldp.api.BinaryMetadata)28 RDFFactory (org.trellisldp.api.RDFFactory)28 Quad (org.apache.commons.rdf.api.Quad)27 Metadata (org.trellisldp.api.Metadata)27 TRELLIS_DATA_PREFIX (org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX)27 MISSING_RESOURCE (org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE)25 AS (org.trellisldp.vocabulary.AS)25 Trellis (org.trellisldp.vocabulary.Trellis)22 Logger (org.slf4j.Logger)21 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)21 DELETED_RESOURCE (org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE)21