-
Notifications
You must be signed in to change notification settings - Fork 0
/
KGjs.js
259 lines (229 loc) · 9.81 KB
/
KGjs.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
"strict on";
var uriOf = require('./lib/rdfUris');
var fileLoader = require('./lib/io/fileLoader');
var fetchStatus = require('./lib/fetchStatus');
function ensureDataLoaded (resourceWrapper, graphComponents, callback) {
if (!resourceWrapper.fetchStatus && graphComponents.accessorList) {
var populator = graphComponents.accessorList.getPopulator(resourceWrapper);
if (populator) {
populator.populate(resourceWrapper, graphComponents.dataProcessor, function(populationError) {
if (populationError) {
resourceWrapper.fetchStatus = fetchStatus.fetchAttemptedButFailed
callback(populationError);
} else {
resourceWrapper.fetchStatus = fetchStatus.successfullyFetched;
callback(null);
}
});
} else {
resourceWrapper.fetchStatus = fetchStatus.cannotBeFetched;
callback(null);
}
} else {
callback(null);
}
}
function determineAbsoluteUri (uriOrAliasedName, aliases) {
var result;
if (uriOrAliasedName.indexOf('http:') === 0 || uriOrAliasedName.indexOf('https:') === 0) {
result = uriOrAliasedName;
} else {
var colonIndex = uriOrAliasedName.indexOf(':');
if (colonIndex == -1) {
throw new Error('KGjs - Uri failure - No colon found in aliased name: ' + uriOrAliasedName);
}
var alias = uriOrAliasedName.substring(0, colonIndex);
var aliasPrefix = aliases[alias];
if (!aliasPrefix) {
throw new Error('KGjs - Uri failure - unknown alias: ' + alias);
}
result = aliasPrefix + uriOrAliasedName.substring(colonIndex + 1);
}
return result;
}
function mergeInto(sourceRoot, destRoot) {
if (sourceRoot && sourceRoot.list) {
sourceRoot.list.forEach(function(item) {
destRoot.list.push(item);
});
}
}
function metaProcessing(subject, predicate, object) {
if (predicate.uri == uriOf.owl_sameAs && object.uri) {
// Ensure subject and object have same sharedRoot
if (subject._sharedRoot) {
mergeInto(object._sharedRoot, subject._sharedRoot);
object._sharedRoot = subject._sharedRoot;
} else if (object._sharedRoot) {
mergeInto(subject._sharedRoot, object._sharedRoot);
subject._sharedRoot = object._sharedRoot;
} else {
var sharedRoot = {list:[subject,object]};
subject._sharedRoot = sharedRoot;
object._sharedRoot = sharedRoot;
}
}
}
function processOutgoingResources(wrappedFacts, graphComponents, callback) {
// simulate outgoing pipeline - which is currently to load items.
forEachInArray(wrappedFacts,
function(wrappedFact, itemCallback) {
ensureDataLoaded(wrappedFact.value, graphComponents, itemCallback);
},
function(err) {
callback(err, err ? null : wrappedFacts);
});
}
function newGraph(accessorList) {
var aliases = {};
var resources = {};
var graphComponents = {
dataProcessor: null,
accessorList: accessorList};
var graph = {
defineAlias: function(alias, uri) {
aliases[alias] = uri;
},
add: function(s,p,o) {
s.add_int(p,o);
},
list: function(s,p, callback, isRootLoaded) {
s.list_int(p, callback, isRootLoaded);
},
getOne: function(s, p) {
return s.getOne_int(p);
},
ensureResource: function(uriOrAliasedName) {
var absoluteUri = determineAbsoluteUri(uriOrAliasedName, aliases);
var resourceWrapper = resources[absoluteUri];
if (!resourceWrapper) {
var data = null;
var resourceWrapper = {
id: uriOrAliasedName,
uri: absoluteUri,
add_int: function(predicateResourceWrapper, object) {
if (data === null) {
data = {};
}
var rawResults = data[predicateResourceWrapper.uri];
if (rawResults == null) {
rawResults = [];
data[predicateResourceWrapper.uri] = rawResults;
}
rawResults.push(object);
// Meta processing.
metaProcessing(this, predicateResourceWrapper, object);
},
getOne_int: function(predicateResourceWrapper) {
var result = null;
if (data) {
var list = data[predicateResourceWrapper.id];
if (list && list.length > 0) {
result = list[0];
}
}
return result;
},
list_int: function(predicateResourceWrapper, callback, isRootLoad) {
var thisResource = this;
ensureDataLoaded(this, graphComponents, function(err) {
if (err) {
return callback(err);
}
if (thisResource._sharedRoot && !isRootLoad) {
// use a special algorithm to populate and
// collate results.
performSharedRootList(graph, thisResource, predicateResourceWrapper, graphComponents, function(err, wrappedItems) {
if (err) {
callback(err);
} else {
processOutgoingResources(wrappedItems, graphComponents, callback);
}
});
} else {
// Simple population.
if (data === null) {
callback(null, []);
} else {
var rawResults = data[predicateResourceWrapper.uri];
if (rawResults == null) {
callback(null, []);
} else {
var wrappedResults = rawResults.map(function(rawResult) {
return {value: rawResult};
});
processOutgoingResources(wrappedResults, graphComponents, callback);
}
}
}
});
}
}
resources[absoluteUri] = resourceWrapper;
}
return resourceWrapper;
},
loadFile: function(fileName, callback) {
fileLoader.loadFile(this, fileName, callback);
},
forEachResource: function(resourceCallback, completeCallback) {
var values = Object.keys(resources).map(function(key) { return resources[key]});
forEachInArray(values, resourceCallback, completeCallback);
}
};
graphComponents.dataProcessor = require('./lib/tripleProcessor').newTripleProcessor(graph);
return graph;
}
function forEachInArrayImpl(index, array, itemCallback, completeCallback) {
if (index < array.length) {
itemCallback(array[index], function(err) {
if (err) {
completeCallback(err);
} else {
forEachInArrayImpl(++index, array, itemCallback, completeCallback);
}
});
} else {
completeCallback(null);
}
}
function forEachInArray(array, itemCallback, completeCallback) {
forEachInArrayImpl(0, array, itemCallback, completeCallback);
}
function performSharedRootList(graph, targetResource, predicateResourceWrapper, graphComponents, callback) {
// We move through the shared root list, populating and collating answers as we go.
// Our list iterate will process items added to the array during processing.
var sharedRoot = targetResource._sharedRoot;
var collatedWrappedAnswers = [];
forEachInArray(sharedRoot.list,
function resourceCallback(resource, itemCallback) {
// Loading will force population of this resource.
// If we find a sameAs link, it will be added to the sharedRoot.list
graph.list(resource, predicateResourceWrapper, function(err, wrappedResults) {
if (err) {
itemCallback(null); // we don't let error stop process.
} else {
// collate
// TODO - remove duplicates
wrappedResults.forEach(function(wrappedResult) {
collatedWrappedAnswers.push(wrappedResult);
});
itemCallback(null);
}
},
true); // true == isRootList load
},
function completeCallback(err) {
if (err) {
callback(err, null);
} else {
callback(null, collatedWrappedAnswers);
}
}
);
}
module.exports.newGraph = newGraph;
module.exports.allAccessors = require('./lib/accessors/dataAccessorList').all;
module.exports.newAccessorList = require('./lib/accessors/dataAccessorList').newList;
module.exports.uriFor = require('./lib/rdfUris');
module.exports.semanticQuery = require('./lib/io/semanticQuery');