Example usage for org.apache.commons.rdf.api Graph size

List of usage examples for org.apache.commons.rdf.api Graph size

Introduction

In this page you can find the example usage for org.apache.commons.rdf.api Graph size.

Prototype

long size();

Source Link

Document

Number of triples contained by the graph.

Usage

From source file:example.UserGuideTest.java

@Test
public void graph() throws Exception {
    IRI nameIri = factory.createIRI("http://example.com/name");
    BlankNode aliceBlankNode = factory.createBlankNode();
    Literal aliceLiteral = factory.createLiteral("Alice");
    Triple triple = factory.createTriple(aliceBlankNode, nameIri, aliceLiteral);

    Graph graph = factory.createGraph();

    graph.add(triple);/*from   w  ww  .ja  va  2 s. c o m*/

    IRI bob = factory.createIRI("http://example.com/bob");
    Literal bobName = factory.createLiteral("Bob");
    graph.add(bob, nameIri, bobName);

    System.out.println(graph.contains(triple));

    System.out.println(graph.contains(null, nameIri, bobName));

    System.out.println(graph.size());

    for (Triple t : graph.iterate()) {
        System.out.println(t.getObject());
    }

    for (Triple t : graph.iterate(null, null, bobName)) {
        System.out.println(t.getPredicate());
    }

    Stream<RDFTerm> subjects = graph.getTriples().map(t -> t.getObject());
    String s = subjects.map(RDFTerm::ntriplesString).collect(Collectors.joining(" "));
    System.out.println(s);

    Stream<? extends Triple> namedB = graph.getTriples(null, nameIri, null)
            .filter(t -> t.getObject().ntriplesString().contains("B"));
    System.out.println(namedB.map(t -> t.getSubject()).findAny().get());

    graph.remove(triple);
    System.out.println(graph.contains(triple));

    graph.remove(null, nameIri, null);

    graph.clear();
    System.out.println(graph.contains(null, null, null));

}

From source file:org.trellisldp.api.RDFUtilsTest.java

@Test
public void testCollectGraph() {
    final Graph graph = generate(() -> rdf.createTriple(getIRI(), getIRI(), getIRI())).parallel().limit(size)
            .collect(toGraph());//  w  ww  .  j  av  a  2 s  . c  o  m

    assertTrue(size >= graph.size());
}

From source file:org.trellisldp.api.TrellisUtilsTest.java

@Test
public void testCollectGraph() {
    final Graph graph = generate(() -> rdf.createTriple(getIRI(), getIRI(), getIRI())).parallel().limit(size)
            .collect(toGraph());// w  ww.ja  va2s .c o  m

    assertTrue(size >= graph.size(), "Generated graph has too many triples!");
}

From source file:org.trellisldp.io.JenaIOServiceTest.java

@Test
public void testUpdateError() {
    final Graph graph = rdf.createGraph();
    getTriples().forEach(graph::add);/*from   w  w w .j a  va 2 s .co  m*/
    assertEquals(3L, graph.size(), "Incorrect graph size!");
    assertThrows(RuntimeTrellisException.class,
            () -> service.update(graph, "blah blah blah blah blah", SPARQL_UPDATE, null),
            "no exception on bad update!");
}

From source file:org.trellisldp.io.JenaIOServiceTest.java

@Test
public void testUpdate() {
    final Graph graph = rdf.createGraph();
    getTriples().forEach(graph::add);// w  w  w . j  a  v  a  2  s .  c  o  m
    assertEquals(3L, graph.size(), "Incorrect graph size!");
    service.update(graph, "DELETE WHERE { ?s <http://purl.org/dc/terms/title> ?o }", SPARQL_UPDATE,
            "test:info");
    assertEquals(2L, graph.size(), "Incorrect graph size, post update!");
    service.update(graph, "INSERT { " + "<> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}",
            SPARQL_UPDATE, "trellis:data/resource");
    assertEquals(3L, graph.size(), "Incorrect graph size, after adding triple!");
    service.update(graph,
            "DELETE WHERE { ?s ?p ?o };"
                    + "INSERT { <> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}",
            SPARQL_UPDATE, "trellis:data/");
    assertEquals(1L, graph.size(), "Incorrect graph size after removing triples!");
    assertEquals("<trellis:data/>",
            graph.stream().findFirst().map(Triple::getSubject).map(RDFTerm::ntriplesString).get(),
            "Incorrect graph subject from updates!");
}

