@Override public Stream<Quad> stream() { return dataset.stream().map(Quad.class::cast); }
/** * Add a quad to the dataset. * * @param quad an RDF Quad */ public void add(final Quad quad) { dataset.add(quad); }
dataset.remove(of(PreferServerManaged), null, type, null); dataset.add(PreferServerManaged, identifier, type, ixnModel); if (LDP.Resource.equals(ixnModel) && dataset.contains(of(PreferAudit), null, type, Delete)) { dataset.add(PreferUserManaged, identifier, type, DeletedResource); } else { dataset.remove(of(PreferUserManaged), identifier, type, DeletedResource);
private ProducerRecord<String, String> buildContainmentMessage(final String topic, final IRI resource, final Resource parent, final Dataset dataset) throws Exception { try (final Dataset data = dataset.stream(of(PreferAudit), null, null, null).map(auditTypeMapper) .collect(toDataset())) { data.add(PreferContainment, parent.getIdentifier(), contains, resource); return new ProducerRecord<>(topic, parent.getIdentifier().getIRIString(), serialize(data)); } }
dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("Indirect Container")); dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("Test LDP-IC")); dataset.add(Trellis.PreferUserManaged, resource, DC.subject, rdf.createIRI("http://example.com/subject")); dataset.add(Trellis.PreferUserManaged, resource, RDF.type, SKOS.Concept); dataset.add(Trellis.PreferUserManaged, resource, LDP.membershipResource, members); dataset.add(Trellis.PreferUserManaged, resource, LDP.hasMemberRelation, RDFS.label); dataset.add(Trellis.PreferUserManaged, resource, LDP.insertedContentRelation, SKOS.prefLabel); dataset.add(Trellis.PreferAudit, resource, PROV.wasGeneratedBy, bnode0); dataset.add(Trellis.PreferAudit, bnode0, PROV.atTime, rdf.createLiteral(now().toString(), XSD.dateTime)); dataset.add(Trellis.PreferAudit, bnode0, RDF.type, PROV.Activity); dataset.add(Trellis.PreferAudit, bnode0, RDF.type, AS.Create); dataset.clear(); dataset.add(Trellis.PreferUserManaged, members, DC.title, rdf.createLiteral("Membership resource")); dataset.add(Trellis.PreferAudit, members, PROV.wasGeneratedBy, bnode1); dataset.add(Trellis.PreferAudit, bnode1, PROV.atTime, rdf.createLiteral(now().toString(), XSD.dateTime)); dataset.add(Trellis.PreferAudit, bnode1, RDF.type, PROV.Activity); dataset.add(Trellis.PreferAudit, bnode1, RDF.type, AS.Create); dataset.clear(); dataset.add(Trellis.PreferUserManaged, child, SKOS.prefLabel, label); dataset.add(Trellis.PreferAudit, child, PROV.wasGeneratedBy, bnode2); dataset.add(Trellis.PreferAudit, bnode2, RDF.type, AS.Create); dataset.add(Trellis.PreferAudit, bnode2, RDF.type, PROV.Activity);
@Test public void remove() throws Exception { final long fullSize = dataset.size(); dataset.remove(Optional.of(graph1), alice, knows, bob); final long shrunkSize = dataset.size(); assertEquals(1, fullSize - shrunkSize); dataset.remove(Optional.of(graph1), alice, knows, bob); assertEquals(shrunkSize, dataset.size()); // unchanged dataset.add(graph1, alice, knows, bob); dataset.add(graph2, alice, knows, bob); dataset.add(graph2, alice, knows, bob); assertTrue(dataset.size() > shrunkSize); dataset.remove(null, alice, knows, bob); assertEquals(shrunkSize, dataset.size()); try (Stream<? extends Quad> stream = dataset.stream()) { final Optional<? extends Quad> anyQuad = stream.findAny(); Assume.assumeTrue(anyQuad.isPresent()); dataset.remove(otherQuad); assertEquals(shrunkSize - 1, dataset.size()); dataset.remove(otherQuad); assertEquals(shrunkSize - 1, dataset.size()); // no change dataset.add(otherQuad);
@Test public void testAuditUpdate() { final Dataset dataset = rdf.createDataset(); final AuditService svc = new DefaultAuditService() {}; svc.update(subject, mockSession).forEach(dataset::add); assertTrue(dataset.getGraph(Trellis.PreferAudit).filter(graph -> graph.size() == dataset.size()).isPresent()); assertTrue(dataset.contains(null, null, type, AS.Update)); assertAll("Event property check", checkEventProperties(dataset)); }
/** * Get a graph from the dataset. * * @param graphName the graph name * @return the graph */ public Optional<Graph> getGraph(final IRI graphName) { return dataset.getGraph(graphName); }
/** * Test replacing a resource. * @throws Exception if the operation failed */ @Test @DisplayName("Test replacing resource") default void testReplaceResource() throws Exception { final RDF rdf = getInstance(); final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + getResourceService().generateIdentifier()); final Dataset dataset = buildDataset(identifier, "Replacement Test", SUBJECT2); assertEquals(MISSING_RESOURCE, getResourceService().get(identifier).toCompletableFuture().join(), "Check for no pre-existing LDP-RS"); assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(identifier) .interactionModel(LDP.RDFSource).container(ROOT_CONTAINER).build(), dataset) .toCompletableFuture().join(), "Check that the LDP-RS was successfully created"); dataset.clear(); dataset.add(Trellis.PreferUserManaged, identifier, SKOS.prefLabel, rdf.createLiteral("preferred label")); dataset.add(Trellis.PreferUserManaged, identifier, SKOS.altLabel, rdf.createLiteral("alternate label")); dataset.add(Trellis.PreferUserManaged, identifier, type, SKOS.Concept); assertDoesNotThrow(() -> getResourceService().replace(Metadata.builder(identifier) .interactionModel(LDP.RDFSource).container(ROOT_CONTAINER).build(), dataset) .toCompletableFuture().join(), "Check that the LDP-RS was successfully replaced"); final Resource res = getResourceService().get(identifier).toCompletableFuture().join(); assertAll("Check the replaced LDP-RS stream", res.stream(Trellis.PreferUserManaged) .map(toQuad(Trellis.PreferUserManaged)) .map(q -> () -> assertTrue(dataset.contains(q), "Check that the quad comes from the dataset: " + q))); assertEquals(3L, res.stream(Trellis.PreferUserManaged).count(), "Check the total user-managed triple count"); }
@Test public void getQuads() throws Exception { long quadCount; try (Stream<? extends Quad> stream = dataset.stream()) { quadCount = stream.count(); } assertTrue(quadCount > 0); try (Stream<? extends Quad> stream = dataset.stream()) { assertTrue(stream.allMatch(t -> dataset.contains(t))); } // Check exact count Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName, companyName, bobNameQuad); assertEquals(10, quadCount); }
@Override public CompletionStage<Void> add(final IRI id, final Dataset dataset) { return runAsync(() -> { final IRI graphName = rdf.createIRI(id.getIRIString() + "?ext=audit"); try (final Dataset data = rdf.createDataset()) { dataset.getGraph(PreferAudit).ifPresent(g -> g.stream().forEach(t -> data.add(graphName, t.getSubject(), t.getPredicate(), t.getObject()))); executeWrite(rdfConnection, () -> rdfConnection.loadDataset(asJenaDataset(data))); } catch (final Exception ex) { throw new RuntimeTrellisException("Error storing audit dataset for " + id, ex); } }); }
factory = createFactory(); dataset = factory.createDataset(); assertEquals(0, dataset.size()); bobName = factory.createLiteral("Bob", "en-US"); dataset.add(graph1, alice, name, aliceName); dataset.add(graph1, alice, knows, bob); dataset.add(graph1, alice, member, bnode1); dataset.add(bobNameQuad); dataset.add(factory.createQuad(graph2, bob, member, bnode1)); dataset.add(factory.createQuad(graph2, bob, member, bnode2)); dataset.add(graph1, bnode1, name, secretClubName); dataset.add(graph2, bnode2, name, companyName); dataset.add(null, alice, isPrimaryTopicOf, graph1); dataset.add(null, bob, isPrimaryTopicOf, graph2);
@Override public boolean contains(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) { return dataset.contains(unionOrNamedGraph(), subject, predicate, object); }
@Test public void testAppendSemantics() { final Quad testFirstQuad = createQuad(testResourceId3, testResourceId2, testResourceId1, badId); final Quad testSecondQuad = createQuad(testResourceId3, testResourceId2, testResourceId1, badId); // store some data in mutable and immutable sides under the same resource ID final Resource testFirstResource = new TestResource(testResourceId3, testFirstQuad); assertNull(testable.add(testResourceId3, testFirstResource.dataset()).toCompletableFuture().join(), "Couldn't create an immutable resource!"); final Resource testSecondResource = new TestResource(testResourceId3, testSecondQuad); assertNull(testable.add(testResourceId3, testSecondResource.dataset()).toCompletableFuture().join(), "Couldn't add to an immutable resource!"); final Resource retrieved = testable.get(testResourceId3).toCompletableFuture().join(); assertEquals(testResourceId3, retrieved.getIdentifier(), "Resource was retrieved with wrong ID!"); final Dataset quads = retrieved.dataset(); assertTrue(quads.contains(testFirstQuad), "Resource was retrieved without its immutable data!"); assertTrue(quads.contains(testSecondQuad), "Resource was retrieved without its mutable data!"); quads.remove(testFirstQuad); quads.remove(testSecondQuad); assertEquals(0, quads.size(), "Resource was retrieved with too much data!"); }
@Test public void getGraph2() throws Exception { // graph2 should be present, even if is named by a BlankNode // We'll look up the potentially mapped graph2 blanknode final BlankNodeOrIRI graph2Name = (BlankNodeOrIRI) dataset.stream(Optional.empty(), bob, isPrimaryTopicOf, null) .map(Quad::getObject).findAny().get(); try (final Graph g2 = dataset.getGraph(graph2Name).get()) { assertEquals(4, g2.size()); final Triple bobNameTriple = bobNameQuad.asTriple(); assertTrue(g2.contains(bobNameTriple)); assertTrue(g2.contains(bob, member, bnode1)); assertTrue(g2.contains(bob, member, bnode2)); assertFalse(g2.contains(bnode1, name, secretClubName)); assertTrue(g2.contains(bnode2, name, companyName)); } }
@Test public void containsLanguageTagsCaseInsensitive() { // COMMONSRDF-51: Ensure we can add/contains/remove with any casing // of literal language tag final Literal lower = factory.createLiteral("Hello there", "en-gb"); final Literal upper = factory.createLiteral("Hello there", "EN-GB"); final Literal mixed = factory.createLiteral("Hello there", "en-GB"); final IRI example1 = factory.createIRI("http://example.com/s1"); final IRI greeting = factory.createIRI("http://example.com/greeting"); dataset.add(null, example1, greeting, upper); // any kind of Triple should match assertTrue(dataset.contains(factory.createQuad(null, example1, greeting, upper))); assertTrue(dataset.contains(factory.createQuad(null, example1, greeting, lower))); assertTrue(dataset.contains(factory.createQuad(null, example1, greeting, mixed))); // or as patterns assertTrue(dataset.contains(null, null, null, upper)); assertTrue(dataset.contains(null, null, null, lower)); assertTrue(dataset.contains(null, null, null, mixed)); }
private Stream<Executable> checkEventProperties(final Dataset dataset) { return Stream.of( () -> assertTrue(dataset.contains(null, null, type, PROV.Activity), "missing prov:Activity triple!"), () -> assertTrue(dataset.contains(null, subject, PROV.wasGeneratedBy, null), "missing prov:wasGeneratedBy triple!"), () -> assertTrue(dataset.contains(null, null, PROV.wasAssociatedWith, Trellis.AnonymousAgent), "missing prov:wasAssociatedWith triple!"), () -> assertTrue(dataset.contains(null, null, PROV.actedOnBehalfOf, Trellis.AdministratorAgent), "missing prov:actedOnBehalfOf triple!"), () -> assertTrue(dataset.contains(null, null, PROV.atTime, rdf.createLiteral(created.toString(), XSD.dateTime)), "missing prov:atTime triple!"), () -> assertEquals(6L, dataset.size(), "Incorrect dataset size!")); } }
/** * Ensure {@link Dataset#getGraphNames()} contains our two graphs. * * @throws Exception * If test fails */ @Test public void getGraphNames() throws Exception { final Set<BlankNodeOrIRI> names = dataset.getGraphNames().collect(Collectors.toSet()); assertTrue("Can't find graph name " + graph1, names.contains(graph1)); assertTrue("Found no quads in graph1", dataset.contains(Optional.of(graph1), null, null, null)); final Optional<BlankNodeOrIRI> graphName2 = dataset.getGraphNames().filter(BlankNode.class::isInstance).findAny(); assertTrue("Could not find graph2-like BlankNode", graphName2.isPresent()); assertTrue("Found no quads in graph2", dataset.contains(graphName2, null, null, null)); // Some implementations like Virtuoso might have additional internal graphs, // so we can't assume this: //assertEquals(2, names.size()); }
@Test public void size() throws Exception { assertEquals(10, dataset.size()); }