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));
}
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));
}
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;
}
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();
}
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);
}
Aggregations