-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTdToUri.js
197 lines (180 loc) · 5.29 KB
/
TdToUri.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import urdf from "urdf";
/**
* Represents a node in a tree structure.
*/
class TreeNode {
constructor(name, td) {
this.name = name;
this.children = [];
this.parent = null;
this.td = td;
}
/**
* Adds a child node to the current node.
* @param {TreeNode} childNode - The child node to be added.
*/
addChild(childNode) {
childNode.parent = this;
this.children.push(childNode);
}
/**
* Sets the parent node of the current node.
* @param {TreeNode} parentNode - The parent node.
*/
setParent(parentNode) {
this.parent = parentNode;
}
}
/**
* Traverse the tree starting from rootNode and builds the URI for each node.
* @param {Object} rootNode - The node from where the traversal starts.
* @param {string} path - The current path.
* @param {Array} uriList - The list where the URIs are stored.
*/
async function traverseTree(rootNode, path, uriList) {
const newPath = `${path}${rootNode.td.title}/`;
// Add properties
const properties = await queryTD(
rootNode.td,
`PREFIX td: <https://www.w3.org/2019/wot/td#>
PREFIX dc: <http://purl.org/dc/terms/>
SELECT ?title
WHERE {
?s td:hasPropertyAffordance ?o .
?o dc:title ?title
}`
);
const propertyArray = properties.map((item) => item.title.value);
for (const property of propertyArray) {
const newPropertyPath = `${newPath}${property}`;
uriList.push(newPropertyPath);
}
// Add actions
const actions = await queryTD(
rootNode.td,
`PREFIX td: <https://www.w3.org/2019/wot/td#>
PREFIX dc: <http://purl.org/dc/terms/>
SELECT ?title
WHERE {
?s td:hasActionAffordance ?o .
?o dc:title ?title
}`
);
const actionArray = actions.map((item) => item.title.value);
for (const action of actionArray) {
const newActionPath = `${newPath}${action}`;
uriList.push(newActionPath);
}
// Add events
const events = await queryTD(
rootNode.td,
`PREFIX td: <https://www.w3.org/2019/wot/td#>
PREFIX dc: <http://purl.org/dc/terms/>
SELECT ?title
WHERE {
?s td:hasEventAffordance ?o .
?o dc:title ?title
}`
);
const eventArray = events.map((item) => item.title.value);
for (const event of eventArray) {
const newEventPath = `${newPath}${event}`;
uriList.push(newEventPath);
}
for (const childNode of rootNode.children) {
await traverseTree(childNode, newPath, uriList);
}
}
/**
* Create the URIs for all nodes in the nodeList using the given base.
* @param {Array} rootNodes - The list of root nodes.
* @param {string} base - The base string for the URI.
* @returns {Array} - The list of URIs.
*/
async function createUris(rootNodes, base) {
const uriList = [];
for (const rootNode of rootNodes) {
await traverseTree(rootNode, `${base}/`, uriList);
}
return uriList;
}
/**
* Generate the hierarchy of nodes and return the URIs.
* @param {Object} thingDescriptionsWithURI - The object with URIs and corresponding descriptions.
* @param {string} baseUri - The base URI of the Pod.
* @returns {Array} - The list of generated URIs.
*/
export async function generateUriHierarchy(thingDescriptionsWithURI, baseUri) {
// Remove / at the end of baseUri to generate corect URIs
if (baseUri.endsWith("/")) {
baseUri = baseUri.slice(0, -1);
}
// Generate TreeNodes for each Device:
const treeNodes = {};
const rootNodes = [];
// Generate TreeNode for each Device
for (const uri in thingDescriptionsWithURI) {
const td = thingDescriptionsWithURI[uri];
const TNode = new TreeNode(uri, td);
treeNodes[uri] = TNode;
}
// Only set relations if multiple TDs are provided
// Used for orchestrator
if (Object.keys(thingDescriptionsWithURI).length > 0) {
// Set Children and Parents of each TreeNode
for (const uri in treeNodes) {
const TNode = treeNodes[uri];
const td = TNode.td;
// Set Children
const resQueryHosts = await queryTD(
td,
`PREFIX sosa: <http://www.w3.org/ns/sosa/>
SELECT ?o
WHERE {
?s sosa:hosts ?o .
}`
);
const urisOfChilds = resQueryHosts.map((item) => item.o.value);
for (const uriOfChild of urisOfChilds) {
TNode.addChild(treeNodes[uriOfChild]);
}
// Set Parent
const resQueryHostedBy = await queryTD(
td,
`PREFIX sosa: <http://www.w3.org/ns/sosa/>
SELECT ?o
WHERE {
?s sosa:isHostedBy ?o .
}`
);
const urisOfParents = resQueryHostedBy.map((item) => item.o.value);
if (urisOfParents.length > 0) {
for (const uriOfParent of urisOfParents) {
TNode.setParent(treeNodes[uriOfParent]);
}
} else {
// If node has no parent, then it's a root node
rootNodes.push(TNode);
}
}
} else {
// Create URIs for Mediator
for (const uri in treeNodes) {
const TNode = treeNodes[uri];
rootNodes.push(TNode);
}
}
const createdURIs = await createUris(Object.values(rootNodes), baseUri);
return createdURIs;
}
/**
* Query a Graph
* @param {Object} td - The Thing Description object.
* @param {string} query - The SPARQL query.
* @returns {Array} - The list of results.
*/
async function queryTD(td, query) {
urdf.clear();
await urdf.load(td);
return await urdf.query(query);
}