Add constraint class (#2822)

* Add constraint class

* Add constraint class

* updated names
This commit is contained in:
Ekta Mishra 2020-06-28 13:50:18 +05:30 committed by GitHub
parent 83f52d4ba5
commit 1b04927d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package org.openrefine.wikidata.qa;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Constraint class is defined to parse the common parameters of property constraints
*
* Most of the scrutinizer will have inner class defining the structure of
* that constraint will extend this Constraint class
*
* @author Ekta Mishra
*/
public class Constraint {
public static String CONSTRAINT_STATUS = "P2316";
public static String CONSTRAINT_EXCEPTIONS = "P2303";
ItemIdValue constraintStatus;
Set<EntityIdValue> constraintExceptions;
Constraint(Statement statement) {
constraintExceptions = new HashSet<>();
List<SnakGroup> snakGroupList = statement.getClaim().getQualifiers();
for(SnakGroup group : snakGroupList) {
for (Snak snak : group.getSnaks()) {
if (group.getProperty().getId().equals(CONSTRAINT_STATUS)) {
constraintStatus = (ItemIdValue) snak.getValue();
}
else if (group.getProperty().getId().equals(CONSTRAINT_EXCEPTIONS)) {
constraintExceptions.add((EntityIdValue) snak.getValue());
}
}
}
}
public ItemIdValue getConstraintStatus() {
return this.constraintStatus;
}
public Set<EntityIdValue> getConstraintExceptions() {
return this.constraintExceptions;
}
}

View File

@ -0,0 +1,65 @@
package org.openrefine.wikidata.qa;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ConstraintTest {
public static ItemIdValue itemIdValue = Datamodel.makeWikidataItemIdValue("Q19474404");
public static PropertyIdValue constraintException = Datamodel.makeWikidataPropertyIdValue("P2303");
public static Value exceptionValue = Datamodel.makeWikidataItemIdValue("Q7409772");
public static PropertyIdValue constraintStatus = Datamodel.makeWikidataPropertyIdValue("P2316");
public static Value statusValue = Datamodel.makeWikidataItemIdValue("Q62026391");
public static Constraint constraint;
public static Statement statement;
@BeforeMethod
public void setUp() {
Snak qualifierSnak1 = Datamodel.makeValueSnak(constraintException, exceptionValue);
List<Snak> qualifierSnakList1 = Collections.singletonList(qualifierSnak1);
Snak qualifierSnak2 = Datamodel.makeValueSnak(constraintStatus, statusValue);
List<Snak> qualifierSnakList2 = Collections.singletonList(qualifierSnak2);
SnakGroup qualifier1 = Datamodel.makeSnakGroup(qualifierSnakList1);
SnakGroup qualifier2 = Datamodel.makeSnakGroup(qualifierSnakList2);
List<SnakGroup> snakGroupList = Arrays.asList(qualifier1, qualifier2);
PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302");
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue,itemIdValue);
Claim claim = Datamodel.makeClaim(itemIdValue, mainSnak, snakGroupList);
Reference reference = Datamodel.makeReference(snakGroupList);
List<Reference> referenceList = Collections.singletonList(reference);
statement = Datamodel.makeStatement(claim, referenceList, StatementRank.NORMAL, "P2302$77BD7FE4-C051-4776-855C-543F0CE697D0");
constraint = new Constraint(statement);
}
@Test
public void testGetConstraintExceptions() {
Set<EntityIdValue> entityIdValueSet = new HashSet<>();
entityIdValueSet.add((EntityIdValue) exceptionValue);
Assert.assertEquals(constraint.getConstraintExceptions(), entityIdValueSet);
}
@Test
public void testGetConstraintStatus() {
ItemIdValue status = (ItemIdValue) statusValue;
Assert.assertEquals(constraint.getConstraintStatus(), status);
}
}