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

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

Introduction

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

Prototype

@SuppressWarnings("unchecked")
default Iterable<Triple> iterate() throws ConcurrentModificationException, IllegalStateException 

Source Link

Document

Get an Iterable for iterating over all triples in 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  w w.ja  va  2 s.com

    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));

}