Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,18 +164,18 @@ public RESTResource post(URI uri, Map<String, String> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -85,30 +81,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 <http://www.w3c.org/wot/td#associatedUri> <" + uri + ">";

String prefix = StrUtils.strjoinNL("PREFIX dc: <http://purl.org/dc/elements/1.1/>"
, "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;
}

Expand Down Expand Up @@ -343,14 +348,14 @@ public static List<String> listThingDescriptionsFromTextSearch(String keyWords)
dataset.begin(ReadWrite.READ);

try {
String query = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + qMatch + " }}";
String query = "SELECT DISTINCT ?s WHERE { GRAPH ?g { " + 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ public void testREST() throws IOException, URISyntaxException {
byte[] content;
String tdId, tdId2, td;

// LOOKUP
Set<String> tdIds;
JsonObject fanQR;

Map<String,String> parameters = new HashMap<String,String>();

//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
Expand All @@ -107,20 +125,16 @@ public void testREST() throws IOException, URISyntaxException {

td = ThingDescriptionUtils.getThingDescriptionIdFromUri(tdUri2);
Assert.assertEquals("TD temperatureSensor not registered", baseUri + tdId2, td);


// LOOKUP
Set<String> tdIds;
JsonObject fanQR;

in.close();

// GET by sparql query
parameters.clear();
parameters.put("query", "?s ?p ?o");
resource = tdch.get(new URI(baseUri + "/td"), parameters);

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));

Expand All @@ -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);
Expand Down