Search in sources :

Example 1 with SkipSchemaExpressionException

use of org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException in project OpenRefine by OpenRefine.

the class WbEntityDocumentExpr method evaluate.

@Override
public TermedStatementEntityEdit evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException {
    EntityIdValue subjectId = getSubject().evaluate(ctxt);
    TermedStatementEntityEditBuilder update = new TermedStatementEntityEditBuilder(subjectId);
    for (WbStatementGroupExpr expr : getStatementGroups()) {
        try {
            StatementGroupEdit statementGroupUpdate = expr.evaluate(ctxt, subjectId);
            // TODO also store statement groups in TermedStatementEntityUpdate?
            for (StatementEdit s : statementGroupUpdate.getStatementEdits()) {
                update.addStatement(s);
            }
        } catch (SkipSchemaExpressionException e) {
            continue;
        }
    }
    for (WbNameDescExpr expr : getNameDescs()) {
        expr.contributeTo(update, ctxt);
    }
    return update.build();
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) StatementGroupEdit(org.openrefine.wikidata.updates.StatementGroupEdit) SkipSchemaExpressionException(org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) StatementEdit(org.openrefine.wikidata.updates.StatementEdit)

Example 2 with SkipSchemaExpressionException

use of org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException in project OpenRefine by OpenRefine.

the class WbQuantityExpr method evaluate.

@Override
public QuantityValue evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException {
    StringValue amount = getAmountExpr().evaluate(ctxt);
    // we know the amount is nonnull, nonempty here
    BigDecimal parsedAmount = null;
    BigDecimal lowerBound = null;
    BigDecimal upperBound = null;
    String originalAmount = amount.getString().toUpperCase();
    try {
        parsedAmount = new BigDecimal(originalAmount);
        if (originalAmount.contains("E")) {
            // engineering notation: we derive the precision from
            // the expression (feature!)
            BigDecimal uncertainty = new BigDecimal("0.5").scaleByPowerOfTen(-parsedAmount.scale());
            lowerBound = new BigDecimal(parsedAmount.subtract(uncertainty).toPlainString());
            upperBound = new BigDecimal(parsedAmount.add(uncertainty).toPlainString());
        }
        // workaround for https://github.com/Wikidata/Wikidata-Toolkit/issues/341
        parsedAmount = new BigDecimal(parsedAmount.toPlainString());
    } catch (NumberFormatException e) {
        if (!originalAmount.isEmpty()) {
            QAWarning issue = new QAWarning("ignored-amount", null, QAWarning.Severity.WARNING, 1);
            issue.setProperty("example_value", originalAmount);
            ctxt.addWarning(issue);
        }
        throw new SkipSchemaExpressionException();
    }
    if (getUnitExpr() != null) {
        ItemIdValue unit = getUnitExpr().evaluate(ctxt);
        return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound, unit);
    }
    return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) SkipSchemaExpressionException(org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException) StringValue(org.wikidata.wdtk.datamodel.interfaces.StringValue) QAWarning(org.openrefine.wikidata.qa.QAWarning) BigDecimal(java.math.BigDecimal)

Example 3 with SkipSchemaExpressionException

use of org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException 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 4 with SkipSchemaExpressionException

use of org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException in project OpenRefine by OpenRefine.

the class WbItemVariable method fromCell.

@Override
public ItemIdValue fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException {
    if (cell.recon != null && (Judgment.Matched.equals(cell.recon.judgment) || Judgment.New.equals(cell.recon.judgment))) {
        if (cell.recon.identifierSpace == null || !cell.recon.identifierSpace.equals(ctxt.getBaseIRI())) {
            QAWarning warning = new QAWarning("invalid-identifier-space", null, QAWarning.Severity.INFO, 1);
            warning.setProperty("example_cell", cell.value.toString());
            ctxt.addWarning(warning);
            throw new SkipSchemaExpressionException();
        }
        return new ReconItemIdValue(cell.recon, cell.value.toString());
    }
    throw new SkipSchemaExpressionException();
}
Also used : ReconItemIdValue(org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue) SkipSchemaExpressionException(org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 5 with SkipSchemaExpressionException

use of org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException 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

SkipSchemaExpressionException (org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException)7 QAWarning (org.openrefine.wikidata.qa.QAWarning)5 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)3 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)3 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)3 ArrayList (java.util.ArrayList)2 ReconItemIdValue (org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue)2 StatementGroupEdit (org.openrefine.wikidata.updates.StatementGroupEdit)2 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)2 BigDecimal (java.math.BigDecimal)1 ReconMediaInfoIdValue (org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue)1 ReconPropertyIdValue (org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue)1 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)1 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)1 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)1 Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)1 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)1 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)1 StatementRank (org.wikidata.wdtk.datamodel.interfaces.StatementRank)1 StringValue (org.wikidata.wdtk.datamodel.interfaces.StringValue)1