diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 32858aa..b429e1a --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2ee0939 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..e03ca9b --- /dev/null +++ b/LICENCE @@ -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 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 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. \ No newline at end of file diff --git a/README.md b/README.md index d7903d6..9ddc09c 100644 --- a/README.md +++ b/README.md @@ -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 +---------- + + + com.github.jsonld-java + jsonld-java-clerezza + 0.7.0-SNAPSHOT + + +(Adjust for most recent , 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. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7de9e08 --- /dev/null +++ b/pom.xml @@ -0,0 +1,80 @@ + + + + jsonld-java-parent + com.github.jsonld-java + 0.7.0-SNAPSHOT + + 4.0.0 + jsonld-java-clerezza + JSONLD Java :: Clerezza Integration + JSON-LD Java integration module for Clerezza + bundle + + + + Reto Bachmann-Gmür + + + Tristan King + + + Peter Ansell + + + + + 0.14 + + + + + ${project.groupId} + jsonld-java + ${project.version} + jar + compile + + + ${project.groupId} + jsonld-java + ${project.version} + test-jar + test + + + org.apache.clerezza + rdf.core + ${clerezza.version} + + + junit + junit + test + + + org.slf4j + slf4j-log4j12 + test + + + + + + org.codehaus.mojo + animal-sniffer-maven-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + org.jacoco + jacoco-maven-plugin + + + + + diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java new file mode 100644 index 0000000..cc91d36 --- /dev/null +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java @@ -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 bNodeMap = new HashMap(); + + public void setMGraph(MGraph mGraph) { + this.mGraph = mGraph; + bNodeMap = new HashMap(); + } + + 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 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(); + } + +} diff --git a/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java new file mode 100644 index 0000000..dac25e7 --- /dev/null +++ b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java @@ -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()); + + } +} diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties new file mode 100644 index 0000000..136eba0 --- /dev/null +++ b/src/test/resources/log4j.properties @@ -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 diff --git a/src/test/resources/testfiles/curies-in-context.jsonld b/src/test/resources/testfiles/curies-in-context.jsonld new file mode 100644 index 0000000..0490d79 --- /dev/null +++ b/src/test/resources/testfiles/curies-in-context.jsonld @@ -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" +} \ No newline at end of file diff --git a/src/test/resources/testfiles/product.jsonld b/src/test/resources/testfiles/product.jsonld new file mode 100644 index 0000000..666d2cf --- /dev/null +++ b/src/test/resources/testfiles/product.jsonld @@ -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" + } +} \ No newline at end of file