Search in sources :

Example 26 with TermedStatementEntityEdit

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

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

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

Example 29 with TermedStatementEntityEdit

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

the class RestrictedPositionScrutinizerTest method testNoProblem.

@Test
public void testNoProblem() {
    ItemIdValue idA = TestingData.existingId;
    Snak mainSnak = Datamodel.makeSomeValueSnak(propertyIdValue);
    Statement statement = new StatementImpl("P22", mainSnak, idA);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement)).build();
    Snak qualifierSnak = Datamodel.makeValueSnak(propertyScopeParameter, asMainSnak);
    List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
    List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(propertyIdValue, SCOPE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(update);
    assertNoWarningRaised();
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Test(org.testng.annotations.Test)

Example 30 with TermedStatementEntityEdit

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

the class RestrictedPositionScrutinizerTest method testTriggerMainSnak.

@Test
public void testTriggerMainSnak() {
    ItemIdValue idA = TestingData.existingId;
    Snak mainSnak = Datamodel.makeSomeValueSnak(propertyIdValue);
    Statement statement = new StatementImpl("P22", mainSnak, idA);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement)).build();
    Snak qualifierSnak = Datamodel.makeValueSnak(propertyScopeParameter, asQualifier);
    List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
    List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(propertyIdValue, SCOPE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(update);
    assertWarningsRaised("property-found-in-mainsnak");
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Test(org.testng.annotations.Test)

Aggregations

TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)109 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)102 Test (org.testng.annotations.Test)97 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)59 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)57 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)52 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)49 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)44 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)34 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)18 WikidataRefineTest (org.openrefine.wikidata.testing.WikidataRefineTest)13 ArrayList (java.util.ArrayList)12 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)9 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)6 ReconEntityIdValue (org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue)5 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)5 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)5 IOException (java.io.IOException)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4