Search in sources :

Example 16 with EntityIdValue

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

Example 17 with EntityIdValue

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

the class InverseConstraintScrutinizer method getInverseConstraint.

protected PropertyIdValue getInverseConstraint(PropertyIdValue pid) {
    if (_inverse.containsKey(pid)) {
        return _inverse.get(pid);
    } else {
        PropertyIdValue inversePid = null;
        List<Statement> statementList = _fetcher.getConstraintsByType(pid, inverseConstraintQid);
        if (!statementList.isEmpty()) {
            InverseConstraint constraint = new InverseConstraint(statementList.get(0));
            inversePid = constraint.propertyParameterValue;
        }
        if (inversePid == null && !_fetcher.getConstraintsByType(pid, symmetricConstraintQid).isEmpty()) {
            inversePid = pid;
        }
        _inverse.put(pid, inversePid);
        _statements.put(pid, new HashMap<EntityIdValue, Set<EntityIdValue>>());
        // the inverse constraints are consistent on Wikidata.
        if (inversePid != null && !_inverse.containsKey(inversePid)) {
            _inverse.put(inversePid, pid);
            _statements.put(inversePid, new HashMap<EntityIdValue, Set<EntityIdValue>>());
        }
        return inversePid;
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Set(java.util.Set) HashSet(java.util.HashSet) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)

Example 18 with EntityIdValue

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

the class WbEntityVariable method fromCell.

@Override
public EntityIdValue fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException {
    if (cell.recon != null && (Judgment.Matched.equals(cell.recon.judgment) || Judgment.New.equals(cell.recon.judgment))) {
        if (Judgment.New.equals(cell.recon.judgment)) {
            return new ReconItemIdValue(cell.recon, cell.value.toString());
        }
        EntityIdValue entityIdValue = EntityIdValueImpl.fromId(cell.recon.match.id, cell.recon.identifierSpace);
        EntityIdValue reconEntityIdValue = null;
        String entityType = null;
        if (entityIdValue instanceof ItemIdValue) {
            reconEntityIdValue = new ReconItemIdValue(cell.recon, cell.value.toString());
            entityType = "item";
        } else if (entityIdValue instanceof MediaInfoIdValue) {
            reconEntityIdValue = new ReconMediaInfoIdValue(cell.recon, cell.value.toString());
            entityType = "mediainfo";
        } else if (entityIdValue instanceof PropertyIdValue) {
            reconEntityIdValue = new ReconPropertyIdValue(cell.recon, cell.value.toString());
            entityType = "property";
        }
        if (reconEntityIdValue == null) {
            throw new SkipSchemaExpressionException();
        }
        if (cell.recon.identifierSpace == null || !cell.recon.identifierSpace.equals(ctxt.getBaseIRIForEntityType(entityType))) {
            QAWarning warning = new QAWarning("invalid-identifier-space", null, QAWarning.Severity.INFO, 1);
            warning.setProperty("example_cell", cell.value.toString());
            warning.setProperty("expected_site_iri", ctxt.getBaseIRIForEntityType(entityType));
            ctxt.addWarning(warning);
            throw new SkipSchemaExpressionException();
        }
        return reconEntityIdValue;
    }
    throw new SkipSchemaExpressionException();
}
Also used : ReconItemIdValue(org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) ReconPropertyIdValue(org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue) ReconItemIdValue(org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue) ReconMediaInfoIdValue(org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) SkipSchemaExpressionException(org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) ReconPropertyIdValue(org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue) QAWarning(org.openrefine.wikidata.qa.QAWarning) ReconMediaInfoIdValue(org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue)

Example 19 with EntityIdValue

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

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

Aggregations

EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)20 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)11 QAWarning (org.openrefine.wikidata.qa.QAWarning)6 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)6 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)6 HashSet (java.util.HashSet)5 Value (org.wikidata.wdtk.datamodel.interfaces.Value)5 Set (java.util.Set)4 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)4 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ReconEntityIdValue (org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue)3 SkipSchemaExpressionException (org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException)3 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)3 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)3 ReconItemIdValue (org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue)2 ReconMediaInfoIdValue (org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue)2