Skip to content

Instantly share code, notes, and snippets.

@stain
Last active August 29, 2015 14:20
Show Gist options
  • Save stain/3c310f87c4465541be66 to your computer and use it in GitHub Desktop.
Save stain/3c310f87c4465541be66 to your computer and use it in GitHub Desktop.
package com.example.commonsrdf;
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 ExampleTest {
private RDFTermFactory factory;
private Triple triple;
private IRI nameIri;
@Before
public void factory() {
factory = new SimpleRDFTermFactory();
}
@Test
public void creating() throws Exception {
BlankNode aliceBlankNode = factory.createBlankNode();
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 = 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 {
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(ExampleTest::tripleAsString);
Files.write(graphFile, stream::iterator, Charset.forName("UTF-8"));
}
}
<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>
<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>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rdf-api</artifactId>
<version>0.1-incubating-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rdf-simple</artifactId>
<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>
<repository>
<id>apache.snapshots</id>
<name>Apache Snapshot Repository</name>
<url>http://repository.apache.org/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment