Add constraint checks for one-of and none-of constraints

This commit is contained in:
Antonin Delpeuch 2018-06-09 13:08:52 +01:00
parent 3c181c9070
commit 48c2b90a93
4 changed files with 110 additions and 7 deletions

View File

@ -37,6 +37,7 @@ import org.openrefine.wikidata.qa.scrutinizers.NoEditsMadeScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.QualifierCompatibilityScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.QuantityScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.RestrictedPositionScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.RestrictedValuesScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.SelfReferentialScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.SingleValueScrutinizer;
import org.openrefine.wikidata.qa.scrutinizers.UnsourcedScrutinizer;
@ -75,6 +76,7 @@ public class EditInspector {
register(new NoEditsMadeScrutinizer());
register(new WhitespaceScrutinizer());
register(new QuantityScrutinizer());
register(new RestrictedValuesScrutinizer());
}
/**

View File

@ -0,0 +1,32 @@
package org.openrefine.wikidata.qa.scrutinizers;
import java.util.Set;
import org.openrefine.wikidata.qa.QAWarning;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Value;
public class RestrictedValuesScrutinizer extends SnakScrutinizer {
public static String type = "forbidden-value";
@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
PropertyIdValue pid = snak.getPropertyId();
Value value = snak.getValue();
Set<Value> allowedValues = _fetcher.allowedValues(pid);
Set<Value> disallowedValues = _fetcher.disallowedValues(pid);
if((allowedValues != null && !allowedValues.contains(value)) ||
(disallowedValues != null && disallowedValues.contains(value))) {
QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value_entity", value);
issue.setProperty("example_subject_entity", entityId);
addIssue(issue);
}
}
}

View File

@ -45,9 +45,10 @@ public class MockConstraintFetcher implements ConstraintFetcher {
public static PropertyIdValue qualifierPid = Datamodel.makeWikidataPropertyIdValue("P987");
public static PropertyIdValue referencePid = Datamodel.makeWikidataPropertyIdValue("P384");
public static PropertyIdValue restrictedValuesPid = Datamodel.makeWikidataPropertyIdValue("P8121");
public static ItemIdValue particularValue1 = Datamodel.makeWikidataItemIdValue("Q389");
public static ItemIdValue particularValue2 = Datamodel.makeWikidataItemIdValue("Q378");
public static PropertyIdValue allowedValuesPid = Datamodel.makeWikidataPropertyIdValue("P8121");
public static ItemIdValue allowedValueQid = Datamodel.makeWikidataItemIdValue("Q389");
public static PropertyIdValue forbiddenValuesPid = Datamodel.makeWikidataPropertyIdValue("P8141");
public static ItemIdValue forbiddenValueQid = Datamodel.makeWikidataItemIdValue("Q378");
public static PropertyIdValue allowedUnitsPid = Datamodel.makeWikidataPropertyIdValue("P34787");
public static ItemIdValue allowedUnit = Datamodel.makeWikidataItemIdValue("Q7887");
@ -115,16 +116,16 @@ public class MockConstraintFetcher implements ConstraintFetcher {
@Override
public Set<Value> allowedValues(PropertyIdValue pid) {
if (restrictedValuesPid.equals(pid)) {
return Collections.singleton(particularValue1);
if (allowedValuesPid.equals(pid)) {
return Arrays.asList(allowedValueQid, null).stream().collect(Collectors.toSet());
}
return null;
}
@Override
public Set<Value> disallowedValues(PropertyIdValue pid) {
if (restrictedValuesPid.equals(pid)) {
return Collections.singleton(particularValue2);
if (forbiddenValuesPid.equals(pid)) {
return Collections.singleton(forbiddenValueQid);
}
return null;
}

View File

@ -0,0 +1,68 @@
package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.MockConstraintFetcher;
import org.openrefine.wikidata.testing.TestingData;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest {
private ItemIdValue qid = Datamodel.makeWikidataItemIdValue("Q3487");
@Override
public EditScrutinizer getScrutinizer() {
return new RestrictedValuesScrutinizer();
}
@Test
public void testNoConstraint() {
scrutinize(TestingData.generateStatement(qid,
Datamodel.makeWikidataPropertyIdValue("P28732"),
qid));
assertNoWarningRaised();
}
@Test
public void testAllowedValue() {
scrutinize(TestingData.generateStatement(qid,
MockConstraintFetcher.allowedValuesPid,
MockConstraintFetcher.allowedValueQid));
assertNoWarningRaised();
}
@Test
public void testAllowedValueFailing() {
scrutinize(TestingData.generateStatement(qid,
MockConstraintFetcher.allowedValuesPid,
qid));
assertWarningsRaised(RestrictedValuesScrutinizer.type);
}
@Test
public void testDisallowedValue() {
scrutinize(TestingData.generateStatement(qid,
MockConstraintFetcher.forbiddenValuesPid,
qid));
assertNoWarningRaised();
}
@Test
public void testDisallowedValueFailing() {
scrutinize(TestingData.generateStatement(qid,
MockConstraintFetcher.forbiddenValuesPid,
MockConstraintFetcher.forbiddenValueQid));
assertWarningsRaised(RestrictedValuesScrutinizer.type);
}
@Test
public void testNoValueAllowed() {
}
@Test
public void testNoValueDisallowed() {
}
}