Merge pull request #2340 from afkbrb/issue-2320-qs-refs

Issue 2320 qs refs
This commit is contained in:
Antonin Delpeuch 2020-02-29 09:12:36 +00:00 committed by GitHub
commit f77f8d93fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 17 deletions

3
.gitignore vendored
View File

@ -28,6 +28,9 @@ test-out/
open-refine.log
.vscode
.idea
*.iml
main/target/
main/webapp/WEB-INF/lib/
server/target/

View File

@ -151,23 +151,40 @@ public class QuickStatementsExporter implements WriterExporter {
Value val = claim.getValue();
ValueVisitor<String> vv = new QSValuePrinter();
String targetValue = val.accept(vv);
if (targetValue != null) {
if (targetValue != null) { // issue #2320
if (!add) {
// According to: https://www.wikidata.org/wiki/Help:QuickStatements#Removing_statements,
// Removing statements won't be followed by qualifiers or references.
writer.write("- ");
}
writer.write(qid + "\t" + pid + "\t" + targetValue);
writer.write("\n");
} else { // add statements
if (statement.getReferences().isEmpty()) {
writer.write(qid + "\t" + pid + "\t" + targetValue);
for (SnakGroup q : claim.getQualifiers()) {
translateSnakGroup(q, false, writer);
}
writer.write("\n");
} else {
// According to: https://www.wikidata.org/wiki/Help:QuickStatements#Add_statement_with_sources
// Existing statements with an exact match (property and value) will not be added again;
// however additional references might be added to the statement.
// So, to handle multiple references, we can duplicate the statement just with different references.
for (Reference r : statement.getReferences()) {
writer.write(qid + "\t" + pid + "\t" + targetValue);
for (SnakGroup q : claim.getQualifiers()) {
translateSnakGroup(q, false, writer);
}
for (SnakGroup g : r.getSnakGroups()) {
translateSnakGroup(g, true, writer);
}
break; // QS only supports one reference
}
writer.write("\n");
}
}
}
}
}
protected void translateSnakGroup(SnakGroup sg, boolean reference, Writer writer)
throws IOException {

View File

@ -38,12 +38,7 @@ import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.ItemUpdateBuilder;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.*;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
@ -139,6 +134,35 @@ public class QuickStatementsExporterTest extends WikidataRefineTest {
assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\n", export(update));
}
/**
* issue #2320
*
* A statement with different references should be duplicated,
* but each with a different reference.
*/
@Test
public void testReferences()
throws IOException {
Statement baseStatement = TestingData.generateStatement(qid1, qid2);
Statement otherStatement = TestingData.generateStatement(qid2, qid1);
Snak snak1 = baseStatement.getClaim().getMainSnak();
Snak snak2 = otherStatement.getClaim().getMainSnak();
SnakGroup group1 = Datamodel.makeSnakGroup(Collections.singletonList(snak1));
SnakGroup group2 = Datamodel.makeSnakGroup(Collections.singletonList(snak2));
Claim claim = Datamodel.makeClaim(qid1, baseStatement.getClaim().getMainSnak(),
Collections.singletonList(group2));
Reference reference1 = Datamodel.makeReference(Collections.singletonList(group1));
Reference reference2 = Datamodel.makeReference(Collections.singletonList(group2));
Statement statement = Datamodel.makeStatement(claim, Arrays.asList(reference1, reference2), StatementRank.NORMAL, "");
ItemUpdate update = new ItemUpdateBuilder(qid1).addStatement(statement).build();
assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\tS38\tQ865528\n" +
"Q1377\tP38\tQ865528\tP38\tQ1377\tS38\tQ1377\n", export(update));
}
@Test
public void testNoSchema()
throws IOException {