Search in sources :

Example 51 with ItemIdValue

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

the class QuantityScrutinizerTest method testWrongUnit.

@Test
public void testWrongUnit() {
    ItemIdValue idA = TestingData.existingId;
    Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, wrongUnitValue);
    Statement statement = new StatementImpl("P1083", mainSnak, idA);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement)).build();
    Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedUnit);
    List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
    SnakGroup snakGroup1 = Datamodel.makeSnakGroup(qualifierSnakList);
    List<SnakGroup> constraintQualifiers = Collections.singletonList(snakGroup1);
    List<Statement> constraintDefinitions = constraintParameterStatementList(allowedUnitEntity, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(propertyIdValue, ALLOWED_UNITS_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(update);
    assertWarningsRaised(QuantityScrutinizer.invalidUnitType);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) ConstraintFetcher(org.openrefine.wikidata.qa.ConstraintFetcher) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) StatementImpl(org.wikidata.wdtk.datamodel.implementation.StatementImpl) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) Test(org.testng.annotations.Test)

Example 52 with ItemIdValue

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

the class ReconEntityRewriterTest method testSubjectNotRewritten.

@Test
public void testSubjectNotRewritten() {
    ItemIdValue subject = TestingData.newIdA;
    rewriter = new ReconEntityRewriter(library, subject);
    assertEquals(subject, rewriter.copy(subject));
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Test(org.testng.annotations.Test)

Example 53 with ItemIdValue

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

the class ReconEntityRewriterTest method testRewriteUpdateOnPreviouslyCreatedEntity.

@Test
public void testRewriteUpdateOnPreviouslyCreatedEntity() throws NewEntityNotCreatedYetException {
    ItemIdValue subject = TestingData.newIdA;
    rewriter = new ReconEntityRewriter(library, subject);
    library.setId(4567L, "Q1234");
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(TestingData.newIdB).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    TermedStatementEntityEdit rewritten = rewriter.rewrite(update);
    TermedStatementEntityEdit expected = new TermedStatementEntityEditBuilder(newlyCreated).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    assertEquals(rewritten, expected);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Test(org.testng.annotations.Test)

Example 54 with ItemIdValue

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

the class ReconEntityRewriterTest method testRewriteUpdateOnExistingEntity.

@Test
public void testRewriteUpdateOnExistingEntity() throws NewEntityNotCreatedYetException {
    ItemIdValue subject = TestingData.matchedId;
    rewriter = new ReconEntityRewriter(library, subject);
    library.setId(4567L, "Q1234");
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, TestingData.newIdB)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingId)).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, newlyCreated)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
    assertEquals(rewritten, expected);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Test(org.testng.annotations.Test)

Example 55 with ItemIdValue

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

ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)68 Test (org.testng.annotations.Test)61 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)57 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)56 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)54 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)52 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)48 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)44 SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)32 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)20 ArrayList (java.util.ArrayList)6 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)5 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)4 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)4 QAWarning (org.openrefine.wikidata.qa.QAWarning)3 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)3 ItemDocument (org.wikidata.wdtk.datamodel.interfaces.ItemDocument)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Collections (java.util.Collections)2