Added mqlwrite exporter.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@854 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-05-24 23:36:42 +00:00
parent d7f7d50e16
commit 638eb4ac24
9 changed files with 75 additions and 18 deletions

View File

@ -23,6 +23,7 @@ Fixes:
Features:
- Row/record sorting (Issue 32)
- CSV exporting (Issue 59)
- Mqlwrite exporting
Changes:
- Moved unit tests from JUnit to TestNG

View File

@ -7,7 +7,6 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import com.metaweb.gridworks.browsing.Engine;
@ -15,7 +14,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.protograph.transpose.MqlreadLikeTransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.MqlwriteLikeTransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.Transposer;
import com.metaweb.gridworks.protograph.transpose.TripleLoaderTransposedNodeFactory;
import com.metaweb.gridworks.util.ParsingUtilities;
@ -52,14 +51,14 @@ public class PreviewProtographCommand extends Command {
}
{
MqlreadLikeTransposedNodeFactory nodeFactory = new MqlreadLikeTransposedNodeFactory();
StringWriter stringWriter = new StringWriter();
MqlwriteLikeTransposedNodeFactory nodeFactory = new MqlwriteLikeTransposedNodeFactory(stringWriter);
Transposer.transpose(project, filteredRows, protograph, protograph.getRootNode(0), nodeFactory);
JSONArray results = nodeFactory.getJSON();
nodeFactory.flush();
sb.append(", \"mqllike\" : ");
sb.append(results.toString());
sb.append(sb.toString());
}
sb.append(" }");

View File

