|
1 | | -# Java API for Thing Descriptions of WoT (JDT) |
| 1 | +# Java API for Thing Descriptions of WoT (JDTs) |
| 2 | +[](https://search.maven.org/search?q=g:%22es.upm.fi.oeg%22%20AND%20a:%22wot-jtd%22) [](https://opensource.org/licenses/Apache-2.0) [](https://github.com/oeg-upm/wot-jtd/stargazers) |
| 3 | + |
| 4 | +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: |
| 5 | + * Serialise: |
| 6 | + * Serialise any Thing Description as a Java Object, i.e., a JDT |
| 7 | + * Serialise a JDT from a JSON-LD framed document |
| 8 | + * Serialise a JDT from a set of RDF triples |
| 9 | + * Round trip-translation: |
| 10 | + * Translate from a JSON-LD framed document into a set of equivalent RDF triples |
| 11 | + * Translate a set of RDF triples into its equivalent JSON-LD framed document |
| 12 | + * Validation **(coming soon)** |
| 13 | + * Validate a JTD using [SHACL shapes](https://www.w3.org/TR/shacl/) |
| 14 | + * Validate a JTD using [JSON schema](https://json-schema.org/) |
| 15 | + * Validate a JTD according to the [restrictions specified in the standard](https://www.w3.org/TR/wot-thing-description/) |
| 16 | + |
| 17 | +If you have any feedback or feature suggestion, please let us know posting an issue with the label <span style="color:#EFA914">**feedback**</span> |
| 18 | + |
| 19 | +## Table of contents |
| 20 | + |
| 21 | +* Installation |
| 22 | +* Model |
| 23 | +* Usage: |
| 24 | + * Serialisation of JTDs: |
| 25 | + * From JSON-LD framed document |
| 26 | + * From RDF triples |
| 27 | + * Deserialisation of JTDs: |
| 28 | + * To JSON-LD framed |
| 29 | + * To RDF triples |
| 30 | + * JDT validation: |
| 31 | + * Using SHACL shapes |
| 32 | + * Using JSON schema (**coming soon**) |
| 33 | + * Using restrictions in the model (**coming soon**) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +## Installation: |
| 38 | +Import the JDTs library as a maven dependency, **be sure to specify the latest version**: |
| 39 | + |
| 40 | +``` |
| 41 | +<dependency> |
| 42 | + <groupId>es.upm.fi.oeg</groupId> |
| 43 | + <artifactId>wot-jtd</artifactId> |
| 44 | + <version>0.1.6</version> |
| 45 | +</dependency> |
| 46 | +``` |
| 47 | + |
| 48 | +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**): |
| 49 | +```` |
| 50 | +mvn install:install-file -Dfile=wot-jtd.jar -DgroupId=es.upm.fi.oeg -DartifactId=wot-jtd -Dversion=0.1.6 -Dpackaging=jar |
| 51 | +```` |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +## Model |
| 56 | + |
| 57 | +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: |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +### Serialisation of JTDs: |
| 65 | + |
| 66 | +For the next examples, let's assume the following java variables containing the same Thing description: |
| 67 | +```` |
| 68 | +String strJsonTD = "{ \"@context\": \"https://www.w3.org/2019/wot/td/v1\",\n" + |
| 69 | +" \"id\": \"urn:dev:ops:32473-WoTLamp-1234\",\n" + |
| 70 | +" \"title\": \"MyLampThing\",\n" + |
| 71 | +" \"securityDefinitions\": { \"nosec_sc\": { \"scheme\": \"nosec\" }},\n" + |
| 72 | +" \"security\": \"nosec_sc\",\n" + |
| 73 | +" \"properties\": {\n" + |
| 74 | +" \"status\": {\n" + |
| 75 | +" \"type\": \"string\",\n" + |
| 76 | +" \"forms\": [{\"href\": \"https://mylamp.example.com/status\"}]\n" + |
| 77 | +" }\n" + |
| 78 | +" }\n" + |
| 79 | +"}"; |
| 80 | +```` |
| 81 | + |
| 82 | +```` |
| 83 | +Model modelTD = ModelFactory.createDefaultModel(); |
| 84 | +String strRdfTD = "@prefix dc: <http://purl.org/dc/terms/> .\n" + |
| 85 | +"@prefix td: <https://www.w3.org/2019/wot/td#> .\n" + |
| 86 | +"@prefix jsonschema: <https://www.w3.org/2019/wot/json-schema#> .\n" + |
| 87 | +"@prefix hctl: <https://www.w3.org/2019/wot/hypermedia#> .\n" + |
| 88 | +"\n" + |
| 89 | +"<urn:dev:ops:32473-WoTLamp-1234>\n" + |
| 90 | +" dc:title \"MyLampThing\" ;\n" + |
| 91 | +" td:hasPropertyAffordance [\n" + |
| 92 | +" a <https://www.w3.org/2019/wot/json-schema#StringSchema> ;\n" + |
| 93 | +" jsonschema:propertyName \"status\" ;\n" + |
| 94 | +" td:hasForm [ hctl:hasTarget <https://mylamp.example.com/status> ]\n" + |
| 95 | +" ] ;\n" + |
| 96 | +" td:hasSecurityConfiguration <https://json-ld.org/playground/nosec_sc> ;\n" + |
| 97 | +" td:securityDefinitions [ td:scheme \"nosec\" ] ."; |
| 98 | +
|
| 99 | +##### Read the string variable into the jena model |
| 100 | +modelTD.read(new ByteArrayInputStream(strRdfTD.getBytes()), null, "Turtle"); |
| 101 | +```` |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +The following serialisation operations consists of building a JTD object Thing from either a JSON-LD framed representation or a set of RDF triples. |
| 106 | +##### From JSON-LD framed document |
| 107 | + |
| 108 | +```` |
| 109 | +JsonObject jsonTD = JTD.parseJson(strJsonTD); |
| 110 | +Thing thing = Thing.fromJson(jsonTD); |
| 111 | +thing = (Thing) JTD.instantiateFromJson(jsonTD, Thing.class); # Alternativelly |
| 112 | +```` |
| 113 | +Notice that using the method `JTD.instantiateFromJson(jsonTD, Thing.class)` any other class from the model can be serialised. |
| 114 | + |
| 115 | +##### From RDF triples |
| 116 | +In order to build a JTD object from a set of RDF triples there are two main methods: |
| 117 | +##### A) Build a list of JTDs in case that the triples contain more than one Thing resource. |
| 118 | +````` |
| 119 | +List<Thing> things = fromRDF(modelTD) |
| 120 | +````` |
| 121 | +##### B) Build a unique JTDs providing the RDF resource URI. |
| 122 | +````` |
| 123 | +Thing thing = fromRDF(modelTD, "urn:dev:ops:32473-WoTLamp-1234") |
| 124 | +````` |
| 125 | + |
| 126 | +### Deserialisation of JTDs: |
| 127 | + |
| 128 | +##### To JSON-LD framed |
| 129 | +```` |
| 130 | +JsonObject jsonTD = thing.toJson() |
| 131 | +jsonTD = JTD.toJson(thing) # Alternativelly |
| 132 | +```` |
| 133 | +Notice that using the method `JTD.toJson(thing)` any other class from the model can be deserialised. |
| 134 | + |
| 135 | +##### To RDF triples |
| 136 | + |
| 137 | +```` |
| 138 | +Model modelTD = JTD.toRDF(thing) |
| 139 | + # Alternativelly |
| 140 | +JsonObject jsonTD = thing.toJson() |
| 141 | +modelTD = JTD.toRDF(jsonTD) |
| 142 | +```` |
| 143 | + |
| 144 | +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. |
| 145 | + |
| 146 | + |
| 147 | +### JDT validation |
| 148 | + |
| 149 | +##### Using SHACL shapes |
| 150 | +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: |
| 151 | + |
| 152 | +```` |
| 153 | +String shapesURI = "https://raw.githubusercontent.com/w3c/wot-thing-description/main/validation/td-validation.ttl" |
| 154 | +Model shapesGraph = RDFDataMgr.loadModel(shapesURI, Lang.TURTLE); |
| 155 | +ValidationReport shapeReport = JTD.validateWithShape(thing, shapesGraph); |
| 156 | +```` |
| 157 | + |
| 158 | +##### Using JSON schema (*comming soon*) |
| 159 | +##### Using restrictions in the model (*comming soon*) |
0 commit comments