Skip to content

Commit 98562e5

Browse files
committed
Merge pull request #1 from jsonld-java/initial-migration
Initial migration from jsonld-java
2 parents f033246 + 96939cd commit 98562e5

File tree

10 files changed

+359
-14
lines changed

10 files changed

+359
-14
lines changed

.gitignore

100644100755
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
*.class
2-
3-
# Mobile Tools for Java (J2ME)
4-
.mtj.tmp/
5-
6-
# Package Files #
7-
*.jar
8-
*.war
9-
*.ear
10-
11-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12-
hs_err_pid*
1+
.classpath
2+
.project
3+
.settings
4+
target/
5+
.svn/
6+
*.iml
7+
.idea
8+
*~
9+
pom.xml.versionsBackup

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: java
2+
jdk:
3+
- openjdk7
4+
- oraclejdk7
5+
- oraclejdk8
6+
notifications:
7+
email:
8+
- ansell.peter@gmail.com
9+
- tristan.king@gmail.com
10+
after_success:
11+
- mvn clean test jacoco:report coveralls:report

LICENCE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright (c) 2012, Deutsche Forschungszentrum für Künstliche Intelligenz GmbH
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright
9+
notice, this list of conditions and the following disclaimer in the
10+
documentation and/or other materials provided with the distribution.
11+
* Neither the name of the <organization> nor the
12+
names of its contributors may be used to endorse or promote products
13+
derived from this software without specific prior written permission.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
19+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
# jsonld-java-clerezza
2-
Integration of JSONLD-Java with Clerezza
1+
JSONLD-Java Clerezza Integration module
2+
=======================================
3+
4+
[![Build Status](https://travis-ci.org/jsonld-java/jsonld-java-clerezza.svg?branch=master)](https://travis-ci.org/jsonld-java/jsonld-java-clerezza)
5+
[![Coverage Status](https://coveralls.io/repos/jsonld-java/jsonld-java-clerezza/badge.svg?branch=master)](https://coveralls.io/r/jsonld-java/jsonld-java-clerezza?branch=master)
6+
7+
USAGE
8+
=====
9+
10+
From Maven
11+
----------
12+
13+
<dependency>
14+
<groupId>com.github.jsonld-java</groupId>
15+
<artifactId>jsonld-java-clerezza</artifactId>
16+
<version>0.7.0-SNAPSHOT</version>
17+
</dependency>
18+
19+
(Adjust for most recent <version>, as found in ``pom.xml``).
20+
21+
ClerezzaTripleCallback
22+
------------------
23+
24+
The ClerezzaTripleCallback returns an instance of `org.apache.clerezza.rdf.core.MGraph`
25+
26+
See [ClerezzaTripleCallbackTest.java](./src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java) for example Usage.

pom.xml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<parent>
5+
<artifactId>jsonld-java-parent</artifactId>
6+
<groupId>com.github.jsonld-java</groupId>
7+
<version>0.7.0-SNAPSHOT</version>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
<artifactId>jsonld-java-clerezza</artifactId>
11+
<name>JSONLD Java :: Clerezza Integration</name>
12+
<description>JSON-LD Java integration module for Clerezza</description>
13+
<packaging>bundle</packaging>
14+
15+
<developers>
16+
<developer>
17+
<name>Reto Bachmann-Gmür</name>
18+
</developer>
19+
<developer>
20+
<name>Tristan King</name>
21+
</developer>
22+
<developer>
23+
<name>Peter Ansell</name>
24+
</developer>
25+
</developers>
26+
27+
<properties>
28+
<clerezza.version>0.14</clerezza.version>
29+
</properties>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>${project.groupId}</groupId>
34+
<artifactId>jsonld-java</artifactId>
35+
<version>${project.version}</version>
36+
<type>jar</type>
37+
<scope>compile</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>${project.groupId}</groupId>
41+
<artifactId>jsonld-java</artifactId>
42+
<version>${project.version}</version>
43+
<type>test-jar</type>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.apache.clerezza</groupId>
48+
<artifactId>rdf.core</artifactId>
49+
<version>${clerezza.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.slf4j</groupId>
58+
<artifactId>slf4j-log4j12</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
</dependencies>
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.codehaus.mojo</groupId>
66+
<artifactId>animal-sniffer-maven-plugin</artifactId>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.felix</groupId>
70+
<artifactId>maven-bundle-plugin</artifactId>
71+
<extensions>true</extensions>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.jacoco</groupId>
75+
<artifactId>jacoco-maven-plugin</artifactId>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
</project>
80+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.github.jsonldjava.clerezza;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.apache.clerezza.rdf.core.BNode;
8+
import org.apache.clerezza.rdf.core.Language;
9+
import org.apache.clerezza.rdf.core.MGraph;
10+
import org.apache.clerezza.rdf.core.NonLiteral;
11+
import org.apache.clerezza.rdf.core.Resource;
12+
import org.apache.clerezza.rdf.core.UriRef;
13+
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
14+
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
15+
import org.apache.clerezza.rdf.core.impl.TripleImpl;
16+
import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;
17+
18+
import com.github.jsonldjava.core.JsonLdTripleCallback;
19+
import com.github.jsonldjava.core.RDFDataset;
20+
21+
public class ClerezzaTripleCallback implements JsonLdTripleCallback {
22+
23+
private MGraph mGraph = new SimpleMGraph();
24+
private Map<String, BNode> bNodeMap = new HashMap<String, BNode>();
25+
26+
public void setMGraph(MGraph mGraph) {
27+
this.mGraph = mGraph;
28+
bNodeMap = new HashMap<String, BNode>();
29+
}
30+
31+
public MGraph getMGraph() {
32+
return mGraph;
33+
}
34+
35+
private void triple(String s, String p, String o, String graph) {
36+
if (s == null || p == null || o == null) {
37+
// TODO: i don't know what to do here!!!!
38+
return;
39+
}
40+
41+
final NonLiteral subject = getNonLiteral(s);
42+
final UriRef predicate = new UriRef(p);
43+
final NonLiteral object = getNonLiteral(o);
44+
mGraph.add(new TripleImpl(subject, predicate, object));
45+
}
46+
47+
private void triple(String s, String p, String value, String datatype, String language,
48+
String graph) {
49+
final NonLiteral subject = getNonLiteral(s);
50+
final UriRef predicate = new UriRef(p);
51+
Resource object;
52+
if (language != null) {
53+
object = new PlainLiteralImpl(value, new Language(language));
54+
} else {
55+
if (datatype != null) {
56+
object = new TypedLiteralImpl(value, new UriRef(datatype));
57+
} else {
58+
object = new PlainLiteralImpl(value);
59+
}
60+
}
61+
62+
mGraph.add(new TripleImpl(subject, predicate, object));
63+
}
64+
65+
private NonLiteral getNonLiteral(String s) {
66+
if (s.startsWith("_:")) {
67+
return getBNode(s);
68+
} else {
69+
return new UriRef(s);
70+
}
71+
}
72+
73+
private BNode getBNode(String s) {
74+
if (bNodeMap.containsKey(s)) {
75+
return bNodeMap.get(s);
76+
} else {
77+
final BNode result = new BNode();
78+
bNodeMap.put(s, result);
79+
return result;
80+
}
81+
}
82+
83+
@Override
84+
public Object call(RDFDataset dataset) {
85+
for (String graphName : dataset.graphNames()) {
86+
final List<RDFDataset.Quad> quads = dataset.getQuads(graphName);
87+
if ("@default".equals(graphName)) {
88+
graphName = null;
89+
}
90+
for (final RDFDataset.Quad quad : quads) {
91+
if (quad.getObject().isLiteral()) {
92+
triple(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad
93+
.getObject().getValue(), quad.getObject().getDatatype(), quad
94+
.getObject().getLanguage(), graphName);
95+
} else {
96+
triple(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad
97+
.getObject().getValue(), graphName);
98+
}
99+
}
100+
}
101+
102+
return getMGraph();
103+
}
104+
105+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.github.jsonldjava.clerezza;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
9+
import org.apache.clerezza.rdf.core.MGraph;
10+
import org.apache.clerezza.rdf.core.Triple;
11+
import org.junit.Test;
12+
13+
import com.github.jsonldjava.core.JsonLdError;
14+
import com.github.jsonldjava.core.JsonLdProcessor;
15+
import com.github.jsonldjava.utils.JsonUtils;
16+
17+
public class ClerezzaTripleCallbackTest {
18+
19+
@Test
20+
public void triplesTest() throws IOException, JsonLdError {
21+
final InputStream in = getClass().getClassLoader().getResourceAsStream(
22+
"testfiles/product.jsonld");
23+
final Object input = JsonUtils.fromInputStream(in);
24+
25+
final ClerezzaTripleCallback callback = new ClerezzaTripleCallback();
26+
27+
final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback);
28+
29+
for (final Triple t : graph) {
30+
System.out.println(t);
31+
}
32+
assertEquals("Graph size", 13, graph.size());
33+
34+
}
35+
36+
@Test
37+
public void curiesInContextTest() throws IOException, JsonLdError {
38+
final InputStream in = getClass().getClassLoader().getResourceAsStream(
39+
"testfiles/curies-in-context.jsonld");
40+
final Object input = JsonUtils.fromInputStream(in);
41+
42+
final ClerezzaTripleCallback callback = new ClerezzaTripleCallback();
43+
44+
final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback);
45+
46+
for (final Triple t : graph) {
47+
System.out.println(t);
48+
assertTrue("Predicate got fully expanded", t.getPredicate().getUnicodeString()
49+
.startsWith("http"));
50+
}
51+
assertEquals("Graph size", 3, graph.size());
52+
53+
}
54+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
log4j.rootLogger=INFO, R
2+
3+
log4j.appender.R=org.apache.log4j.ConsoleAppender
4+
log4j.appender.R.layout=org.apache.log4j.PatternLayout
5+
log4j.appender.R.layout.ConversionPattern=[%d] %-5p (%c:%L) %m%n
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"@context": {
3+
"Person": "foaf:Person",
4+
"foaf": "http://xmlns.com/foaf/0.1/",
5+
"name": "foaf:name"
6+
},
7+
"@type": "Person",
8+
"name": "Santa Claus",
9+
"foaf:nick": "Sämi"
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"@context": {
3+
"gr": "http://purl.org/goodrelations/v1#",
4+
"pto": "http://www.productontology.org/id/",
5+
"foaf": "http://xmlns.com/foaf/0.1/",
6+
"xsd": "http://www.w3.org/2001/XMLSchema#",
7+
"foaf:page": {
8+
"@type": "@id"
9+
},
10+
"gr:acceptedPaymentMethods": {
11+
"@type": "@id"
12+
},
13+
"gr:hasBusinessFunction": {
14+
"@type": "@id"
15+
},
16+
"gr:hasCurrencyValue": {
17+
"@type": "xsd:float"
18+
}
19+
},
20+
"@id": "http://example.org/cars/for-sale#tesla",
21+
"@type": "gr:Offering",
22+
"gr:name": "Used Tesla Roadster",
23+
"gr:description": "Need to sell fast and furiously",
24+
"gr:hasBusinessFunction": "gr:Sell",
25+
"gr:acceptedPaymentMethods": "gr:Cash",
26+
"gr:hasPriceSpecification": {
27+
"gr:hasCurrencyValue": "85000",
28+
"gr:hasCurrency": "USD"
29+
},
30+
"gr:includes": {
31+
"@type": ["gr:Individual", "pto:Vehicle"],
32+
"gr:name": "Tesla Roadster",
33+
"foaf:page": "http://www.teslamotors.com/roadster"
34+
}
35+
}

0 commit comments

Comments
 (0)