Search in sources :

Example 1 with ValueSnak

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);
        }
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) QuantityValue(org.wikidata.wdtk.datamodel.interfaces.QuantityValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 2 with ValueSnak

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);
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Value(org.wikidata.wdtk.datamodel.interfaces.Value) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 3 with ValueSnak

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");
                }
            }
        }
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Pattern(java.util.regex.Pattern) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) StringValue(org.wikidata.wdtk.datamodel.interfaces.StringValue) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 4 with ValueSnak

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);
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) TimeValueImpl(org.wikidata.wdtk.datamodel.implementation.TimeValueImpl) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) Test(org.testng.annotations.Test)

Example 5 with ValueSnak

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();
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) TimeValueImpl(org.wikidata.wdtk.datamodel.implementation.TimeValueImpl) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) TimeValue(org.wikidata.wdtk.datamodel.interfaces.TimeValue) Test(org.testng.annotations.Test)

Aggregations

ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)24 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)20 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)16 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)14 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)14 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)14 Test (org.testng.annotations.Test)14 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)14 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)14 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)10 QAWarning (org.openrefine.wikidata.qa.QAWarning)8 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)8 Value (org.wikidata.wdtk.datamodel.interfaces.Value)7 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)6 HashMap (java.util.HashMap)3 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TimeValueImpl (org.wikidata.wdtk.datamodel.implementation.TimeValueImpl)2