You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commonsrdf.apache.org by st...@apache.org on 2016/11/21 14:05:40 UTC
incubator-commonsrdf git commit: updated example from
introduction.html
Repository: incubator-commonsrdf
Updated Branches:
refs/heads/master c6c4cbfcf -> 1f19fd831
updated example from introduction.html
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/1f19fd83
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/1f19fd83
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/1f19fd83
Branch: refs/heads/master
Commit: 1f19fd831f13e5e1a443719b2a9a0e38ba8f0e99
Parents: c6c4cbf
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Nov 21 14:05:28 2016 +0000
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Nov 21 14:05:28 2016 +0000
----------------------------------------------------------------------
examples/src/example/IntroToRDF.java | 176 +++++++++++++++++++++---------
1 file changed, 122 insertions(+), 54 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/1f19fd83/examples/src/example/IntroToRDF.java
----------------------------------------------------------------------
diff --git a/examples/src/example/IntroToRDF.java b/examples/src/example/IntroToRDF.java
index 0aa97f0..58aea85 100644
--- a/examples/src/example/IntroToRDF.java
+++ b/examples/src/example/IntroToRDF.java
@@ -20,60 +20,128 @@ package example;
import org.apache.commons.rdf.api.*;
import org.apache.commons.rdf.simple.SimpleRDF;
+/** See http://commonsrdf.incubator.apache.org/introduction.html
+ */
public class IntroToRDF {
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();
- 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, " + aliceFriend);
- }
- }
-
- }
+ 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();
+ 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());
+ }
+ }
+
+ }
}