-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonem2m2owl.js
198 lines (162 loc) · 4.63 KB
/
onem2m2owl.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
198
const fs = require('fs');
const defined = [];
function inputOrOutputClass(type) {
let [prefix, name] = type.split(/:/);
let id = 'base:' + name + 'InputOrOutput';
if (defined.indexOf(id) > -1) {
return {
'@id': id
};
}
defined.push(id);
return {
// TODO more accurate @id?
'@id': id,
'rdfs:subClassOf': {
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasDataTypeAndRange',
'owl:hasValue': {
// TODO datatype IRI represented as IRI (problem in ontology)...
'@value': type
}
}
}
}
function actionClass(row) {
let [str, ret, name, arg] = row.match(/^(\w+) (\w+) (\w+) (true|false) (.*)$/);
let def = {
'rdfs:label': name,
'rdfs:subClassOf': ['sdt:Action']
};
if (ret != 'none') {
def['rdfs:subClassOf'].push({
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasOutput',
'owl:someValuesFrom': id + '.' + ret
});
}
if (arg != 'none') {
def['rdfs:subClassOf'].push({
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasInput',
'owl:someValuesFrom': id + '.' + arg
});
}
return def;
}
function dataPointClass(row) {
let [str, name, type, r, w] = row.match(/^(\w+) (.+) (true|false) (true|false) (true|false) (.*)$/);
let def = {
'rdfs:label': name,
'rdfs:seeAlso': {
// datatype property used in payload
'@id': 'haim:' + name,
'@type': 'owl:DatatypeProperty'
},
'rdfs:subClassOf': ['sdt:DataPoint']
}
let multiple = false;
if (type.startsWith('list of ')) {
multiple = true; // TODO use it?
type = type.substring(8);
}
if (r === 'true') {
def['rdfs:subClassOf'].push({
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasOutput',
'owl:allValuesFrom': inputOrOutputClass(type)
});
}
if (w === 'true') {
def['rdfs:subClassOf'].push({
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasInput',
'owl:allValuesFrom': inputOrOutputClass(type)
});
}
return def;
}
function propertyClass(row) {
let [str, name, type] = row.match(/^(\w+) (.+) (true|false) (.*)$/);
let def = {
'rdfs:label': name,
'rdfs:subClassOf': ['sdt:Property']
}
// TODO not an operation?
// TODO relate property to datatype?
return def;
}
function operationRestrictions(id, txt) {
let restrictions = [];
txt.match(/Table 5.3.\d+-\d: [\w ]+\r\n(.+\r\n)+/g).forEach(t => {
let [title, header, ...rows] = t.split(/\r\n/);
let xClass = null;
if (title.match(/Actions of/)) xClass = actionClass;
else if (title.match(/DataPoints of/)) xClass = dataPointClass;
else if (title.match(/Properties of/)) xClass = propertyClass;
else return; // error case
rows.filter(r => r.length > 0).forEach(r => {
let [str, x, opt, doc] = r.match(/(.*) (true|false) (.*)$/);
let def = xClass(r);
def['@id'] = id + '.' + def['rdfs:label'];
def['rdfs:comment'] = doc;
restrictions.push({
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasOperation',
'owl:allValuesFrom': def
});
if (opt === 'false') {
restrictions.push({
'@type': 'owl:Restriction',
'owl:onProperty': 'base:hasOperation',
'owl:someValuesFrom': {
'@id': def['@id']
}
});
}
});
});
return restrictions;
}
// TODO enumerated datatypes
function moduleClass(txt) {
let [str, title, desc] = txt.match(/5.3.\d+ (\w+)\r\n(.*)\r\n/);
let def = {
'@id': 'urn:org.onem2m.home.moduleclass.' + title,
'rdfs:label': title,
'rdfs:comment': desc
};
let restrictions = operationRestrictions(def['@id'], txt);
def['rdfs:subClassOf'] = ['sdt:ModuleClass'].concat(restrictions);
return def;
}
const txt = fs.readFileSync('descriptions.txt', 'utf-8');
let titles = txt.match(/5.3.\d+ (\w+)\r\n/g);
let sections = [];
for (let i = 0; i < titles.length; i++) {
let beginning = txt.indexOf(titles[i]);
let end = i + 1 < titles.length ? txt.indexOf(titles[i + 1]) : txt.length;
sections.push(txt.substring(beginning, end));
}
let jsonld = {
'@context': {
'xsd': 'http://www.w3.org/2001/XMLSchema#',
'xs': 'http://www.w3.org/2001/XMLSchema#',
'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'rdfs:subClassOf': { '@type': '@vocab' },
'owl': 'http://www.w3.org/2002/07/owl#',
'base': 'http://www.onem2m.org/ontology/Base_Ontology#',
'sdt': 'http://www.onem2m.org/ontology/SDT#',
'haim': 'http://www.onem2m.org/ontology/HAIM#', // TODO proper ns
'hd': 'http://www.onem2m.org/ontology/HD#',
'owl:imports': { '@type': '@vocab' },
'owl:onProperty': { '@type': '@vocab' },
'owl:allValuesFrom': { '@type': '@vocab' },
'owl:someValuesFrom': { '@type': '@vocab' }
},
'@graph': [{
'@type': 'owl:Ontology',
'owl:imports': 'base:'
}].concat(sections.map(moduleClass))
}
console.log(JSON.stringify(jsonld));