diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/Constraint.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/Constraint.java new file mode 100644 index 000000000..aaa1990a5 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/Constraint.java @@ -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 constraintExceptions; + + Constraint(Statement statement) { + constraintExceptions = new HashSet<>(); + List 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 getConstraintExceptions() { + return this.constraintExceptions; + } +} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/ConstraintTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/ConstraintTest.java new file mode 100644 index 000000000..1453af318 --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/ConstraintTest.java @@ -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 qualifierSnakList1 = Collections.singletonList(qualifierSnak1); + Snak qualifierSnak2 = Datamodel.makeValueSnak(constraintStatus, statusValue); + List qualifierSnakList2 = Collections.singletonList(qualifierSnak2); + SnakGroup qualifier1 = Datamodel.makeSnakGroup(qualifierSnakList1); + SnakGroup qualifier2 = Datamodel.makeSnakGroup(qualifierSnakList2); + List 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 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 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); + } +}