@ -14,7 +14,7 @@ import org.json.JSONObject;
import com.metaweb.gridworks.ProjectManager;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.exporters.TripleloaderExporter;
import com.metaweb.gridworks.exporters.ProtographTransposeExporter.TripleLoaderExporter;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.util.FreebaseUtils;
@ -28,7 +28,7 @@ public class UploadDataCommand extends Command {
try {
Project project = getProject(request);
Engine engine = getEngine(request, project);
TripleloaderExporter exporter = new TripleloaderExporter();
TripleLoaderExporter exporter = new TripleLoaderExporter();
StringWriter triples = new StringWriter(10 * 1024 * 1024);
exporter.export(project, new Properties(), engine, triples);

View File

@ -16,7 +16,8 @@ import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.exporters.CsvExporter;
import com.metaweb.gridworks.exporters.Exporter;
import com.metaweb.gridworks.exporters.HtmlTableExporter;
import com.metaweb.gridworks.exporters.TripleloaderExporter;
import com.metaweb.gridworks.exporters.ProtographTransposeExporter.TripleLoaderExporter;
import com.metaweb.gridworks.exporters.ProtographTransposeExporter.MqlwriteLikeExporter;
import com.metaweb.gridworks.exporters.TsvExporter;
import com.metaweb.gridworks.exporters.XlsExporter;
import com.metaweb.gridworks.model.Project;
@ -26,10 +27,11 @@ public class ExportRowsCommand extends Command {
static final protected Map<String, Exporter> s_formatToExporter = new HashMap<String, Exporter>();
static {
s_formatToExporter.put("tripleloader", new TripleloaderExporter());
s_formatToExporter.put("html", new HtmlTableExporter());
s_formatToExporter.put("xls", new XlsExporter());
s_formatToExporter.put("csv", new CsvExporter());
s_formatToExporter.put("tripleloader", new TripleLoaderExporter());
s_formatToExporter.put("mqlwrite", new MqlwriteLikeExporter());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)

View File

@ -8,10 +8,18 @@ import java.util.Properties;
import com.metaweb.gridworks.browsing.Engine;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.protograph.Protograph;
import com.metaweb.gridworks.protograph.transpose.MqlwriteLikeTransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.TransposedNodeFactory;
import com.metaweb.gridworks.protograph.transpose.Transposer;
import com.metaweb.gridworks.protograph.transpose.TripleLoaderTransposedNodeFactory;
public class TripleloaderExporter implements Exporter {
abstract public class ProtographTransposeExporter implements Exporter {
final protected String _contentType;
public ProtographTransposeExporter(String contentType) {
_contentType = contentType;
}
public String getContentType() {
return "application/x-unknown";
}
@ -31,11 +39,36 @@ public class TripleloaderExporter implements Exporter {
if (project.protograph != null) {
Protograph protograph = project.protograph;
TripleLoaderTransposedNodeFactory nodeFactory = new TripleLoaderTransposedNodeFactory(writer);
TransposedNodeFactory nodeFactory = createNodeFactory(writer);
Transposer.transpose(project, engine.getAllFilteredRows(), protograph, protograph.getRootNode(0), nodeFactory, -1);
nodeFactory.flush();
}
}
abstract protected TransposedNodeFactory createNodeFactory(Writer writer);
static public class TripleLoaderExporter extends ProtographTransposeExporter {
public TripleLoaderExporter() {
super("application/x-unknown");
}
@Override
protected TransposedNodeFactory createNodeFactory(Writer writer) {
return new TripleLoaderTransposedNodeFactory(writer);
}
}
static public class MqlwriteLikeExporter extends ProtographTransposeExporter {
public MqlwriteLikeExporter() {
super("application/x-unknown");
}
@Override
protected TransposedNodeFactory createNodeFactory(Writer writer) {
return new MqlwriteLikeTransposedNodeFactory(writer);
}
}
}

View File

@ -1,5 +1,7 @@
package com.metaweb.gridworks.protograph.transpose;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;
@ -19,7 +21,8 @@ import com.metaweb.gridworks.protograph.FreebaseTopicNode;
import com.metaweb.gridworks.protograph.ValueNode;
import com.metaweb.gridworks.util.JSONUtilities;
public class MqlreadLikeTransposedNodeFactory implements TransposedNodeFactory {
public class MqlwriteLikeTransposedNodeFactory implements TransposedNodeFactory {
protected Writer writer;
protected List<JSONObject> rootObjects = new LinkedList<JSONObject>();
private static final String TYPE = "type";
@ -30,10 +33,20 @@ public class MqlreadLikeTransposedNodeFactory implements TransposedNodeFactory {
private static final String CONNECT = "connect";
private static final String LANG = "lang";
public JSONArray getJSON() {
public MqlwriteLikeTransposedNodeFactory(Writer writer) {
this.writer = writer;
}
protected JSONArray getJSON() {
return new JSONArray(rootObjects);
}
@Override
public void flush() throws IOException {
writer.write(getJSON().toString());
writer.flush();
}
abstract protected class JsonTransposedNode implements TransposedNode {
abstract public Object getJSON();
}

View File

@ -1,5 +1,7 @@
package com.metaweb.gridworks.protograph.transpose;
import java.io.IOException;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.protograph.AnonymousNode;
import com.metaweb.gridworks.protograph.CellNode;
@ -32,4 +34,6 @@ public interface TransposedNodeFactory {
FreebaseProperty property,
FreebaseTopicNode node
);
public void flush() throws IOException;
}

View File

@ -35,7 +35,8 @@ public class TripleLoaderTransposedNodeFactory implements TransposedNodeFactory
this.writer = writer;
}
public void flush() {
@Override
public void flush() throws IOException {
if (lastRootNode != null) {
lastRootNode.write(null, null, null);
lastRootNode = null;

View File

@ -55,7 +55,11 @@ MenuBar.prototype._initializeUI = function() {
{},
{
"label": "Tripleloader",
"click": function() { self._doExportTripleloader(); }
"click": function() { self._doExportTripleloader("tripleloader"); }
},
{
"label": "MQLWrite",
"click": function() { self._doExportTripleloader("mqlwrite"); }
}
]
},
@ -178,14 +182,14 @@ MenuBar.prototype._doDenormalizeRecords = function() {
);
};
MenuBar.prototype._doExportTripleloader = function() {
MenuBar.prototype._doExportTripleloader = function(format) {
if (!theProject.protograph) {
alert(
"You haven't done any schema alignment yet,\nso there is no triple to export.\n\n" +
"Use the Schemas > Edit Schema Alignment Skeleton...\ncommand to align your data with Freebase schemas first."
);
} else {
this._doExportRows("tripleloader", "txt");
this._doExportRows(format, "txt");
}
};