-
Notifications
You must be signed in to change notification settings - Fork 9
/
recon.js
315 lines (284 loc) · 11.4 KB
/
recon.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// ========================================================================
// Copyright (c) 2008-2009, Metaweb Technologies, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY METAWEB TECHNOLOGIES AND CONTRIBUTORS
// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL METAWEB
// TECHNOLOGIES OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
// ========================================================================
/*
** Automatic reconciliation
*/
var manualQueue = {};
var automaticQueue = [];
function beginAutoReconciliation() {
$(".nowReconciling").show();
$(".notReconciling").hide();
$("#gettingInput").remove();
reconciliationBegun = true;
autoReconcile();
}
function finishedAutoReconciling() {
$(".nowReconciling").hide();
$('.notReconciling').show();
}
function autoReconcile() {
if (automaticQueue.length == 0) {
finishedAutoReconciling();
return;
}
updateUnreconciledCount();
getCandidates(automaticQueue[0], autoReconcileResults);
}
function constructReconciliationQuery(entity, typeless) {
function constructQueryPart(value) {
if (value.id != undefined && value.id != "" && value.id != "None")
return {"id":value.id, "name":value["/type/object/name"]}
if (value['/rec_ui/id'] !== undefined)
return value["/type/object/name"];
return value;
}
var query = {}
var headers = entity["/rec_ui/headers"];
for (var i = 0; i < headers.length; i++) {
var prop = headers[i];
var parts = prop.split(":");
$.each($.makeArray(entity.getChainedProperty(prop)),function(j, value) {
var slot = query;
if (value == undefined || value == "")
return;
if (parts.length === 1){
slot[prop] = slot[prop] || [];
slot[prop][j] = constructQueryPart(value);
return;
}
slot[parts[0]] = slot[parts[0]] || [];
slot[parts[0]][j] = slot[parts[0]][j] || {};
slot = slot[parts[0]][j];
$.each(parts.slice(1,parts.length-1), function(k,part) {
slot[part] = slot[part] || {};
slot = slot[part];
});
var lastPart = parts[parts.length-1];
slot[lastPart] = constructQueryPart(value);
})
}
if (typeless || !query['/type/object/type'])
query['/type/object/type'] = ['/common/topic'];
return query;
}
function getCandidates(entity, callback,typeless) {
function handler(results) {
entity.reconResults = results;
callback(entity);
}
var defaultLimit = 4;
var limit = defaultLimit;
if (entity.reconResults)
limit = entity.reconResults.length * 2;
if (!entity.typelessRecon && typeless){
entity.typelessRecon = true;
limit = defaultLimit;
}
var query = constructReconciliationQuery(entity,typeless);
$.getJSON(reconciliation_url + "query?jsonp=?", {q:JSON.stringify(query), limit:limit}, handler);
}
function autoReconcileResults(entity) {
automaticQueue.shift();
// no results, set to None:
if(entity.reconResults.length == 0) {
if (!entity.typelessRecon)
getCandidates(entity,autoReconcileResults,true);
entity["id"] = "None";
warn("No candidates found for the object:");
warn(entity);
addColumnRecCases(entity);
}
// match found:
else if(entity.reconResults[0]["match"] == true) {
entity.id = entity.reconResults[0].id;
canonicalizeFreebaseId(entity);
entity["/rec_ui/freebase_name"] = entity.reconResults[0].name;
addColumnRecCases(entity);
}
else {
var wasEmpty = isObjectEmpty(manualQueue);
manualQueue[entity["/rec_ui/id"]] = entity;
if (wasEmpty)
manualReconcile();
}
autoReconcile();
}
/*
** Manual Reconciliation
*/
function manualReconcile() {
if ($(".manualReconChoices:visible").length === 0) {
var val = getFirstValue(manualQueue);
if(val != undefined) {
$.historyLoad(val["/rec_ui/id"])
renderReconChoices(getSecondValue(manualQueue)); //render-ahead the next one
}
else{
$(".manualQueueEmpty").show();
$(".manualReconciliation").hide();
}
}
}
function displayReconChoices(entityID) {
var entity = entities[entityID];
if (entity === undefined) return;
$(".manualQueueEmpty").hide();
$(".manualReconciliation").show();
if (! $("#manualReconcile" + entityID)[0])
renderReconChoices(entity);
$(".manualReconChoices:visible").remove();
$("#manualReconcile" + entityID).show();
}
function renderReconChoices(entity) {
if (entity == undefined) return;
var template = $("#manualReconcileTemplate").clone();
template[0].id = "manualReconcile" + entity['/rec_ui/id'];
var headers = entity["/rec_ui/headers"];
var mqlProps = entity["/rec_ui/mql_props"];
var currentRecord = $(".recordVals",template);
for(var i = 0; i < headers.length; i++) {
currentRecord.append(node("label", headers[i] + ":", {"for":idToClass(headers[i])}));
currentRecord.append(node("div").append(displayValue(entity.getChainedProperty(headers[i]))));
}
var tableHeader = $(".reconciliationCandidates table thead", template).empty();
var columnHeaders = ["","Image","Names","Types"].concat(mqlProps).concat(["Score"]);
for (var i = 0; i < columnHeaders.length; i++)
tableHeader.append(node("th",columnHeaders[i]));
var tableBody = $(".reconciliationCandidates table tbody", template).empty();
for (var i = 0; i < entity.reconResults.length; i++){
tableBody.append(renderCandidate(entity.reconResults[i], mqlProps, entity));
fetchMqlProps(entity.reconResults[i], entity);
}
function updateCandidates() {
$('.reconciliationCandidates table tbody tr:odd', template).addClass('odd');
$('.reconciliationCandidates table tbody tr:even', template).addClass('even');
numCandidates = entity.reconResults.length;
}
updateCandidates();
$(".find_topic", template)
.suggest({type:entity['/type/object/type'],
type_strict:"should",
flyout:true})
.bind("fb-select", function(e, data) {
entity['/rec_ui/freebase_name'] = $.makeArray(data.name);
handleReconChoice(entity, data.id);
});
$(".otherSelection", template).click(function() {handleReconChoice(entity, this.name)});
$(".moreButton",template).click(function() {
$(".loadingMoreCandidates", template).fadeIn();
getCandidates(entity, function() {
$(".loadingMoreCandidates", template).hide();
if (entity.reconResults.length <= numCandidates)
return $(".moreButton",template).fadeOut();
for (var i = numCandidates; i < entity.reconResults.length; i++){
var candidate = renderCandidate(entity.reconResults[i], mqlProps, entity).hide().appendTo(tableBody);
candidate.fadeIn();
fetchMqlProps(entity.reconResults[i], entity);
}
updateCandidates();
});
});
template.insertAfter("#manualReconcileTemplate")
}
function renderCandidate(result, mqlProps, entity) {
var url = freebase_url + "/view/" + result['id'];
var tableRow = node("tr", {"class":idToClass(result["id"])});
var button = node("button", "Select",
{"class":'manualSelection',
"name":result.id})
tableRow.append(node("td",button));
button.click(function(val) {entity['/rec_ui/freebase_name'] = result.name; handleReconChoice(entity, result.id)})
node("td",
node("img",{src:freebase_url + "/api/trans/image_thumb/"+result['id']+"?maxwidth=100&maxheight=100"})
).appendTo(tableRow);
tableRow.append(node("td",displayValue(result)));
tableRow.append(node("td",displayValue(result.type)));
for(var j = 0; j < mqlProps.length; j++)
tableRow.append(
node("td", node("img",{src:"spinner.gif"}),
{"class":"replaceme "+idToClass(mqlProps[j])})
);
tableRow.append(node("td",result["score"]));
return tableRow;
}
function fetchMqlProps(reconResult, entity) {
var mqlProps = entity["/rec_ui/mql_props"];
var query = {"id":reconResult["id"]};
$.each(mqlProps, function(_, prop) {
var slot = query;
var parts = prop.split(":");
$.each(parts.slice(0,parts.length-1), function(_, part) {
slot[part] = slot[part] || [{optional:true}];
slot = slot[part][0];
});
var lastPart = parts[parts.length-1];
if (isValueProperty(lastPart))
slot[lastPart] = [];
else
slot[lastPart] = [{"name":null,"id":null,"optional":true}];
})
var envelope = {query:query};
function handler(results) {
fillInMQLProps(entity, results);
//don't show annoying loading symbols indefinitely if there's an error
$("#manualReconcile" + entity["/rec_ui/id"] + " .replaceme").empty();
}
freebase.mqlRead(envelope,handler);
}
function fillInMQLProps(entity, mqlResult) {
var context = $("#manualReconcile" + entity["/rec_ui/id"]);
if (!mqlResult || mqlResult["code"] != "/api/status/ok" || mqlResult["result"] == null) {
error(mqlResult);
return;
}
var result = mqlResult.result;
var entity = $("tr." + idToClass(result.id),context);
for (var i = 0; i < mqlProps.length; i++) {
var cell = $("td." + idToClass(mqlProps[i]), entity).empty();
cell.append(displayValue(getChainedProperty(result, mqlProps[i])));
cell.removeClass("replaceme");
}
}
function handleReconChoice(entity,freebaseId) {
delete manualQueue[entity["/rec_ui/id"]];
$("#manualReconcile" + entity['/rec_ui/id']).remove();
if (freebaseId != undefined){
entity.id = freebaseId;
canonicalizeFreebaseId(entity);
}
addColumnRecCases(entity);
updateUnreconciledCount();
manualReconcile();
}
function canonicalizeFreebaseId(entity) {
var envelope = {query:{"myId:id":entity.id, "id":null}}
freebase.mqlRead(envelope, function(results){
if (results && results.result && results.result.id)
entity.id = results.result.id
});
}