Search in sources :

Example 6 with EntityIdValue

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

the class QualifierCompatibilityScrutinizer method scrutinize.

@Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
    if (!added) {
        // not scrutinizing deleted statements
        return;
    }
    PropertyIdValue statementProperty = statement.getClaim().getMainSnak().getPropertyId();
    Set<PropertyIdValue> qualifiers = statement.getClaim().getQualifiers().stream().map(e -> e.getProperty()).collect(Collectors.toSet());
    Set<PropertyIdValue> missingQualifiers = mandatoryQualifiers(statementProperty).stream().filter(p -> !qualifiers.contains(p)).collect(Collectors.toSet());
    Set<PropertyIdValue> disallowedQualifiers = qualifiers.stream().filter(p -> !qualifierIsAllowed(statementProperty, p)).collect(Collectors.toSet());
    for (PropertyIdValue missing : missingQualifiers) {
        QAWarning issue = new QAWarning(missingMandatoryQualifiersType, statementProperty.getId() + "-" + missing.getId(), QAWarning.Severity.WARNING, 1);
        issue.setProperty("statement_property_entity", statementProperty);
        issue.setProperty("missing_property_entity", missing);
        issue.setProperty("example_item_entity", entityId);
        addIssue(issue);
    }
    for (PropertyIdValue disallowed : disallowedQualifiers) {
        QAWarning issue = new QAWarning(disallowedQualifiersType, statementProperty.getId() + "-" + disallowed.getId(), QAWarning.Severity.WARNING, 1);
        issue.setProperty("statement_property_entity", statementProperty);
        issue.setProperty("disallowed_property_entity", disallowed);
        issue.setProperty("example_item_entity", entityId);
        addIssue(issue);
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Set(java.util.Set) HashMap(java.util.HashMap) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Collectors(java.util.stream.Collectors) QAWarning(org.openrefine.wikidata.qa.QAWarning) Value(org.wikidata.wdtk.datamodel.interfaces.Value) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 7 with EntityIdValue

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

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

the class QuickStatementsUpdateScheduler method splitUpdate.

/**
 * Separates out the statements which refer to new entities from the rest of the
 * update. The resulting updates are stored in {@link referencingUpdates} and
 * {@link updatesWithoutReferences}.
 *
 * @param update
 * @throws ImpossibleSchedulingException
 *             if two new entity ids are referred to in the same statement
 */
protected void splitUpdate(TermedStatementEntityEdit update) throws ImpossibleSchedulingException {
    TermedStatementEntityEditBuilder remainingUpdateBuilder = new TermedStatementEntityEditBuilder(update.getEntityId()).addLabels(update.getLabels(), true).addLabels(update.getLabelsIfNew(), false).addDescriptions(update.getDescriptions(), true).addDescriptions(update.getDescriptionsIfNew(), false).addAliases(update.getAliases());
    Map<EntityIdValue, TermedStatementEntityEditBuilder> referencingUpdates = new HashMap<>();
    for (StatementEdit statement : update.getStatementEdits()) {
        Set<ReconEntityIdValue> pointers = extractor.extractPointers(statement.getStatement());
        if (pointers.isEmpty()) {
            remainingUpdateBuilder.addStatement(statement);
        } else if (pointers.size() == 1 && !update.isNew()) {
            EntityIdValue pointer = pointers.stream().findFirst().get();
            TermedStatementEntityEditBuilder referencingBuilder = referencingUpdates.get(pointer);
            if (referencingBuilder == null) {
                referencingBuilder = new TermedStatementEntityEditBuilder(update.getEntityId());
            }
            referencingBuilder.addStatement(statement);
            referencingUpdates.put(pointer, referencingBuilder);
        } else if (pointers.size() == 1 && pointers.stream().findFirst().get().equals(update.getEntityId())) {
            remainingUpdateBuilder.addStatement(statement);
        } else {
            throw new ImpossibleSchedulingException();
        }
    }
    // Add the update that is not referring to anything to the schedule
    TermedStatementEntityEdit pointerFree = remainingUpdateBuilder.build();
    if (!pointerFree.isNull()) {
        pointerFreeUpdates.add(pointerFree);
    }
    // Add the other updates to the map
    for (Entry<EntityIdValue, TermedStatementEntityEditBuilder> entry : referencingUpdates.entrySet()) {
        TermedStatementEntityEdit pointerUpdate = entry.getValue().build();
        UpdateSequence pointerUpdatesForKey = pointerUpdates.get(entry.getKey());
        if (pointerUpdatesForKey == null) {
            pointerUpdatesForKey = new UpdateSequence();
        }
        pointerUpdatesForKey.add(pointerUpdate);
        pointerUpdates.put(entry.getKey(), pointerUpdatesForKey);
    }
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) HashMap(java.util.HashMap) ReconEntityIdValue(org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) ReconEntityIdValue(org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) StatementEdit(org.openrefine.wikidata.updates.StatementEdit)

Example 9 with EntityIdValue

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

the class QuickStatementsUpdateScheduler method schedule.

@Override
public List<TermedStatementEntityEdit> schedule(List<TermedStatementEntityEdit> updates) throws ImpossibleSchedulingException {
    pointerUpdates = new HashMap<>();
    pointerFreeUpdates = new UpdateSequence();
    for (TermedStatementEntityEdit update : updates) {
        splitUpdate(update);
    }
    // Reconstruct
    List<TermedStatementEntityEdit> fullSchedule = new ArrayList<>();
    Set<EntityIdValue> mentionedNewEntities = new HashSet<>(pointerUpdates.keySet());
    for (TermedStatementEntityEdit update : pointerFreeUpdates.getUpdates()) {
        fullSchedule.add(update);
        UpdateSequence backPointers = pointerUpdates.get(update.getEntityId());
        if (backPointers != null) {
            fullSchedule.addAll(backPointers.getUpdates());
        }
        mentionedNewEntities.remove(update.getEntityId());
    }
    // as the entities would remain blank in this batch).
    for (EntityIdValue missingId : mentionedNewEntities) {
        fullSchedule.add(new TermedStatementEntityEditBuilder(missingId).build());
        fullSchedule.addAll(pointerUpdates.get(missingId).getUpdates());
    }
    return fullSchedule;
}
Also used : TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) ArrayList(java.util.ArrayList) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) ReconEntityIdValue(org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue) HashSet(java.util.HashSet)

Example 10 with EntityIdValue

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

the class UpdateSequence method add.

/**
 * Adds a new update to the list, merging it with any existing one with the same
 * subject.
 *
 * @param update
 */
public void add(TermedStatementEntityEdit update) {
    EntityIdValue subject = update.getEntityId();
    if (index.containsKey(subject)) {
        int i = index.get(subject);
        TermedStatementEntityEdit oldUpdate = updates.get(i);
        updates.set(i, oldUpdate.merge(update));
    } else {
        index.put(subject, updates.size());
        updates.add(update);
    }
}
Also used : 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