commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [3/6] incubator-commonsrdf git commit: Source code of examples from User guide
Date Tue, 28 Apr 2015 09:05:07 GMT
Source code of examples from User guide

See
http://mail-archives.apache.org/mod_mbox/commonsrdf-dev/201504.mbox/%3CCAOfJQJ0ifthZMeFna6jMPCcZGdaGaKVwc2djuJMi02Vz1odPvg%40mail.gmail.com%3E

NOT part of the build as it should not be released
to Maven Central.  Also non-Apache groupId, package etc.
as it is just an example.

TODO: Some kind of way to include the <module> in the <profile>
when it is NOT the -Papache-release profile? Would be great
if the <version> were automatically maintained or at least
the test regularly run in development build.


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/d57f8ce1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/d57f8ce1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/d57f8ce1

Branch: refs/heads/master
Commit: d57f8ce11c5a9042f697365ed9921beff50cc3ef
Parents: f60969b
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue Apr 28 09:57:53 2015 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue Apr 28 09:57:53 2015 +0100

----------------------------------------------------------------------
 examples/README.md                      |  36 +++++
 examples/pom.xml                        |  80 ++++++++++
 examples/src/example/UserGuideTest.java | 219 +++++++++++++++++++++++++++
 3 files changed, 335 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/d57f8ce1/examples/README.md
