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);
}
}
}
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++;
}
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();
}
Aggregations