Update Wikidata-Toolkit to 0.12.1. (#4216)

* Update Wikidata-Toolkit to 0.12.1. Fixes #3202.

* Update groupId for WDTK

* Update our code after change of interfaces in WDTK.
This commit is contained in:
Antonin Delpeuch 2021-10-20 10:20:42 +02:00 committed by GitHub
parent f948e8dd4b
commit 00e425fd9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 330 additions and 96 deletions

View File

@ -16,7 +16,7 @@
<properties> <properties>
<powermock.version>2.0.9</powermock.version> <powermock.version>2.0.9</powermock.version>
<wdtk.version>0.12.0-SNAPSHOT</wdtk.version> <wdtk.version>0.12.1</wdtk.version>
</properties> </properties>
<build> <build>
@ -127,7 +127,7 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openrefine.dependencies.wdtk</groupId> <groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-wikibaseapi</artifactId> <artifactId>wdtk-wikibaseapi</artifactId>
<version>${wdtk.version}</version> <version>${wdtk.version}</version>
</dependency> </dependency>
@ -137,12 +137,12 @@
<version>${jackson.version}</version> <version>${jackson.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openrefine.dependencies.wdtk</groupId> <groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-datamodel</artifactId> <artifactId>wdtk-datamodel</artifactId>
<version>${wdtk.version}</version> <version>${wdtk.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openrefine.dependencies.wdtk</groupId> <groupId>org.wikidata.wdtk</groupId>
<artifactId>wdtk-util</artifactId> <artifactId>wdtk-util</artifactId>
<version>${wdtk.version}</version> <version>${wdtk.version}</version>
</dependency> </dependency>

View File

@ -0,0 +1,52 @@
package org.openrefine.wikidata.exporters;
import org.wikidata.wdtk.datamodel.interfaces.NoValueSnak;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.SnakVisitor;
import org.wikidata.wdtk.datamodel.interfaces.SomeValueSnak;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
/**
* Represents a snak in QuickStatements format.
*
* @author Antonin Delpeuch
*
*/
public class QSSnakPrinter implements SnakVisitor<String> {
protected final boolean reference;
protected final ValueVisitor<String> valuePrinter = new QSValuePrinter();
/**
* @param reference indicates whether to print snaks as reference, or as main/qualifier snaks
*/
public QSSnakPrinter(boolean reference) {
this.reference = reference;
}
@Override
public String visit(ValueSnak snak) {
String valStr = snak.getValue().accept(valuePrinter);
return toQS(snak.getPropertyId(), valStr);
}
@Override
public String visit(SomeValueSnak snak) {
return toQS(snak.getPropertyId(), "somevalue");
}
@Override
public String visit(NoValueSnak snak) {
return toQS(snak.getPropertyId(), "novalue");
}
protected String toQS(PropertyIdValue property, String value) {
String pid = property.getId();
if (reference) {
pid = pid.replace('P', 'S');
}
return "\t" + pid + "\t" + value;
}
}

View File

@ -42,8 +42,6 @@ import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.exporters.WriterExporter; import com.google.refine.exporters.WriterExporter;
@ -56,7 +54,12 @@ public class QuickStatementsExporter implements WriterExporter {
public static final String impossibleSchedulingErrorMessage = "This edit batch cannot be performed with QuickStatements due to the structure of its new items."; public static final String impossibleSchedulingErrorMessage = "This edit batch cannot be performed with QuickStatements due to the structure of its new items.";
public static final String noSchemaErrorMessage = "No schema was provided. You need to align your project with Wikidata first."; public static final String noSchemaErrorMessage = "No schema was provided. You need to align your project with Wikidata first.";
protected final QSSnakPrinter mainSnakPrinter;
protected final QSSnakPrinter referenceSnakPrinter;
public QuickStatementsExporter() { public QuickStatementsExporter() {
mainSnakPrinter = new QSSnakPrinter(false);
referenceSnakPrinter = new QSSnakPrinter(true);
} }
@Override @Override
@ -148,19 +151,17 @@ public class QuickStatementsExporter implements WriterExporter {
throws IOException { throws IOException {
Claim claim = statement.getClaim(); Claim claim = statement.getClaim();
Value val = claim.getValue(); Snak mainSnak = claim.getMainSnak();
ValueVisitor<String> vv = new QSValuePrinter(); String mainSnakQS = mainSnak.accept(mainSnakPrinter);
String targetValue = val.accept(vv);
if (targetValue != null) { // issue #2320
if (!add) { if (!add) {
// According to: https://www.wikidata.org/wiki/Help:QuickStatements#Removing_statements, // According to: https://www.wikidata.org/wiki/Help:QuickStatements#Removing_statements,
// Removing statements won't be followed by qualifiers or references. // Removing statements won't be followed by qualifiers or references.
writer.write("- "); writer.write("- ");
writer.write(qid + "\t" + pid + "\t" + targetValue); writer.write(qid + mainSnakQS);
writer.write("\n"); writer.write("\n");
} else { // add statements } else { // add statements
if (statement.getReferences().isEmpty()) { if (statement.getReferences().isEmpty()) {
writer.write(qid + "\t" + pid + "\t" + targetValue); writer.write(qid + mainSnakQS);
for (SnakGroup q : claim.getQualifiers()) { for (SnakGroup q : claim.getQualifiers()) {
translateSnakGroup(q, false, writer); translateSnakGroup(q, false, writer);
} }
@ -172,7 +173,7 @@ public class QuickStatementsExporter implements WriterExporter {
// So, to handle multiple references, we can duplicate the statement just with different references. // So, to handle multiple references, we can duplicate the statement just with different references.
for (Reference r : statement.getReferences()) { for (Reference r : statement.getReferences()) {
writer.write(qid + "\t" + pid + "\t" + targetValue); writer.write(qid + mainSnakQS);
for (SnakGroup q : claim.getQualifiers()) { for (SnakGroup q : claim.getQualifiers()) {
translateSnakGroup(q, false, writer); translateSnakGroup(q, false, writer);
} }
@ -184,26 +185,15 @@ public class QuickStatementsExporter implements WriterExporter {
} }
} }
} }
}
protected void translateSnakGroup(SnakGroup sg, boolean reference, Writer writer) protected void translateSnakGroup(SnakGroup sg, boolean reference, Writer writer)
throws IOException { throws IOException {
for (Snak s : sg.getSnaks()) { for (Snak s : sg.getSnaks()) {
translateSnak(s, reference, writer);
}
}
protected void translateSnak(Snak s, boolean reference, Writer writer)
throws IOException {
String pid = s.getPropertyId().getId();
if (reference) { if (reference) {
pid = pid.replace('P', 'S'); writer.write(s.accept(referenceSnakPrinter));
} else {
writer.write(s.accept(mainSnakPrinter));
} }
Value val = s.getValue();
ValueVisitor<String> vv = new QSValuePrinter();
String valStr = val.accept(vv);
if (valStr != null) {
writer.write("\t" + pid + "\t" + valStr);
} }
} }

View File

@ -5,6 +5,7 @@ import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -31,11 +32,11 @@ public class Constraint {
List<SnakGroup> snakGroupList = statement.getClaim().getQualifiers(); List<SnakGroup> snakGroupList = statement.getClaim().getQualifiers();
for(SnakGroup group : snakGroupList) { for(SnakGroup group : snakGroupList) {
for (Snak snak : group.getSnaks()) { for (Snak snak : group.getSnaks()) {
if (group.getProperty().getId().equals(CONSTRAINT_STATUS)) { if (group.getProperty().getId().equals(CONSTRAINT_STATUS) && snak instanceof ValueSnak) {
constraintStatus = (ItemIdValue) snak.getValue(); constraintStatus = (ItemIdValue) ((ValueSnak)snak).getValue();
} }
else if (group.getProperty().getId().equals(CONSTRAINT_EXCEPTIONS)) { else if (group.getProperty().getId().equals(CONSTRAINT_EXCEPTIONS) && snak instanceof ValueSnak) {
constraintExceptions.add((EntityIdValue) snak.getValue()); constraintExceptions.add((EntityIdValue) ((ValueSnak)snak).getValue());
} }
} }
} }

View File

@ -7,6 +7,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -32,11 +33,11 @@ public class ConflictsWithScrutinizer extends EditScrutinizer {
this.itemList = new ArrayList<>(); this.itemList = new ArrayList<>();
for(SnakGroup group : specs) { for(SnakGroup group : specs) {
for (Snak snak : group.getSnaks()) { for (Snak snak : group.getSnaks()) {
if (group.getProperty().getId().equals(conflictsWithPropertyPid)){ if (group.getProperty().getId().equals(conflictsWithPropertyPid) && snak instanceof ValueSnak) {
pid = (PropertyIdValue) snak.getValue(); pid = (PropertyIdValue) ((ValueSnak)snak).getValue();
} }
if (group.getProperty().getId().equals(itemOfPropertyConstraintPid)){ if (group.getProperty().getId().equals(itemOfPropertyConstraintPid) && snak instanceof ValueSnak) {
this.itemList.add(snak.getValue()); this.itemList.add(((ValueSnak)snak).getValue());
} }
} }
} }
@ -59,7 +60,11 @@ public class ConflictsWithScrutinizer extends EditScrutinizer {
Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>(); Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>();
for (Statement statement : update.getAddedStatements()){ for (Statement statement : update.getAddedStatements()){
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
Value value = statement.getClaim().getMainSnak().getValue(); Value value = null;
Snak mainSnak = statement.getClaim().getMainSnak();
if (mainSnak instanceof ValueSnak) {
value = ((ValueSnak)mainSnak).getValue();
}
Set<Value> values; Set<Value> values;
if (value != null) { if (value != null) {
if (propertyIdValueValueMap.containsKey(pid)) { if (propertyIdValueValueMap.containsKey(pid)) {

View File

@ -53,10 +53,13 @@ public class DifferenceWithinRangeScrutinizer extends EditScrutinizer {
public void scrutinize(ItemUpdate update) { public void scrutinize(ItemUpdate update) {
Map<PropertyIdValue, Value> propertyIdValueValueMap = new HashMap<>(); Map<PropertyIdValue, Value> propertyIdValueValueMap = new HashMap<>();
for (Statement statement : update.getAddedStatements()){ for (Statement statement : update.getAddedStatements()){
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); Snak mainSnak = statement.getClaim().getMainSnak();
Value value = statement.getClaim().getMainSnak().getValue(); if (mainSnak instanceof ValueSnak) {
PropertyIdValue pid = mainSnak.getPropertyId();
Value value = ((ValueSnak)mainSnak).getValue();
propertyIdValueValueMap.put(pid, value); propertyIdValueValueMap.put(pid, value);
} }
}
for(PropertyIdValue propertyId : propertyIdValueValueMap.keySet()){ for(PropertyIdValue propertyId : propertyIdValueValueMap.keySet()){
List<Statement> statementList = _fetcher.getConstraintsByType(propertyId, differenceWithinRangeConstraintQid); List<Statement> statementList = _fetcher.getConstraintsByType(propertyId, differenceWithinRangeConstraintQid);

View File

@ -26,8 +26,10 @@ package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -59,10 +61,11 @@ public class DistinctValuesScrutinizer extends StatementScrutinizer {
@Override @Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) { public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); Snak mainSnak = statement.getClaim().getMainSnak();
PropertyIdValue pid = mainSnak.getPropertyId();
List<Statement> statementList = _fetcher.getConstraintsByType(pid, distinctValuesConstraintQid); List<Statement> statementList = _fetcher.getConstraintsByType(pid, distinctValuesConstraintQid);
if (!statementList.isEmpty()) { if (!statementList.isEmpty() && mainSnak instanceof ValueSnak) {
Value mainSnakValue = statement.getClaim().getMainSnak().getValue(); Value mainSnakValue = ((ValueSnak)mainSnak).getValue();
Map<Value, EntityIdValue> seen = _seenValues.get(pid); Map<Value, EntityIdValue> seen = _seenValues.get(pid);
if (seen == null) { if (seen == null) {
seen = new HashMap<Value, EntityIdValue>(); seen = new HashMap<Value, EntityIdValue>();

View File

@ -32,6 +32,7 @@ import org.openrefine.wikidata.updates.ItemUpdate;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -155,7 +156,9 @@ public abstract class EditScrutinizer {
for (SnakGroup group : groups) { for (SnakGroup group : groups) {
if (group.getProperty().getId().equals(pid)) { if (group.getProperty().getId().equals(pid)) {
for (Snak snak : group.getSnaks()) for (Snak snak : group.getSnaks())
results.add(snak.getValue()); if (snak instanceof ValueSnak) {
results.add(((ValueSnak)snak).getValue());
}
} }
} }
return results; return results;

View File

@ -33,6 +33,7 @@ import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StringValue; import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -118,8 +119,8 @@ public class FormatScrutinizer extends SnakScrutinizer {
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (snak.getValue() instanceof StringValue) { if (snak instanceof ValueSnak && ((ValueSnak)snak).getValue() instanceof StringValue) {
String value = ((StringValue) snak.getValue()).getString(); String value = ((StringValue) ((ValueSnak)snak).getValue()).getString();
PropertyIdValue pid = snak.getPropertyId(); PropertyIdValue pid = snak.getPropertyId();
Set<Pattern> patterns = getPattern(pid); Set<Pattern> patterns = getPattern(pid);
for (Pattern pattern : patterns) { for (Pattern pattern : patterns) {

View File

@ -27,9 +27,11 @@ import org.openrefine.wikidata.qa.QAWarning;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -115,9 +117,13 @@ public class InverseConstraintScrutinizer extends StatementScrutinizer {
return; // TODO support for deleted statements return; // TODO support for deleted statements
} }
Value mainSnakValue = statement.getClaim().getMainSnak().getValue(); Snak mainSnak = statement.getClaim().getMainSnak();
if (! (mainSnak instanceof ValueSnak)) {
return;
}
Value mainSnakValue = ((ValueSnak)mainSnak).getValue();
if (mainSnakValue instanceof ItemIdValue) { if (mainSnakValue instanceof ItemIdValue) {
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); PropertyIdValue pid = mainSnak.getPropertyId();
PropertyIdValue inversePid = getInverseConstraint(pid); PropertyIdValue inversePid = getInverseConstraint(pid);
if (inversePid != null) { if (inversePid != null) {
EntityIdValue targetEntityId = (EntityIdValue) mainSnakValue; EntityIdValue targetEntityId = (EntityIdValue) mainSnakValue;

View File

@ -7,6 +7,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -35,11 +36,14 @@ public class ItemRequiresScrutinizer extends EditScrutinizer {
this.itemList = new ArrayList<>(); this.itemList = new ArrayList<>();
for(SnakGroup group : specs) { for(SnakGroup group : specs) {
for (Snak snak : group.getSnaks()) { for (Snak snak : group.getSnaks()) {
if (! (snak instanceof ValueSnak)) {
continue;
}
if (group.getProperty().getId().equals(itemRequiresPropertyPid)){ if (group.getProperty().getId().equals(itemRequiresPropertyPid)){
pid = (PropertyIdValue) snak.getValue(); pid = (PropertyIdValue) ((ValueSnak)snak).getValue();
} }
if (group.getProperty().getId().equals(itemOfPropertyConstraintPid)){ if (group.getProperty().getId().equals(itemOfPropertyConstraintPid)){
this.itemList.add(snak.getValue()); this.itemList.add(((ValueSnak)snak).getValue());
} }
} }
} }
@ -60,10 +64,11 @@ public class ItemRequiresScrutinizer extends EditScrutinizer {
public void scrutinize(ItemUpdate update) { public void scrutinize(ItemUpdate update) {
Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>(); Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>();
for (Statement statement : update.getAddedStatements()) { for (Statement statement : update.getAddedStatements()) {
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); Snak mainSnak = statement.getClaim().getMainSnak();
Value value = statement.getClaim().getMainSnak().getValue(); PropertyIdValue pid = mainSnak.getPropertyId();
Set<Value> values; Set<Value> values;
if (value != null) { if (mainSnak instanceof ValueSnak) {
Value value = ((ValueSnak)mainSnak).getValue();
if (propertyIdValueValueMap.containsKey(pid)) { if (propertyIdValueValueMap.containsKey(pid)) {
values = propertyIdValueValueMap.get(pid); values = propertyIdValueValueMap.get(pid);
} else { } else {

View File

@ -9,6 +9,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -58,9 +59,9 @@ public class QuantityScrutinizer extends SnakScrutinizer {
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (snak.getValue() instanceof QuantityValue && added) { if (snak instanceof ValueSnak && ((ValueSnak)snak).getValue() instanceof QuantityValue && added) {
PropertyIdValue pid = snak.getPropertyId(); PropertyIdValue pid = snak.getPropertyId();
QuantityValue value = (QuantityValue)snak.getValue(); QuantityValue value = (QuantityValue)((ValueSnak)snak).getValue();
if(!_fetcher.getConstraintsByType(pid, noBoundsConstraintQid).isEmpty() && (value.getUpperBound() != null || value.getLowerBound() != null)) { if(!_fetcher.getConstraintsByType(pid, noBoundsConstraintQid).isEmpty() && (value.getUpperBound() != null || value.getLowerBound() != null)) {
QAWarning issue = new QAWarning(boundsDisallowedType, pid.getId(), QAWarning.Severity.IMPORTANT, 1); QAWarning issue = new QAWarning(boundsDisallowedType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);

View File

@ -7,6 +7,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -57,7 +58,10 @@ public class RestrictedValuesScrutinizer extends SnakScrutinizer {
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
PropertyIdValue pid = snak.getPropertyId(); PropertyIdValue pid = snak.getPropertyId();
Value value = snak.getValue(); Value value = null;
if (snak instanceof ValueSnak) {
value = ((ValueSnak)snak).getValue();
}
List<Statement> allowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, allowedValuesConstraintQid); List<Statement> allowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, allowedValuesConstraintQid);
List<Statement> disallowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, disallowedValuesConstraintQid); List<Statement> disallowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, disallowedValuesConstraintQid);
Set<Value> allowedValues = null, disallowedValues = null; Set<Value> allowedValues = null, disallowedValues = null;

View File

@ -26,6 +26,7 @@ package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
/** /**
* A scrutinizer that checks for self-referential statements. These statements * A scrutinizer that checks for self-referential statements. These statements
@ -40,7 +41,7 @@ public class SelfReferentialScrutinizer extends SnakScrutinizer {
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (entityId.equals(snak.getValue())) { if (snak instanceof ValueSnak && entityId.equals(((ValueSnak)snak).getValue())) {
QAWarning issue = new QAWarning(type, null, QAWarning.Severity.WARNING, 1); QAWarning issue = new QAWarning(type, null, QAWarning.Severity.WARNING, 1);
issue.setProperty("example_entity", entityId); issue.setProperty("example_entity", entityId);
addIssue(issue); addIssue(issue);

View File

@ -8,6 +8,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -30,11 +31,14 @@ public class UseAsQualifierScrutinizer extends EditScrutinizer {
this.itemList = new ArrayList<>(); this.itemList = new ArrayList<>();
for(SnakGroup group : specs) { for(SnakGroup group : specs) {
for (Snak snak : group.getSnaks()) { for (Snak snak : group.getSnaks()) {
if (! (snak instanceof ValueSnak)) {
continue;
}
if (group.getProperty().getId().equals(property)){ if (group.getProperty().getId().equals(property)){
pid = (PropertyIdValue) snak.getValue(); pid = (PropertyIdValue) ((ValueSnak)snak).getValue();
} }
if (group.getProperty().getId().equals(itemOfPropertyConstraintPid)){ if (group.getProperty().getId().equals(itemOfPropertyConstraintPid)){
this.itemList.add(snak.getValue()); this.itemList.add(((ValueSnak)snak).getValue());
} }
} }
} }
@ -58,15 +62,18 @@ public class UseAsQualifierScrutinizer extends EditScrutinizer {
List<SnakGroup> qualifiersList = statement.getClaim().getQualifiers(); List<SnakGroup> qualifiersList = statement.getClaim().getQualifiers();
for(SnakGroup qualifier : qualifiersList) { for(SnakGroup qualifier : qualifiersList) {
PropertyIdValue qualifierPid = Datamodel.makeWikidataPropertyIdValue(qualifier.getProperty().getId()); PropertyIdValue qualifierPid = qualifier.getProperty();
List<Value> itemList; List<Value> itemList;
for (Snak snak : qualifier.getSnaks()) { for (Snak snak : qualifier.getSnaks()) {
if (!(snak instanceof ValueSnak)) {
continue;
}
if (qualifiersMap.containsKey(qualifierPid)){ if (qualifiersMap.containsKey(qualifierPid)){
itemList = qualifiersMap.get(qualifierPid); itemList = qualifiersMap.get(qualifierPid);
}else { } else {
itemList = new ArrayList<>(); itemList = new ArrayList<>();
} }
itemList.add(snak.getValue()); itemList.add(((ValueSnak)snak).getValue());
qualifiersMap.put(qualifierPid, itemList); qualifiersMap.put(qualifierPid, itemList);
} }
} }

View File

@ -28,6 +28,7 @@ import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
/** /**
* A scrutinizer that inspects the values of snaks and terms * A scrutinizer that inspects the values of snaks and terms
@ -56,7 +57,9 @@ public abstract class ValueScrutinizer extends SnakScrutinizer {
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
scrutinize(snak.getValue()); if (snak instanceof ValueSnak) {
scrutinize(((ValueSnak)snak).getValue());
}
} }
} }

View File

@ -92,7 +92,7 @@ public class WbQuantityExpr implements WbExpression<QuantityValue> {
if (getUnitExpr() != null) { if (getUnitExpr() != null) {
ItemIdValue unit = getUnitExpr().evaluate(ctxt); ItemIdValue unit = getUnitExpr().evaluate(ctxt);
return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound, unit.getIri()); return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound, unit);
} }
return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound); return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound);

View File

@ -40,6 +40,7 @@ import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue; import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import org.wikidata.wdtk.datamodel.interfaces.UnsupportedValue; import org.wikidata.wdtk.datamodel.interfaces.UnsupportedValue;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor; import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;
/** /**
@ -101,7 +102,9 @@ public class PointerExtractor implements ValueVisitor<Set<ReconItemIdValue>> {
public Set<ReconItemIdValue> extractPointers(Snak snak) { public Set<ReconItemIdValue> extractPointers(Snak snak) {
Set<ReconItemIdValue> result = new HashSet<>(); Set<ReconItemIdValue> result = new HashSet<>();
result.addAll(extractPointers(snak.getPropertyId())); result.addAll(extractPointers(snak.getPropertyId()));
result.addAll(extractPointers(snak.getValue())); if (snak instanceof ValueSnak) {
result.addAll(extractPointers(((ValueSnak)snak).getValue()));
}
return result; return result;
} }

View File

@ -0,0 +1,42 @@
package org.openrefine.wikidata.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
public class SnakUtils {
/**
* Groups snaks into a list of snak groups.
* The order of the first snaks in each group is preserved.
*
* @param snaks
* @return
*/
public static List<SnakGroup> groupSnaks(List<Snak> snaks) {
Map<PropertyIdValue, List<Snak>> snakGroups = new HashMap<>();
List<PropertyIdValue> propertyOrder = new ArrayList<PropertyIdValue>();
for (Snak snak : snaks) {
List<Snak> existingSnaks = snakGroups.get(snak.getPropertyId());
if(existingSnaks == null) {
existingSnaks = new ArrayList<Snak>();
snakGroups.put(snak.getPropertyId(), existingSnaks);
propertyOrder.add(snak.getPropertyId());
}
if (!existingSnaks.contains(snak)) {
existingSnaks.add(snak);
}
}
return propertyOrder.stream()
.map(pid -> Datamodel.makeSnakGroup(snakGroups.get(pid)))
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,40 @@
package org.openrefine.wikidata.exporters;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
public class QSSnakPrinterTest {
private QSSnakPrinter refPrinter = new QSSnakPrinter(true);
private QSSnakPrinter mainPrinter = new QSSnakPrinter(false);
private PropertyIdValue pid = Datamodel.makeWikidataPropertyIdValue("P123");
private ItemIdValue qid = Datamodel.makeWikidataItemIdValue("Q456");
@Test
public void testReferenceValueSnak() {
Snak snak = Datamodel.makeValueSnak(pid, qid);
Assert.assertEquals(snak.accept(refPrinter), "\tS123\tQ456");
}
@Test
public void testMainValueSnak() {
Snak snak = Datamodel.makeValueSnak(pid, qid);
Assert.assertEquals(snak.accept(mainPrinter), "\tP123\tQ456");
}
@Test
public void testReferenceNoValueSnak() {
Snak snak = Datamodel.makeNoValueSnak(pid);
Assert.assertEquals(snak.accept(refPrinter), "\tS123\tnovalue");
}
@Test
public void testMainSomeValueSnak() {
Snak snak = Datamodel.makeSomeValueSnak(pid);
Assert.assertEquals(snak.accept(mainPrinter), "\tP123\tsomevalue");
}
}

View File

@ -92,25 +92,25 @@ public class QSValuePrinterTest {
@Test @Test
public void printSimpleQuantityValue() { public void printSimpleQuantityValue() {
assertPrints("10.00", Datamodel.makeQuantityValue(new BigDecimal("10.00"), null, null, "1")); assertPrints("10.00", Datamodel.makeQuantityValue(new BigDecimal("10.00")));
} }
@Test @Test
public void printQuantityValueWithUnit() { public void printQuantityValueWithUnit() {
assertPrints("10.00U11573", Datamodel.makeQuantityValue(new BigDecimal("10.00"), null, null, assertPrints("10.00U11573", Datamodel.makeQuantityValue(new BigDecimal("10.00"),
"http://www.wikidata.org/entity/Q11573")); Datamodel.makeWikidataItemIdValue("Q11573")));
} }
@Test @Test
public void printQuantityValueWithBounds() { public void printQuantityValueWithBounds() {
assertPrints("10.00[9.0,11.05]", Datamodel.makeQuantityValue(new BigDecimal("10.00"), new BigDecimal("9.0"), assertPrints("10.00[9.0,11.05]", Datamodel.makeQuantityValue(new BigDecimal("10.00"), new BigDecimal("9.0"),
new BigDecimal("11.05"), "1")); new BigDecimal("11.05")));
} }
@Test @Test
public void printFullQuantity() { public void printFullQuantity() {
assertPrints("10.00[9.0,11.05]U11573", Datamodel.makeQuantityValue(new BigDecimal("10.00"), assertPrints("10.00[9.0,11.05]U11573", Datamodel.makeQuantityValue(new BigDecimal("10.00"),
new BigDecimal("9.0"), new BigDecimal("11.05"), "http://www.wikidata.org/entity/Q11573")); new BigDecimal("9.0"), new BigDecimal("11.05"), Datamodel.makeWikidataItemIdValue("Q11573")));
} }
// String values // String values

View File

@ -134,6 +134,30 @@ public class QuickStatementsExporterTest extends WikidataRefineTest {
assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\n", export(update)); assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\n", export(update));
} }
@Test
public void testSomeValue()
throws IOException {
PropertyIdValue pid = Datamodel.makeWikidataPropertyIdValue("P123");
Claim claim = Datamodel.makeClaim(qid1, Datamodel.makeSomeValueSnak(pid), Collections.emptyList());
Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
ItemUpdate update = new ItemUpdateBuilder(qid1).addStatement(statement).build();
assertEquals("Q1377\tP123\tsomevalue\n", export(update));
}
@Test
public void testNoValue()
throws IOException {
PropertyIdValue pid = Datamodel.makeWikidataPropertyIdValue("P123");
Claim claim = Datamodel.makeClaim(qid1, Datamodel.makeNoValueSnak(pid), Collections.emptyList());
Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
ItemUpdate update = new ItemUpdateBuilder(qid1).addStatement(statement).build();
assertEquals("Q1377\tP123\tnovalue\n", export(update));
}
/** /**
* issue #2320 * issue #2320
* *

View File

@ -45,7 +45,7 @@ public class QuantityScrutinizerTest extends ValueScrutinizerTest{
new BigDecimal("1.545")); new BigDecimal("1.545"));
private QuantityValue wrongUnitValue = Datamodel.makeQuantityValue( private QuantityValue wrongUnitValue = Datamodel.makeQuantityValue(
new BigDecimal("1.234"), "Q346721"); new BigDecimal("1.234"), Datamodel.makeWikidataItemIdValue("Q346721"));
private QuantityValue goodUnitValue = Datamodel.makeQuantityValue( private QuantityValue goodUnitValue = Datamodel.makeQuantityValue(
new BigDecimal("1.234"), (ItemIdValue) allowedUnit); new BigDecimal("1.234"), (ItemIdValue) allowedUnit);

View File

@ -44,13 +44,13 @@ public class WbQuantityExprTest extends WbExpressionTest<QuantityValue> {
public void testWithoutUnit() public void testWithoutUnit()
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
setRow("4.00"); setRow("4.00");
evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("4.00"), null, null, "1"), exprWithoutUnit); evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("4.00")), exprWithoutUnit);
} }
@Test @Test
public void testOverflow() { public void testOverflow() {
setRow(14341937500d); setRow(14341937500d);
evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("14341937500"), null, null, "1"), exprWithoutUnit); evaluatesTo(Datamodel.makeQuantityValue(new BigDecimal("14341937500")), exprWithoutUnit);
} }
@Test @Test
@ -64,7 +64,7 @@ public class WbQuantityExprTest extends WbExpressionTest<QuantityValue> {
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
setRow("56.094", recon("Q42")); setRow("56.094", recon("Q42"));
evaluatesTo( evaluatesTo(
Datamodel.makeQuantityValue(new BigDecimal("56.094"), null, null, "http://www.wikidata.org/entity/Q42"), Datamodel.makeQuantityValue(new BigDecimal("56.094"), Datamodel.makeWikidataItemIdValue("Q42")),
exprWithUnit); exprWithUnit);
} }

View File

@ -73,7 +73,7 @@ public class PointerExtractorTest {
assertEmpty(Datamodel.makeMonolingualTextValue("srtu", "en")); assertEmpty(Datamodel.makeMonolingualTextValue("srtu", "en"));
assertEmpty(Datamodel.makeWikidataPropertyIdValue("P78")); assertEmpty(Datamodel.makeWikidataPropertyIdValue("P78"));
assertEmpty(Datamodel.makeQuantityValue(new BigDecimal("898"))); assertEmpty(Datamodel.makeQuantityValue(new BigDecimal("898")));
assertEmpty(Datamodel.makeQuantityValue(new BigDecimal("7.87"), "http://www.wikidata.org/entity/Q34")); assertEmpty(Datamodel.makeQuantityValue(new BigDecimal("7.87"), Datamodel.makeWikidataItemIdValue("Q34")));
assertEmpty(Datamodel.makeTimeValue(1898, (byte) 2, (byte) 3, TimeValue.CM_GREGORIAN_PRO)); assertEmpty(Datamodel.makeTimeValue(1898, (byte) 2, (byte) 3, TimeValue.CM_GREGORIAN_PRO));
assertEmpty(mock(UnsupportedValue.class)); assertEmpty(mock(UnsupportedValue.class));
} }

View File

@ -0,0 +1,40 @@
package org.openrefine.wikidata.utils;
import java.util.Arrays;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
public class SnakUtilsTests {
PropertyIdValue pidA = Datamodel.makeWikidataPropertyIdValue("P1");
PropertyIdValue pidB = Datamodel.makeWikidataPropertyIdValue("P2");
PropertyIdValue pidC = Datamodel.makeWikidataPropertyIdValue("P3");
StringValue value1 = Datamodel.makeStringValue("value1");
StringValue value2 = Datamodel.makeStringValue("value2");
Snak snakA1 = Datamodel.makeValueSnak(pidA, value1);
Snak snakA2 = Datamodel.makeValueSnak(pidA, value2);
Snak snakB1 = Datamodel.makeValueSnak(pidB, value1);
Snak snakC1 = Datamodel.makeValueSnak(pidC, value1);
Snak snakC2 = Datamodel.makeValueSnak(pidC, value2);
@Test
public void testGroupSnaks() {
List<SnakGroup> snakGroups = SnakUtils.groupSnaks(Arrays.asList(snakA2, snakB1, snakC1, snakA1, snakC2));
List<SnakGroup> expected = Arrays.asList(
Datamodel.makeSnakGroup(Arrays.asList(snakA2, snakA1)),
Datamodel.makeSnakGroup(Arrays.asList(snakB1)),
Datamodel.makeSnakGroup(Arrays.asList(snakC1, snakC2))
);
Assert.assertEquals(snakGroups, expected);
}
}