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);
}
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.");
}
}
Aggregations