Search in sources :

Example 36 with SnakGroup

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

the class QuickStatementsExporterTest method testQualifier.

@Test
public void testQualifier() throws IOException {
    Statement baseStatement = TestingData.generateStatement(qid1, qid2);
    Statement otherStatement = TestingData.generateStatement(qid2, qid1);
    Snak qualifierSnak = otherStatement.getClaim().getMainSnak();
    SnakGroup group = Datamodel.makeSnakGroup(Collections.singletonList(qualifierSnak));
    Claim claim = Datamodel.makeClaim(qid1, baseStatement.getClaim().getMainSnak(), Collections.singletonList(group));
    Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
    StatementEdit statementUpdate = new StatementEdit(statement, StatementMerger.FORMER_DEFAULT_STRATEGY, StatementEditingMode.ADD_OR_MERGE);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(qid1).addStatement(statementUpdate).build();
    assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\n", export(update));
}
Also used : Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Example 37 with SnakGroup

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

the class QuickStatementsExporterTest method testReferences.

/**
 * issue #2320
 *
 * A statement with different references should be duplicated, but each with a different reference.
 */
@Test
public void testReferences() throws IOException {
    Statement baseStatement = TestingData.generateStatement(qid1, qid2);
    Statement otherStatement = TestingData.generateStatement(qid2, qid1);
    Snak snak1 = baseStatement.getClaim().getMainSnak();
    Snak snak2 = otherStatement.getClaim().getMainSnak();
    SnakGroup group1 = Datamodel.makeSnakGroup(Collections.singletonList(snak1));
    SnakGroup group2 = Datamodel.makeSnakGroup(Collections.singletonList(snak2));
    Claim claim = Datamodel.makeClaim(qid1, baseStatement.getClaim().getMainSnak(), Collections.singletonList(group2));
    Reference reference1 = Datamodel.makeReference(Collections.singletonList(group1));
    Reference reference2 = Datamodel.makeReference(Collections.singletonList(group2));
    Statement statement = Datamodel.makeStatement(claim, Arrays.asList(reference1, reference2), StatementRank.NORMAL, "");
    StatementEdit statementUpdate = new StatementEdit(statement, StatementMerger.FORMER_DEFAULT_STRATEGY, StatementEditingMode.ADD_OR_MERGE);
    TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(qid1).addStatement(statementUpdate).build();
    assertEquals("Q1377\tP38\tQ865528\tP38\tQ1377\tS38\tQ865528\n" + "Q1377\tP38\tQ865528\tP38\tQ1377\tS38\tQ1377\n", export(update));
}
Also used : Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) TermedStatementEntityEditBuilder(org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Reference(org.wikidata.wdtk.datamodel.interfaces.Reference) SnakGroup(org.wikidata.wdtk.datamodel.interfaces.SnakGroup) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) Claim(org.wikidata.wdtk.datamodel.interfaces.Claim) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) Test(org.testng.annotations.Test) WikidataRefineTest(org.openrefine.wikidata.testing.WikidataRefineTest)

Example 38 with SnakGroup

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

Example 39 with SnakGroup

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

the class ItemRequiresScrutinizerTest method testCorrectValue.

@Test
public void testCorrectValue() {
    ItemIdValue idA = TestingData.existingId;
    Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, itemValue);
    Statement statement = new StatementImpl("P157", mainSnak, idA);
    Snak requiredPropertySnak = Datamodel.makeValueSnak(propertyParameterValue, requiredValue);
    Statement requiredStatement = new StatementImpl("P1196", requiredPropertySnak, idA);
    TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement)).addStatement(add(requiredStatement)).build();
    Snak qualifierSnak1 = Datamodel.makeValueSnak(propertyParameterPID, propertyParameterValue);
    Snak qualifierSnak2 = Datamodel.makeValueSnak(itemParameterPID, requiredValue);
    List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak1, qualifierSnak2);
    List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(propertyIdValue, ITEM_REQUIRES_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(updateA);
    assertNoWarningRaised();
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) 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 40 with SnakGroup

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

the class ItemRequiresScrutinizerTest method testExistingItemTrigger.

@Test
public void testExistingItemTrigger() {
    ItemIdValue idA = TestingData.existingId;
    Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, itemValue);
    Statement statement = new StatementImpl("P157", mainSnak, idA);
    TermedStatementEntityEdit updateA = new TermedStatementEntityEditBuilder(idA).addStatement(add(statement)).build();
    Snak qualifierSnak1 = Datamodel.makeValueSnak(propertyParameterPID, propertyParameterValue);
    Snak qualifierSnak2 = Datamodel.makeValueSnak(itemParameterPID, requiredValue);
    List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak1, qualifierSnak2);
    List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
    ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
    when(fetcher.getConstraintsByType(propertyIdValue, ITEM_REQUIRES_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
    setFetcher(fetcher);
    scrutinize(updateA);
    assertWarningsRaised(ItemRequiresScrutinizer.existingItemRequirePropertyType);
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Snak(org.wikidata.wdtk.datamodel.interfaces.Snak) ValueSnak(org.wikidata.wdtk.datamodel.interfaces.ValueSnak) 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)

Aggregations

SnakGroup (org.wikidata.wdtk.datamodel.interfaces.SnakGroup)49 Snak (org.wikidata.wdtk.datamodel.interfaces.Snak)48 Statement (org.wikidata.wdtk.datamodel.interfaces.Statement)41 Test (org.testng.annotations.Test)38 ConstraintFetcher (org.openrefine.wikidata.qa.ConstraintFetcher)36 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)34 TermedStatementEntityEditBuilder (org.openrefine.wikidata.updates.TermedStatementEntityEditBuilder)34 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)32 StatementImpl (org.wikidata.wdtk.datamodel.implementation.StatementImpl)28 ValueSnak (org.wikidata.wdtk.datamodel.interfaces.ValueSnak)17 ArrayList (java.util.ArrayList)10 PropertyIdValue (org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue)8 Claim (org.wikidata.wdtk.datamodel.interfaces.Claim)7 Reference (org.wikidata.wdtk.datamodel.interfaces.Reference)7 NoValueSnak (org.wikidata.wdtk.datamodel.interfaces.NoValueSnak)6 List (java.util.List)5 HashMap (java.util.HashMap)4 QAWarning (org.openrefine.wikidata.qa.QAWarning)4 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)4 Value (org.wikidata.wdtk.datamodel.interfaces.Value)4