From source file:org.trellisldp.rosid.file.RDFPatchTest.java

@Test
public void testStream1() throws Exception {
    final File file = new File(getClass().getResource("/journal1.txt").toURI());
    final Instant time = parse("2017-02-11T02:51:35Z");
    final Graph graph = rdf.createGraph();
    RDFPatch.asStream(rdf, file, identifier, time).map(Quad::asTriple).forEach(graph::add);
    assertEquals(2L, graph.size());
    assertTrue(/*from w ww . ja  v  a  2 s.c om*/
            graph.contains(identifier, rdf.createIRI("http://www.w3.org/2004/02/skos/core#prefLabel"), null));
}

From source file:org.trellisldp.rosid.file.RDFPatchTest.java

@Test
public void testStream2() throws Exception {
    final File file = new File(getClass().getResource("/journal1.txt").toURI());
    final Instant time = parse("2017-02-09T02:51:35Z");
    final Graph graph = rdf.createGraph();
    RDFPatch.asStream(rdf, file, identifier, time).map(Quad::asTriple).forEach(graph::add);
    assertEquals(3L, graph.size());
    assertTrue(/*from  www  .j  a  v  a 2s. com*/
            graph.contains(identifier, rdf.createIRI("http://www.w3.org/2004/02/skos/core#prefLabel"), null));
    assertTrue(graph.contains(identifier, DC.isPartOf, null));
}

From source file:org.trellisldp.rosid.file.RDFPatchTest.java

@Test
public void testStream3() throws Exception {
    final File file = new File(getClass().getResource("/journal1.txt").toURI());
    final Instant time = parse("2017-01-30T02:51:35Z");
    final Graph graph = rdf.createGraph();
    RDFPatch.asStream(rdf, file, identifier, time).map(Quad::asTriple).forEach(graph::add);
    assertEquals(7L, graph.size());
    assertFalse(//  w  w  w  .ja  va 2  s.c  om
            graph.contains(identifier, rdf.createIRI("http://www.w3.org/2004/02/skos/core#prefLabel"), null));
    assertTrue(graph.contains(identifier, DC.extent, null));
    assertTrue(graph.contains(identifier, DC.spatial, null));
    assertTrue(graph.contains(identifier, DC.title, null));
    assertTrue(graph.contains(identifier, DC.description, null));
    assertTrue(graph.contains(identifier, DC.subject, null));
    assertEquals(2L, graph.stream(identifier, DC.subject, null).count());
}

From source file:org.trellisldp.rosid.file.RDFPatchTest.java

@Test
public void testStream4() throws Exception {
    final File file = new File(getClass().getResource("/journal1.txt").toURI());
    final Instant time = parse("2017-01-15T09:14:00Z");
    final Graph graph = rdf.createGraph();
    RDFPatch.asStream(rdf, file, identifier, time).map(Quad::asTriple).forEach(graph::add);
    assertEquals(5L, graph.size());
    assertFalse(/*w  w  w  .  j  ava2s  .  c  om*/
            graph.contains(identifier, rdf.createIRI("http://www.w3.org/2004/02/skos/core#prefLabel"), null));
    assertFalse(graph.contains(identifier, DC.extent, null));
    assertFalse(graph.contains(identifier, DC.spatial, null));
    assertTrue(graph.contains(identifier, DC.title, null));
    assertTrue(graph.contains(identifier, DC.description, null));
    assertTrue(graph.contains(identifier, DC.subject, null));
    assertEquals(2L, graph.stream(identifier, DC.subject, null).count());
}

From source file:org.trellisldp.test.AuditTests.java

/**
 * Check the absense of audit triples.// w  w  w  . j a v a 2 s .  c o  m
 */
@Test
@DisplayName("Check the absense of audit triples.")
default void testNoAuditTriples() {
    try (final Response res = target(getResourceLocation()).request().get()) {
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        assertEquals(2L, g.size(), "Check that the graph has 2 triples");
    }
}