Restrict QS export to filtered rows

This commit is contained in:
Antonin Delpeuch 2017-09-06 15:45:09 +01:00
parent 9fcdbc31e9
commit c0f2468236
4 changed files with 40 additions and 22 deletions

View File

@ -51,25 +51,16 @@
<pathelement path="${refine.classes.dir}"/>
</path>
<target name="build_java" depends="copyfiles">
<target name="build_java">
<mkdir dir="${classes.dir}" />
<javac destdir="${classes.dir}" debug="true" includeAntRuntime="no">
<src path="${src.dir}"/>
<src path="${rsrc.dir}"/>
<classpath refid="class.path" />
</javac>
</target>
<target name="build" depends="build_java"/>
<target name="copyfiles">
<copy todir="${classes.dir}">
<fileset dir="${rsrc.dir}">
<include name="**/*"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${classes.dir}" />
</target>

View File

@ -46,7 +46,6 @@ import org.json.JSONObject;
import org.json.JSONWriter;
import com.google.refine.browsing.Engine;
import com.google.refine.browsing.FilteredRows;
import com.google.refine.commands.Command;
import org.openrefine.wikidata.exporters.QuickStatementsExporter;
@ -76,15 +75,19 @@ public class PreviewWikibaseSchemaCommand extends Command {
{
StringWriter stringWriter = new StringWriter();
QuickStatementsExporter exporter = new QuickStatementsExporter();
exporter.translateSchema(project, schema, stringWriter);
Engine engine = getEngine(request, project);
exporter.translateSchema(project, engine, schema, stringWriter);
String fullQS = stringWriter.toString();
stringWriter = new StringWriter();
LineNumberReader reader = new LineNumberReader(new StringReader(fullQS));
reader.setLineNumber(0);
int maxQSLinesForPreview = 50;
for(int i = 0; i != maxQSLinesForPreview; i++) {
stringWriter.write(reader.readLine()+"\n");
reader.setLineNumber(0);
String line = reader.readLine();
for(int i = 1; i != maxQSLinesForPreview && line != null; i++) {
stringWriter.write(line+"\n");
line = reader.readLine();
}
if (reader.getLineNumber() == maxQSLinesForPreview) {
stringWriter.write("...");

View File

@ -47,11 +47,11 @@ public class QuickStatementsExporter implements WriterExporter {
if (schema == null) {
return;
}
translateSchema(project, schema, writer);
translateSchema(project, engine, schema, writer);
}
public void translateSchema(Project project, WikibaseSchema schema, Writer writer) throws IOException {
List<ItemDocument> items = schema.evaluate(project);
public void translateSchema(Project project, Engine engine, WikibaseSchema schema, Writer writer) throws IOException {
List<ItemDocument> items = schema.evaluate(project, engine);
for (ItemDocument item : items) {
translateItem(item, writer);
}

View File

@ -11,8 +11,10 @@ import org.json.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
import com.google.refine.browsing.Engine;
import com.google.refine.browsing.FilteredRows;
import com.google.refine.browsing.RowVisitor;
import com.google.refine.model.OverlayModel;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
@ -69,13 +71,35 @@ public class WikibaseSchema implements OverlayModel {
return result;
}
public List<ItemDocument> evaluate(Project project) {
public List<ItemDocument> evaluate(Project project, Engine engine) {
List<ItemDocument> result = new ArrayList<ItemDocument>();
for (Row row : project.rows) {
FilteredRows filteredRows = engine.getAllFilteredRows();
filteredRows.accept(project, new EvaluatingRowVisitor(result));
return result;
}
protected class EvaluatingRowVisitor implements RowVisitor {
private List<ItemDocument> result;
public EvaluatingRowVisitor(List<ItemDocument> result) {
this.result = result;
}
@Override
public void start(Project project) {
;
}
@Override
public boolean visit(Project project, int rowIndex, Row row) {
ExpressionContext ctxt = new ExpressionContext(baseUri, row, project.columnModel);
result.addAll(evaluate(ctxt));
return false;
}
@Override
public void end(Project project) {
;
}
return result;
}
static public WikibaseSchema reconstruct(JSONObject o) throws JSONException {