Search in sources :

Example 1 with StatementEdit

use of org.openrefine.wikidata.updates.StatementEdit 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 StatementEdit

use of org.openrefine.wikidata.updates.StatementEdit 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 3 with StatementEdit

use of org.openrefine.wikidata.updates.StatementEdit 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 StatementEdit

use of org.openrefine.wikidata.updates.StatementEdit in project OpenRefine by OpenRefine.

the class QuickStatementsExporterTest method testNoValue.

@Test
public void testNoValue() throws IOException {
    PropertyIdValue pid = Datamodel.makeWikidataPropertyIdValue("P123");
    Claim claim = Datamodel.makeClaim(qid1, Datamodel.makeNoValueSnak(pid), Collections.emptyList());
    Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
    StatementEdit statementUpdate = new StatementEdit(statement, StatementMerger.FORMER_DEFAULT_STRATEGY, StatementEditingMode.ADD_OR_MERGE);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(qid1).addStatement(statementUpdate).build();
    assertEquals("Q1377\tP123\tnovalue\n", export(update));
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Example 5 with StatementEdit

use of org.openrefine.wikidata.updates.StatementEdit in project OpenRefine by OpenRefine.

the class QuickStatementsExporterTest method testSomeValue.

@Test
public void testSomeValue() throws IOException {
    PropertyIdValue pid = Datamodel.makeWikidataPropertyIdValue("P123");
    Claim claim = Datamodel.makeClaim(qid1, Datamodel.makeSomeValueSnak(pid), Collections.emptyList());
    Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
    StatementEdit statementUpdate = new StatementEdit(statement, StatementMerger.FORMER_DEFAULT_STRATEGY, StatementEditingMode.ADD_OR_MERGE);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(qid1).addStatement(statementUpdate).build();
    assertEquals("Q1377\tP123\tsomevalue\n", export(update));
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Aggregations

StatementEdit (org.openrefine.wikidata.updates.StatementEdit)17 Test (org.testng.annotations.Test)10 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)9 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)9 WikidataRefineTest (org.openrefine.wikidata.testing.WikidataRefineTest)6 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)6 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)5 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)5 JacksonSerializationTest (org.openrefine.wikidata.testing.JacksonSerializationTest)4 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)4 SkipSchemaExpressionException (org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException)3 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)3 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)3 ArrayList (java.util.ArrayList)2 ReconEntityIdValue (org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue)2 StatementGroupEdit (org.openrefine.wikidata.updates.StatementGroupEdit)2 Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Set (java.util.Set)1