2010-02-07 04:36:40 +01:00
|
|
|
var SchemaAlignment = {};
|
|
|
|
|
|
|
|
SchemaAlignment.autoAlign = function() {
|
|
|
|
var protograph = {};
|
|
|
|
|
|
|
|
var columns = theProject.columnModel.columns;
|
|
|
|
|
2010-02-09 00:57:37 +01:00
|
|
|
var typedCandidates = [];
|
2010-02-07 04:36:40 +01:00
|
|
|
var candidates = [];
|
|
|
|
|
|
|
|
for (var c = 0; c < columns.length; c++) {
|
|
|
|
var column = columns[c];
|
2010-04-09 03:00:44 +02:00
|
|
|
var typed = "reconConfig" in column && column.reconConfig !== null;
|
2010-02-09 00:57:37 +01:00
|
|
|
var candidate = {
|
2010-02-07 04:36:40 +01:00
|
|
|
status: "unbound",
|
2010-02-09 00:57:37 +01:00
|
|
|
typed: typed,
|
2010-02-07 04:36:40 +01:00
|
|
|
index: c,
|
|
|
|
column: column
|
2010-02-09 00:57:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
candidates.push(candidate);
|
|
|
|
if (typed) {
|
|
|
|
typedCandidates.push(candidate);
|
|
|
|
}
|
2010-02-07 04:36:40 +01:00
|
|
|
}
|
|
|
|
|
2010-02-09 00:57:37 +01:00
|
|
|
if (typedCandidates.length > 0) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var queries = {};
|
|
|
|
for (var i = 0; i < candidates.length; i++) {
|
|
|
|
var candidate = candidates[i];
|
2010-03-06 08:43:45 +01:00
|
|
|
var name = SchemaAlignment._cleanName(candidate.column.name);
|
2010-02-09 00:57:37 +01:00
|
|
|
var key = "t" + i + ":search";
|
|
|
|
queries[key] = {
|
|
|
|
"query" : name,
|
|
|
|
"limit" : 10,
|
|
|
|
"type" : "/type/type,/type/property",
|
|
|
|
"type_strict" : "any"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
SchemaAlignment._batchSearch(queries, function(result) {
|
|
|
|
console.log(result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignment._batchSearch = function(queries, onDone) {
|
|
|
|
var keys = [];
|
|
|
|
for (var n in queries) {
|
|
|
|
if (queries.hasOwnProperty(n)) {
|
|
|
|
keys.push(n);
|
|
|
|
}
|
|
|
|
}
|
2010-02-07 04:36:40 +01:00
|
|
|
|
2010-02-09 00:57:37 +01:00
|
|
|
var result = {};
|
|
|
|
var args = [];
|
|
|
|
var makeBatch = function(keyBatch) {
|
|
|
|
var batch = {};
|
|
|
|
for (var k = 0; k < keyBatch.length; k++) {
|
|
|
|
var key = keyBatch[k];
|
|
|
|
batch[key] = queries[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
args.push("http://api.freebase.com/api/service/search?" +
|
|
|
|
$.param({ "queries" : JSON.stringify(batch) }) + "&callback=?");
|
|
|
|
|
|
|
|
args.push(null); // no data
|
|
|
|
args.push(function(data) {
|
|
|
|
for (var k = 0; k < keyBatch.length; k++) {
|
|
|
|
var key = keyBatch[k];
|
|
|
|
result[key] = data[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < keys.length; i += 10) {
|
|
|
|
makeBatch(keys.slice(i, i + 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
args.push(function() {
|
|
|
|
onDone(result);
|
2010-04-08 21:52:23 +02:00
|
|
|
});
|
2010-02-09 00:57:37 +01:00
|
|
|
|
|
|
|
Ajax.chainGetJSON.apply(null, args);
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignment._cleanName = function(s) {
|
|
|
|
return s.replace(/\W/g, " ").replace(/\s+/g, " ").toLowerCase();
|
2010-04-08 21:52:23 +02:00
|
|
|
};
|
2010-02-09 01:45:54 +01:00
|
|
|
|
2010-02-19 19:32:48 +01:00
|
|
|
SchemaAlignment.createNewRootNode = function() {
|
|
|
|
var rootNode = null;
|
|
|
|
var links = [];
|
|
|
|
var columns = theProject.columnModel.columns;
|
|
|
|
for (var i = 0; i < columns.length; i++) {
|
|
|
|
var column = columns[i];
|
|
|
|
var target = {
|
|
|
|
nodeType: "cell-as-topic",
|
2010-03-06 08:43:45 +01:00
|
|
|
columnName: column.name,
|
2010-02-19 19:32:48 +01:00
|
|
|
createForNoReconMatch: true
|
|
|
|
};
|
2010-04-09 03:00:44 +02:00
|
|
|
if ("reconConfig" in column && column.reconConfig !== null) {
|
2010-02-19 19:32:48 +01:00
|
|
|
target.type = {
|
|
|
|
id: column.reconConfig.type.id,
|
|
|
|
name: column.reconConfig.type.name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-03-06 08:43:45 +01:00
|
|
|
if (column.name == theProject.columnModel.keyColumnName) {
|
2010-02-19 19:32:48 +01:00
|
|
|
rootNode = target;
|
|
|
|
} else {
|
|
|
|
links.push({
|
|
|
|
property: null,
|
|
|
|
target: target
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rootNode = rootNode || { nodeType: "cell-as-topic" };
|
|
|
|
rootNode.links = links;
|
|
|
|
|
|
|
|
return rootNode;
|
|
|
|
};
|
|
|
|
|
2010-02-11 07:44:48 +01:00
|
|
|
function SchemaAlignmentDialog(protograph, onDone) {
|
|
|
|
this._onDone = onDone;
|
2010-02-12 21:29:56 +01:00
|
|
|
this._originalProtograph = protograph || { rootNodes: [] };
|
2010-02-13 01:11:42 +01:00
|
|
|
this._protograph = cloneDeep(this._originalProtograph); // this is what can be munched on
|
2010-02-09 01:45:54 +01:00
|
|
|
|
2010-04-08 22:16:08 +02:00
|
|
|
if (!this._protograph.rootNodes.length) {
|
2010-02-19 19:32:48 +01:00
|
|
|
this._protograph.rootNodes.push(SchemaAlignment.createNewRootNode());
|
2010-02-12 21:29:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this._nodeUIs = [];
|
2010-02-09 01:45:54 +01:00
|
|
|
this._createDialog();
|
2010-02-16 21:15:19 +01:00
|
|
|
this.preview();
|
2010-04-08 21:52:23 +02:00
|
|
|
}
|
2010-02-09 01:45:54 +01:00
|
|
|
|
|
|
|
SchemaAlignmentDialog.prototype._createDialog = function() {
|
|
|
|
var self = this;
|
|
|
|
var frame = DialogSystem.createDialog();
|
|
|
|
|
|
|
|
frame.width("1000px");
|
|
|
|
|
|
|
|
var header = $('<div></div>').addClass("dialog-header").text("Schema Alignment").appendTo(frame);
|
|
|
|
var body = $('<div></div>').addClass("dialog-body").appendTo(frame);
|
|
|
|
var footer = $('<div></div>').addClass("dialog-footer").appendTo(frame);
|
|
|
|
|
2010-02-16 21:45:04 +01:00
|
|
|
this._constructFooter(footer);
|
|
|
|
this._constructBody(body);
|
2010-02-09 01:45:54 +01:00
|
|
|
|
|
|
|
this._level = DialogSystem.showDialog(frame);
|
2010-02-16 21:45:04 +01:00
|
|
|
|
|
|
|
this._renderBody(body);
|
2010-02-09 01:45:54 +01:00
|
|
|
};
|
|
|
|
|
2010-02-16 21:45:04 +01:00
|
|
|
SchemaAlignmentDialog.prototype._constructFooter = function(footer) {
|
2010-02-09 01:45:54 +01:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$('<button></button>').html(" OK ").click(function() {
|
2010-02-11 07:44:48 +01:00
|
|
|
var protograph = self.getJSON();
|
|
|
|
|
2010-02-26 22:56:41 +01:00
|
|
|
Gridworks.postProcess(
|
|
|
|
"save-protograph",
|
|
|
|
{},
|
2010-02-11 07:44:48 +01:00
|
|
|
{ protograph: JSON.stringify(protograph) },
|
2010-03-04 08:02:03 +01:00
|
|
|
{},
|
2010-02-26 22:56:41 +01:00
|
|
|
{
|
|
|
|
onDone: function() {
|
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
|
|
theProject.protograph = protograph;
|
2010-02-11 07:44:48 +01:00
|
|
|
}
|
2010-02-26 22:56:41 +01:00
|
|
|
}
|
2010-02-11 07:44:48 +01:00
|
|
|
);
|
2010-02-09 01:45:54 +01:00
|
|
|
}).appendTo(footer);
|
|
|
|
|
|
|
|
$('<button></button>').text("Cancel").click(function() {
|
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
|
|
|
}).appendTo(footer);
|
|
|
|
};
|
|
|
|
|
2010-02-16 21:45:04 +01:00
|
|
|
SchemaAlignmentDialog.prototype._constructBody = function(body) {
|
2010-02-12 03:23:26 +01:00
|
|
|
$('<p>' +
|
2010-02-19 23:56:29 +01:00
|
|
|
'The schema alignment skeleton below specifies how the graph-shaped data that will get generated ' +
|
2010-02-12 03:23:26 +01:00
|
|
|
'from your grid-shaped data and written into Freebase. The cells in each record of your data will ' +
|
2010-02-19 23:56:29 +01:00
|
|
|
'get placed into nodes within the skeleton. Configure the skeleton by specifying which ' +
|
2010-02-12 03:23:26 +01:00
|
|
|
'column to substitute into which node. A node can also be an automatically generated ' +
|
|
|
|
'anonymous node, or it can be an explicit value or topic that is the same for all records.' +
|
|
|
|
'</p>').appendTo(body);
|
2010-02-16 21:45:04 +01:00
|
|
|
|
|
|
|
$(
|
2010-04-26 02:10:36 +02:00
|
|
|
'<div id="schema-alignment-tabs" class="gridworks-tabs">' +
|
2010-02-16 21:45:04 +01:00
|
|
|
'<ul>' +
|
2010-02-19 23:56:29 +01:00
|
|
|
'<li><a href="#schema-alignment-tabs-protograph">Skeleton</a></li>' +
|
2010-02-17 00:32:12 +01:00
|
|
|
'<li><a href="#schema-alignment-tabs-preview-mqllike">MQL-like Preview</a></li>' +
|
|
|
|
'<li><a href="#schema-alignment-tabs-preview-tripleloader">TripleLoader Preview</a></li>' +
|
2010-02-16 21:45:04 +01:00
|
|
|
'</ul>' +
|
|
|
|
'<div id="schema-alignment-tabs-protograph">' +
|
|
|
|
'<div class="schema-alignment-dialog-canvas"></div>' +
|
|
|
|
'</div>' +
|
2010-02-17 00:32:12 +01:00
|
|
|
'<div id="schema-alignment-tabs-preview-mqllike" style="display: none;">' +
|
|
|
|
'<div class="schema-alignment-dialog-preview"></div>' +
|
|
|
|
'</div>' +
|
|
|
|
'<div id="schema-alignment-tabs-preview-tripleloader" style="display: none;">' +
|
2010-02-16 21:45:04 +01:00
|
|
|
'<div class="schema-alignment-dialog-preview"></div>' +
|
|
|
|
'</div>' +
|
|
|
|
'</div>'
|
|
|
|
).appendTo(body);
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignmentDialog.prototype._renderBody = function(body) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$("#schema-alignment-tabs").tabs();
|
2010-02-17 00:32:12 +01:00
|
|
|
$("#schema-alignment-tabs-preview-mqllike").css("display", "");
|
|
|
|
$("#schema-alignment-tabs-preview-tripleloader").css("display", "");
|
2010-02-12 03:23:26 +01:00
|
|
|
|
2010-02-16 21:45:04 +01:00
|
|
|
this._canvas = $(".schema-alignment-dialog-canvas");
|
2010-02-10 02:15:25 +01:00
|
|
|
this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
|
2010-02-09 07:46:40 +01:00
|
|
|
|
2010-02-12 21:29:56 +01:00
|
|
|
for (var i = 0; i < this._protograph.rootNodes.length; i++) {
|
2010-02-11 07:44:48 +01:00
|
|
|
this._nodeUIs.push(new SchemaAlignmentDialog.UINode(
|
2010-02-16 21:15:19 +01:00
|
|
|
this,
|
2010-02-10 02:15:25 +01:00
|
|
|
this._protograph.rootNodes[i],
|
2010-02-09 07:46:40 +01:00
|
|
|
this._nodeTable,
|
2010-02-10 02:15:25 +01:00
|
|
|
{
|
|
|
|
expanded: true,
|
|
|
|
mustBeCellTopic: true
|
|
|
|
}
|
2010-02-11 07:44:48 +01:00
|
|
|
));
|
|
|
|
}
|
2010-02-13 02:08:12 +01:00
|
|
|
|
2010-02-17 00:32:12 +01:00
|
|
|
this._previewPanes = $(".schema-alignment-dialog-preview");
|
2010-02-11 07:44:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaAlignmentDialog.prototype.getJSON = function() {
|
|
|
|
var rootNodes = [];
|
|
|
|
for (var i = 0; i < this._nodeUIs.length; i++) {
|
|
|
|
var node = this._nodeUIs[i].getJSON();
|
2010-04-09 03:00:44 +02:00
|
|
|
if (node !== null) {
|
2010-02-11 07:44:48 +01:00
|
|
|
rootNodes.push(node);
|
|
|
|
}
|
2010-02-09 07:46:40 +01:00
|
|
|
}
|
2010-02-11 07:44:48 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
rootNodes: rootNodes
|
|
|
|
};
|
2010-02-09 07:46:40 +01:00
|
|
|
};
|
|
|
|
|
2010-02-16 21:15:19 +01:00
|
|
|
SchemaAlignmentDialog.prototype.preview = function() {
|
|
|
|
var self = this;
|
|
|
|
|
2010-02-17 00:32:12 +01:00
|
|
|
this._previewPanes.empty();
|
2010-02-16 21:15:19 +01:00
|
|
|
|
|
|
|
var protograph = this.getJSON();
|
|
|
|
$.post(
|
|
|
|
"/command/preview-protograph?" + $.param({ project: theProject.id }),
|
2010-05-25 01:01:34 +02:00
|
|
|
{ protograph: JSON.stringify(protograph), engine: JSON.stringify(ui.browsingEngine.getJSON()) },
|
2010-02-16 21:15:19 +01:00
|
|
|
function(data) {
|
2010-02-17 00:32:12 +01:00
|
|
|
if ("mqllike" in data) {
|
|
|
|
$(self._previewPanes[0]).text(JSON.stringify(data.mqllike, null, 2));
|
|
|
|
}
|
|
|
|
if ("tripleloader" in data) {
|
|
|
|
$(self._previewPanes[1]).text(data.tripleloader);
|
2010-02-16 21:15:19 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2010-02-10 02:15:25 +01:00
|
|
|
SchemaAlignmentDialog._findColumn = function(cellIndex) {
|
|
|
|
var columns = theProject.columnModel.columns;
|
|
|
|
for (var i = 0; i < columns.length; i++) {
|
|
|
|
var column = columns[i];
|
|
|
|
if (column.cellIndex == cellIndex) {
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2010-02-09 07:46:40 +01:00
|
|
|
};
|