Skip to content

Commit

Permalink
Merge pull request #13 from AndreaCimminoArriaga/master
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
AndreaCimminoArriaga authored Apr 21, 2021
2 parents f5f671d + a6d0673 commit 2357a6b
Show file tree
Hide file tree
Showing 200 changed files with 10,049 additions and 3,560 deletions.
160 changes: 159 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,159 @@
# Java API for Thing Descriptions of WoT (JDT)
# Java API for Thing Descriptions of WoT (JDTs)
[![Maven Central](https://img.shields.io/badge/Maven%20Central-v0.1.6-orange)](https://search.maven.org/search?q=g:%22es.upm.fi.oeg%22%20AND%20a:%22wot-jtd%22) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub stars](https://img.shields.io/github/stars/Naereen/StrapDown.js.svg?style=social&label=Star&maxAge=2592000)](https://github.com/oeg-upm/wot-jtd/stargazers)

The JDT is an ORM implementation of the current [Thing Description](https://www.w3.org/TR/wot-thing-description/) model standardised by the [W3C Web of Things group](https://www.w3.org/WoT/). The current features are:
* Serialise:
* Serialise any Thing Description as a Java Object, i.e., a JDT
* Serialise a JDT from a JSON-LD framed document
* Serialise a JDT from a set of RDF triples
* Round trip-translation:
* Translate from a JSON-LD framed document into a set of equivalent RDF triples
* Translate a set of RDF triples into its equivalent JSON-LD framed document
* Validation **(coming soon)**
* Validate a JTD using [SHACL shapes](https://www.w3.org/TR/shacl/)
* Validate a JTD using [JSON schema](https://json-schema.org/)
* Validate a JTD according to the [restrictions specified in the standard](https://www.w3.org/TR/wot-thing-description/)

If you have any feedback or feature suggestion, please let us know posting an issue with the label <span style="color:#EFA914">**feedback**</span>

## Table of contents

* Installation
* Model
* Usage:
* Serialisation of JTDs:
* From JSON-LD framed document
* From RDF triples
* Deserialisation of JTDs:
* To JSON-LD framed
* To RDF triples
* JDT validation:
* Using SHACL shapes
* Using JSON schema (**coming soon**)
* Using restrictions in the model (**coming soon**)



## Installation:
Import the JDTs library as a maven dependency, **be sure to specify the latest version**:

```
<dependency>
<groupId>es.upm.fi.oeg</groupId>
<artifactId>wot-jtd</artifactId>
<version>0.1.6</version>
</dependency>
```

Alternatively, the dependency can be installed manually. First, download the latest jar from the [releases section](), and then install the dependency as follows (**be sure to specify the latest version**):
````
mvn install:install-file -Dfile=wot-jtd.jar -DgroupId=es.upm.fi.oeg -DartifactId=wot-jtd -Dversion=0.1.6 -Dpackaging=jar
````

Check our [Maven Central Repository page](https://search.maven.org/artifact/es.upm.fi.oeg/wot-jtd/0.1.6/jar) to discover other installation options like Gradle Groovy or Kotlin, Scala, and others.

## Model

The JDT library implement as Java objects the whole model, and its restrictions, defined in the [Thing Description standard](https://www.w3.org/TR/wot-thing-description/). The overview of the model is the following:

![Thing Description model](https://www.w3.org/TR/wot-thing-description/visualization/td.png)


## Usage

### Serialisation of JTDs:

For the next examples, let's assume the following java variables containing the same Thing description:
````
String strJsonTD = "{ \"@context\": \"https://www.w3.org/2019/wot/td/v1\",\n" +
" \"id\": \"urn:dev:ops:32473-WoTLamp-1234\",\n" +
" \"title\": \"MyLampThing\",\n" +
" \"securityDefinitions\": { \"nosec_sc\": { \"scheme\": \"nosec\" }},\n" +
" \"security\": \"nosec_sc\",\n" +
" \"properties\": {\n" +
" \"status\": {\n" +
" \"type\": \"string\",\n" +
" \"forms\": [{\"href\": \"https://mylamp.example.com/status\"}]\n" +
" }\n" +
" }\n" +
"}";
````

````
Model modelTD = ModelFactory.createDefaultModel();
String strRdfTD = "@prefix dc: <http://purl.org/dc/terms/> .\n" +
"@prefix td: <https://www.w3.org/2019/wot/td#> .\n" +
"@prefix jsonschema: <https://www.w3.org/2019/wot/json-schema#> .\n" +
"@prefix hctl: <https://www.w3.org/2019/wot/hypermedia#> .\n" +
"\n" +
"<urn:dev:ops:32473-WoTLamp-1234>\n" +
" dc:title \"MyLampThing\" ;\n" +
" td:hasPropertyAffordance [\n" +
" a <https://www.w3.org/2019/wot/json-schema#StringSchema> ;\n" +
" jsonschema:propertyName \"status\" ;\n" +
" td:hasForm [ hctl:hasTarget <https://mylamp.example.com/status> ]\n" +
" ] ;\n" +
" td:hasSecurityConfiguration <https://json-ld.org/playground/nosec_sc> ;\n" +
" td:securityDefinitions [ td:scheme \"nosec\" ] .";
##### Read the string variable into the jena model
modelTD.read(new ByteArrayInputStream(strRdfTD.getBytes()), null, "Turtle");
````



The following serialisation operations consists of building a JTD object Thing from either a JSON-LD framed representation or a set of RDF triples.
##### From JSON-LD framed document

````
JsonObject jsonTD = JTD.parseJson(strJsonTD);
Thing thing = Thing.fromJson(jsonTD);
thing = (Thing) JTD.instantiateFromJson(jsonTD, Thing.class); # Alternativelly
````
Notice that using the method `JTD.instantiateFromJson(jsonTD, Thing.class)` any other class from the model can be serialised.

##### From RDF triples
In order to build a JTD object from a set of RDF triples there are two main methods:
##### A) Build a list of JTDs in case that the triples contain more than one Thing resource.
`````
List<Thing> things = fromRDF(modelTD)
`````
##### B) Build a unique JTDs providing the RDF resource URI.
`````
Thing thing = fromRDF(modelTD, "urn:dev:ops:32473-WoTLamp-1234")
`````

### Deserialisation of JTDs:

##### To JSON-LD framed
````
JsonObject jsonTD = thing.toJson()
jsonTD = JTD.toJson(thing) # Alternativelly
````
Notice that using the method `JTD.toJson(thing)` any other class from the model can be deserialised.

##### To RDF triples

````
Model modelTD = JTD.toRDF(thing)
# Alternativelly
JsonObject jsonTD = thing.toJson()
modelTD = JTD.toRDF(jsonTD)
````

Notice that using the method alternative `JTD.toRDF(jsonTD)` there is actually no need to serialise the JSON-LD framed `jsonTD` as a Java object, it can be directly translated into RDF.


### JDT validation

##### Using SHACL shapes
Currently, the Web of Things provides [an official SHACL shape document](https://github.com/w3c/wot-thing-description/blob/main/validation/td-validation.ttl) for validating Thing Descriptions. This shape, or any other, can be used to validate a JTD Thing as follows:

````
String shapesURI = "https://raw.githubusercontent.com/w3c/wot-thing-description/main/validation/td-validation.ttl"
Model shapesGraph = RDFDataMgr.loadModel(shapesURI, Lang.TURTLE);
ValidationReport shapeReport = JTD.validateWithShape(thing, shapesGraph);
````

##### Using JSON schema (*comming soon*)
##### Using restrictions in the model (*comming soon*)
124 changes: 109 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.upm.fi.oeg</groupId>
<artifactId>wot-jtd</artifactId>
<version>0.1.5-SNAPSHOT</version>
<version>0.2.0</version>
<url>https://oeg-upm.github.io/wot-jtd</url>
<name>Java Thing Description API</name>
<description>This API aims at assisting developers for handling WoT Thing Descriptions, providing special support for RDF.</description>
Expand Down Expand Up @@ -48,9 +48,96 @@
</repository>
</distributionManagement>

<build>

</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
<!-- Javadoc and sources attached -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<source>8</source>
<detectJavaApiLink>false</detectJavaApiLink>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG signing -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
Expand All @@ -77,6 +164,8 @@
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>


<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
Expand Down Expand Up @@ -107,18 +196,20 @@
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

<!-- JSON-LD -->
<dependency>
<groupId>com.apicatalog</groupId>
<artifactId>titanium-json-ld</artifactId>
<version>1.0.0</version>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>2.0.0</version>
</dependency>

<!-- Json schema -->
<dependency>
<groupId>com.github.everit-org.json-schema</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>1.12.2</version>
</dependency>

<!-- jena -->
<!-- https://mvnrepository.com/artifact/org.apache.jena/apache-jena-libs -->
<dependency>
Expand All @@ -128,6 +219,8 @@
<type>pom</type>
</dependency>



<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
Expand All @@ -136,6 +229,7 @@
<scope>test</scope>
</dependency>


</dependencies>


Expand Down
17 changes: 17 additions & 0 deletions src/main/java/kehio/annotations/RdfContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kehio.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
public @interface RdfContainer {
String[] ignore() default {};
RdfUrlMap[] prefixes() default {};
RdfUrlMap[] aliases(); // if property is present in alias, overrides the ignore properties
String[] identifiers();
}
19 changes: 19 additions & 0 deletions src/main/java/kehio/annotations/RdfDatatype.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package kehio.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
public @interface RdfDatatype {
String value() default "";
String lang() default "";
String datatype() default "";
boolean sinkLang() default false;
boolean sinkDatatype() default false;
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wot.jtd.annotations;
package kehio.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
Expand All @@ -9,6 +9,8 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
public @interface RdfObjectProperty {
public @interface RdfDatatypeGroup {
String value() default "";
boolean byLang() default false;
boolean byDatatype() default false;
}
Loading

0 comments on commit 2357a6b

Please sign in to comment.