Move pids from Strings to PropertyIdValues, cleaner

This commit is contained in:
Antonin Delpeuch 2018-01-10 09:55:51 +00:00
parent fea53aa900
commit 0a0aacb0cd
5 changed files with 34 additions and 30 deletions

View File

@ -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<SnakGroup> specs = getSingleConstraint(pid, FORMAT_CONSTRAINT_QID);
if (specs != null) {
List<Value> 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<SnakGroup> specs = getSingleConstraint(pid, INVERSE_CONSTRAINT_QID);
if(specs != null) {
List<Value> 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<PropertyIdValue> allowedQualifiers(PropertyIdValue pid) {
List<SnakGroup> specs = getSingleConstraint(pid.getId(), ALLOWED_QUALIFIERS_CONSTRAINT_QID);
List<SnakGroup> specs = getSingleConstraint(pid, ALLOWED_QUALIFIERS_CONSTRAINT_QID);
if (specs != null) {
List<Value> 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<PropertyIdValue> mandatoryQualifiers(PropertyIdValue pid) {
List<SnakGroup> specs = getSingleConstraint(pid.getId(), MANDATORY_QUALIFIERS_CONSTRAINT_QID);
List<SnakGroup> specs = getSingleConstraint(pid, MANDATORY_QUALIFIERS_CONSTRAINT_QID);
if (specs != null) {
List<Value> 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<SnakGroup> getSingleConstraint(String pid, String qid) {
protected List<SnakGroup> 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<Statement> getConstraintsByType(String pid, String qid) {
protected Stream<Statement> getConstraintsByType(PropertyIdValue pid, String qid) {
Stream<Statement> 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<Statement> getConstraintStatements(String pid) {
protected List<Statement> getConstraintStatements(PropertyIdValue pid) {
PropertyDocument doc = (PropertyDocument) EntityCache.getEntityDocument(pid);
StatementGroup group = doc.findStatementGroup(WIKIDATA_CONSTRAINT_PID);
return group.getStatements();

View File

@ -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<String, Pattern> _patterns;
private Map<PropertyIdValue, Pattern> _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) {

View File

@ -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<String, String> _inverse;
private Map<String, Map<EntityIdValue, Set<EntityIdValue> >> _statements;
private Map<PropertyIdValue, PropertyIdValue> _inverse;
private Map<PropertyIdValue, Map<EntityIdValue, Set<EntityIdValue> >> _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<EntityIdValue,Set<EntityIdValue>>());
@ -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<EntityIdValue> 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<String,String> propertyPair : _inverse.entrySet()) {
for(Entry<PropertyIdValue,PropertyIdValue> propertyPair : _inverse.entrySet()) {
// Get the statements made for the first
for(Entry<EntityIdValue, Set<EntityIdValue>> itemLinks : _statements.get(propertyPair.getKey()).entrySet()) {
// For each outgoing link

View File

@ -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<String, SnakPosition> _restrictedPids;
private Set<String> _unrestrictedPids;
private Map<PropertyIdValue, SnakPosition> _restrictedPids;
private Set<PropertyIdValue> _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();

View File

@ -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<String, EntityDocument> _cache;
private LoadingCache<EntityIdValue, EntityDocument> _cache;
private WikibaseDataFetcher _fetcher;
@ -26,15 +27,15 @@ public class EntityCache {
.maximumSize(4096)
.expireAfterWrite(1, TimeUnit.HOURS)
.build(
new CacheLoader<String, EntityDocument>() {
public EntityDocument load(String entityId) throws Exception {
EntityDocument doc = _fetcher.getEntityDocument(entityId);
new CacheLoader<EntityIdValue, EntityDocument>() {
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);
}
}