----------------------------------------------------------------------
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 0000000..6d970e4
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,36 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements. See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership. The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+
+# Examples of using Commons RDF
+
+[UserGuideTest.java](src/example/UserGuideTest.java) contains the source code
+of the examples in the 
+[Commons RDF user guide](http://commonsrdf.incubator.apache.org/userguide.html).
+
+## Usage
+
+Update [pom.xml](pom.xml) to the latest versions of `commons-rdf-api`
+and `commons-rdf-simple`.
+
+Ensure you have Java 8 installed and configured as `JAVA_HOME`.
+
+Use [Apache Maven 3](http://maven.apache.org/download.cgi) and run:
+
+    mvn clean install

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/d57f8ce1/examples/pom.xml
----------------------------------------------------------------------
diff --git a/examples/pom.xml b/examples/pom.xml
new file mode 100644
index 0000000..9ed3b90
--- /dev/null
+++ b/examples/pom.xml
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements. See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership. The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <!-- NOTE: Deliberately not using parent pom and separate groupId/artifactId
+    
+    This module should not be built as part of a release.  
+    -->
+    
+    <groupId>com.example</groupId> <!--  Change to your groupId -->
+    <artifactId>commons-rdf-examples</artifactId> <!-- Change to your artifactId
-->
+    <version>0.1-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
+    <build>
+      <!-- simplify folder structure for this example -
+           real projects should use regular
+           src/main/java
+           src/test/java
+      -->
+      <testSourceDirectory>src</testSourceDirectory>
+      <sourceDirectory>src-2</sourceDirectory> <!-- not used -->
+    </build>
+
+
+    <dependencies>
+    	<dependency>
+    		<groupId>org.apache.commons</groupId>
+    		<artifactId>commons-rdf-api</artifactId>
+        <!-- update to latest version -->
+    		<version>0.1-incubating-SNAPSHOT</version>
+    	</dependency>
+    	<dependency>
+    		<groupId>org.apache.commons</groupId>
+    		<artifactId>commons-rdf-simple</artifactId>
+        <!-- update to latest version -->
+    		<version>0.1.0-incubating-SNAPSHOT</version>
+    	</dependency>
+    	<dependency>
+    		<groupId>junit</groupId>
+    		<artifactId>junit</artifactId>
+    		<version>4.12</version>
+    		<scope>test</scope>
+    	</dependency>
+    </dependencies>
+    <repositories>
+      <!-- Only needed if testing -SNAPSHOT release of commons-rdf -->
+    	<repository>
+    		<id>apache.snapshots</id>
+    		<name>Apache Snapshot Repository</name>
+    		<url>http://repository.apache.org/snapshots</url>
+    		<releases>
+    			<enabled>false</enabled>
+    		</releases>
+    	</repository>
+    </repositories>
+    <name>Commons RDF: Examples</name>
+</project>

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/d57f8ce1/examples/src/example/UserGuideTest.java
----------------------------------------------------------------------
diff --git a/examples/src/example/UserGuideTest.java b/examples/src/example/UserGuideTest.java
new file mode 100644
index 0000000..1bf80d8
--- /dev/null
+++ b/examples/src/example/UserGuideTest.java
@@ -0,0 +1,219 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package example;
+
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.commons.rdf.api.BlankNode;
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.Graph;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Literal;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.RDFTermFactory;
+import org.apache.commons.rdf.api.Triple;
+import org.apache.commons.rdf.simple.SimpleRDFTermFactory;
+import org.apache.commons.rdf.simple.Types;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UserGuideTest {
+
+	private RDFTermFactory factory;
+
+	@Before
+	public void factory() {
+		factory = new SimpleRDFTermFactory();
+	}
+
+	@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());
+
+	}
+
+	@Test
+	public void ntriples() throws Exception {
+		IRI iri = factory.createIRI("http://example.com/alice");
+		System.out.println(iri.getIRIString());
+
+		IRI iri2 = factory.createIRI("http://example.com/alice");
+		System.out.println(iri.equals(iri2));
+
+		IRI iri3 = factory.createIRI("http://example.com/alice/./");
+		System.out.println(iri.equals(iri3));
+
+		System.out.println(iri.equals("http://example.com/alice"));
+		System.out.println(iri.equals(factory.createLiteral("http://example.com/alice")));
+
+	}
+
+	@Test
+	public void blanknode() throws Exception {
+		BlankNode bnode = factory.createBlankNode();
+		System.out.println(bnode.equals(bnode));
+		System.out.println(bnode.equals(factory.createBlankNode()));
+
+		BlankNode b1 = factory.createBlankNode("b1");
+
+		System.out.println(b1.ntriplesString());
+
+		System.out.println(b1.equals(factory.createBlankNode("b1")));
+		System.out.println(b1.equals(new SimpleRDFTermFactory().createBlankNode("b1")));
+
+		System.out.println(bnode.internalIdentifier());
+	}
+
+	@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);
+
+		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());
+	}
+
+	@Test
+	public void triple() throws Exception {
+		BlankNodeOrIRI subject = factory.createBlankNode();
+		IRI predicate = factory.createIRI("http://example.com/says");
+		RDFTerm object = factory.createLiteral("Hello");
+		Triple triple = factory.createTriple(subject, predicate, object);
+
+		BlankNodeOrIRI subj = triple.getSubject();
+		System.out.println(subj.ntriplesString());
+
+		IRI pred = triple.getPredicate();
+		System.out.println(pred.getIRIString());
+
+		RDFTerm obj = triple.getObject();
+		System.out.println(obj.ntriplesString());
+
+		if (subj instanceof IRI) {
+		    String s = ((IRI) subj).getIRIString();
+		    System.out.println(s);
+		}
+		// ..
+		if (obj instanceof Literal) {
+		    IRI type = ((Literal) obj).getDatatype();
+		    System.out.println(type);
+		}
+
+		System.out.println(triple.equals(factory.createTriple(subj, pred, obj)));
+	}
+
+
+	@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);
+
+		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));
+
+
+	}
+
+
+    public static String tripleAsString(Triple t) {
+        return t.getSubject().ntriplesString() + " "
+                + t.getPredicate().ntriplesString() + " " +
+                t.getObject().ntriplesString() + " .";
+    }
+
+	public static void writeGraph(Graph graph, Path graphFile) throws Exception {
+        Stream<CharSequence> stream = graph.getTriples().map(UserGuideTest::tripleAsString);
+        Files.write(graphFile, stream::iterator, Charset.forName("UTF-8"));
+	}
+}


Mime
View raw message