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

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

Introduction

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

Prototype

Stream<? extends Triple> getTriples();

Source Link

Document

Get all triples contained by the graph.

The iteration does not contain any duplicate triples, as determined by the Triple#equals(Object) method for each Triple .

Usage

From source file:example.UserGuideTest.java

public static void writeGraph(Graph graph, Path graphFile) throws Exception {
    Stream<CharSequence> stream = graph.getTriples().map(UserGuideTest::tripleAsString);
    Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
}

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 . j  a v a  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.apache.jena.commonsrdf.examples.Ex_JenaGraphToCommonsRDFGraph.java

public static void main(String... a) {
    org.apache.jena.graph.Graph jGraph = GraphFactory.createGraphMem();
    RDFDataMgr.read(jGraph, "D.ttl");

    // "graph" is a CommonsRDF graph 
    Graph graph = JenaCommonsRDF.fromJena(jGraph);

    // Add to CommonsRDF Graph
    RDFTermFactory rft = new RDFTermFactoryJena();
    graph.add(rft.createIRI("http://example/s2"), rft.createIRI("http://example/p2"), rft.createLiteral("foo"));
    System.out.println("==== Write CommonsRDF graph\n");
    graph.getTriples().forEach(System.out::println);

    System.out.println("\n==== Write Jena graph directly\n");
    // And its in the Jena graph
    RDFDataMgr.write(System.out, jGraph, Lang.TTL);
}

From source file:org.apache.jena.commonsrdf.examples.Ex_ParseIntoCommonsRDFGraph.java

public static void parse(RDFTermFactory rft) {
    Graph graph = rft.createGraph();
    StreamRDF dest = JenaCommonsRDF.streamJenaToCommonsRDF(rft, graph);
    RDFDataMgr.parse(dest, "D.ttl");
    graph.getTriples().forEach(System.out::println);
}

From source file:org.apache.jena.commonsrdf.JenaCommonsRDF.java

/** Convert a CommonsRDF Graph to a Jena Graph.
 * If the Graph was from Jena originally, return that original object else
 * create a copy using Jena objects. /* w  w w. j  a  va 2 s  .  c om*/
 */
public static org.apache.jena.graph.Graph toJena(Graph graph) {
    if (graph instanceof JenaGraph)
        return ((JenaGraph) graph).getGraph();
    org.apache.jena.graph.Graph g = GraphFactory.createGraphMem();
    graph.getTriples().forEach(t -> g.add(toJena(t)));
    return g;
}