Search in sources :

Example 1 with Statement

use of org.wikidata.wdtk.datamodel.interfaces.Statement 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 Statement

use of org.wikidata.wdtk.datamodel.interfaces.Statement 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 Statement

use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.

the class ConstraintFetcher method getConstraintStatements.

/**
 * Gets all the constraint statements for a given property
 *
 * @param pid
 *             the id of the property to retrieve the constraints for
 * @return the list of constraint statements
 */
private List<Statement> getConstraintStatements(PropertyIdValue pid) {
    PropertyDocument doc = (PropertyDocument) entityCache.get(pid);
    StatementGroup group = doc.findStatementGroup(wikibaseConstraintPid);
    if (group != null) {
        return group.getStatements().stream().filter(s -> s.getValue() != null && s.getValue() instanceof EntityIdValue).collect(Collectors.toList());
    } else {
        return Collections.emptyList();
    }
}
Also used : StatementRank(org.wikidata.wdtk.datamodel.interfaces.StatementRank) List(java.util.List) Stream(java.util.stream.Stream) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument) EntityCache(org.openrefine.wikidata.utils.EntityCache) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument)

Example 4 with Statement

use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.

the class MultiValueScrutinizer method scrutinize.

@Override
public void scrutinize(TermedStatementEntityEdit update) {
    Map<PropertyIdValue, Integer> propertyCount = new HashMap<>();
    for (Statement statement : update.getAddedStatements()) {
        PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
        List<Statement> statementList = _fetcher.getConstraintsByType(pid, multiValueConstraintQid);
        if (propertyCount.containsKey(pid)) {
            propertyCount.put(pid, propertyCount.get(pid) + 1);
        } else if (!statementList.isEmpty()) {
            propertyCount.put(pid, 1);
        }
    }
    if (update.isNew()) {
        for (PropertyIdValue pid : propertyCount.keySet()) {
            if (propertyCount.get(pid) == 1) {
                QAWarning issue = new QAWarning(new_type, pid.getId(), QAWarning.Severity.WARNING, 1);
                issue.setProperty("property_entity", pid);
                issue.setProperty("example_entity", update.getEntityId());
                addIssue(issue);
            }
        }
    } else {
        for (PropertyIdValue pid : propertyCount.keySet()) {
            if (propertyCount.get(pid) == 1) {
                QAWarning issue = new QAWarning(existing_type, pid.getId(), QAWarning.Severity.INFO, 1);
                issue.setProperty("property_entity", pid);
                issue.setProperty("example_entity", update.getEntityId());
                addIssue(issue);
            }
        }
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) HashMap(java.util.HashMap) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 5 with Statement

use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.

the class QualifierCompatibilityScrutinizer method scrutinize.

@Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
    if (!added) {
        // not scrutinizing deleted statements
        return;
    }
    PropertyIdValue statementProperty = statement.getClaim().getMainSnak().getPropertyId();
    Set<PropertyIdValue> qualifiers = statement.getClaim().getQualifiers().stream().map(e -> e.getProperty()).collect(Collectors.toSet());
    Set<PropertyIdValue> missingQualifiers = mandatoryQualifiers(statementProperty).stream().filter(p -> !qualifiers.contains(p)).collect(Collectors.toSet());
    Set<PropertyIdValue> disallowedQualifiers = qualifiers.stream().filter(p -> !qualifierIsAllowed(statementProperty, p)).collect(Collectors.toSet());
    for (PropertyIdValue missing : missingQualifiers) {
        QAWarning issue = new QAWarning(missingMandatoryQualifiersType, statementProperty.getId() + "-" + missing.getId(), QAWarning.Severity.WARNING, 1);
        issue.setProperty("statement_property_entity", statementProperty);
        issue.setProperty("missing_property_entity", missing);
        issue.setProperty("example_item_entity", entityId);
        addIssue(issue);
    }
    for (PropertyIdValue disallowed : disallowedQualifiers) {
        QAWarning issue = new QAWarning(disallowedQualifiersType, statementProperty.getId() + "-" + disallowed.getId(), QAWarning.Severity.WARNING, 1);
        issue.setProperty("statement_property_entity", statementProperty);
        issue.setProperty("disallowed_property_entity", disallowed);
        issue.setProperty("example_item_entity", entityId);
        addIssue(issue);
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Set(java.util.Set) HashMap(java.util.HashMap) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Collectors(java.util.stream.Collectors) QAWarning(org.openrefine.wikidata.qa.QAWarning) Value(org.wikidata.wdtk.datamodel.interfaces.Value) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Aggregations

Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)82 Test (org.testng.annotations.Test)61 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)59 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)56 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)56 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)56 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)53 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)48 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)42 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)25 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)17 QAWarning (org.openrefine.wikidata.qa.QAWarning)12 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)8 HashSet (java.util.HashSet)7 Value (org.wikidata.wdtk.datamodel.interfaces.Value)7 ArrayList (java.util.ArrayList)6 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)5