-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (230 loc) · 8.6 KB
/
index.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
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
const prefixes = {
rr: 'http://www.w3.org/ns/r2rml#',
rml: 'http://semweb.mmlab.be/ns/rml#',
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
rdfs: 'http://www.w3.org/2000/01/rdf-schema#'
};
const TERMS = {
a: prefixes.rdf + 'type'
};
let counter = 0;
/**
* normalize RML triples in the store, by modifying the existing store, and then write them semi-pretty using the writer.
* @param store N3.Store
* @param writer N3.Writer
*/
function writeNormalizeRml(store, writer, useBlankNodes = true) {
normalizeRml(store, function () {
if (useBlankNodes) {
let parsedBlankSubjects = {};
writeBlanks(store, parsedBlankSubjects);
let triples = store.getTriples();
triples.forEach(function (triple) {
if (parsedBlankSubjects[triple.subject]) {
return;
}
if (parsedBlankSubjects[triple.object]) {
writer.addTriple(triple.subject, triple.predicate, writer.blank(parsedBlankSubjects[triple.object]));
return;
}
writer.addTriple(triple);
});
} else {
writer.addTriples(store.getTriples());
}
}, useBlankNodes);
}
/**
* normalize RML triples in the store, by modifying the existing store, callback cb is fired when done.
* @param store
* @param cb
*/
function normalizeRml(store, cb, useBlankNodes = true) {
let triples;
//subject
triples = store.getTriples(null, prefixes.rr + 'subject', null);
store.removeTriples(triples);
triples.forEach(function (triple) {
let subject;
if (useBlankNodes) {
subject = store.createBlankNode();
} else {
subject = _getNewIRI(triple.subject);
}
store.addTriple(triple.subject, prefixes.rr + 'subjectMap', subject);
store.addTriple(subject, prefixes.rr + 'constant', triple.object);
});
//class
triples = store.getTriples(null, prefixes.rr + 'class', null);
store.removeTriples(triples);
triples.forEach(function (triple) {
let poMap;
if (useBlankNodes) {
poMap = store.createBlankNode();
} else {
poMap = _getNewIRI(triple.subject);
}
let tripleMaps = store.getTriples(null, prefixes.rr + 'subjectMap', triple.subject);
tripleMaps.forEach(function(tripleMap) {
store.addTriple(tripleMap.subject, prefixes.rr + 'predicateObjectMap', poMap);
let pMap;
let oMap;
if (useBlankNodes) {
pMap = store.createBlankNode();
oMap = store.createBlankNode();
} else {
pMap = _getNewIRI(tripleMap.subject);
oMap = _getNewIRI(tripleMap.subject);
}
store.addTriple(poMap, prefixes.rr + 'predicateMap', pMap);
store.addTriple(pMap, prefixes.rr + 'constant', TERMS.a);
store.addTriple(poMap, prefixes.rr + 'objectMap', oMap);
store.addTriple(oMap, prefixes.rr + 'constant', triple.object);
});
});
//predicate
triples = store.getTriples(null, prefixes.rr + 'predicate', null);
store.removeTriples(triples);
triples.forEach(function (triple) {
let pMap;
if (useBlankNodes) {
pMap = store.createBlankNode();
} else {
pMap = _getNewIRI(triple.subject);}
store.addTriple(triple.subject, prefixes.rr + 'predicateMap', pMap);
store.addTriple(pMap, prefixes.rr + 'constant', triple.object);
});
//object
triples = store.getTriples(null, prefixes.rr + 'object', null);
store.removeTriples(triples);
triples.forEach(function (triple) {
let pMap;
if (useBlankNodes) {
pMap = store.createBlankNode();
} else {
pMap = _getNewIRI(triple.subject);
}
store.addTriple(triple.subject, prefixes.rr + 'objectMap', pMap);
store.addTriple(pMap, prefixes.rr + 'constant', triple.object);
});
// logicalTable
triples = store.getTriples(null, prefixes.rr + 'logicalTable', null);
triples.forEach(function (triple) {
store.addTriple(triple.subject, 'a', prefixes.rr + 'TriplesMap');
store.addTriple(triple.object, 'a', prefixes.rr + 'LogicalTable');
});
// subjectMap
triples = store.getTriples(null, prefixes.rr + 'subjectMap', null);
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'TriplesMap');
store.addTriple(triple.object, TERMS.a, prefixes.rr + 'SubjectMap');
});
// poMap
triples = store.getTriples(null, prefixes.rr + 'predicateObjectMap', null);
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'TriplesMap');
store.addTriple(triple.object, TERMS.a, prefixes.rr + 'PredicateObjectMap');
});
// pMap
triples = store.getTriples(null, prefixes.rr + 'predicateMap', null);
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'PredicateObjectMap');
store.addTriple(triple.object, TERMS.a, prefixes.rr + 'PredicateMap');
});
// oMap
triples = store.getTriples(null, prefixes.rr + 'objectMap', null);
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'PredicateObjectMap');
store.addTriple(triple.object, TERMS.a, prefixes.rr + 'ObjectMap');
});
// refOMap
triples = store.getTriples(null, prefixes.rr + 'parentTriplesMap', null);
triples.forEach(function (triple) {
store.removeTriple(triple.subject, TERMS.a, prefixes.rr + 'ObjectMap');
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'RefObjectMap');
store.addTriple(triple.object, TERMS.a, prefixes.rr + 'TriplesMap');
});
//graphMap
triples = store.getTriples(null, prefixes.rr + 'graphMap', null);
triples.forEach(function (triple) {
store.addTriple(triple.object, TERMS.a, prefixes.rr + 'GraphMap');
});
//termmaps
triples = store.getTriples(null, TERMS.a, prefixes.rr + 'SubjectMap');
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'TermMap');
});
triples = store.getTriples(null, TERMS.a, prefixes.rr + 'PredicateMap');
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'TermMap');
});
triples = store.getTriples(null, TERMS.a, prefixes.rr + 'ObjectMap');
triples.forEach(function (triple) {
store.addTriple(triple.subject, TERMS.a, prefixes.rr + 'TermMap');
});
// TODO graphMap
//IRI or Literal
// triples = store.getTriples(null, TERMS.a, prefixes.rr + 'ObjectMap');
// triples.forEach(function (triple) {
// let termtypetriples = store.getTriples(triple.subject, prefixes.rr + 'termType', null);
// if (termtypetriples.length === 0) {
// store.addTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'IRI');
// }
// });
//column
triples = store.getTriples(null, prefixes.rr + 'column', null);
triples.forEach(function (triple) {
store.removeTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'IRI');
store.addTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'Literal');
});
//language
triples = store.getTriples(null, prefixes.rr + 'language', null);
triples.forEach(function (triple) {
store.removeTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'IRI');
store.addTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'Literal');
});
//datatype
triples = store.getTriples(null, prefixes.rr + 'datatype', null);
triples.forEach(function (triple) {
store.removeTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'IRI');
store.addTriple(triple.subject, prefixes.rr + 'termType', prefixes.rr + 'Literal');
});
cb();
}
function writeBlanks(store, blanks) {
let triples = store.getTriples(null, TERMS.a, prefixes.rr + 'ObjectMap');
triples.forEach(function (triple) {
let oMaps = store.getTriples(null, prefixes.rr + 'objectMap', triple.subject);
if (oMaps.length > 1) {
return;
}
let myBlanks = [];
let oStuff = store.getTriples(triple.subject, null, null);
oStuff.forEach(function (oTriple) {
myBlanks.push({predicate: oTriple.predicate, object: oTriple.object});
});
blanks[triple.subject] = myBlanks;
});
triples = store.getTriples(null, TERMS.a, prefixes.rr + 'PredicateMap');
triples.forEach(function (triple) {
let pMaps = store.getTriples(null, prefixes.rr + 'predicateMap', triple.subject);
if (pMaps.length > 1) {
return;
}
let myBlanks = [];
let oStuff = store.getTriples(triple.subject, null, null);
oStuff.forEach(function (oTriple) {
myBlanks.push({predicate: oTriple.predicate, object: oTriple.object});
});
blanks[triple.subject] = myBlanks;
});
}
function _getNewIRI(baseIRI) {
const temp = counter;
counter ++;
return `${baseIRI}-${temp}`;
}
module.exports = {
normalizeRml: normalizeRml,
writeNormalizeRml: writeNormalizeRml
};