-
Notifications
You must be signed in to change notification settings - Fork 0
/
niem2vocab.mjs
275 lines (255 loc) · 9.97 KB
/
niem2vocab.mjs
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
import libxmljs from "libxmljs";
import {createWriteStream, readFileSync} from 'node:fs';
import {basename, dirname} from 'node:path';
const xsNamespace = {'xs': 'http://www.w3.org/2001/XMLSchema'};
const niemDirectory = dirname(process.argv[2]);
const niemFilename = basename(process.argv[2]);
const niemType = process.argv[3];
// open files for writing
const vocabStream = createWriteStream(process.cwd() + '/vocabulary.yml');
const contextStream = createWriteStream(process.cwd() + '/fs-v1.jsonld');
// load schema file
console.log('Loading schema from', niemFilename);
process.chdir(niemDirectory);
const niemFile = readFileSync(niemFilename);
const xsdModel = libxmljs.parseXml(niemFile);
// get base type
const typeModel = xsdModel.find(
`//xs:complexType[@name="${niemType}"]`, xsNamespace)[0];
//console.log('Got model', typeModel);
const allClasses = {};
const allProperties = {};
function buildRdfClass(subModel) {
//console.log("GASP", subModel.getAttribute('name').value());
// add class information
const rdfClass = {};
rdfClass.id = subModel.getAttribute('name').value();
//console.log("PROCESS CLASS", rdfClass.id);
rdfClass.label = rdfClass.id.match(/[A-Z][a-z]+|[0-9]+/g).join(' ');
const comment = subModel.childNodes()[1].childNodes()[1].childNodes()[0].toString() + '.';
rdfClass.comment = comment;
allClasses[rdfClass.id] = rdfClass;
//console.log("ADD CLASS", rdfClass);
// get all properties for class
subModel.childNodes().forEach(node => {
//console.log('SM', node.name());
if(node.name() === 'complexContent') {
const elements = node.childNodes()[1].childNodes()[1].childNodes();
elements.forEach(element => {
if(element.name() === 'element') {
const property = {};
property.curie = element.getAttribute('ref').value();
const propertyName = property.curie.split(':')[1].replace(/^NIST/, 'nist').replace(/^FIPS/, 'fips').replace(/^PIV/, 'piv');
property.label = propertyName.match(/[A-Z][a-z]+|[0-9]+/g)?.join(' ');
property.id = propertyName[0].toLowerCase() + propertyName.slice(1);
//console.log('ELEMENT', property);
//console.log('propertyName', propertyName);
//console.log('ADD PROPERTY', property);
allProperties[property.id] = property;
const elementDefinition = subModel.find(
`//xs:element[@name="${propertyName}"]`, xsNamespace)[0];
if(elementDefinition) {
//console.log('GED', elementDefinition.toString());
const type = elementDefinition.getAttribute('type')?.value();
//console.log('type', type);
const comment = elementDefinition.childNodes()[1].childNodes()[1].childNodes()[0].toString() + '.';
//console.log('COMMENT', comment);
property.comment = comment;
property.domain = rdfClass.id;
if(type) {
const namespace = type.split(':')[0];
const bareType = type.split(':')[1];
const className = type.split(':')[1];
if(namespace === 'em') {
property.range = className;
} else {
property.range = type;
}
const subTypeModel = subModel.find(
`//xs:complexType[@name="${bareType}"]`, xsNamespace)[0];
if(subTypeModel) {
if(!(className in allClasses)) {
buildRdfClass(subTypeModel);
}
}
}
}
}
});
}
});
}
buildRdfClass(typeModel);
vocabStream.write(`vocab:
- id: fr
value: https://w3id.org/first-responder#
prefix:
- id: aamva_d20
value: http://release.niem.gov/niem/codes/aamva_d20/5.0/#
- id: ag
value: http://release.niem.gov/niem/domains/agriculture/5.2/#
- id: ag_codes
value: http://release.niem.gov/niem/codes/ag_codes/5.0/#
- id: apco
value: http://release.niem.gov/niem/codes/apco/5.0/#
- id: atf
value: http://release.niem.gov/niem/codes/atf/5.0/#
- id: biom
value: http://release.niem.gov/niem/domains/biometrics/5.2/#
- id: bls
value: http://release.niem.gov/niem/codes/bls/5.0/#
- id: can
value: http://release.niem.gov/niem/codes/canada_post/5.0/#
- id: cbrn
value: http://release.niem.gov/niem/domains/cbrn/5.2/#
- id: cbrncl
value: http://release.niem.gov/niem/codes/cbrncl/5.0/#
- id: cli
value: http://reference.niem.gov/niem/specification/code-lists/5.0/code-lists-instance/#
- id: commodity
value: http://release.niem.gov/niem/codes/commodity/5.0/#
- id: cui
value: http://release.niem.gov/niem/auxiliary/cui/5.1/#
- id: cyber
value: http://release.niem.gov/niem/domains/cyber/5.2/#
- id: dea
value: http://release.niem.gov/niem/codes/dea_ctlsub/5.0/#
- id: edxl_rm
value: http://release.niem.gov/niem/codes/edxl_rm/5.0/#
- id: edxl-cap
value: http://release.niem.gov/niem/adapters/edxl-cap/5.0/#
- id: edxl-de
value: http://release.niem.gov/niem/adapters/edxl-de/5.0/#
- id: edxl-have
value: http://release.niem.gov/niem/adapters/edxl-have/5.0/#
- id: em
value: http://release.niem.gov/niem/domains/emergencyManagement/5.2/#
- id: fema
value: http://release.niem.gov/niem/codes/fema/5.1/#
- id: fips
value: http://release.niem.gov/niem/codes/fips/5.2/#
- id: genc
value: http://release.niem.gov/niem/codes/genc/5.0/#
- id: geo
value: http://release.niem.gov/niem/adapters/geospatial/5.1/#
- id: have-codes
value: http://release.niem.gov/niem/codes/edxl_have/5.0/#
- id: hazmat
value: http://release.niem.gov/niem/codes/hazmat/5.0/#
- id: hl7
value: http://release.niem.gov/niem/codes/hl7/5.0/#
- id: hs
value: http://release.niem.gov/niem/domains/humanServices/5.2/#
- id: im
value: http://release.niem.gov/niem/domains/immigration/5.2/#
- id: intel
value: http://release.niem.gov/niem/domains/intelligence/5.2/#
- id: ip
value: http://release.niem.gov/niem/domains/infrastructureProtection/5.0/#
- id: iso_3166
value: http://release.niem.gov/niem/codes/iso_3166-1/5.0/#
- id: iso_4217
value: http://release.niem.gov/niem/codes/iso_4217/5.0/#
- id: iso_639-3
value: http://release.niem.gov/niem/codes/iso_639-3/5.1/#
- id: iso_639-5
value: http://release.niem.gov/niem/codes/iso_639-5/5.2/#
- id: it
value: http://release.niem.gov/niem/domains/internationalTrade/5.2/#
- id: itcodes
value: http://release.niem.gov/niem/codes/it_codes/5.0/#
- id: j
value: http://release.niem.gov/niem/domains/jxdm/7.2/#
- id: jc3iedm
value: http://release.niem.gov/niem/codes/jc3iedm/5.0/#
- id: jp2-0
value: http://release.niem.gov/niem/codes/jp2-0/5.0/#
- id: jp3-52
value: http://release.niem.gov/niem/codes/jp3-52/5.0/#
- id: lrn-dev
value: http://release.niem.gov/niem/domains/learn-dev/5.2/#
- id: m
value: http://release.niem.gov/niem/domains/maritime/5.2/#
- id: mmucc
value: http://release.niem.gov/niem/codes/mmucc/5.0/#
- id: mo
value: http://release.niem.gov/niem/domains/militaryOperations/5.2/#
- id: nc
value: http://release.niem.gov/niem/niem-core/5.0/#
- id: nces
value: http://release.niem.gov/niem/codes/nces/5.0/#
- id: ncic
value: http://release.niem.gov/niem/codes/ncic/5.2/#
- id: ndex
value: http://release.niem.gov/niem/codes/ndex/5.0/#
- id: nga
value: http://release.niem.gov/niem/codes/nga/5.0/#
- id: niem-xs
value: http://release.niem.gov/niem/proxy/niem-xs/5.0/#
- id: nlets
value: http://release.niem.gov/niem/codes/nlets/5.0/#
- id: occs
value: http://release.niem.gov/niem/codes/occs/5.0/#
- id: opm
value: http://release.niem.gov/niem/codes/opm/5.0/#
- id: sar
value: http://release.niem.gov/niem/codes/pmise_sar/5.0/#
- id: scr
value: http://release.niem.gov/niem/domains/screening/5.2/#
- id: st
value: http://release.niem.gov/niem/domains/surfaceTransportation/5.0/#
- id: stat
value: http://release.niem.gov/niem/auxiliary/statistics/5.0/#
- id: stix
value: http://release.niem.gov/niem/codes/stix/5.1/#
- id: structures
value: http://release.niem.gov/niem/structures/5.0/#
- id: ucr
value: http://release.niem.gov/niem/codes/ucr/5.1/#
- id: unece
value: http://release.niem.gov/niem/codes/unece_rec20/5.0/#
- id: usda_fsa
value: http://release.niem.gov/niem/codes/usda_fsa/5.0/#
- id: usmtf
value: http://release.niem.gov/niem/domains/usmtf/5.2/#
- id: usps
value: http://release.niem.gov/niem/codes/usps/5.0/#
- id: xCard
value: http://release.niem.gov/niem/codes/xCard/5.0/#
- id: xml
value: http://www.w3.org/XML/1998/namespace#
- id: xs
value: http://www.w3.org/2001/XMLSchema#
ontology:
- property: dc:title
value: First Responder Verifiable Credentials Specification v0.1
- property: dc:description
value: specification for describing First Responders using Verifiable Credentials that is compatible with NIEMOpen, a community-driven, common vocabulary that enables efficient information exchange across diverse public and private organizations. This document provides an introduction and examples for developers issuing and verifying Verifiable Credentials for use in the First Responder community
- property: rdfs:seeAlso
value: http://niem.github.io/
`);
vocabStream.write('\nclass:');
Object.values(allClasses).forEach(rdfClass => {
vocabStream.write(`
- id: ${rdfClass.id}
label: ${rdfClass.label}
comment: ${rdfClass.comment.replace(/\:/g, '-')}\n`);
});
vocabStream.write('\nproperty:');
Object.values(allProperties).forEach(rdfProperty => {
vocabStream.write(`
- id: ${rdfProperty.id}
label: ${rdfProperty.label}\n`)
if(rdfProperty.domain) {
vocabStream.write(` domain: ${rdfProperty.domain}\n`);
}
if(rdfProperty.range) {
vocabStream.write(` range: ${rdfProperty.range}\n`);
}
if(rdfProperty.comment) {
vocabStream.write(` comment: ${rdfProperty.comment.replace(/\:/g, '-')}\n`);
} else {
vocabStream.write(` comment: ${rdfProperty.label}\n`);
}
});
vocabStream.end();