Search in sources :

Example 1 with EntityDocument

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

the class EntityCacheTests method testGetAll.

@Test
public void testGetAll() throws MediaWikiApiErrorException, IOException, ExecutionException {
    WikibaseDataFetcher fetcher = mock(WikibaseDataFetcher.class);
    PropertyIdValue idA = Datamodel.makeWikidataPropertyIdValue("P42");
    PropertyIdValue idB = Datamodel.makeWikidataPropertyIdValue("P43");
    PropertyIdValue idC = Datamodel.makeWikidataPropertyIdValue("P44");
    PropertyIdValue idD = Datamodel.makeWikidataPropertyIdValue("P45");
    PropertyDocument docA = Datamodel.makePropertyDocument(idA, Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_GEO_SHAPE));
    PropertyDocument docB = Datamodel.makePropertyDocument(idB, Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_GEO_SHAPE));
    PropertyDocument docC = Datamodel.makePropertyDocument(idC, Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_GEO_SHAPE));
    PropertyDocument docD = Datamodel.makePropertyDocument(idD, Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_GEO_SHAPE));
    EntityCache SUT = new EntityCache(fetcher);
    List<String> entityIdListA = Arrays.asList(idA.getId(), idB.getId());
    List<String> entityIdListB = Arrays.asList(idC.getId(), idD.getId());
    List<String> entityIdListC = Arrays.asList(idB.getId(), idC.getId());
    List<EntityDocument> docListA = Arrays.asList(docA, docB);
    List<EntityDocument> docListB = Arrays.asList(docC, docD);
    List<EntityDocument> docListC = Arrays.asList(docB, docC);
    Map<String, EntityDocument> docMapA = new HashMap<>();
    docMapA.put(idA.getId(), docA);
    docMapA.put(idB.getId(), docB);
    Map<String, EntityDocument> docMapB = new HashMap<>();
    docMapB.put(idC.getId(), docC);
    docMapB.put(idD.getId(), docD);
    Map<String, EntityDocument> docMapC = new HashMap<>();
    docMapC.put(idB.getId(), docB);
    docMapC.put(idC.getId(), docC);
    when(fetcher.getEntityDocuments(entityIdListA)).thenReturn(docMapA);
    when(fetcher.getEntityDocuments(entityIdListB)).thenReturn(docMapB);
    when(fetcher.getEntityDocuments(entityIdListC)).thenReturn(docMapC);
    Assert.assertEquals(SUT.getMultipleDocuments(Arrays.asList(idA, idB)), docListA);
    Assert.assertEquals(SUT.getMultipleDocuments(Arrays.asList(idC, idD)), docListB);
    Assert.assertEquals(SUT.getMultipleDocuments(Arrays.asList(idB, idC)), docListC);
    verify(fetcher, times(0)).getEntityDocuments(entityIdListC);
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) HashMap(java.util.HashMap) EntityDocument(org.wikidata.wdtk.datamodel.interfaces.EntityDocument) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument) WikibaseDataFetcher(org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher) Test(org.testng.annotations.Test)

Example 2 with EntityDocument

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

HashMap (java.util.HashMap)2 EntityDocument (org.wikidata.wdtk.datamodel.interfaces.EntityDocument)2 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 Validate (org.jsoup.helper.Validate)1 StatementEditingMode (org.openrefine.wikidata.schema.strategies.StatementEditingMode)1 Test (org.testng.annotations.Test)1 Datamodel (org.wikidata.wdtk.datamodel.helpers.Datamodel)1