/** * Add a triple to the graph. * * @param triple an RDF Triple */ public void add(final Triple triple) { graph.add(triple); }
return target(q -> { if (!q.getGraphName().isPresent()) { graph.add(q.asTriple());
@Test public void add() throws Exception { graph.add(new DummyTriple()); }
private Graph createGraph2() { final RDF factory2 = createFactory(); final IRI name = factory2.createIRI("http://xmlns.com/foaf/0.1/name"); final Graph g2 = factory2.createGraph(); final BlankNode b1 = createOwnBlankNode("b1", "bc8d3e45-a08f-421d-85b3-c25b373abf87"); g2.add(b1, name, factory2.createLiteral("Charlie")); final BlankNode b2 = createOwnBlankNode("b2", "2209097a-5078-4b03-801a-6a2d2f50d739"); g2.add(b2, name, factory2.createLiteral("Dave")); final IRI hasChild = factory2.createIRI("http://example.com/hasChild"); // NOTE: Opposite direction of loadGraph1 g2.add(b2, hasChild, b1); return g2; }
/** * Make a new graph with two BlankNodes - each with a different * uniqueReference */ private Graph createGraph1() { final RDF factory1 = createFactory(); final IRI name = factory1.createIRI("http://xmlns.com/foaf/0.1/name"); final Graph g1 = factory1.createGraph(); final BlankNode b1 = createOwnBlankNode("b1", "0240eaaa-d33e-4fc0-a4f1-169d6ced3680"); g1.add(b1, name, factory1.createLiteral("Alice")); final BlankNode b2 = createOwnBlankNode("b2", "9de7db45-0ce7-4b0f-a1ce-c9680ffcfd9f"); g1.add(b2, name, factory1.createLiteral("Bob")); final IRI hasChild = factory1.createIRI("http://example.com/hasChild"); g1.add(b1, hasChild, b2); return g1; }
@BeforeEach public void setUp() { final IRI other = rdf.createIRI("trellis:data/other"); graph.clear(); graph.add(rdf.createTriple(subject, ACL.agent, rdf.createIRI("info:agent/foo"))); graph.add(rdf.createTriple(subject, ACL.agent, rdf.createIRI("info:agent/bar"))); graph.add(rdf.createTriple(other, ACL.agent, rdf.createIRI("info:agent/baz"))); graph.add(rdf.createTriple(subject, ACL.agentClass, rdf.createIRI("info:agent/SomeClass"))); graph.add(rdf.createTriple(other, ACL.agentClass, rdf.createIRI("info:agent/SomeOtherClass"))); graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group1"))); graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group2"))); graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group3"))); graph.add(rdf.createTriple(subject, ACL.agentGroup, rdf.createIRI("info:group/group4"))); graph.add(rdf.createTriple(subject, ACL.mode, ACL.Read)); graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource2"))); graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource3"))); graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource4"))); graph.add(rdf.createTriple(subject, ACL.accessTo, rdf.createIRI("trellis:data/resource4"))); graph.add(rdf.createTriple(other, ACL.accessTo, rdf.createIRI("trellis:data/resource5"))); graph.add(rdf.createTriple(subject, ACL.accessToClass, PROV.Activity)); graph.add(rdf.createTriple(other, ACL.accessToClass, PROV.Entity)); graph.add(rdf.createTriple(subject, ACL.default_, rdf.createIRI("trellis:data/container"))); }
/** * Add all triples from the source to the target. * <p> * The triples may be copied in any order. No special conversion or * adaptation of {@link BlankNode}s are performed. * * @param source * Source Graph to copy triples from * @param target * Target Graph where triples will be added */ private void addAllTriples(final Graph source, final Graph target) { // unordered() as we don't need to preserve triple order // sequential() as we don't (currently) require target Graph to be // thread-safe try (Stream<? extends Triple> stream = source.stream()) { stream.unordered().sequential().forEach(t -> target.add(t)); } }
@Test public void addSPO() throws Exception { graph.add(new DummyIRI(1), new DummyIRI(2), new DummyIRI(3)); }
graph.add(alice, name, aliceName); graph.add(alice, knows, bob); graph.add(alice, member, bnode1); graph.add(bobNameTriple); graph.add(factory.createTriple(bob, member, bnode1)); graph.add(factory.createTriple(bob, member, bnode2)); if (secretClubName != null) { graph.add(bnode1, name, secretClubName); graph.add(bnode2, name, companyName);
graph.add(alice, name, aliceName); graph.add(alice, knows, bob); graph.add(alice, member, org1); graph.add(bobNameTriple); graph.add(factory.createTriple(bob, member, org1)); graph.add(factory.createTriple(bob, member, org2)); if (secretClubName != null) { graph.add(org1, name, secretClubName); graph.add(org2, name, companyName);
@Test public void remove() throws Exception { long fullSize = graph.size(); graph.remove(alice, knows, bob); long shrunkSize = graph.size(); assertEquals(1, fullSize - shrunkSize); graph.remove(alice, knows, bob); assertEquals(shrunkSize, graph.size()); // unchanged graph.add(alice, knows, bob); graph.add(alice, knows, bob); graph.add(alice, knows, bob); // Undetermined size at this point -- but at least it // should be bigger assertTrue(graph.size() > shrunkSize); // and after a single remove they should all be gone graph.remove(alice, knows, bob); assertEquals(shrunkSize, graph.size()); Optional<? extends Triple> anyTriple = graph.getTriples().findAny(); Assume.assumeTrue(anyTriple.isPresent()); Triple otherTriple = anyTriple.get(); graph.remove(otherTriple); assertEquals(shrunkSize - 1, graph.size()); graph.remove(otherTriple); assertEquals(shrunkSize - 1, graph.size()); // no change graph.add(otherTriple); assertEquals(shrunkSize, graph.size()); }
@Test public void removeLanguageTagsCaseInsensitive() throws Exception { // COMMONSRDF-51: Ensure we can remove with any casing // of literal language tag final Literal lower = factory.createLiteral("Hello", "en-gb"); final Literal upper = factory.createLiteral("Hello", "EN-GB"); final Literal mixed = factory.createLiteral("Hello", "en-GB"); final IRI example1 = factory.createIRI("http://example.com/s1"); final IRI greeting = factory.createIRI("http://example.com/greeting"); try (final Graph graph = factory.createGraph()) { graph.add(example1, greeting, upper); // Remove should also honour any case graph.remove(example1, null, mixed); assertFalse(graph.contains(null, greeting, null)); graph.add(example1, greeting, lower); graph.remove(example1, null, upper); // Check with Triple graph.add(factory.createTriple(example1, greeting, mixed)); graph.remove(factory.createTriple(example1, greeting, upper)); assertFalse(graph.contains(null, greeting, null)); } }
@Test public void streamLanguageTagsCaseInsensitive() throws Exception { // COMMONSRDF-51: Ensure we can add/contains/remove with any casing // of literal language tag final Literal lower = factory.createLiteral("Hello", "en-gb"); final Literal upper = factory.createLiteral("Hello", "EN-GB"); final Literal mixed = factory.createLiteral("Hello", "en-GB"); final IRI example1 = factory.createIRI("http://example.com/s1"); final IRI greeting = factory.createIRI("http://example.com/greeting"); try (final Graph graph = factory.createGraph()) { graph.add(example1, greeting, upper); // or as patterns assertTrue(closableFindAny(graph.stream(null, null, upper)).isPresent()); assertTrue(closableFindAny(graph.stream(null, null, lower)).isPresent()); assertTrue(closableFindAny(graph.stream(null, null, mixed)).isPresent()); // Check the triples returned equal a new triple final Triple t = closableFindAny(graph.stream(null, null, lower)).get(); assertEquals(t, factory.createTriple(example1, greeting, mixed)); } }
@Test public void testCreateGraph() throws Exception { try (final Graph graph = factory.createGraph(); final Graph graph2 = factory.createGraph()) { assertEquals("Graph was not empty", 0, graph.size()); graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode()); assertNotSame(graph, graph2); assertEquals("Graph was empty after adding", 1, graph.size()); assertEquals("New graph was not empty", 0, graph2.size()); } }
@Test public void testCreateGraph() { Graph graph; try { graph = factory.createGraph(); } catch (UnsupportedOperationException ex) { Assume.assumeNoException(ex); return; } assertEquals("Graph was not empty", 0, graph.size()); graph.add(factory.createBlankNode(), factory.createIRI("http://example.com/"), factory.createBlankNode()); Graph graph2 = factory.createGraph(); assertNotSame(graph, graph2); assertEquals("Graph was empty after adding", 1, graph.size()); assertEquals("New graph was not empty", 0, graph2.size()); }
final IRI exampleROOT = factory.createIRI("http://example.com/s1"); final IRI greeting = factory.createIRI("http://example.com/greeting"); g.add(exampleROOT, greeting, mixedROOT); g.add(exampleTR, greeting, upper); assertTrue(g.contains(factory.createTriple(exampleTR, greeting, upper))); assertTrue(g.contains(factory.createTriple(exampleTR, greeting, upperROOT)));
@Test public void containsLanguageTagsCaseInsensitive() throws Exception { // COMMONSRDF-51: Ensure we can add/contains/remove with any casing // of literal language tag final Literal lower = factory.createLiteral("Hello", "en-gb"); final Literal upper = factory.createLiteral("Hello", "EN-GB"); final Literal mixed = factory.createLiteral("Hello", "en-GB"); final IRI example1 = factory.createIRI("http://example.com/s1"); final IRI greeting = factory.createIRI("http://example.com/greeting"); try (final Graph graph = factory.createGraph()) { graph.add(example1, greeting, upper); // any kind of Triple should match assertTrue(graph.contains(factory.createTriple(example1, greeting, upper))); assertTrue(graph.contains(factory.createTriple(example1, greeting, lower))); assertTrue(graph.contains(factory.createTriple(example1, greeting, mixed))); // or as patterns assertTrue(graph.contains(null, null, upper)); assertTrue(graph.contains(null, null, lower)); assertTrue(graph.contains(null, null, mixed)); } }