use of org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue in project OpenRefine by OpenRefine.
the class DistinctValuesScrutinizer method scrutinize.
@Override
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
if (!added) {
// not scrutinizing removed statements
return;
}
Snak mainSnak = statement.getClaim().getMainSnak();
PropertyIdValue pid = mainSnak.getPropertyId();
List<Statement> statementList = _fetcher.getConstraintsByType(pid, distinctValuesConstraintQid);
if (!statementList.isEmpty() && mainSnak instanceof ValueSnak) {
Value mainSnakValue = ((ValueSnak) mainSnak).getValue();
Map<Value, EntityIdValue> seen = _seenValues.get(pid);
if (seen == null) {
seen = new HashMap<Value, EntityIdValue>();
_seenValues.put(pid, seen);
}
if (seen.containsKey(mainSnakValue)) {
EntityIdValue otherId = seen.get(mainSnakValue);
QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("item1_entity", entityId);
issue.setProperty("item2_entity", otherId);
addIssue(issue);
} else {
seen.put(mainSnakValue, entityId);
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue in project OpenRefine by OpenRefine.
the class ItemRequiresScrutinizer method scrutinize.
@Override
public void scrutinize(TermedStatementEntityEdit update) {
Map<PropertyIdValue, Set<Value>> propertyIdValueValueMap = new HashMap<>();
for (Statement statement : update.getAddedStatements()) {
Snak mainSnak = statement.getClaim().getMainSnak();
PropertyIdValue pid = mainSnak.getPropertyId();
Set<Value> values;
if (mainSnak instanceof ValueSnak) {
Value value = ((ValueSnak) mainSnak).getValue();
if (propertyIdValueValueMap.containsKey(pid)) {
values = propertyIdValueValueMap.get(pid);
} else {
values = new HashSet<>();
}
values.add(value);
propertyIdValueValueMap.put(pid, values);
}
}
for (PropertyIdValue propertyId : propertyIdValueValueMap.keySet()) {
List<Statement> constraintDefinitions = _fetcher.getConstraintsByType(propertyId, itemRequiresConstraintQid);
for (Statement statement : constraintDefinitions) {
ItemRequiresConstraint constraint = new ItemRequiresConstraint(statement);
PropertyIdValue itemRequiresPid = constraint.itemRequiresPid;
List<Value> itemList = constraint.itemList;
if (!propertyIdValueValueMap.containsKey(itemRequiresPid)) {
QAWarning issue = new QAWarning(update.isNew() ? newItemRequirePropertyType : existingItemRequirePropertyType, propertyId.getId() + itemRequiresPid.getId(), update.isNew() ? QAWarning.Severity.WARNING : QAWarning.Severity.INFO, 1);
issue.setProperty("property_entity", propertyId);
issue.setProperty("added_property_entity", itemRequiresPid);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
} else if (raiseWarning(propertyIdValueValueMap, itemRequiresPid, itemList)) {
QAWarning issue = new QAWarning(update.isNew() ? newItemRequireValuesType : existingItemRequireValuesType, propertyId.getId() + itemRequiresPid.getId(), update.isNew() ? QAWarning.Severity.WARNING : QAWarning.Severity.INFO, 1);
issue.setProperty("property_entity", propertyId);
issue.setProperty("added_property_entity", itemRequiresPid);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
}
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue in project OpenRefine by OpenRefine.
the class EditInspector method inspect.
/**
* Inspect a batch of edits with the registered scrutinizers
*
* @param editBatch
*/
public void inspect(List<TermedStatementEntityEdit> editBatch, WikibaseSchema schema) throws ExecutionException {
// First, schedule them with some scheduler,
// so that all newly created entities appear in the batch
SchemaPropertyExtractor fetcher = new SchemaPropertyExtractor();
Set<PropertyIdValue> properties = fetcher.getAllProperties(schema);
if (entityCache != null) {
// Prefetch property documents in one API call rather than requesting them one by one.
entityCache.getMultipleDocuments(properties.stream().collect(Collectors.toList()));
}
WikibaseAPIUpdateScheduler scheduler = new WikibaseAPIUpdateScheduler();
editBatch = scheduler.schedule(editBatch);
Map<EntityIdValue, TermedStatementEntityEdit> updates = TermedStatementEntityEdit.groupBySubject(editBatch);
List<TermedStatementEntityEdit> mergedUpdates = updates.values().stream().collect(Collectors.toList());
for (EditScrutinizer scrutinizer : scrutinizers.values()) {
scrutinizer.batchIsBeginning();
}
for (TermedStatementEntityEdit update : mergedUpdates) {
if (!update.isNull()) {
for (EditScrutinizer scrutinizer : scrutinizers.values()) {
scrutinizer.scrutinize(update);
}
}
}
for (EditScrutinizer scrutinizer : scrutinizers.values()) {
scrutinizer.batchIsFinished();
}
if (warningStore.getNbWarnings() == 0) {
warningStore.addWarning(new QAWarning("no-issue-detected", null, QAWarning.Severity.INFO, 0));
}
}
use of org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue in project OpenRefine by OpenRefine.
the class SchemaPropertyExtractor method getAllProperties.
public Set<PropertyIdValue> getAllProperties(WikibaseSchema schema) {
Set<PropertyIdValue> properties = new HashSet<>();
List<WbEntityDocumentExpr> entityDocumentExprs = schema.getEntityDocumentExpressions();
for (WbEntityDocumentExpr entityDocumentExpr : entityDocumentExprs) {
List<WbStatementGroupExpr> statementGroups = entityDocumentExpr.getStatementGroups();
for (WbStatementGroupExpr statementGroup : statementGroups) {
WbExpression<? extends PropertyIdValue> statementGroupProperty = statementGroup.getProperty();
if (statementGroupProperty instanceof WbPropConstant) {
properties.add(Datamodel.makeWikidataPropertyIdValue(((WbPropConstant) statementGroupProperty).getPid()));
}
List<WbStatementExpr> statementExprs = statementGroup.getStatements();
for (WbStatementExpr statementExpr : statementExprs) {
List<WbSnakExpr> snakExprs = new ArrayList<>(statementExpr.getQualifiers());
List<WbReferenceExpr> referenceExprs = statementExpr.getReferences();
for (WbReferenceExpr referenceExpr : referenceExprs) {
snakExprs.addAll(referenceExpr.getSnaks());
}
for (WbSnakExpr snakExpr : snakExprs) {
WbExpression<? extends PropertyIdValue> qualifierProperty = snakExpr.getProp();
if (qualifierProperty instanceof WbPropConstant) {
properties.add(Datamodel.makeWikidataPropertyIdValue(((WbPropConstant) qualifierProperty).getPid()));
}
}
}
}
}
return properties;
}
use of org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue in project OpenRefine by OpenRefine.
the class WbSnakExpr method evaluate.
@Override
public Snak evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException {
PropertyIdValue propertyId = getProp().evaluate(ctxt);
Value evaluatedValue = value.evaluate(ctxt);
return new FullyPropertySerializingValueSnak(propertyId, evaluatedValue);
}
Aggregations