Search in sources :

Example 1 with StatementEditingMode

use of org.openrefine.wikidata.schema.strategies.StatementEditingMode in project OpenRefine by OpenRefine.

the class StatementGroupEdit method contributeToStatementUpdate.

/**
 * Given an existing statement group on the target entity, translate this edit
 * into concrete changes of statements, by logging them into the supplied builder.
 * @param builder
 *      the statement update builder in which to add the changes
 * @param statementGroup
 * 		the corresponding existing statement group on the entity, or null if there is no such statement yet
 */
public void contributeToStatementUpdate(StatementUpdateBuilder builder, StatementGroup statementGroup) {
    List<Statement> statements = statementGroup == null ? Collections.emptyList() : statementGroup.getStatements();
    for (StatementEdit edit : statementEdits) {
        StatementMerger merger = edit.getMerger();
        Stream<Statement> matchingStatements = statements.stream().filter(statement -> {
            return merger.match(statement, edit.getStatement());
        });
        StatementEditingMode mode = edit.getMode();
        switch(mode) {
            case ADD:
                Optional<Statement> anyMatching = matchingStatements.findAny();
                if (anyMatching.isEmpty()) {
                    builder.add(edit.getStatement());
                }
                break;
            case ADD_OR_MERGE:
                Optional<Statement> firstMatching = matchingStatements.findFirst();
                if (firstMatching.isEmpty()) {
                    builder.add(edit.getStatement());
                } else {
                    builder.replace(merger.merge(firstMatching.get(), edit.getStatement()));
                }
                break;
            case DELETE:
                matchingStatements.forEach(matchingStatement -> {
                    builder.remove(matchingStatement.getStatementId());
                });
                break;
            default:
                throw new IllegalStateException("Unsupported statement editing mode " + mode);
        }
    }
}
Also used : Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) StatementMerger(org.openrefine.wikidata.schema.strategies.StatementMerger) StatementEditingMode(org.openrefine.wikidata.schema.strategies.StatementEditingMode)

Aggregations

StatementEditingMode (org.openrefine.wikidata.schema.strategies.StatementEditingMode)1 StatementMerger (org.openrefine.wikidata.schema.strategies.StatementMerger)1 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)1