Search in sources :

Example 31 with PropertyIdValue

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

the class DistinctValuesScrutinizer method scrutinize.

@Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
    if (!added) {
        // not scrutinizing removed statements
        return;
    }
    Snak mainSnak = statement.getClaim().getMainSnak();
    PropertyIdValue pid = mainSnak.getPropertyId();
    List<Statement> statementList = _fetcher.getConstraintsByType(pid, distinctValuesConstraintQid);
    if (!statementList.isEmpty() && mainSnak instanceof ValueSnak) {
        Value mainSnakValue = ((ValueSnak) mainSnak).getValue();
        Map<Value, EntityIdValue> seen = _seenValues.get(pid);
        if (seen == null) {
            seen = new HashMap<Value, EntityIdValue>();
            _seenValues.put(pid, seen);
        }
        if (seen.containsKey(mainSnakValue)) {
            EntityIdValue otherId = seen.get(mainSnakValue);
            QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
            issue.setProperty("property_entity", pid);
            issue.setProperty("item1_entity", entityId);
            issue.setProperty("item2_entity", otherId);
            addIssue(issue);
        } else {
            seen.put(mainSnakValue, entityId);
        }
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) 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) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 32 with PropertyIdValue

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

the class ItemRequiresScrutinizer method scrutinize.

