Search in sources :

Example 1 with PropertyDocument

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

the class ConstraintFetcher method getConstraintStatements.

/**
 * Gets all the constraint statements for a given property
 *
 * @param pid
 *             the id of the property to retrieve the constraints for
 * @return the list of constraint statements
 */
private List<Statement> getConstraintStatements(PropertyIdValue pid) {
    PropertyDocument doc = (PropertyDocument) entityCache.get(pid);
    StatementGroup group = doc.findStatementGroup(wikibaseConstraintPid);
    if (group != null) {
        return group.getStatements().stream().filter(s -> s.getValue() != null && s.getValue() instanceof EntityIdValue).collect(Collectors.toList());
    } else {
        return Collections.emptyList();
    }
}
Also used : StatementRank(org.wikidata.wdtk.datamodel.interfaces.StatementRank) List(java.util.List) Stream(java.util.stream.Stream) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument) EntityCache(org.openrefine.wikidata.utils.EntityCache) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) StatementGroup(org.wikidata.wdtk.datamodel.interfaces.StatementGroup) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument)

Example 2 with PropertyDocument

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

the class EntityCacheTests method testGet.

@Test
public void testGet() throws MediaWikiApiErrorException, IOException {
    WikibaseDataFetcher fetcher = mock(WikibaseDataFetcher.class);
    PropertyIdValue id = Datamodel.makeWikidataPropertyIdValue("P42");
    PropertyDocument doc = Datamodel.makePropertyDocument(id, Datamodel.makeDatatypeIdValue(DatatypeIdValue.DT_GEO_SHAPE));
    when(fetcher.getEntityDocument(id.getId())).thenReturn(doc);
    EntityCache SUT = new EntityCache(fetcher);
    Assert.assertEquals(SUT.get(id), doc);
    // try another time, it is now cached
    Assert.assertEquals(SUT.get(id), doc);
    // the fetcher was only called once thanks to caching
    verify(fetcher, times(1)).getEntityDocument(id.getId());
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) PropertyDocument(org.wikidata.wdtk.datamodel.interfaces.PropertyDocument) WikibaseDataFetcher(org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher) Test(org.testng.annotations.Test)

Example 3 with PropertyDocument

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

Aggregations

PropertyDocument (org.wikidata.wdtk.datamodel.interfaces.PropertyDocument)3 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)3 Test (org.testng.annotations.Test)2 WikibaseDataFetcher (org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 EntityCache (org.openrefine.wikidata.utils.EntityCache)1 EntityDocument (org.wikidata.wdtk.datamodel.interfaces.EntityDocument)1 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)1 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)1 StatementGroup (org.wikidata.wdtk.datamodel.interfaces.StatementGroup)1 StatementRank (org.wikidata.wdtk.datamodel.interfaces.StatementRank)1