Search in sources :

Example 1 with Value

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

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

the class WbStatementExpr method evaluate.

public StatementEdit evaluate(ExpressionContext ctxt, EntityIdValue subject, PropertyIdValue propertyId) throws SkipSchemaExpressionException {
    Snak mainSnak = null;
    if (mainSnakValueExpr != null) {
        Value mainSnakValue = mainSnakValueExpr.evaluate(ctxt);
        mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue);
    } else {
        // hack to make sure we have a non-null snak
        mainSnak = Datamodel.makeNoValueSnak(propertyId);
    }
    // evaluate qualifiers
    List<Snak> qualifiers = new ArrayList<Snak>(getQualifiers().size());
    for (WbSnakExpr qExpr : getQualifiers()) {
        try {
            qualifiers.add(qExpr.evaluate(ctxt));
        } catch (SkipSchemaExpressionException e) {
            QAWarning warning = new QAWarning("ignored-qualifiers", null, QAWarning.Severity.INFO, 1);
            warning.setProperty("example_entity", subject);
            warning.setProperty("example_property_entity", mainSnak.getPropertyId());
            ctxt.addWarning(warning);
        }
    }
    List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
    Claim claim = Datamodel.makeClaim(subject, mainSnak, groupedQualifiers);
    // evaluate references
    List<Reference> references = new ArrayList<Reference>();
    for (WbReferenceExpr rExpr : getReferences()) {
        try {
            references.add(rExpr.evaluate(ctxt));
        } catch (SkipSchemaExpressionException e) {
            QAWarning warning = new QAWarning("ignored-references", null, QAWarning.Severity.INFO, 1);
            warning.setProperty("example_entity", subject);
            warning.setProperty("example_property_entity", mainSnak.getPropertyId());
            ctxt.addWarning(warning);
        }
    }
    StatementRank rank = StatementRank.NORMAL;
    return new StatementEdit(Datamodel.makeStatement(claim, references, rank, ""), merger, mode);
}
Also used : Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) SkipSchemaExpressionException(org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) StatementRank(org.wikidata.wdtk.datamodel.interfaces.StatementRank) ArrayList(java.util.ArrayList) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Value(org.wikidata.wdtk.datamodel.interfaces.Value) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) QAWarning(org.openrefine.wikidata.qa.QAWarning) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim)

Example 3 with Value

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

the class SnakOnlyStatementMerger method match.

/**
 * Matches two snaks using the underlying value matcher.
 * The snaks must have the same property id to match.
 *
 * @param existingSnak
 * @param addedSnak
 * @return
 */
public boolean match(Snak existingSnak, Snak addedSnak) {
    // Deliberately only comparing the pids and not the siteIRIs to avoid spurious mismatches due to federation
    if (!existingSnak.getPropertyId().getId().equals(addedSnak.getPropertyId().getId())) {
        return false;
    } else if (existingSnak instanceof NoValueSnak && addedSnak instanceof NoValueSnak) {
        return true;
    } else if (existingSnak instanceof SomeValueSnak && addedSnak instanceof SomeValueSnak) {
        return true;
    } else {
        Value existingValue = ((ValueSnak) existingSnak).getValue();
        Value addedValue = ((ValueSnak) addedSnak).getValue();
        return valueMatcher.match(existingValue, addedValue);
    }
}
Also used : NoValueSnak(org.wikidata.wdtk.datamodel.interfaces.NoValueSnak) Value(org.wikidata.wdtk.datamodel.interfaces.Value) SomeValueSnak(org.wikidata.wdtk.datamodel.interfaces.SomeValueSnak) SomeValueSnak(org.wikidata.wdtk.datamodel.interfaces.SomeValueSnak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) NoValueSnak(org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)

Example 4 with Value

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

the class ConflictsWithScrutinizer method scrutinize.

@Override
public void scrutinize(TermedStatementEntityEdit update) {
    Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>();
    for (Statement statement : update.getAddedStatements()) {
        PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
        Value value = null;
        Snak mainSnak = statement.getClaim().getMainSnak();
        if (mainSnak instanceof ValueSnak) {
            value = ((ValueSnak) mainSnak).getValue();
        }
        Set<Value> values;
        if (value != null) {
            if (propertyIdValueValueMap.containsKey(pid)) {
                values = propertyIdValueValueMap.get(pid);
            } else {
                values = new HashSet<>();
            }
            values.add(value);
            propertyIdValueValueMap.put(pid, values);
        }
    }
    for (PropertyIdValue propertyId : propertyIdValueValueMap.keySet()) {
        List<Statement> statementList = _fetcher.getConstraintsByType(propertyId, conflictsWithConstraintQid);
        for (Statement statement : statementList) {
            ConflictsWithConstraint constraint = new ConflictsWithConstraint(statement);
            PropertyIdValue conflictingPid = constraint.conflictingPid;
            List<Value> itemList = constraint.itemList;
            if (propertyIdValueValueMap.containsKey(conflictingPid) && raiseWarning(propertyIdValueValueMap, conflictingPid, itemList)) {
                QAWarning issue = new QAWarning(type, propertyId.getId() + conflictingPid.getId(), QAWarning.Severity.WARNING, 1);
                issue.setProperty("property_entity", propertyId);
                issue.setProperty("added_property_entity", conflictingPid);
                issue.setProperty("example_entity", update.getEntityId());
                addIssue(issue);
            }
        }
    }
}
Also used : Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Value(org.wikidata.wdtk.datamodel.interfaces.Value) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 5 with Value

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

the class InverseConstraintScrutinizer method scrutinize.

@Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
    if (!added) {
        // TODO support for deleted statements
        return;
    }
    Snak mainSnak = statement.getClaim().getMainSnak();
    if (!(mainSnak instanceof ValueSnak)) {
        return;
    }
    Value mainSnakValue = ((ValueSnak) mainSnak).getValue();
    if (mainSnakValue instanceof ItemIdValue) {
        PropertyIdValue pid = mainSnak.getPropertyId();
        PropertyIdValue inversePid = getInverseConstraint(pid);
        if (inversePid != null) {
            EntityIdValue targetEntityId = (EntityIdValue) mainSnakValue;
            Set<EntityIdValue> currentValues = _statements.get(pid).get(entityId);
            if (currentValues == null) {
                currentValues = new HashSet<EntityIdValue>();
                _statements.get(pid).put(entityId, currentValues);
            }
            currentValues.add(targetEntityId);
        }
    }
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Value(org.wikidata.wdtk.datamodel.interfaces.Value) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak)

Aggregations

Value (org.wikidata.wdtk.datamodel.interfaces.Value)9 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)8 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)7 QAWarning (org.openrefine.wikidata.qa.QAWarning)6 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)6 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)5 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)4 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)2 List (java.util.List)1 FullyPropertySerializingValueSnak (org.openrefine.wikidata.schema.entityvalues.FullyPropertySerializingValueSnak)1 SkipSchemaExpressionException (org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException)1 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)1 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)1 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)1 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)1 Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)1