Search in sources :

Example 66 with ItemIdValue

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

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

the class EditBatchProcessor method performEdit.

/**
 * Performs the next edit in the batch.
 *
 * @throws InterruptedException
 */
public void performEdit() throws InterruptedException {
    if (remainingEdits() == 0) {
        return;
    }
    if (batchCursor == currentBatch.size()) {
        prepareNewBatch();
    }
    TermedStatementEntityEdit update = currentBatch.get(batchCursor);
    // Rewrite mentions to new entities
    ReconEntityRewriter rewriter = new ReconEntityRewriter(library, update.getEntityId());
    try {
        update = rewriter.rewrite(update);
    } catch (NewEntityNotCreatedYetException e) {
        logger.warn("Failed to rewrite update on entity " + update.getEntityId() + ". Missing entity: " + e.getMissingEntity() + ". Skipping update.");
        batchCursor++;
        return;
    }
    try {
        // New entities
        if (update.isNew()) {
            ReconEntityIdValue newCell = (ReconEntityIdValue) update.getEntityId();
            // TODO Antonin, 2022-02-11: remove this casting once we have https://github.com/Wikidata/Wikidata-Toolkit/issues/651
            if (newCell instanceof ItemIdValue) {
                update = update.normalizeLabelsAndAliases();
                ItemDocument itemDocument = (ItemDocument) update.toNewEntity();
                ItemDocument createdDoc = editor.createItemDocument(itemDocument, summary, tags);
                library.setId(newCell.getReconInternalId(), createdDoc.getEntityId().getId());
            } else if (newCell instanceof MediaInfoIdValue) {
                update = update.normalizeLabelsAndAliases();
                throw new NotImplementedException();
            }
        } else {
            // Existing entities
            EntityUpdate entityUpdate = update.toEntityUpdate(currentDocs.get(update.getEntityId().getId()));
            editor.editEntityDocument(entityUpdate, false, summary, tags);
        }
    } catch (MediaWikiApiErrorException e) {
        // TODO find a way to report these errors to the user in a nice way
        logger.warn("MediaWiki error while editing [" + e.getErrorCode() + "]: " + e.getErrorMessage());
    } catch (IOException e) {
        logger.warn("IO error while editing: " + e.getMessage());
    }
    batchCursor++;
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) EntityUpdate(org.wikidata.wdtk.datamodel.interfaces.EntityUpdate) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) NewEntityNotCreatedYetException(org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) ReconEntityIdValue(org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue) NotImplementedException(org.apache.commons.lang.NotImplementedException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) IOException(java.io.IOException) MediaWikiApiErrorException(org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException)

Example 68 with ItemIdValue

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

Aggregations

ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)68 Test (org.testng.annotations.Test)61 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)57 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)56 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)54 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)52 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)48 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)44 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)32 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)20 ArrayList (java.util.ArrayList)6 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)5 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)4 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)4 QAWarning (org.openrefine.wikidata.qa.QAWarning)3 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)3 ItemDocument (org.wikidata.wdtk.datamodel.interfaces.ItemDocument)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Collections (java.util.Collections)2