forked from w3c/wot-binding-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.js
86 lines (73 loc) · 3.21 KB
/
render.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
let fs = require('fs');
let sttl = require('sttl');
let urdf = require('urdf');
const toRDf = require('./toRdf.js').context
const tpl = fs.readFileSync('ontology/templates.rq', 'utf-8');
sttl.register(tpl);
sttl.connect(q => {
return urdf.query(q)
.then(b => {
const bindings = b.reduce((accumulator, value) => {
// Flat bindings that have list variables
// Note: this method only works for one list variable per binding
const found = Object.keys(value).find(k => value[k].type === "list")
if (found) {
value[found].value.forEach(listElement => {
const newBinding = Object.assign({}, value)
newBinding[found] = listElement
accumulator.push(newBinding);
});
} else {
accumulator.push(value)
}
return accumulator
}, []);
return { results: { bindings } }
});
});
const ontologies = [
'ontology/coap.ttl',
'ontology/mqtt.ttl',
'ontology/modbus.ttl'
];
console.log("Rendering ontology documentation...");
const promiseChain = ontologies.reduce((p, src) => {
const ontologyFile = src
const templateURI = 'http://w3c.github.io/wot-binding-templates/ontology#main'
return p.then( _ => render(ontologyFile,templateURI,ontologyFile.replace('.ttl', '.html'))).then(() => urdf.clear())
}, Promise.resolve());
console.log("Rendering WoT binding documentation...");
const mappings = [
['bindings/protocols/modbus/mapping.ttl', 'ontology/modbus.ttl', 'http://w3c.github.io/wot-binding-templates/mappings#modbus','bindings/protocols/modbus/context.jsonld'],
['bindings/protocols/mqtt/mapping.ttl', 'ontology/mqtt.ttl', 'http://w3c.github.io/wot-binding-templates/mappings#mqtt', 'bindings/protocols/mqtt/context.jsonld']
];
const modbusTemplate = fs.readFileSync('bindings/protocols/modbus/template.sparql', 'utf-8');
const mqttTemplate = fs.readFileSync('bindings/protocols/mqtt/template.sparql', 'utf-8');
sttl.register(modbusTemplate);
sttl.register(mqttTemplate);
mappings.reduce((p, src) => {
const ontologyFile = src[0]
const baseOntologyFile = src[1]
const templateURI = src[2]
const contextFile = src[3]
let base = fs.readFileSync( baseOntologyFile, 'UTF-8');
return p.then( _ => urdf.load(base,{ format: 'text/turtle' }))
.then(() => {
const jsonld = fs.readFileSync(contextFile,'utf8');
const result = toRDf(JSON.parse(jsonld));
return urdf.load(result)
})
.then( _ => render(ontologyFile,templateURI,ontologyFile.replace('mapping.ttl', 'index.html')))
.then(() => urdf.clear());
},promiseChain);
function render(ontologyFile,templateURI,output) {
let ttl = fs.readFileSync(ontologyFile, 'UTF-8');
return Promise.resolve()
.then(() => urdf.load(ttl, { format: 'text/turtle' }))
.then(() => sttl.callTemplate(templateURI, ttl))
.then(html => fs.writeFileSync(output, html))
.then(_ => console.log("File ",output,"generated"))
.catch((e) => {
console.error('Error while rendering ' + ontologyFile + ': ' + e);
});
}