@Override
public void scrutinize(TermedStatementEntityEdit update) {
    Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>();
    for (Statement statement : update.getAddedStatements()) {
        Snak mainSnak = statement.getClaim().getMainSnak();
        PropertyIdValue pid = mainSnak.getPropertyId();
        Set<Value> values;
        if (mainSnak instanceof ValueSnak) {
            Value value = ((ValueSnak) mainSnak).getValue();
            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> constraintDefinitions = _fetcher.getConstraintsByType(propertyId, itemRequiresConstraintQid);
        for (Statement statement : constraintDefinitions) {
            ItemRequiresConstraint constraint = new ItemRequiresConstraint(statement);
            PropertyIdValue itemRequiresPid = constraint.itemRequiresPid;
            List<Value> itemList = constraint.itemList;
            if (!propertyIdValueValueMap.containsKey(itemRequiresPid)) {
                QAWarning issue = new QAWarning(update.isNew() ? newItemRequirePropertyType : existingItemRequirePropertyType, propertyId.getId() + itemRequiresPid.getId(), update.isNew() ? QAWarning.Severity.WARNING : QAWarning.Severity.INFO, 1);
                issue.setProperty("property_entity", propertyId);
                issue.setProperty("added_property_entity", itemRequiresPid);
                issue.setProperty("example_entity", update.getEntityId());
                addIssue(issue);
            } else if (raiseWarning(propertyIdValueValueMap, itemRequiresPid, itemList)) {
                QAWarning issue = new QAWarning(update.isNew() ? newItemRequireValuesType : existingItemRequireValuesType, propertyId.getId() + itemRequiresPid.getId(), update.isNew() ? QAWarning.Severity.WARNING : QAWarning.Severity.INFO, 1);
                issue.setProperty("property_entity", propertyId);
                issue.setProperty("added_property_entity", itemRequiresPid);
                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 33 with PropertyIdValue

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

the class EditInspector method inspect.

/**
 * Inspect a batch of edits with the registered scrutinizers
 *
 * @param editBatch
 */
public void inspect(List<TermedStatementEntityEdit> editBatch, WikibaseSchema schema) throws ExecutionException {
    // First, schedule them with some scheduler,
    // so that all newly created entities appear in the batch
    SchemaPropertyExtractor fetcher = new SchemaPropertyExtractor();
    Set<PropertyIdValue> properties = fetcher.getAllProperties(schema);
    if (entityCache != null) {
        // Prefetch property documents in one API call rather than requesting them one by one.
        entityCache.getMultipleDocuments(properties.stream().collect(Collectors.toList()));
    }
    WikibaseAPIUpdateScheduler scheduler = new WikibaseAPIUpdateScheduler();
    editBatch = scheduler.schedule(editBatch);
    Map<EntityIdValue, TermedStatementEntityEdit> updates = TermedStatementEntityEdit.groupBySubject(editBatch);
    List<TermedStatementEntityEdit> mergedUpdates = updates.values().stream().collect(Collectors.toList());
    for (EditScrutinizer scrutinizer : scrutinizers.values()) {
        scrutinizer.batchIsBeginning();
    }
    for (TermedStatementEntityEdit update : mergedUpdates) {
        if (!update.isNull()) {
            for (EditScrutinizer scrutinizer : scrutinizers.values()) {
                scrutinizer.scrutinize(update);
            }
        }
    }
    for (EditScrutinizer scrutinizer : scrutinizers.values()) {
        scrutinizer.batchIsFinished();
    }
    if (warningStore.getNbWarnings() == 0) {
        warningStore.addWarning(new QAWarning("no-issue-detected", null, QAWarning.Severity.INFO, 0));
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) WikibaseAPIUpdateScheduler(org.openrefine.wikidata.updates.scheduler.WikibaseAPIUpdateScheduler) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit)

Example 34 with PropertyIdValue

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

the class SchemaPropertyExtractor method getAllProperties.

public Set<PropertyIdValue> getAllProperties(WikibaseSchema schema) {
    Set<PropertyIdValue> properties = new HashSet<>();
    List<WbEntityDocumentExpr> entityDocumentExprs = schema.getEntityDocumentExpressions();
    for (WbEntityDocumentExpr entityDocumentExpr : entityDocumentExprs) {
        List<WbStatementGroupExpr> statementGroups = entityDocumentExpr.getStatementGroups();
        for (WbStatementGroupExpr statementGroup : statementGroups) {
            WbExpression<? extends PropertyIdValue> statementGroupProperty = statementGroup.getProperty();
            if (statementGroupProperty instanceof WbPropConstant) {
                properties.add(Datamodel.makeWikidataPropertyIdValue(((WbPropConstant) statementGroupProperty).getPid()));
            }
            List<WbStatementExpr> statementExprs = statementGroup.getStatements();
            for (WbStatementExpr statementExpr : statementExprs) {
                List<WbSnakExpr> snakExprs = new ArrayList<>(statementExpr.getQualifiers());
                List<WbReferenceExpr> referenceExprs = statementExpr.getReferences();
                for (WbReferenceExpr referenceExpr : referenceExprs) {
                    snakExprs.addAll(referenceExpr.getSnaks());
                }
                for (WbSnakExpr snakExpr : snakExprs) {
                    WbExpression<? extends PropertyIdValue> qualifierProperty = snakExpr.getProp();
                    if (qualifierProperty instanceof WbPropConstant) {
                        properties.add(Datamodel.makeWikidataPropertyIdValue(((WbPropConstant) qualifierProperty).getPid()));
                    }
                }
            }
        }
    }
    return properties;
}
Also used : WbSnakExpr(org.openrefine.wikidata.schema.WbSnakExpr) ArrayList(java.util.ArrayList) WbReferenceExpr(org.openrefine.wikidata.schema.WbReferenceExpr) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) WbStatementGroupExpr(org.openrefine.wikidata.schema.WbStatementGroupExpr) WbStatementExpr(org.openrefine.wikidata.schema.WbStatementExpr) WbEntityDocumentExpr(org.openrefine.wikidata.schema.WbEntityDocumentExpr) WbPropConstant(org.openrefine.wikidata.schema.WbPropConstant) HashSet(java.util.HashSet)

Example 35 with PropertyIdValue

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

the class WbSnakExpr method evaluate.

@Override
public Snak evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException {
    PropertyIdValue propertyId = getProp().evaluate(ctxt);
    Value evaluatedValue = value.evaluate(ctxt);
    return new FullyPropertySerializingValueSnak(propertyId, evaluatedValue);
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) FullyPropertySerializingValueSnak(org.openrefine.wikidata.schema.entityvalues.FullyPropertySerializingValueSnak) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Value(org.wikidata.wdtk.datamodel.interfaces.Value)

Aggregations

PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)36 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)17 QAWarning (org.openrefine.wikidata.qa.QAWarning)16 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)12 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)11 Value (org.wikidata.wdtk.datamodel.interfaces.Value)11 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)9 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)9 List (java.util.List)8 Set (java.util.Set)6 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)6 Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)6 Collectors (java.util.stream.Collectors)5 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)5 Test (org.testng.annotations.Test)5 SkipSchemaExpressionException (org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException)4 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)4