forked from OMICRONEnergyOSS/oscd-template-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-templates.ts
332 lines (301 loc) · 9.67 KB
/
generate-templates.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { cyrb64 } from '@openscd/open-scd-core';
import type { TreeSelection } from '@openscd/oscd-tree-grid';
function describeEnumType(element: Element): { vals: Record<string, string> } {
const vals: Record<string, string> = {};
for (const val of Array.from(element.children)
.filter(child => child.tagName === 'EnumVal')
.sort(
(v1, v2) =>
parseInt(v1.getAttribute('ord') ?? '', 10) -
parseInt(v2.getAttribute('ord') ?? '', 10)
))
vals[val.getAttribute('ord') ?? ''] = val.textContent ?? '';
return { vals };
}
function describeDAType(element: Element): {
bdas: Record<string, Record<string, string | null>>;
} {
const bdas: Record<string, Record<string, string | null>> = {};
for (const bda of Array.from(element.children)
.filter(child => child.tagName === 'BDA')
.sort((c1, c2) => c1.outerHTML.localeCompare(c2.outerHTML))) {
const [bType, type, dchg, dupd, qchg] = [
'bType',
'type',
'dchg',
'dupd',
'qchg',
].map(attr => bda.getAttribute(attr));
bdas[bda.getAttribute('name') ?? ''] = { bType, type, dchg, dupd, qchg };
}
return { bdas };
}
function describeDOType(element: Element) {
const sdos: Record<string, Record<string, string | null>> = {};
for (const sdo of Array.from(element.children)
.filter(child => child.tagName === 'SDO')
.sort((c1, c2) => c1.outerHTML.localeCompare(c2.outerHTML))) {
const [name, type, transient] = ['name', 'type', 'transient'].map(attr =>
sdo.getAttribute(attr)
);
sdos[name ?? ''] = { type, transient };
}
const das: Record<string, Record<string, string | null>> = {};
for (const da of Array.from(element.children)
.filter(child => child.tagName === 'DA')
.sort((c1, c2) => c1.outerHTML.localeCompare(c2.outerHTML))) {
const [name, fc, bType, type, dchg, dupd, qchg] = [
'name',
'fc',
'bType',
'type',
'dchg',
'dupd',
'qchg',
].map(attr => da.getAttribute(attr));
das[name ?? ''] = {
fc,
bType,
type,
dchg,
dupd,
qchg,
};
}
return {
sdos,
das,
cdc: element.getAttribute('cdc'),
};
}
function describeLNodeType(element: Element) {
const dos: Record<string, Record<string, string | null>> = {};
for (const doElement of Array.from(element.children)
.filter(child => child.tagName === 'DO')
.sort((c1, c2) => c1.outerHTML.localeCompare(c2.outerHTML))) {
const [name, type, transient] = ['name', 'type', 'transient'].map(attr =>
doElement.getAttribute(attr)
);
dos[name ?? ''] = { type, transient };
}
return {
dos,
lnClass: element.getAttribute('lnClass'),
};
}
const typeDescriptions = {
EnumType: describeEnumType,
DAType: describeDAType,
DOType: describeDOType,
LNodeType: describeLNodeType,
} as Partial<Record<string, (e: Element) => object>>;
function describeElement(element: Element): object {
return (
typeDescriptions[element.tagName]?.(element) ?? { xml: element.outerHTML }
);
}
function hashElement(element: Element): string {
return cyrb64(JSON.stringify(describeElement(element)));
}
export type Templates = {
EnumType: Element[];
DAType: Element[];
DOType: Element[];
LNodeType: Element[];
};
export function generateTemplates(
selection: TreeSelection,
doc: XMLDocument,
data: Record<string, any>,
lnClass: string
): Templates {
const types = new Set<string>();
const elements: Templates = {
LNodeType: [],
DOType: [],
DAType: [],
EnumType: [],
};
function identify(element: Element, name: string): string {
const hash = hashElement(element);
const id = `${name}$oscd$_${hash}`;
element.setAttribute('id', id);
if (!types.has(id)) {
types.add(id);
elements[
element.tagName as 'LNodeType' | 'DOType' | 'DAType' | 'EnumType'
]?.push(element);
}
return id;
}
function createElement(
tag: string,
attrs: Record<string, string | null | undefined> = {}
): Element {
const element = doc.createElementNS(doc.documentElement.namespaceURI, tag);
Object.entries(attrs)
.filter(([_name, value]) => value !== null && value !== undefined)
.forEach(([name, value]) => element.setAttribute(name, value!));
return element;
}
function addEnumType(path: string[], sel: TreeSelection): string {
let d = data;
for (const slug of path) d = d[slug].children;
const vals = [];
for (const content of Object.keys(sel)) {
const ord = d[content].literalVal;
const val = createElement('EnumVal', { ord });
val.textContent = content;
vals.push(val);
}
vals.sort(
(v1, v2) =>
parseInt(v1.getAttribute('ord') ?? '', 10) -
parseInt(v2.getAttribute('ord') ?? '', 10)
);
const enumType = createElement('EnumType');
vals.forEach(val => enumType.append(val));
return identify(enumType, path[path.length - 1]);
}
function addDAType(
path: string[],
sel: TreeSelection,
underlyingValSel: TreeSelection = {}
): string {
let d = data;
for (const slug of path.slice(0, -1)) d = d[slug].children;
const { children, underlyingTypeKind, underlyingType, typeKind } =
d[path[path.length - 1]];
if (typeKind !== 'CONSTRUCTED')
throw new Error(`DAType typeKind is not CONSTRUCTED, but ${typeKind}`);
const daType = createElement('DAType');
for (const [name, dep] of Object.entries(children) as [
string,
{
tagName: string;
transient?: string;
fc: string;
dchg?: string;
dupd?: string;
qchg?: string;
typeKind?: 'BASIC' | 'ENUMERATED' | 'CONSTRUCTED' | 'undefined';
type?: string;
}
][]) {
// eslint-disable-next-line no-continue
if (!sel[name]) continue;
const bda = createElement('BDA', { name });
if (dep.typeKind === 'BASIC' || !dep.typeKind) {
bda.setAttribute('bType', dep.type ?? '');
}
if (dep.typeKind === 'ENUMERATED') {
const enumId = addEnumType(path.concat([name]), sel[name]);
bda.setAttribute('bType', 'Enum');
bda.setAttribute('type', enumId);
}
if (dep.typeKind === 'undefined') {
if (underlyingTypeKind === 'BASIC')
bda.setAttribute('bType', underlyingType);
else if (underlyingTypeKind === 'ENUMERATED') {
const enumId = addEnumType(
path.slice(0, -1).concat(['stVal']),
underlyingValSel
);
bda.setAttribute('bType', 'Enum');
bda.setAttribute('type', enumId);
} else if (underlyingTypeKind === 'CONSTRUCTED') {
let daId = '';
try {
daId = addDAType(
path.slice(0, -1).concat(['mxVal']),
underlyingValSel
);
} catch {
throw new Error(
`Unexpected selection ${JSON.stringify(
path
)} without mxVal sibling`
);
}
bda.setAttribute('bType', 'Struct');
bda.setAttribute('type', daId);
} else
throw new Error(
`Unexpected underlyingTypeKind ${underlyingTypeKind}`
);
}
if (dep.typeKind === 'CONSTRUCTED') {
const daId = addDAType(
path.concat([name]),
sel[name],
underlyingValSel
);
bda.setAttribute('bType', 'Struct');
bda.setAttribute('type', daId);
}
daType.append(bda);
}
return identify(daType, path[path.length - 1]);
}
function addDOType(path: string[], sel: TreeSelection): string {
if (!sel)
throw new Error(
`adding DO type for empty selection at ${JSON.stringify(path, null, 2)}`
);
let d = data;
for (const slug of path.slice(0, -1)) d = d[slug].children;
const dO = d[path[path.length - 1]];
const doType = createElement('DOType', { cdc: dO.type });
const deps: [
string,
{
tagName: string;
transient?: string;
fc: string;
dchg?: string;
dupd?: string;
qchg?: string;
typeKind?: 'BASIC' | 'ENUMERATED' | 'CONSTRUCTED' | 'undefined';
type?: string;
}
][] = Object.entries(dO.children);
for (const [name, dep] of deps) {
// eslint-disable-next-line no-continue
if (!sel[name]) continue;
if (dep.tagName === 'SubDataObject') {
const { transient } = dep;
const type = addDOType(path.concat([name]), sel[name]);
const sdo = createElement('SDO', { name, transient, type });
doType.prepend(sdo);
} else {
const { fc, dchg, dupd, qchg } = dep;
const da = createElement('DA', { name, fc, dchg, dupd, qchg });
if (dep.typeKind === 'BASIC' || !dep.typeKind) {
da.setAttribute('bType', dep.type ?? '');
}
if (dep.typeKind === 'ENUMERATED') {
const enumId = addEnumType(path.concat([name]), sel[name]);
da.setAttribute('bType', 'Enum');
da.setAttribute('type', enumId);
}
if (dep.typeKind === 'CONSTRUCTED') {
const underlyingVal = sel.stVal || sel.mxVal;
const daId = addDAType(path.concat([name]), sel[name], underlyingVal);
da.setAttribute('bType', 'Struct');
da.setAttribute('type', daId);
}
doType.append(da);
}
}
return identify(doType, path[path.length - 1]);
}
const lnType = createElement('LNodeType', { lnClass });
Object.keys(selection).forEach(name => {
const type = addDOType([name], selection[name]);
const { transient } = data[name];
const doElement = createElement('DO', { name, type, transient });
lnType.append(doElement);
});
identify(lnType, lnClass);
return elements;
}