From 0a0aacb0cd57103f679687e714e7ae2e140ae2c5 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Wed, 10 Jan 2018 09:55:51 +0000 Subject: [PATCH] Move pids from Strings to PropertyIdValues, cleaner --- .../wikidata/qa/ConstraintFetcher.java | 22 +++++++++---------- .../FormatConstraintScrutinizer.java | 7 +++--- .../InverseConstraintScrutinizer.java | 15 +++++++------ .../RestrictedPositionScrutinizer.java | 9 ++++---- .../wikidata/utils/EntityCache.java | 11 +++++----- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java index b9059487e..b6788e5e5 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/ConstraintFetcher.java @@ -61,7 +61,7 @@ public class ConstraintFetcher { * @param pid * @return the expression of a regular expression which should be compatible with java.util.regex */ - public String getFormatRegex(String pid) { + public String getFormatRegex(PropertyIdValue pid) { List specs = getSingleConstraint(pid, FORMAT_CONSTRAINT_QID); if (specs != null) { List regexes = findValues(specs, FORMAT_REGEX_PID); @@ -77,13 +77,13 @@ public class ConstraintFetcher { * @param pid: the property to retrieve the inverse for * @return the pid of the inverse property */ - public String getInversePid(String pid) { + public PropertyIdValue getInversePid(PropertyIdValue pid) { List specs = getSingleConstraint(pid, INVERSE_CONSTRAINT_QID); if(specs != null) { List inverses = findValues(specs, INVERSE_PROPERTY_PID); if (! inverses.isEmpty()) { - return ((EntityIdValue)inverses.get(0)).getId(); + return (PropertyIdValue)inverses.get(0); } } return null; @@ -92,21 +92,21 @@ public class ConstraintFetcher { /** * Is this property for values only? */ - public boolean isForValuesOnly(String pid) { + public boolean isForValuesOnly(PropertyIdValue pid) { return getSingleConstraint(pid, USED_ONLY_AS_VALUES_CONSTRAINT_QID) != null; } /** * Is this property for qualifiers only? */ - public boolean isForQualifiersOnly(String pid) { + public boolean isForQualifiersOnly(PropertyIdValue pid) { return getSingleConstraint(pid, USED_ONLY_AS_QUALIFIER_CONSTRAINT_QID) != null; } /** * Is this property for references only? */ - public boolean isForReferencesOnly(String pid) { + public boolean isForReferencesOnly(PropertyIdValue pid) { return getSingleConstraint(pid, USED_ONLY_AS_REFERENCE_CONSTRAINT_QID) != null; } @@ -114,7 +114,7 @@ public class ConstraintFetcher { * Get the list of allowed qualifiers (as property ids) for this property (null if any) */ public Set allowedQualifiers(PropertyIdValue pid) { - List specs = getSingleConstraint(pid.getId(), ALLOWED_QUALIFIERS_CONSTRAINT_QID); + List specs = getSingleConstraint(pid, ALLOWED_QUALIFIERS_CONSTRAINT_QID); if (specs != null) { List properties = findValues(specs, ALLOWED_QUALIFIERS_CONSTRAINT_PID); @@ -127,7 +127,7 @@ public class ConstraintFetcher { * Get the list of mandatory qualifiers (as property ids) for this property (null if any) */ public Set mandatoryQualifiers(PropertyIdValue pid) { - List specs = getSingleConstraint(pid.getId(), MANDATORY_QUALIFIERS_CONSTRAINT_QID); + List specs = getSingleConstraint(pid, MANDATORY_QUALIFIERS_CONSTRAINT_QID); if (specs != null) { List properties = findValues(specs, MANDATORY_QUALIFIERS_CONSTRAINT_PID); @@ -143,7 +143,7 @@ public class ConstraintFetcher { * @param qid: the type of the constraints * @return the list of qualifiers for the constraint, or null if it does not exist */ - protected List getSingleConstraint(String pid, String qid) { + protected List getSingleConstraint(PropertyIdValue pid, String qid) { Statement statement = getConstraintsByType(pid, qid).findFirst().orElse(null); if (statement != null) { return statement.getClaim().getQualifiers(); @@ -157,7 +157,7 @@ public class ConstraintFetcher { * @param qid: the type of the constraints * @return the stream of matching constraint statements */ - protected Stream getConstraintsByType(String pid, String qid) { + protected Stream getConstraintsByType(PropertyIdValue pid, String qid) { Stream allConstraints = getConstraintStatements(pid) .stream() .filter(s -> ((EntityIdValue) s.getValue()).getId().equals(qid)); @@ -169,7 +169,7 @@ public class ConstraintFetcher { * @param pid : the id of the property to retrieve the constraints for * @return the list of constraint statements */ - protected List getConstraintStatements(String pid) { + protected List getConstraintStatements(PropertyIdValue pid) { PropertyDocument doc = (PropertyDocument) EntityCache.getEntityDocument(pid); StatementGroup group = doc.findStatementGroup(WIKIDATA_CONSTRAINT_PID); return group.getStatements(); diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java index d560f0cd4..2b5a809d2 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/FormatConstraintScrutinizer.java @@ -6,6 +6,7 @@ import java.util.regex.Pattern; import org.openrefine.wikidata.qa.ConstraintFetcher; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; +import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.StringValue; @@ -18,7 +19,7 @@ import org.wikidata.wdtk.datamodel.interfaces.StringValue; */ public class FormatConstraintScrutinizer extends SnakScrutinizer { - private Map _patterns; + private Map _patterns; private ConstraintFetcher _fetcher; public FormatConstraintScrutinizer() { @@ -33,7 +34,7 @@ public class FormatConstraintScrutinizer extends SnakScrutinizer { * @param pid the id of the property to fetch the constraints for * @return */ - protected Pattern getPattern(String pid) { + protected Pattern getPattern(PropertyIdValue pid) { if(_patterns.containsKey(pid)) { return _patterns.get(pid); } else { @@ -51,7 +52,7 @@ public class FormatConstraintScrutinizer extends SnakScrutinizer { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { if(StringValue.class.isInstance(snak.getValue())) { String value = ((StringValue) snak.getValue()).getString(); - String pid = snak.getPropertyId().getId(); + PropertyIdValue pid = snak.getPropertyId(); Pattern pattern = getPattern(pid); if (!pattern.matcher(value).matches()) { if (added) { diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/InverseConstraintScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/InverseConstraintScrutinizer.java index 60ac66ee9..00c646a81 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/InverseConstraintScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/InverseConstraintScrutinizer.java @@ -9,6 +9,7 @@ import java.util.Set; import org.openrefine.wikidata.qa.ConstraintFetcher; 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.Statement; import org.wikidata.wdtk.datamodel.interfaces.Value; @@ -22,8 +23,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Value; public class InverseConstraintScrutinizer extends StatementScrutinizer { private ConstraintFetcher _fetcher; - private Map _inverse; - private Map >> _statements; + private Map _inverse; + private Map >> _statements; public InverseConstraintScrutinizer() { _fetcher = new ConstraintFetcher(); @@ -31,11 +32,11 @@ public class InverseConstraintScrutinizer extends StatementScrutinizer { _statements = new HashMap<>(); } - protected String getInverseConstraint(String pid) { + protected PropertyIdValue getInverseConstraint(PropertyIdValue pid) { if (_inverse.containsKey(pid)) { return _inverse.get(pid); } else { - String inversePid = _fetcher.getInversePid(pid); + PropertyIdValue inversePid = _fetcher.getInversePid(pid); _inverse.put(pid, inversePid); _statements.put(pid, new HashMap>()); @@ -57,8 +58,8 @@ public class InverseConstraintScrutinizer extends StatementScrutinizer { Value mainSnakValue = statement.getClaim().getMainSnak().getValue(); if (ItemIdValue.class.isInstance(mainSnakValue)) { - String pid = statement.getClaim().getMainSnak().getPropertyId().getId(); - String inversePid = getInverseConstraint(pid); + PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId(); + PropertyIdValue inversePid = getInverseConstraint(pid); if (inversePid != null) { EntityIdValue targetEntityId = (EntityIdValue) mainSnakValue; Set currentValues = _statements.get(pid).get(entityId); @@ -74,7 +75,7 @@ public class InverseConstraintScrutinizer extends StatementScrutinizer { @Override public void batchIsFinished() { // For each pair of inverse properties (in each direction) - for(Entry propertyPair : _inverse.entrySet()) { + for(Entry propertyPair : _inverse.entrySet()) { // Get the statements made for the first for(Entry> itemLinks : _statements.get(propertyPair.getKey()).entrySet()) { // For each outgoing link diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedPositionScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedPositionScrutinizer.java index d2a21f856..1ee90591e 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedPositionScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedPositionScrutinizer.java @@ -8,6 +8,7 @@ import java.util.Set; import org.openrefine.wikidata.qa.ConstraintFetcher; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; +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.Statement; @@ -21,8 +22,8 @@ public class RestrictedPositionScrutinizer extends StatementScrutinizer { REFERENCE } - private Map _restrictedPids; - private Set _unrestrictedPids; + private Map _restrictedPids; + private Set _unrestrictedPids; private ConstraintFetcher _fetcher; public RestrictedPositionScrutinizer() { @@ -31,7 +32,7 @@ public class RestrictedPositionScrutinizer extends StatementScrutinizer { _fetcher = new ConstraintFetcher(); } - SnakPosition positionRestriction(String pid) { + SnakPosition positionRestriction(PropertyIdValue pid) { if(_unrestrictedPids.contains(pid)) { return null; } @@ -79,7 +80,7 @@ public class RestrictedPositionScrutinizer extends StatementScrutinizer { } public void scrutinize(Snak snak, EntityIdValue entityId, SnakPosition position, boolean added) { - SnakPosition restriction = positionRestriction(snak.getPropertyId().getId()); + SnakPosition restriction = positionRestriction(snak.getPropertyId()); if (restriction != null && position != restriction) { String positionStr = position.toString().toLowerCase(); String restrictionStr = restriction.toString().toLowerCase(); diff --git a/extensions/wikidata/src/org/openrefine/wikidata/utils/EntityCache.java b/extensions/wikidata/src/org/openrefine/wikidata/utils/EntityCache.java index f6567f547..b27a4abf1 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/utils/EntityCache.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/utils/EntityCache.java @@ -4,6 +4,7 @@ import java.util.concurrent.TimeUnit; import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.interfaces.EntityDocument; +import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.wikibaseapi.ApiConnection; import org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher; @@ -14,7 +15,7 @@ import com.google.common.cache.LoadingCache; public class EntityCache { private static EntityCache _entityCache = new EntityCache(); - private LoadingCache _cache; + private LoadingCache _cache; private WikibaseDataFetcher _fetcher; @@ -26,15 +27,15 @@ public class EntityCache { .maximumSize(4096) .expireAfterWrite(1, TimeUnit.HOURS) .build( - new CacheLoader() { - public EntityDocument load(String entityId) throws Exception { - EntityDocument doc = _fetcher.getEntityDocument(entityId); + new CacheLoader() { + public EntityDocument load(EntityIdValue entityId) throws Exception { + EntityDocument doc = _fetcher.getEntityDocument(entityId.getId()); return doc; } }); } - public static EntityDocument getEntityDocument(String qid) { + public static EntityDocument getEntityDocument(EntityIdValue qid) { return _entityCache._cache.apply(qid); } }