use of org.wikidata.wdtk.datamodel.interfaces.ValueSnak in project OpenRefine by OpenRefine.
the class QuantityScrutinizer method scrutinize.
@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (!added) {
return;
}
if (snak instanceof ValueSnak && ((ValueSnak) snak).getValue() instanceof QuantityValue && added) {
PropertyIdValue pid = snak.getPropertyId();
QuantityValue value = (QuantityValue) ((ValueSnak) snak).getValue();
if (!_fetcher.getConstraintsByType(pid, noBoundsConstraintQid).isEmpty() && (value.getUpperBound() != null || value.getLowerBound() != null)) {
QAWarning issue = new QAWarning(boundsDisallowedType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value", value.getNumericValue().toString());
issue.setProperty("example_item_entity", entityId);
addIssue(issue);
}
if (!_fetcher.getConstraintsByType(pid, integerValuedConstraintQid).isEmpty() && value.getNumericValue().scale() > 0) {
QAWarning issue = new QAWarning(integerConstraintType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value", value.getNumericValue().toString());
issue.setProperty("example_item_entity", entityId);
addIssue(issue);
}
List<Statement> statementList = _fetcher.getConstraintsByType(pid, allowedUnitsConstraintQid);
Set<ItemIdValue> allowedUnits = null;
if (!statementList.isEmpty()) {
AllowedUnitsConstraint allowedUnitsConstraint = new AllowedUnitsConstraint(statementList.get(0));
allowedUnits = allowedUnitsConstraint.allowedUnits;
}
ItemIdValue currentUnit = null;
if (value.getUnitItemId() != null) {
currentUnit = value.getUnitItemId();
}
if (allowedUnits != null && !allowedUnits.contains(currentUnit)) {
String issueType = currentUnit == null ? noUnitProvidedType : invalidUnitType;
QAWarning issue = new QAWarning(issueType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value", value.getNumericValue().toString());
issue.setProperty("example_item_entity", entityId);
if (currentUnit != null) {
issue.setProperty("unit_entity", value.getUnitItemId());
}
addIssue(issue);
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.ValueSnak in project OpenRefine by OpenRefine.
the class RestrictedValuesScrutinizer method scrutinize.
@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (!added) {
return;
}
PropertyIdValue pid = snak.getPropertyId();
Value value = null;
if (snak instanceof ValueSnak) {
value = ((ValueSnak) snak).getValue();
}
List<Statement> allowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, allowedValuesConstraintQid);
List<Statement> disallowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, disallowedValuesConstraintQid);
Set<Value> allowedValues = null, disallowedValues = null;
if (!allowedValueConstraintDefinitions.isEmpty()) {
AllowedValueConstraint constraint = new AllowedValueConstraint(allowedValueConstraintDefinitions.get(0));
allowedValues = constraint.allowedValues;
}
if (!disallowedValueConstraintDefinitions.isEmpty()) {
DisallowedValueConstraint constraint = new DisallowedValueConstraint(disallowedValueConstraintDefinitions.get(0));
disallowedValues = constraint.disallowedValues;
}
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);
}
}
use of org.wikidata.wdtk.datamodel.interfaces.ValueSnak in project OpenRefine by OpenRefine.
the class FormatScrutinizer method scrutinize.
@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (snak instanceof ValueSnak && ((ValueSnak) snak).getValue() instanceof StringValue) {
String value = ((StringValue) ((ValueSnak) snak).getValue()).getString();
PropertyIdValue pid = snak.getPropertyId();
Set<Pattern> patterns = getPattern(pid);
for (Pattern pattern : patterns) {
if (!pattern.matcher(value).matches()) {
if (added) {
QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("regex", pattern.toString());
issue.setProperty("example_value", value);
issue.setProperty("example_item_entity", entityId);
addIssue(issue);
} else {
info("remove-statements-with-invalid-format");
}
}
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.ValueSnak in project OpenRefine by OpenRefine.
the class DifferenceWithinScrutinizerTest method testTrigger.
@Test
public void testTrigger() {
ItemIdValue idA = TestingData.existingId;
TimeValue lowerYear = new TimeValueImpl(1800, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
TimeValue upperYear = new TimeValueImpl(2020, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
Statement statement1 = new StatementImpl("P569", value1, idA);
Statement statement2 = new StatementImpl("P570", value2, idA);
TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement1)).addStatement(add(statement2)).build();
Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(upperBoundPid, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
setFetcher(fetcher);
scrutinize(updateA);
assertWarningsRaised(DifferenceWithinRangeScrutinizer.type);
}
use of org.wikidata.wdtk.datamodel.interfaces.ValueSnak in project OpenRefine by OpenRefine.
the class DifferenceWithinScrutinizerTest method testNoIssue.
@Test
public void testNoIssue() {
ItemIdValue idA = TestingData.existingId;
TimeValue lowerYear = new TimeValueImpl(2000, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
TimeValue upperYear = new TimeValueImpl(2020, (byte) 10, (byte) 15, (byte) 0, (byte) 0, (byte) 0, (byte) 11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
Statement statement1 = new StatementImpl("P569", value1, idA);
Statement statement2 = new StatementImpl("P570", value2, idA);
TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement1)).addStatement(add(statement2)).build();
Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(upperBoundPid, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
setFetcher(fetcher);
scrutinize(updateA);
assertNoWarningRaised();
}
Aggregations