Search in sources :

Example 16 with PropertyIdValue

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

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

the class ScrutinizerTest method makeSnakGroupList.

public List<SnakGroup> makeSnakGroupList(Snak... snaks) {
    Map<PropertyIdValue, List<Snak>> propertySnakMap = new HashMap<>();
    for (Snak snak : snaks) {
        PropertyIdValue pid = snak.getPropertyId();
        List<Snak> snakList;
        if (propertySnakMap.containsKey(pid)) {
            snakList = propertySnakMap.get(pid);
        } else {
            snakList = new ArrayList<>();
        }
        snakList.add(snak);
        propertySnakMap.put(pid, snakList);
    }
    List<SnakGroup> snakGroupList = new ArrayList<>();
    for (List<Snak> snakList : propertySnakMap.values()) {
        snakGroupList.add(Datamodel.makeSnakGroup(snakList));
    }
    return snakGroupList;
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) HashMap(java.util.HashMap) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with PropertyIdValue

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

the class ScrutinizerTest method constraintParameterStatementList.

public List<Statement> constraintParameterStatementList(ItemIdValue itemIdValue, List<SnakGroup> listSnakGroup) {
    PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302");
    Snak snakValue = Datamodel.makeValueSnak(propertyIdValue, itemIdValue);
    Claim claim = Datamodel.makeClaim(itemIdValue, snakValue, listSnakGroup);
    Reference reference = Datamodel.makeReference(listSnakGroup);
    List<Reference> referenceList = Collections.singletonList(reference);
    Statement statement = Datamodel.makeStatement(claim, referenceList, StatementRank.NORMAL, "P2302$77BD7FE4-C051-4776-855C-543F0CE697D0");
    List<Statement> statements = Collections.singletonList(statement);
    return statements;
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim)

Example 19 with PropertyIdValue

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

the class ReconEntityRewriterTest method testRewritePropertyUpdateOnExistingEntity.

@Test
public void testRewritePropertyUpdateOnExistingEntity() throws NewEntityNotCreatedYetException {
    PropertyIdValue subject = TestingData.matchedPropertyID;
    rewriter = new ReconEntityRewriter(library, subject);
    library.setId(7654L, "P1234");
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, TestingData.newPropertyIdB)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingPropertyId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    TermedStatementEntityEdit rewritten = rewriter.rewrite(update);
    TermedStatementEntityEdit expected = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, newlyCreatedProperty)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingPropertyId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    assertEquals(rewritten, expected);
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Test(org.testng.annotations.Test)

Example 20 with PropertyIdValue

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

the class QualifiersStatementMerger method merge.

@Override
public Statement merge(Statement existing, Statement added) {
    List<SnakGroup> finalQualifiers = new ArrayList<>(existing.getQualifiers());
    for (SnakGroup addedSnakGroup : added.getQualifiers()) {
        PropertyIdValue pid = addedSnakGroup.getProperty();
        // otherwise:
        if (!pids.contains(pid.getId())) {
            OptionalInt index = IntStream.range(0, finalQualifiers.size()).filter(i -> finalQualifiers.get(i).getProperty().getId().equals(pid.getId())).findFirst();
            if (index.isEmpty()) {
                finalQualifiers.add(addedSnakGroup);
            } else {
                finalQualifiers.set(index.getAsInt(), mergeSnakGroups(finalQualifiers.get(index.getAsInt()), addedSnakGroup));
            }
        }
    }
    List<Reference> allReferences = new ArrayList<>(existing.getReferences());
    Set<Reference> seenReferences = new HashSet<>(existing.getReferences());
    for (Reference reference : added.getReferences()) {
        if (!seenReferences.contains(reference)) {
            seenReferences.add(reference);
            allReferences.add(reference);
        }
    }
    Statement merged = Datamodel.makeStatement(Datamodel.makeClaim(existing.getSubject(), existing.getMainSnak(), finalQualifiers), allReferences, existing.getRank(), existing.getStatementId());
    return merged;
}
Also used : PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) IntStream(java.util.stream.IntStream) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Set(java.util.Set) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) SomeValueSnak(org.wikidata.wdtk.datamodel.interfaces.SomeValueSnak) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) Datamodel(org.wikidata.wdtk.datamodel.helpers.Datamodel) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Optional(java.util.Optional) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) NoValueSnak(org.wikidata.wdtk.datamodel.interfaces.NoValueSnak) Collections(java.util.Collections) Value(org.wikidata.wdtk.datamodel.interfaces.Value) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) ArrayList(java.util.ArrayList) OptionalInt(java.util.OptionalInt) HashSet(java.util.HashSet)

Aggregations

PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)36 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)17 QAWarning (org.openrefine.wikidata.qa.QAWarning)16 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)12 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)11 Value (org.wikidata.wdtk.datamodel.interfaces.Value)11 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)9 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)9 List (java.util.List)8 Set (java.util.Set)6 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)6 Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)6 Collectors (java.util.stream.Collectors)5 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)5 Test (org.testng.annotations.Test)5 SkipSchemaExpressionException (org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException)4 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)4