Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.classpath
.project
.settings
target/
.svn/
*.iml
.idea
*~
pom.xml.versionsBackup
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: java
jdk:
- openjdk7
- oraclejdk7
- oraclejdk8
notifications:
email:
- ansell.peter@gmail.com
- tristan.king@gmail.com
after_success:
- mvn clean test jacoco:report coveralls:report
24 changes: 24 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2012, Deutsche Forschungszentrum für Künstliche Intelligenz GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# jsonld-java-clerezza
Integration of JSONLD-Java with Clerezza
JSONLD-Java Clerezza Integration module
=======================================

[![Build Status](https://travis-ci.org/jsonld-java/jsonld-java-clerezza.svg?branch=master)](https://travis-ci.org/jsonld-java/jsonld-java-clerezza)
[![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)

USAGE
=====

From Maven
----------

<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java-clerezza</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>

(Adjust for most recent <version>, as found in ``pom.xml``).

ClerezzaTripleCallback
------------------

The ClerezzaTripleCallback returns an instance of `org.apache.clerezza.rdf.core.MGraph`

See [ClerezzaTripleCallbackTest.java](./src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java) for example Usage.
80 changes: 80 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<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/maven-v4_0_0.xsd">

<parent>
<artifactId>jsonld-java-parent</artifactId>
<groupId>com.github.jsonld-java</groupId>
<version>0.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jsonld-java-clerezza</artifactId>
<name>JSONLD Java :: Clerezza Integration</name>
<description>JSON-LD Java integration module for Clerezza</description>
<packaging>bundle</packaging>

<developers>
<developer>
<name>Reto Bachmann-Gmür</name>
</developer>
<developer>
<name>Tristan King</name>
</developer>
<developer>
<name>Peter Ansell</name>
</developer>
</developers>

<properties>
<clerezza.version>0.14</clerezza.version>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsonld-java</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jsonld-java</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>rdf.core</artifactId>
<version>${clerezza.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.github.jsonldjava.clerezza;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.clerezza.rdf.core.BNode;
import org.apache.clerezza.rdf.core.Language;
import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.NonLiteral;
import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl;

import com.github.jsonldjava.core.JsonLdTripleCallback;
import com.github.jsonldjava.core.RDFDataset;

public class ClerezzaTripleCallback implements JsonLdTripleCallback {

private MGraph mGraph = new SimpleMGraph();
private Map<String, BNode> bNodeMap = new HashMap<String, BNode>();

public void setMGraph(MGraph mGraph) {
this.mGraph = mGraph;
bNodeMap = new HashMap<String, BNode>();
}

public MGraph getMGraph() {
return mGraph;
}

private void triple(String s, String p, String o, String graph) {
if (s == null || p == null || o == null) {
// TODO: i don't know what to do here!!!!
return;
}

final NonLiteral subject = getNonLiteral(s);
final UriRef predicate = new UriRef(p);
final NonLiteral object = getNonLiteral(o);
mGraph.add(new TripleImpl(subject, predicate, object));
}

private void triple(String s, String p, String value, String datatype, String language,
String graph) {
final NonLiteral subject = getNonLiteral(s);
final UriRef predicate = new UriRef(p);
Resource object;
if (language != null) {
object = new PlainLiteralImpl(value, new Language(language));
} else {
if (datatype != null) {
object = new TypedLiteralImpl(value, new UriRef(datatype));
} else {
object = new PlainLiteralImpl(value);
}
}

mGraph.add(new TripleImpl(subject, predicate, object));
}

private NonLiteral getNonLiteral(String s) {
if (s.startsWith("_:")) {
return getBNode(s);
} else {
return new UriRef(s);
}
}

private BNode getBNode(String s) {
if (bNodeMap.containsKey(s)) {
return bNodeMap.get(s);
} else {
final BNode result = new BNode();
bNodeMap.put(s, result);
return result;
}
}

@Override
public Object call(RDFDataset dataset) {
for (String graphName : dataset.graphNames()) {
final List<RDFDataset.Quad> quads = dataset.getQuads(graphName);
if ("@default".equals(graphName)) {
graphName = null;
}
for (final RDFDataset.Quad quad : quads) {
if (quad.getObject().isLiteral()) {
triple(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad
.getObject().getValue(), quad.getObject().getDatatype(), quad
.getObject().getLanguage(), graphName);
} else {
triple(quad.getSubject().getValue(), quad.getPredicate().getValue(), quad
.getObject().getValue(), graphName);
}
}
}

return getMGraph();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.jsonldjava.clerezza;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;

import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.Triple;
import org.junit.Test;

import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;

public class ClerezzaTripleCallbackTest {

@Test
public void triplesTest() throws IOException, JsonLdError {
final InputStream in = getClass().getClassLoader().getResourceAsStream(
"testfiles/product.jsonld");
final Object input = JsonUtils.fromInputStream(in);

final ClerezzaTripleCallback callback = new ClerezzaTripleCallback();

final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback);

for (final Triple t : graph) {
System.out.println(t);
}
assertEquals("Graph size", 13, graph.size());

}

@Test
public void curiesInContextTest() throws IOException, JsonLdError {
final InputStream in = getClass().getClassLoader().getResourceAsStream(
"testfiles/curies-in-context.jsonld");
final Object input = JsonUtils.fromInputStream(in);

final ClerezzaTripleCallback callback = new ClerezzaTripleCallback();

final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback);

for (final Triple t : graph) {
System.out.println(t);
assertTrue("Predicate got fully expanded", t.getPredicate().getUnicodeString()
.startsWith("http"));
}
assertEquals("Graph size", 3, graph.size());

}
}
5 changes: 5 additions & 0 deletions src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
log4j.rootLogger=INFO, R

log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d] %-5p (%c:%L) %m%n
10 changes: 10 additions & 0 deletions src/test/resources/testfiles/curies-in-context.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"@context": {
"Person": "foaf:Person",
"foaf": "http://xmlns.com/foaf/0.1/",
"name": "foaf:name"
},
"@type": "Person",
"name": "Santa Claus",
"foaf:nick": "S�mi"
}
35 changes: 35 additions & 0 deletions src/test/resources/testfiles/product.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"@context": {
"gr": "http://purl.org/goodrelations/v1#",
"pto": "http://www.productontology.org/id/",
"foaf": "http://xmlns.com/foaf/0.1/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"foaf:page": {
"@type": "@id"
},
"gr:acceptedPaymentMethods": {
"@type": "@id"
},
"gr:hasBusinessFunction": {
"@type": "@id"
},
"gr:hasCurrencyValue": {
"@type": "xsd:float"
}
},
"@id": "http://example.org/cars/for-sale#tesla",
"@type": "gr:Offering",
"gr:name": "Used Tesla Roadster",
"gr:description": "Need to sell fast and furiously",
"gr:hasBusinessFunction": "gr:Sell",
"gr:acceptedPaymentMethods": "gr:Cash",
"gr:hasPriceSpecification": {
"gr:hasCurrencyValue": "85000",
"gr:hasCurrency": "USD"
},
"gr:includes": {
"@type": ["gr:Individual", "pto:Vehicle"],
"gr:name": "Tesla Roadster",
"foaf:page": "http://www.teslamotors.com/roadster"
}
}