-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRexUtils.js
More file actions
154 lines (117 loc) · 4.42 KB
/
RexUtils.js
File metadata and controls
154 lines (117 loc) · 4.42 KB
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
var NodeUtils = require('../rdf/NodeUtils');
var HashMap = require('../util/collection/HashMap');
var MappedConceptUtils = require('./MappedConceptUtils');
var BestLabelConfig = require('../sparql/BestLabelConfig');
var StoreFacade = require('./facade/StoreFacade');
var LookupServiceListServiceSparql = require('../service/lookup_service/LookupServiceListServiceSparql');
var RexUtils = {
/**
* TODO Probably not the best place to put this method, but it has to go
* somewhere
*
* @param sparqlService
* @returns {jassa.service.LookupServiceListServiceSparql}
*/
createRexLookupService: function(sparqlService) {
var store = new StoreFacade(sparqlService, {
// 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
// 'llo': 'http://www.linklion.org/ontology#'
});
var bestLiteralConfig = new BestLabelConfig(); //['ja', 'ko', 'en', '']);
var mappedConcept = MappedConceptUtils.createMappedConceptBestLabel(bestLiteralConfig);
var indexResource = function(map) {
var result = {};
var subjects = map.entries();
subjects.forEach(function(entry) {
var subjectIri = entry.key;
var predicates = entry.val.predicates.entries();
var s = result[subjectIri] = {};
predicates.forEach(function(entry) {
var predicateIri = entry.key;
var p = s[predicateIri] = {};
var objects = entry.val.values.entries();
objects.forEach(function(entry) {
var node = entry.val.id;
var json = NodeUtils.toTalisRdfJson(node);
p.push(json);
});
});
});
};
var toMap = function(arr) {
//console.log('ARGH', arr);
var result = new HashMap();
arr.forEach(function(item) {
result.put(item.id, item);
});
return result;
};
var toTalisRdfJson = function(items) {
var result = items.map(function(item) {
var r = NodeUtils.toTalisRdfJson(item.id);
return r;
});
return result;
};
/*
store.addMap({
name: 'spo',
template: [{
id: '?s',
displayLabel: { $ref: { target: mappedConcept, attr: 'displayLabel' }},
predicates: [[{
id: '?p',
displayLabel: { $ref: { target: mappedConcept, attr: 'displayLabel', on: '?p' }},
values: [[{
id: '?o | node',
displayLabel: { $ref: { target: mappedConcept, attr: 'displayLabel', on: '?o' }}
}], toTalisRdfJson],
}], toMap],
}],
from: '?s ?p ?o',
});
*/
var changeIndex = function(triples) {
var spToCount = {};
var result = new HashMap();
//var result = {};
triples.forEach(function(triple) {
var sp = triple.s + ' ' + triple.p;
var i = spToCount[sp] = (spToCount[sp] || 0);
++spToCount[sp];
var json = NodeUtils.toTalisRdfJson(triple.o);
var attrs = Object.keys(json);
attrs.forEach(function(attr) {
var key = {
s: triple.s,
p: triple.p,
i: i,
c: attr,
};
var val = json[attr];
result.put(key, val);
});
});
// var assembled = assembleTalisRdfJson(result);
// console.log('Assembled: ', assembled);
return result;
};
store.addMap({
name: 'spo',
template: [{
id: '?s',
data: [[{
id: '?rowId',
s: '?s',
p: '?p',
o: '?o | node'
}], changeIndex]
}],
from: '?s ?p ?o',
});
var ls = store.spo.getListService();
var lookupService = new LookupServiceListServiceSparql(ls);
return lookupService;
}
};
module.exports = RexUtils;