Search in sources :

Example 1 with EntityIdValue

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

the class ReconEntityRewriter method rewrite.

/**
 * Rewrite an edit, replacing references to all entities already
 * created by their fresh identifiers. The subject id might not have been
 * created already, in which case it will be left untouched. All the other
 * entities need to have been created already.
 *
 * @param edit
 *      the edit to rewrite
 * @return
 *      the rewritten update
 * @throws NewEntityNotCreatedYetException
 *      if any non-subject entity had not been created yet
 */
public TermedStatementEntityEdit rewrite(TermedStatementEntityEdit edit) throws NewEntityNotCreatedYetException {
    try {
        EntityIdValue subject = (EntityIdValue) copyValue(edit.getEntityId());
        Set<MonolingualTextValue> labels = edit.getLabels().stream().map(l -> copy(l)).collect(Collectors.toSet());
        Set<MonolingualTextValue> labelsIfNew = edit.getLabelsIfNew().stream().map(l -> copy(l)).collect(Collectors.toSet());
        Set<MonolingualTextValue> descriptions = edit.getDescriptions().stream().map(l -> copy(l)).collect(Collectors.toSet());
        Set<MonolingualTextValue> descriptionsIfNew = edit.getDescriptionsIfNew().stream().map(l -> copy(l)).collect(Collectors.toSet());
        Set<MonolingualTextValue> aliases = edit.getAliases().stream().map(l -> copy(l)).collect(Collectors.toSet());
        List<StatementEdit> addedStatements = edit.getStatementEdits().stream().map(l -> copy(l)).collect(Collectors.toList());
        return new TermedStatementEntityEdit(subject, addedStatements, labels, labelsIfNew, descriptions, descriptionsIfNew, aliases);
    } catch (MissingEntityIdFound e) {
        throw new NewEntityNotCreatedYetException(e.value);
    }
}
Also used : NewEntityNotCreatedYetException(org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) DataObjectFactoryImpl(org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl) ReconMediaInfoIdValue(org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Set(java.util.Set) ReconPropertyIdValue(org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Collectors(java.util.stream.Collectors) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) List(java.util.List) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) ReconItemIdValue(org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Datamodel(org.wikidata.wdtk.datamodel.helpers.Datamodel) DatamodelConverter(org.wikidata.wdtk.datamodel.helpers.DatamodelConverter) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) NewEntityNotCreatedYetException(org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) StatementEdit(org.openrefine.wikidata.updates.StatementEdit)

Example 2 with EntityIdValue

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

the class RestrictedValuesScrutinizer method scrutinize.

@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
    if (!added) {
        return;
    }
    PropertyIdValue pid = snak.getPropertyId();
    Value value = null;
    if (snak instanceof ValueSnak) {
        value = ((ValueSnak) snak).getValue();
    }
    List<Statement> allowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, allowedValuesConstraintQid);
    List<Statement> disallowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, disallowedValuesConstraintQid);
    Set<Value> allowedValues = null, disallowedValues = null;
    if (!allowedValueConstraintDefinitions.isEmpty()) {
        AllowedValueConstraint constraint = new AllowedValueConstraint(allowedValueConstraintDefinitions.get(0));
        allowedValues = constraint.allowedValues;
    }
    if (!disallowedValueConstraintDefinitions.isEmpty()) {
        DisallowedValueConstraint constraint = new DisallowedValueConstraint(disallowedValueConstraintDefinitions.get(0));
        disallowedValues = constraint.disallowedValues;
    }
    if ((allowedValues != null && !allowedValues.contains(value)) || (disallowedValues != null && disallowedValues.contains(value))) {
        QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
        issue.setProperty("property_entity", pid);
        issue.setProperty("example_value_entity", value);
        issue.setProperty("example_subject_entity", entityId);
        addIssue(issue);
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) 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) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) QAWarning(org.openrefine.wikidata.qa.QAWarning)

Example 3 with EntityIdValue

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

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

the class ConstraintFetcher method getConstraintStatements.

/**
 * Gets all the constraint statements for a given property
 *
 * @param pid
 *             the id of the property to retrieve the constraints for
 * @return the list of constraint statements
 */
private List<Statement> getConstraintStatements(PropertyIdValue pid) {
    PropertyDocument doc = (PropertyDocument) entityCache.get(pid);
    StatementGroup group = doc.findStatementGroup(wikibaseConstraintPid);
    if (group != null) {
        return group.getStatements().stream().filter(s -> s.getValue() != null && s.getValue() instanceof EntityIdValue).collect(Collectors.toList());
    } else {
        return Collections.emptyList();
    }
}
Also used : StatementRank(org.wikidata.wdtk.datamodel.interfaces.StatementRank) List(java.util.List) Stream(java.util.stream.Stream) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument) EntityCache(org.openrefine.wikidata.utils.EntityCache) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument)

Example 5 with EntityIdValue

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

the class InverseConstraintScrutinizer method batchIsFinished.

@Override
public void batchIsFinished() {
    // For each pair of inverse properties (in each direction)
    for (Entry<PropertyIdValue, PropertyIdValue> propertyPair : _inverse.entrySet()) {
        // Get the statements made for the first
        PropertyIdValue ourProperty = propertyPair.getKey();
        for (Entry<EntityIdValue, Set<EntityIdValue>> itemLinks : _statements.get(ourProperty).entrySet()) {
            // For each outgoing link
            for (EntityIdValue idValue : itemLinks.getValue()) {
                // Check that they are in the statements made for the second
                PropertyIdValue missingProperty = propertyPair.getValue();
                Set<EntityIdValue> reciprocalLinks = _statements.get(missingProperty).get(idValue);
                if (reciprocalLinks == null || !reciprocalLinks.contains(itemLinks.getKey())) {
                    QAWarning issue = new QAWarning(type, ourProperty.getId(), QAWarning.Severity.IMPORTANT, 1);
                    issue.setProperty("added_property_entity", ourProperty);
                    issue.setProperty("inverse_property_entity", missingProperty);
                    issue.setProperty("source_entity", itemLinks.getKey());
                    issue.setProperty("target_entity", idValue);
                    addIssue(issue);
                }
            }
        }
    }
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Set(java.util.Set) HashSet(java.util.HashSet) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) QAWarning(org.openrefine.wikidata.qa.QAWarning)

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