Search in sources :

Example 1 with TermUpdate

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

the class EditBatchProcessorTest method testMultipleBatchesMediaInfo.

@Test
public void testMultipleBatchesMediaInfo() throws MediaWikiApiErrorException, InterruptedException, IOException {
    // Prepare test data
    MonolingualTextValue label = Datamodel.makeMonolingualTextValue("village in Nepal", "en");
    List<MonolingualTextValue> labels = Collections.singletonList(label);
    TermUpdate labelsUpdate = Datamodel.makeTermUpdate(labels, Collections.emptyList());
    List<String> ids = new ArrayList<>();
    for (int i = 124; i < 190; i++) {
        ids.add("M" + String.valueOf(i));
    }
    List<MediaInfoIdValue> mids = ids.stream().map(e -> Datamodel.makeWikimediaCommonsMediaInfoIdValue(e)).collect(Collectors.toList());
    List<TermedStatementEntityEdit> batch = mids.stream().map(mid -> new TermedStatementEntityEditBuilder(mid).addLabel(label, false).build()).collect(Collectors.toList());
    int batchSize = 50;
    List<MediaInfoDocument> fullBatch = mids.stream().map(mid -> Datamodel.makeMediaInfoDocument(mid)).collect(Collectors.toList());
    List<MediaInfoDocument> firstBatch = fullBatch.subList(0, batchSize);
    List<MediaInfoDocument> secondBatch = fullBatch.subList(batchSize, fullBatch.size());
    when(fetcher.getEntityDocuments(toMids(firstBatch))).thenReturn(toMapMediaInfo(firstBatch));
    when(fetcher.getEntityDocuments(toMids(secondBatch))).thenReturn(toMapMediaInfo(secondBatch));
    // Run edits
    EditBatchProcessor processor = new EditBatchProcessor(fetcher, editor, batch, library, summary, maxlag, tags, batchSize);
    assertEquals(0, processor.progress());
    for (int i = 124; i < 190; i++) {
        assertEquals(processor.remainingEdits(), 190 - i);
        processor.performEdit();
    }
    assertEquals(0, processor.remainingEdits());
    assertEquals(100, processor.progress());
    // Check result
    assertEquals(new NewEntityLibrary(), library);
    verify(fetcher, times(1)).getEntityDocuments(toMids(firstBatch));
    verify(fetcher, times(1)).getEntityDocuments(toMids(secondBatch));
    for (MediaInfoDocument doc : fullBatch) {
        StatementUpdate statementUpdate = Datamodel.makeStatementUpdate(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
        verify(editor, times(1)).editEntityDocument(Datamodel.makeMediaInfoUpdate((MediaInfoIdValue) doc.getEntityId(), doc.getRevisionId(), labelsUpdate, statementUpdate), false, summary, tags);
    }
}
Also used : Arrays(java.util.Arrays) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) MediaInfoDocument(org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument) EntityDocument(org.wikidata.wdtk.datamodel.interfaces.EntityDocument) ArrayList(java.util.ArrayList) TermUpdate(org.wikidata.wdtk.datamodel.interfaces.TermUpdate) WikibaseDataFetcher(org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Map(java.util.Map) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) BeforeMethod(org.testng.annotations.BeforeMethod) ItemDocumentBuilder(org.wikidata.wdtk.datamodel.helpers.ItemDocumentBuilder) WikibaseDataEditor(org.wikidata.wdtk.wikibaseapi.WikibaseDataEditor) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) Mockito.verify(org.mockito.Mockito.verify) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) List(java.util.List) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Datamodel(org.wikidata.wdtk.datamodel.helpers.Datamodel) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) MediaWikiApiErrorException(org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException) Collections(java.util.Collections) TestingData(org.openrefine.wikidata.testing.TestingData) Mockito.mock(org.mockito.Mockito.mock) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) MediaInfoDocument(org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) ArrayList(java.util.ArrayList) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) TermUpdate(org.wikidata.wdtk.datamodel.interfaces.TermUpdate) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Example 2 with TermUpdate

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

the class TermedStatementEntityEdit method toEntityUpdate.

/**
 * In case the subject id is not new, returns the corresponding update given
 * the current state of the entity.
 */
