-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathnodeMap.js
291 lines (264 loc) · 8.22 KB
/
nodeMap.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2017 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';
const {isKeyword} = require('./context');
const graphTypes = require('./graphTypes');
const types = require('./types');
const util = require('./util');
const JsonLdError = require('./JsonLdError');
const api = {};
module.exports = api;
/**
* Creates a merged JSON-LD node map (node ID => node).
*
* @param input the expanded JSON-LD to create a node map of.
* @param [options] the options to use:
* [issuer] a jsonld.IdentifierIssuer to use to label blank nodes.
*
* @return the node map.
*/
api.createMergedNodeMap = (input, options) => {
options = options || {};
// produce a map of all subjects and name each bnode
const issuer = options.issuer || new util.IdentifierIssuer('_:b');
const graphs = {'@default': {}};
api.createNodeMap(input, graphs, '@default', issuer);
// add all non-default graphs to default graph
return api.mergeNodeMaps(graphs);
};
/**
* Recursively flattens the subjects in the given JSON-LD expanded input
* into a node map.
*
* @param input the JSON-LD expanded input.
* @param graphs a map of graph name to subject map.
* @param graph the name of the current graph.
* @param issuer the blank node identifier issuer.
* @param name the name assigned to the current input if it is a bnode.
* @param list the list to append to, null for none.
*/
api.createNodeMap = (input, graphs, graph, issuer, name, list) => {
// recurse through array
if(types.isArray(input)) {
for(const node of input) {
api.createNodeMap(node, graphs, graph, issuer, undefined, list);
}
return;
}
// add non-object to list
if(!types.isObject(input)) {
if(list) {
list.push(input);
}
return;
}
// add values to list
if(graphTypes.isValue(input)) {
if('@type' in input) {
let type = input['@type'];
// rename @type blank node
if(type.indexOf('_:') === 0) {
input['@type'] = type = issuer.getId(type);
}
}
if(list) {
list.push(input);
}
return;
} else if(list && graphTypes.isList(input)) {
const _list = [];
api.createNodeMap(input['@list'], graphs, graph, issuer, name, _list);
list.push({'@list': _list});
return;
}
// Note: At this point, input must be a subject.
// spec requires @type to be named first, so assign names early
if('@type' in input) {
const types = input['@type'];
for(const type of types) {
if(type.indexOf('_:') === 0) {
issuer.getId(type);
}
}
}
// get name for subject
if(types.isUndefined(name)) {
name = graphTypes.isBlankNode(input) ?
issuer.getId(input['@id']) : input['@id'];
}
// add subject reference to list
if(list) {
list.push({'@id': name});
}
// create new subject or merge into existing one
const subjects = graphs[graph];
const subject = subjects[name] = subjects[name] || {};
subject['@id'] = name;
const properties = Object.keys(input).sort();
for(let property of properties) {
// skip @id
if(property === '@id') {
continue;
}
// handle reverse properties
if(property === '@reverse') {
const referencedNode = {'@id': name};
const reverseMap = input['@reverse'];
for(const reverseProperty in reverseMap) {
const items = reverseMap[reverseProperty];
for(const item of items) {
let itemName = item['@id'];
if(graphTypes.isBlankNode(item)) {
itemName = issuer.getId(itemName);
}
api.createNodeMap(item, graphs, graph, issuer, itemName);
util.addValue(
subjects[itemName], reverseProperty, referencedNode,
{propertyIsArray: true, allowDuplicate: false});
}
}
continue;
}
// recurse into graph
if(property === '@graph') {
// add graph subjects map entry
if(!(name in graphs)) {
graphs[name] = {};
}
api.createNodeMap(input[property], graphs, name, issuer);
continue;
}
// recurse into included
if(property === '@included') {
api.createNodeMap(input[property], graphs, graph, issuer);
continue;
}
// copy non-@type keywords
if(property !== '@type' && isKeyword(property)) {
if(property === '@index' && property in subject &&
(input[property] !== subject[property] ||
input[property]['@id'] !== subject[property]['@id'])) {
throw new JsonLdError(
'Invalid JSON-LD syntax; conflicting @index property detected.',
'jsonld.SyntaxError',
{code: 'conflicting indexes', subject});
}
subject[property] = input[property];
continue;
}
// iterate over objects
const objects = input[property];
// if property is a bnode, assign it a new id
if(property.indexOf('_:') === 0) {
property = issuer.getId(property);
}
// ensure property is added for empty arrays
if(objects.length === 0) {
util.addValue(subject, property, [], {propertyIsArray: true});
continue;
}
for(let o of objects) {
if(property === '@type') {
// rename @type blank nodes
o = (o.indexOf('_:') === 0) ? issuer.getId(o) : o;
}
// handle embedded subject or subject reference
if(graphTypes.isSubject(o) || graphTypes.isSubjectReference(o)) {
// skip null @id
if('@id' in o && !o['@id']) {
continue;
}
// relabel blank node @id
const id = graphTypes.isBlankNode(o) ?
issuer.getId(o['@id']) : o['@id'];
// add reference and recurse
util.addValue(
subject, property, {'@id': id},
{propertyIsArray: true, allowDuplicate: false});
api.createNodeMap(o, graphs, graph, issuer, id);
} else if(graphTypes.isValue(o)) {
// handle @value
util.addValue(
subject, property, o,
{propertyIsArray: true, allowDuplicate: false});
} else if(graphTypes.isList(o)) {
// handle @list
const _list = [];
api.createNodeMap(o['@list'], graphs, graph, issuer, name, _list);
o = {'@list': _list};
util.addValue(
subject, property, o,
{propertyIsArray: true, allowDuplicate: false});
} else {
// handle remaining cases
api.createNodeMap(o, graphs, graph, issuer, name);
util.addValue(
subject, property, o, {propertyIsArray: true, allowDuplicate: false});
}
}
}
};
/**
* Merge separate named graphs into a single merged graph including
* all nodes from the default graph and named graphs.
*
* @param graphs a map of graph name to subject map.
*
* @return the merged graph map.
*/
api.mergeNodeMapGraphs = graphs => {
const merged = {};
for(const name of Object.keys(graphs).sort()) {
for(const id of Object.keys(graphs[name]).sort()) {
const node = graphs[name][id];
if(!(id in merged)) {
merged[id] = {'@id': id};
}
const mergedNode = merged[id];
for(const property of Object.keys(node).sort()) {
if(isKeyword(property) && property !== '@type') {
// copy keywords
mergedNode[property] = util.clone(node[property]);
} else {
// merge objects
for(const value of node[property]) {
util.addValue(
mergedNode, property, util.clone(value),
{propertyIsArray: true, allowDuplicate: false});
}
}
}
}
}
return merged;
};
api.mergeNodeMaps = graphs => {
// add all non-default graphs to default graph
const defaultGraph = graphs['@default'];
const graphNames = Object.keys(graphs).sort();
for(const graphName of graphNames) {
if(graphName === '@default') {
continue;
}
const nodeMap = graphs[graphName];
let subject = defaultGraph[graphName];
if(!subject) {
defaultGraph[graphName] = subject = {
'@id': graphName,
'@graph': []
};
} else if(!('@graph' in subject)) {
subject['@graph'] = [];
}
const graph = subject['@graph'];
for(const id of Object.keys(nodeMap).sort()) {
const node = nodeMap[id];
// only add full subjects
if(!graphTypes.isSubjectReference(node)) {
graph.push(node);
}
}
}
return defaultGraph;
};