Implemented triple loader preview.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@97 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-16 23:32:12 +00:00
parent aa530395d2
commit e6a98f23bd
3 changed files with 336 additions and 12 deletions

View File

@ -14,6 +14,7 @@ import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.protograph.Protograph; import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.protograph.transpose.MqlreadLikeTransposedNodeFactory; import com.metaweb.gridworks.protograph.transpose.MqlreadLikeTransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.Transposer; import com.metaweb.gridworks.protograph.transpose.Transposer;
import com.metaweb.gridworks.protograph.transpose.TripleLoaderTransposedNodeFactory;
public class PreviewProtographCommand extends Command { public class PreviewProtographCommand extends Command {
@Override @Override
@ -27,14 +28,32 @@ public class PreviewProtographCommand extends Command {
JSONObject json = jsonStringToObject(jsonString); JSONObject json = jsonStringToObject(jsonString);
Protograph protograph = Protograph.reconstruct(json); Protograph protograph = Protograph.reconstruct(json);
MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory(); StringBuffer sb = new StringBuffer();
sb.append("{ ");
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory); {
TripleLoaderTransposedNodeFactory nodeFactory = new TripleLoaderTransposedNodeFactory();
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory);
sb.append("\"tripleloader\" : ");
sb.append(JSONObject.quote(nodeFactory.getLoad()));
}
JSONArray results = nodeFactory.getJSON(); {
MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory();
respond(response, "{ \"result\" : " + results.toString() + " }");
Transposer.transpose(project, protograph, protograph.getRootNode(0), nodeFactory);
JSONArray results = nodeFactory.getJSON();
sb.append(", \"mqllike\" : ");
sb.append(results.toString());
}
sb.append(" }");
respond(response, sb.toString());
} catch (Exception e) { } catch (Exception e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -0,0 +1,297 @@
package com.metaweb.gridworks.protograph.transpose;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONObject;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Recon;
import com.metaweb.gridworks.protograph.AnonymousNode;
import com.metaweb.gridworks.protograph.CellKeyNode;
import com.metaweb.gridworks.protograph.CellNode;
import com.metaweb.gridworks.protograph.CellTopicNode;
import com.metaweb.gridworks.protograph.CellValueNode;
import com.metaweb.gridworks.protograph.FreebaseProperty;
import com.metaweb.gridworks.protograph.FreebaseTopicNode;
import com.metaweb.gridworks.protograph.ValueNode;
public class TripleLoaderTransposedNodeFactory implements TransposedNodeFactory {
protected List<WritingTransposedNode> rootNodes = new LinkedList<WritingTransposedNode>();
protected StringBuffer stringBuffer;
public String getLoad() {
stringBuffer = new StringBuffer();
for (WritingTransposedNode node : rootNodes) {
node.write(null, null);
}
return stringBuffer.toString();
}
protected void writeLine(String line) {
if (stringBuffer.length() > 0) {
stringBuffer.append('\n');
}
stringBuffer.append(line);
}
protected void writeLine(String subject, String predicate, String object) {
if (subject != null) {
writeLine("{ 's' : '" + subject + "', 'p' : '" + predicate + "', 'o' : " + object + " }");
}
}
protected interface WritingTransposedNode extends TransposedNode {
public String write(String subject, String predicate);
}
abstract protected class TransposedNodeWithChildren implements WritingTransposedNode {
public List<FreebaseProperty> properties = new LinkedList<FreebaseProperty>();
public List<WritingTransposedNode> children = new LinkedList<WritingTransposedNode>();
protected void writeChildren(String subject) {
for (int i = 0; i < children.size(); i++) {
WritingTransposedNode child = children.get(i);
String predicate = properties.get(i).id;
child.write(subject, predicate);
}
}
}
protected class AnonymousTransposedNode extends TransposedNodeWithChildren {
AnonymousNode node;
protected AnonymousTransposedNode(
AnonymousNode node
) {
this.node = node;
}
public String write(String subject, String predicate) {
if (children.size() == 0 || subject == null) {
return null;
}
StringBuffer sb = new StringBuffer();
sb.append("{ ");
boolean first = true;
for (int i = 0; i < children.size(); i++) {
String s = children.get(i).write(null, null);
if (s != null) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append("\"" + properties.get(i).id + "\": ");
sb.append(s);
}
}
sb.append(" }");
writeLine(subject, predicate, sb.toString());
return null;
}
}
protected class CellTopicTransposedNode extends TransposedNodeWithChildren {
protected CellTopicNode node;
protected Cell cell;
public CellTopicTransposedNode(CellTopicNode node, Cell cell) {
this.node = node;
this.cell = cell;
}
public String write(String subject, String predicate) {
String id = null;
if (cell.recon != null &&
cell.recon.judgment == Recon.Judgment.Matched &&
cell.recon.match != null) {
id = cell.recon.match.topicID;
} else {
id = "$" + node.columnName.replaceAll("\\W", "_");
writeLine("{ 's' : '" + id + "', 'p' : 'type', 'o' : '" + node.type.id + "' }");
writeLine("{ 's' : '" + id + "', 'p' : 'name', 'o' : " + JSONObject.quote(cell.value.toString()) + " }");
}
writeChildren(id);
return JSONObject.quote(id);
}
}
protected class CellValueTransposedNode implements WritingTransposedNode {
protected JSONObject obj;
protected CellValueNode node;
protected Cell cell;
public CellValueTransposedNode(CellValueNode node, Cell cell) {
this.node = node;
this.cell = cell;
}
public String write(String subject, String predicate) {
String object = cell.value instanceof String ?
JSONObject.quote((String) cell.value) : cell.value.toString();
if ("/type/text".equals(node.lang)) {
writeLine(
"{ 's' : '" + subject +
"', 'p' : '" + predicate +
"', 'o' : " + object +
", 'lang' : '" + node.lang +
"' }"
);
} else {
writeLine(
"{ 's' : '" + subject +
"', 'p' : '" + predicate +
"', 'o' : " + object + " }"
);
}
return object;
}
}
protected class CellKeyTransposedNode implements WritingTransposedNode {
protected CellKeyNode node;
protected Cell cell;
public CellKeyTransposedNode(CellKeyNode node, Cell cell) {
this.node = node;
this.cell = cell;
}
public String write(String subject, String predicate) {
writeLine(subject, "key", JSONObject.quote(node.namespace.id + "/" + cell.value));
return null;
}
}
protected class TopicTransposedNode extends TransposedNodeWithChildren {
protected FreebaseTopicNode node;
public TopicTransposedNode(FreebaseTopicNode node) {
this.node = node;
}
public String write(String subject, String predicate) {
String object = JSONObject.quote(node.topic.id);
writeLine(subject, predicate, object);
writeChildren(object);
return object;
}
}
protected class ValueTransposedNode implements WritingTransposedNode {
protected ValueNode node;
public ValueTransposedNode(ValueNode node) {
this.node = node;
}
public String write(String subject, String predicate) {
String object = node.value instanceof String ?
JSONObject.quote((String) node.value) : node.value.toString();
if ("/type/text".equals(node.lang)) {
writeLine(
"{ 's' : '" + subject +
"', 'p' : '" + predicate +
"', 'o' : " + object +
", 'lang' : '" + node.lang +
"' }"
);
} else {
writeLine(
"{ 's' : '" + subject +
"', 'p' : '" + predicate +
"', 'o' : " + object + " }"
);
}
return object;
}
}
public TransposedNode transposeAnonymousNode(
TransposedNode parentNode,
FreebaseProperty property,
AnonymousNode node) {
WritingTransposedNode tnode = new AnonymousTransposedNode(node);
processTransposedNode(tnode, parentNode, property);
return tnode;
}
public TransposedNode transposeCellNode(
TransposedNode parentNode,
FreebaseProperty property,
CellNode node,
Cell cell) {
WritingTransposedNode tnode = null;
if (node instanceof CellTopicNode) {
tnode = new CellTopicTransposedNode((CellTopicNode) node, cell);
} else if (node instanceof CellValueNode) {
tnode = new CellValueTransposedNode((CellValueNode) node, cell);
} else if (node instanceof CellKeyNode) {
tnode = new CellKeyTransposedNode((CellKeyNode) node, cell);
}
if (tnode != null) {
processTransposedNode(tnode, parentNode, property);
}
return tnode;
}
public TransposedNode transposeTopicNode(
TransposedNode parentNode,
FreebaseProperty property,
FreebaseTopicNode node) {
WritingTransposedNode tnode = new TopicTransposedNode(node);
processTransposedNode(tnode, parentNode, property);
return tnode;
}
public TransposedNode transposeValueNode(
TransposedNode parentNode,
FreebaseProperty property,
ValueNode node) {
WritingTransposedNode tnode = new ValueTransposedNode(node);
processTransposedNode(tnode, parentNode, property);
return tnode;
}
protected void processTransposedNode(
WritingTransposedNode tnode,
TransposedNode parentNode,
FreebaseProperty property
) {
if (parentNode != null) {
if (parentNode instanceof TransposedNodeWithChildren) {
TransposedNodeWithChildren parentNode2 = (TransposedNodeWithChildren) parentNode;
parentNode2.children.add(tnode);
parentNode2.properties.add(property);
}
} else {
rootNodes.add(tnode);
}
}
}

