From 6b82fec6b15caf173e63761871695d8182fd7b63 Mon Sep 17 00:00:00 2001 From: Oscar Novo Date: Mon, 10 Apr 2017 15:04:04 +0300 Subject: [PATCH 1/6] remove tabs from the jsonTD --- .../samples/temperatureSensorTD.jsonld | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/thingweb-repository/src/main/resources/samples/temperatureSensorTD.jsonld b/thingweb-repository/src/main/resources/samples/temperatureSensorTD.jsonld index 64c5648..edd2754 100644 --- a/thingweb-repository/src/main/resources/samples/temperatureSensorTD.jsonld +++ b/thingweb-repository/src/main/resources/samples/temperatureSensorTD.jsonld @@ -1,38 +1,38 @@ { - "@context": ["http://w3c.github.io/wot/w3c-wot-td-context.jsonld", - "http://w3c.github.io/wot/w3c-wot-common-context.jsonld"], - "@type": "Sensor", - "name": "myTempSensor", - "uris" : ["coap:///www.example.com:5687/temp"], - "encodings": ["JSON"], - "properties": [ - { - "@type": "Temperature", - "unit": "celsius", - "reference": "threshold", - "name": "myTemp", - "valueType": "number", - "writable": false, - "hrefs": ["val"] - }, { - "@type": "Temperature", - "unit": "celsius", - "name": "myThreshold", - "valueType": "number", - "writable": true, - "hrefs": ["threshold"] - } - ], - "events": [ - { - "valueType": "number", - "name": "myChange", - "property": "temp", - "hrefs": ["val/changed"] - }, { - "valueType": "number", - "name": "myWarning", - "hrefs": ["val/high"] - } - ] + "@context": ["http://w3c.github.io/wot/w3c-wot-td-context.jsonld", + "http://w3c.github.io/wot/w3c-wot-common-context.jsonld"], + "@type": "Sensor", + "name": "myTempSensor", + "uris": ["coap:///www.example.com:5687/temp"], + "encodings": ["JSON"], + "properties": [ + { + "@type": "Temperature", + "unit": "celsius", + "reference": "threshold", + "name": "myTemp", + "valueType": "number", + "writable": false, + "hrefs": ["val"] + }, { + "@type": "Temperature", + "unit": "celsius", + "name": "myThreshold", + "valueType": "number", + "writable": true, + "hrefs": ["threshold"] + } + ], + "events": [ + { + "valueType": "number", + "name": "myChange", + "property": "temp", + "hrefs": ["val/changed"] + }, { + "valueType": "number", + "name": "myWarning", + "hrefs": ["val/high"] + } + ] } \ No newline at end of file From fffd552a4346be329e047a16c9dd0e1d50dc404a Mon Sep 17 00:00:00 2001 From: Oscar Novo Date: Mon, 10 Apr 2017 15:17:12 +0300 Subject: [PATCH 2/6] fix the search in function listThingDescriptionsFromTextSearch --- .../java/de/thingweb/repository/ThingDescriptionUtils.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java index 3fb71c6..4ba4204 100644 --- a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java +++ b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java @@ -343,14 +343,15 @@ public static List listThingDescriptionsFromTextSearch(String keyWords) dataset.begin(ReadWrite.READ); try { - String query = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + qMatch + " }}"; + //String query = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + qMatch + " }}"; + String query = "SELECT DISTINCT ?s {" + qMatch + "}"; Query q = QueryFactory.create(prefix + "\n" + query); try { QueryExecution qexec = QueryExecutionFactory.create(q , dataset); ResultSet result = qexec.execSelect(); while (result.hasNext()) { - tds.add(result.next().get("g").asResource().getURI()); + tds.add(result.next().get("s").asResource().getURI()); } } catch (Exception e) { throw e; From 924b2915c6192fe18342411e8d1d454a120b93f2 Mon Sep 17 00:00:00 2001 From: Oscar Novo Date: Mon, 10 Apr 2017 15:19:32 +0300 Subject: [PATCH 3/6] fix the listThingDescriptionsFromTextSearch --- .../java/de/thingweb/repository/ThingDescriptionUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java index 4ba4204..d888855 100644 --- a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java +++ b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java @@ -343,8 +343,7 @@ public static List listThingDescriptionsFromTextSearch(String keyWords) dataset.begin(ReadWrite.READ); try { - //String query = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + qMatch + " }}"; - String query = "SELECT DISTINCT ?s {" + qMatch + "}"; + String query = "SELECT DISTINCT ?s WHERE { GRAPH ?g { " + qMatch + " }}"; Query q = QueryFactory.create(prefix + "\n" + query); try { From 338de5fca309c4c3c033680ab4a9acdc7858bce6 Mon Sep 17 00:00:00 2001 From: Oscar Novo Date: Mon, 10 Apr 2017 16:21:48 +0300 Subject: [PATCH 4/6] refactoring the ThingDescriptionCollectionHandler --- .../ThingDescriptionCollectionHandler.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java index 459d9af..8e55b41 100644 --- a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java +++ b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java @@ -166,18 +166,18 @@ public RESTResource post(URI uri, Map parameters, InputStream pa dataset.begin(ReadWrite.WRITE); try { - Model tdb = dataset.getNamedModel(resourceUri.toString()); - tdb.read(new ByteArrayInputStream(data.getBytes()), endpointName, "JSON-LD"); + //New graph model + Model tdbnm = dataset.getNamedModel(resourceUri.toString()); + tdbnm.read(new ByteArrayInputStream(data.getBytes()), null, "JSON-LD"); + ThingDescriptionUtils utils = new ThingDescriptionUtils(); + keyWords = utils.getModelKeyWords(tdbnm); // TODO check TD validity + //dataset.close(); - tdb = dataset.getDefaultModel(); + //Adding the information of the new TD into the Default Model + Model tdb = dataset.getDefaultModel(); tdb.createResource(resourceUri.toString()).addProperty(DC.source, data); - // Get key words from statements - ThingDescriptionUtils utils = new ThingDescriptionUtils(); - Model newThing = dataset.getNamedModel(resourceUri.toString()); - keyWords = utils.getModelKeyWords(newThing); - // Store key words as triple: ?g_id rdfs:comment "keyWordOrWords" tdb.getResource(resourceUri.toString()).addProperty(RDFS.comment, StrUtils.strjoin(" ", keyWords)); From c02f88487f5dd706aff70d6cb554d14045339b0a Mon Sep 17 00:00:00 2001 From: Oscar Novo Date: Mon, 10 Apr 2017 16:29:59 +0300 Subject: [PATCH 5/6] Fix the SPARQL searches functions from getThingDescriptionIdFromUri and listThingDescriptionsFromTextSearch functions of ThingDescriptionUtils; Removing any TD before executing the test --- .../repository/ThingDescriptionUtils.java | 31 ++++++++++++------- .../thingweb/repository/ThingWebRepoTest.java | 30 ++++++++++++------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java index d888855..db743ff 100644 --- a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java +++ b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java @@ -85,30 +85,39 @@ public static String streamToString(InputStream s) throws IOException { * Returns the ID of a thing description stored in the database given its URI. * @param uri URI of the thing description we want to return. * @return the ID of the thing description. - */ + */ public static String getThingDescriptionIdFromUri(String uri) { - String query = "?td <" + uri + ">"; + + String prefix = StrUtils.strjoinNL("PREFIX dc: " + , "PREFIX : <.>"); + String id = "NOT FOUND"; - + // Run the query Dataset dataset = Repository.get().dataset; dataset.begin(ReadWrite.READ); try { - String q = "SELECT ?g_id WHERE { GRAPH ?g_id { " + query + " }}"; - try (QueryExecution qexec = QueryExecutionFactory.create(q, dataset)) { + + String query = "SELECT DISTINCT ?s WHERE {?s dc:source ?uris FILTER regex(?uris, \""+ uri +"\", \"i\")}"; + Query q = QueryFactory.create(prefix + "\n" + query); + + try { + QueryExecution qexec = QueryExecutionFactory.create(q , dataset); ResultSet result = qexec.execSelect(); - while (result.hasNext()) { - id = result.next().get("g_id").toString(); + while (result.hasNext()) { + QuerySolution i = result.next(); + id = i.get("s").toString(); } + qexec.close(); + + } catch (Exception e) { + throw e; } - catch (Exception e) { - throw e; - } } finally { dataset.end(); } - + return id; } diff --git a/thingweb-repository/src/test/java/de/thingweb/repository/ThingWebRepoTest.java b/thingweb-repository/src/test/java/de/thingweb/repository/ThingWebRepoTest.java index 4111ec3..bfbfb14 100644 --- a/thingweb-repository/src/test/java/de/thingweb/repository/ThingWebRepoTest.java +++ b/thingweb-repository/src/test/java/de/thingweb/repository/ThingWebRepoTest.java @@ -86,7 +86,25 @@ public void testREST() throws IOException, URISyntaxException { byte[] content; String tdId, tdId2, td; + // LOOKUP + Set tdIds; + JsonObject fanQR; + Map parameters = new HashMap(); + + //DELETE EXISTING TDs FIRST + + parameters.clear(); + resource = tdch.get(new URI(baseUri + "/td"), parameters); + + fanQR = JSON.parse(resource.content); + tdIds = fanQR.keys(); + + for (String item : tdIds){ + tdch.delete(new URI(baseUri + item), null, null); + } + + parameters.clear(); parameters.put("ep", baseUri); // POST TD fan @@ -107,12 +125,8 @@ public void testREST() throws IOException, URISyntaxException { td = ThingDescriptionUtils.getThingDescriptionIdFromUri(tdUri2); Assert.assertEquals("TD temperatureSensor not registered", baseUri + tdId2, td); - - - // LOOKUP - Set tdIds; - JsonObject fanQR; - + in.close(); + // GET by sparql query parameters.clear(); parameters.put("query", "?s ?p ?o"); @@ -120,7 +134,7 @@ public void testREST() throws IOException, URISyntaxException { fanQR = JSON.parse(resource.content); tdIds = fanQR.keys(); - Assert.assertFalse("TD fan not found", tdIds.isEmpty()); + Assert.assertFalse("TD is Empty", tdIds.isEmpty()); //Assert.assertEquals("Found more than one TD", 1, tdIds.size()); Assert.assertTrue("TD fan not found", tdIds.contains(tdId)); @@ -136,8 +150,6 @@ public void testREST() throws IOException, URISyntaxException { Assert.assertTrue("TD fan not found", tdIds.contains(tdId)); Assert.assertFalse("TD temperatureSensor found", tdIds.contains(tdId2)); - - // GET TD by id ThingDescriptionHandler tdh = new ThingDescriptionHandler(tdId, Repository.get().servers); resource = tdh.get(new URI(baseUri + tdId), null); From c853a3b4d07e984fbaf1a97232c86cf826abdad4 Mon Sep 17 00:00:00 2001 From: Oscar Novo Date: Mon, 10 Apr 2017 16:33:42 +0300 Subject: [PATCH 6/6] remove unused imports --- .../repository/ThingDescriptionCollectionHandler.java | 2 -- .../java/de/thingweb/repository/ThingDescriptionUtils.java | 4 ---- 2 files changed, 6 deletions(-) diff --git a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java index 8e55b41..4895678 100644 --- a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java +++ b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionCollectionHandler.java @@ -10,12 +10,10 @@ import java.util.Map; import java.util.UUID; -import org.apache.jena.atlas.json.JsonParseException; import org.apache.jena.atlas.lib.StrUtils; import org.apache.jena.query.Dataset; import org.apache.jena.query.ReadWrite; import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Resource; import org.apache.jena.vocabulary.DC; import org.apache.jena.vocabulary.DCTerms; import org.apache.jena.vocabulary.RDFS; diff --git a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java index db743ff..e7cc9f7 100644 --- a/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java +++ b/thingweb-repository/src/main/java/de/thingweb/repository/ThingDescriptionUtils.java @@ -5,7 +5,6 @@ import java.io.InputStreamReader; import java.io.StringWriter; import java.net.URI; -import java.net.URISyntaxException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -30,16 +29,13 @@ import org.apache.jena.rdf.model.Literal; import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.NodeIterator; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; import org.apache.jena.rdf.model.Statement; import org.apache.jena.rdf.model.StmtIterator; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; -import org.apache.jena.sparql.util.QueryExecUtils; public class ThingDescriptionUtils {