public EntityUpdate toEntityUpdate(EntityDocument entityDocument) {
    Validate.isFalse(isNew(), "Cannot create a corresponding entity update for a creation of a new entity.");
    if (id instanceof ItemIdValue) {
        ItemDocument itemDocument = (ItemDocument) entityDocument;
        // Labels
        List<MonolingualTextValue> labels = getLabels().stream().collect(Collectors.toList());
        labels.addAll(getLabelsIfNew().stream().filter(label -> !itemDocument.getLabels().containsKey(label.getLanguageCode())).collect(Collectors.toList()));
        TermUpdate labelUpdate = Datamodel.makeTermUpdate(labels, Collections.emptyList());
        // Descriptions
        List<MonolingualTextValue> descriptions = getDescriptions().stream().collect(Collectors.toList());
        descriptions.addAll(getDescriptionsIfNew().stream().filter(desc -> !itemDocument.getDescriptions().containsKey(desc.getLanguageCode())).collect(Collectors.toList()));
        TermUpdate descriptionUpdate = Datamodel.makeTermUpdate(descriptions, Collections.emptyList());
        // Aliases
        Set<MonolingualTextValue> aliases = getAliases();
        Map<String, List<MonolingualTextValue>> aliasesMap = aliases.stream().collect(Collectors.groupingBy(MonolingualTextValue::getLanguageCode));
        Map<String, AliasUpdate> aliasMap = aliasesMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> Datamodel.makeAliasUpdate(e.getValue(), Collections.emptyList())));
        // Statements
        StatementUpdate statementUpdate = toStatementUpdate(itemDocument);
        return Datamodel.makeItemUpdate((ItemIdValue) getEntityId(), entityDocument.getRevisionId(), labelUpdate, descriptionUpdate, aliasMap, statementUpdate, Collections.emptyList(), Collections.emptyList());
    } else if (id instanceof MediaInfoIdValue) {
        MediaInfoDocument mediaInfoDocument = (MediaInfoDocument) entityDocument;
        // Labels (captions)
        List<MonolingualTextValue> labels = getLabels().stream().collect(Collectors.toList());
        labels.addAll(getLabelsIfNew().stream().filter(label -> !mediaInfoDocument.getLabels().containsKey(label.getLanguageCode())).collect(Collectors.toList()));
        TermUpdate labelUpdate = Datamodel.makeTermUpdate(labels, Collections.emptyList());
        // Statements
        StatementUpdate statementUpdate = toStatementUpdate(mediaInfoDocument);
        return Datamodel.makeMediaInfoUpdate((MediaInfoIdValue) id, entityDocument.getRevisionId(), labelUpdate, statementUpdate);
    } else {
        throw new NotImplementedException("Editing entities of type " + id.getEntityType() + " is not supported yet.");
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StatementUpdateBuilder(org.wikidata.wdtk.datamodel.helpers.StatementUpdateBuilder) HashMap(java.util.HashMap) MediaInfoDocument(org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument) EntityDocument(org.wikidata.wdtk.datamodel.interfaces.EntityDocument) ArrayList(java.util.ArrayList) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) TermUpdate(org.wikidata.wdtk.datamodel.interfaces.TermUpdate) HashSet(java.util.HashSet) StatementEditingMode(org.openrefine.wikidata.schema.strategies.StatementEditingMode) TermedStatementDocument(org.wikidata.wdtk.datamodel.interfaces.TermedStatementDocument) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Map(java.util.Map) NotImplementedException(org.apache.commons.lang.NotImplementedException) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) EntityUpdate(org.wikidata.wdtk.datamodel.interfaces.EntityUpdate) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) StatementDocument(org.wikidata.wdtk.datamodel.interfaces.StatementDocument) LinkedList(java.util.LinkedList) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) Validate(org.jsoup.helper.Validate) Collection(java.util.Collection) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Set(java.util.Set) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Collectors(java.util.stream.Collectors) AliasUpdate(org.wikidata.wdtk.datamodel.interfaces.AliasUpdate) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) List(java.util.List) Datamodel(org.wikidata.wdtk.datamodel.helpers.Datamodel) Entry(java.util.Map.Entry) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) Collections(java.util.Collections) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) MediaInfoDocument(org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument) NotImplementedException(org.apache.commons.lang.NotImplementedException) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AliasUpdate(org.wikidata.wdtk.datamodel.interfaces.AliasUpdate) TermUpdate(org.wikidata.wdtk.datamodel.interfaces.TermUpdate)

Aggregations

ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Datamodel (org.wikidata.wdtk.datamodel.helpers.Datamodel)2 EntityDocument (org.wikidata.wdtk.datamodel.interfaces.EntityDocument)2 ItemDocument (org.wikidata.wdtk.datamodel.interfaces.ItemDocument)2 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)2 MediaInfoDocument (org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument)2 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)2 MonolingualTextValue (org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue)2 StatementUpdate (org.wikidata.wdtk.datamodel.interfaces.StatementUpdate)2 TermUpdate (org.wikidata.wdtk.datamodel.interfaces.TermUpdate)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1