Example usage for org.apache.commons.rdf.api IRI ntriplesString

List of usage examples for org.apache.commons.rdf.api IRI ntriplesString

Introduction

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

Prototype

String ntriplesString();

Source Link

Document

Return the term serialised as specified by the RDF-1.1 N-Triples Canonical form.

Usage

From source file:example.IntroToRDF.java

public static void main(String[] args) {
    RDF rdf = new SimpleRDF();

    IRI alice = rdf.createIRI("Alice");
    System.out.println(alice.ntriplesString());

    IRI knows = rdf.createIRI("knows");
    IRI bob = rdf.createIRI("Bob");

    Triple aliceKnowsBob = rdf.createTriple(alice, knows, bob);
    System.out.println(aliceKnowsBob.getSubject().ntriplesString());

    System.out.println(aliceKnowsBob);

    Graph graph = rdf.createGraph();// w ww  . j  a v a  2s .  c  o  m
    graph.add(aliceKnowsBob);

    IRI charlie = rdf.createIRI("Charlie");

    IRI plays = rdf.createIRI("plays");

    IRI football = rdf.createIRI("Football");
    IRI tennis = rdf.createIRI("Tennis");

    graph.add(alice, knows, charlie);
    graph.add(alice, plays, tennis);
    graph.add(bob, knows, charlie);
    graph.add(bob, plays, football);
    graph.add(charlie, plays, tennis);

    System.out.println("Who plays Tennis?");
    for (Triple triple : graph.iterate(null, plays, tennis)) {
        System.out.println(triple.getSubject());
        System.out.println(plays.equals(triple.getPredicate()));
        System.out.println(tennis.equals(triple.getObject()));
    }

    System.out.println("Who does Alice know?");
    for (Triple triple : graph.iterate(alice, knows, null)) {
        System.out.println(triple.getObject());
    }

    System.out.println("Does Alice anyone that plays Football?");
    for (Triple triple : graph.iterate(alice, knows, null)) {
        RDFTerm aliceFriend = triple.getObject();
        if (!(aliceFriend instanceof BlankNodeOrIRI)) {
            continue;
        }
        if (graph.contains((BlankNodeOrIRI) aliceFriend, plays, football)) {
            System.out.println("Yes, it is " + aliceFriend);
        }
    }

    Literal aliceName = rdf.createLiteral("Alice W. Land");
    IRI name = rdf.createIRI("name");
    graph.add(alice, name, aliceName);

    Optional<? extends Triple> nameTriple = graph.stream(alice, name, null).findAny();
    if (nameTriple.isPresent()) {
        System.out.println(nameTriple.get());
    }

    graph.stream(alice, name, null).findAny().map(Triple::getObject).filter(obj -> obj instanceof Literal)
            .map(literalName -> ((Literal) literalName).getLexicalForm()).ifPresent(System.out::println);

    IRI playerRating = rdf.createIRI("playerRating");
    Literal aliceRating = rdf.createLiteral("13.37", Types.XSD_FLOAT);
    graph.add(alice, playerRating, aliceRating);

    Literal footballInEnglish = rdf.createLiteral("football", "en");
    Literal footballInNorwegian = rdf.createLiteral("fotball", "no");
    graph.add(football, name, footballInEnglish);
    graph.add(football, name, footballInNorwegian);

    Literal footballInAmericanEnglish = rdf.createLiteral("soccer", "en-US");
    graph.add(football, name, footballInAmericanEnglish);

    BlankNode someone = rdf.createBlankNode();
    graph.add(charlie, knows, someone);
    graph.add(someone, plays, football);

    BlankNode someoneElse = rdf.createBlankNode();
    graph.add(charlie, knows, someoneElse);

    for (Triple heKnows : graph.iterate(charlie, knows, null)) {
        if (!(heKnows.getObject() instanceof BlankNodeOrIRI)) {
            continue;
        }
        BlankNodeOrIRI who = (BlankNodeOrIRI) heKnows.getObject();
        System.out.println("Charlie knows " + who);
        for (Triple whoPlays : graph.iterate(who, plays, null)) {
            System.out.println("  who plays " + whoPlays.getObject());
        }
    }

    // Delete previous BlankNode statements
    graph.remove(null, null, someone);
    graph.remove(someone, null, null);

    // no Java variable for the new BlankNode instance
    graph.add(charlie, knows, rdf.createBlankNode("someone"));
    // at any point later (with the same RDF instance)
    graph.add(rdf.createBlankNode("someone"), plays, football);

    for (Triple heKnows : graph.iterate(charlie, knows, null)) {
        if (!(heKnows.getObject() instanceof BlankNodeOrIRI)) {
            continue;
        }
        BlankNodeOrIRI who = (BlankNodeOrIRI) heKnows.getObject();
        System.out.println("Charlie knows " + who);
        for (Triple whoPlays : graph.iterate(who, plays, null)) {
            System.out.println("  who plays " + whoPlays.getObject());
        }
    }

}

From source file:example.UserGuideTest.java

@Test
public void literal() throws Exception {
    Literal literal = factory.createLiteral("Hello world!");
    System.out.println(literal.ntriplesString());

    String lexical = literal.getLexicalForm();
    System.out.println(lexical);// ww  w . j a  v a 2 s  . co m

    IRI datatype = literal.getDatatype();
    System.out.println(datatype.ntriplesString());

    IRI xsdDouble = factory.createIRI("http://www.w3.org/2001/XMLSchema#double");
    Literal literalDouble = factory.createLiteral("13.37", xsdDouble);
    System.out.println(literalDouble.ntriplesString());

    Literal literalDouble2 = factory.createLiteral("13.37", Types.XSD_DOUBLE);

    System.out.println(literal.getDatatype().equals(Types.XSD_STRING));

    Literal inSpanish = factory.createLiteral("Hola, Mundo!", "es");
    System.out.println(inSpanish.ntriplesString());
    System.out.println(inSpanish.getLexicalForm());
    System.out.println(inSpanish.getDatatype().ntriplesString());

    Optional<String> tag = inSpanish.getLanguageTag();
    if (tag.isPresent()) {
        System.out.println(tag.get());
    }

    System.out.println(literal.getLanguageTag().isPresent());
    System.out.println(literalDouble.getLanguageTag().isPresent());
}

From source file:example.UserGuideTest.java

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

    System.out.println(aliceBlankNode.ntriplesString());
    System.out.println(nameIri.ntriplesString());
    System.out.println(aliceLiteral.ntriplesString());

}