Search in sources :

Example 21 with MonolingualTextValue

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

Example 22 with MonolingualTextValue

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

the class TermedStatementEntityEdit method normalizeLabelsAndAliases.

/**
 * This should only be used when creating a new entity. This ensures that we never
 * add an alias without adding a label in the same language.
 */
public TermedStatementEntityEdit normalizeLabelsAndAliases() {
    // Ensure that we are only adding aliases with labels
    Set<MonolingualTextValue> filteredAliases = new HashSet<>();
    Map<String, MonolingualTextValue> newLabels = new HashMap<>(labelsIfNew);
    newLabels.putAll(labels);
    for (MonolingualTextValue alias : getAliases()) {
        if (!newLabels.containsKey(alias.getLanguageCode())) {
            newLabels.put(alias.getLanguageCode(), alias);
        } else {
            filteredAliases.add(alias);
        }
    }
    Map<String, MonolingualTextValue> newDescriptions = new HashMap<>(descriptionsIfNew);
    newDescriptions.putAll(descriptions);
    return new TermedStatementEntityEdit(id, statements, newLabels, Collections.emptyMap(), newDescriptions, Collections.emptyMap(), constructTermListMap(filteredAliases));
}
Also used : HashMap(java.util.HashMap) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) HashSet(java.util.HashSet)

Example 23 with MonolingualTextValue

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

the class TermedStatementEntityEdit method merge.

/**
 * Merges all the changes in other with this instance. Both updates should have
 * the same subject. Changes coming from `other` have priority over changes
 * from this instance. This instance is not modified, the merged update is returned
 * instead.
 *
 * @param other
 *            the other change that should be merged
 */
public TermedStatementEntityEdit merge(TermedStatementEntityEdit other) {
    Validate.isTrue(id.equals(other.getEntityId()));
    List<StatementEdit> newStatements = new ArrayList<>(statements);
    for (StatementEdit statement : other.getStatementEdits()) {
        if (!newStatements.contains(statement)) {
            newStatements.add(statement);
        }
    }
    Map<String, MonolingualTextValue> newLabels = new HashMap<>(labels);
    Map<String, MonolingualTextValue> newLabelsIfNew = new HashMap<>(labelsIfNew);
    mergeSingleTermMaps(newLabels, newLabelsIfNew, other.getLabels(), other.getLabelsIfNew());
    Map<String, MonolingualTextValue> newDescriptions = new HashMap<>(descriptions);
    Map<String, MonolingualTextValue> newDescriptionsIfNew = new HashMap<>(descriptionsIfNew);
    mergeSingleTermMaps(newDescriptions, newDescriptionsIfNew, other.getDescriptions(), other.getDescriptionsIfNew());
    Map<String, List<MonolingualTextValue>> newAliases = new HashMap<>(aliases);
    for (MonolingualTextValue alias : other.getAliases()) {
        List<MonolingualTextValue> aliases = newAliases.get(alias.getLanguageCode());
        if (aliases == null) {
            aliases = new LinkedList<>();
            newAliases.put(alias.getLanguageCode(), aliases);
        }
        if (!aliases.contains(alias)) {
            aliases.add(alias);
        }
    }
    return new TermedStatementEntityEdit(id, newStatements, newLabels, newLabelsIfNew, newDescriptions, newDescriptionsIfNew, newAliases);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

MonolingualTextValue (org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue)23 Test (org.testng.annotations.Test)13 ArrayList (java.util.ArrayList)6 List (java.util.List)6 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)4 Datamodel (org.wikidata.wdtk.datamodel.helpers.Datamodel)4 ItemDocument (org.wikidata.wdtk.datamodel.interfaces.ItemDocument)4 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)4 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)4 StatementUpdate (org.wikidata.wdtk.datamodel.interfaces.StatementUpdate)4 Collections (java.util.Collections)3 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 WikidataRefineTest (org.openrefine.wikidata.testing.WikidataRefineTest)3 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)3 EntityDocument (org.wikidata.wdtk.datamodel.interfaces.EntityDocument)3 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)3 MediaInfoDocument (org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument)3