View File

@ -173,12 +173,16 @@ SchemaAlignmentDialog.prototype._constructBody = function(body) {
'<div id="schema-alignment-tabs">' + '<div id="schema-alignment-tabs">' +
'<ul>' + '<ul>' +
'<li><a href="#schema-alignment-tabs-protograph">Protograph</a></li>' + '<li><a href="#schema-alignment-tabs-protograph">Protograph</a></li>' +
'<li><a href="#schema-alignment-tabs-preview">Preview</a></li>' + '<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>' +
'</ul>' + '</ul>' +
'<div id="schema-alignment-tabs-protograph">' + '<div id="schema-alignment-tabs-protograph">' +
'<div class="schema-alignment-dialog-canvas"></div>' + '<div class="schema-alignment-dialog-canvas"></div>' +
'</div>' + '</div>' +
'<div id="schema-alignment-tabs-preview" style="display: none;">' + '<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;">' +
'<div class="schema-alignment-dialog-preview"></div>' + '<div class="schema-alignment-dialog-preview"></div>' +
'</div>' + '</div>' +
'</div>' '</div>'
@ -189,7 +193,8 @@ SchemaAlignmentDialog.prototype._renderBody = function(body) {
var self = this; var self = this;
$("#schema-alignment-tabs").tabs(); $("#schema-alignment-tabs").tabs();
$("#schema-alignment-tabs-preview").css("display", ""); $("#schema-alignment-tabs-preview-mqllike").css("display", "");
$("#schema-alignment-tabs-preview-tripleloader").css("display", "");
this._canvas = $(".schema-alignment-dialog-canvas"); this._canvas = $(".schema-alignment-dialog-canvas");
this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0]; this._nodeTable = $('<table></table>').addClass("schema-alignment-table-layout").appendTo(this._canvas)[0];
@ -206,7 +211,7 @@ SchemaAlignmentDialog.prototype._renderBody = function(body) {
)); ));
} }
this._previewPane = $(".schema-alignment-dialog-preview"); this._previewPanes = $(".schema-alignment-dialog-preview");
}; };
SchemaAlignmentDialog.prototype.getJSON = function() { SchemaAlignmentDialog.prototype.getJSON = function() {
@ -226,15 +231,18 @@ SchemaAlignmentDialog.prototype.getJSON = function() {
SchemaAlignmentDialog.prototype.preview = function() { SchemaAlignmentDialog.prototype.preview = function() {
var self = this; var self = this;
this._previewPane.empty(); this._previewPanes.empty();
var protograph = this.getJSON(); var protograph = this.getJSON();
$.post( $.post(
"/command/preview-protograph?" + $.param({ project: theProject.id }), "/command/preview-protograph?" + $.param({ project: theProject.id }),
{ protograph: JSON.stringify(protograph) }, { protograph: JSON.stringify(protograph) },
function(data) { function(data) {
if ("result" in data) { if ("mqllike" in data) {
self._previewPane.text(JSON.stringify(data.result, null, 2)); $(self._previewPanes[0]).text(JSON.stringify(data.mqllike, null, 2));
}
if ("tripleloader" in data) {
$(self._previewPanes[1]).text(data.tripleloader);
} }
}, },
"json" "json"