use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.
the class FormatScrutinizer method getPattern.
/**
* Loads the regex for a property and compiles it to a pattern (this is cached
* upstream, plus we are doing it only once per property and batch).
*
* @param pid
* the id of the property to fetch the constraints for
* @return
*/
protected Set<Pattern> getPattern(PropertyIdValue pid) {
if (_patterns.containsKey(pid)) {
return _patterns.get(pid);
} else {
List<Statement> statementList = _fetcher.getConstraintsByType(pid, formatConstraintQid);
Set<Pattern> patterns = new HashSet<>();
for (Statement statement : statementList) {
FormatConstraint constraint = new FormatConstraint(statement);
String regex = constraint.regularExpressionFormat;
Pattern pattern = null;
if (regex != null) {
try {
pattern = Pattern.compile(regex);
patterns.add(pattern);
} catch (PatternSyntaxException e) {
logger.info(String.format("Ignoring invalid format constraint for property %s. Regex %s is invalid: %s", pid.getId(), regex, e.getMessage()));
}
}
}
_patterns.put(pid, patterns);
return patterns;
}
}
use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.
the class NewItemScrutinizer method scrutinize.
@Override
public void scrutinize(TermedStatementEntityEdit update) {
if (update.isNew()) {
info(newItemType);
if (update.getLabels().isEmpty() && update.getLabelsIfNew().isEmpty() && update.getAliases().isEmpty()) {
QAWarning issue = new QAWarning(noLabelType, null, QAWarning.Severity.CRITICAL, 1);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
}
if (update.getDescriptions().isEmpty() && update.getDescriptionsIfNew().isEmpty()) {
QAWarning issue = new QAWarning(noDescType, null, QAWarning.Severity.WARNING, 1);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
}
if (!update.getDeletedStatements().isEmpty()) {
QAWarning issue = new QAWarning(deletedStatementsType, null, QAWarning.Severity.WARNING, 1);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
}
// Try to find a "instance of" or "subclass of" claim
boolean typeFound = false;
for (Statement statement : update.getAddedStatements()) {
String pid = statement.getMainSnak().getPropertyId().getId();
if (manifest.getInstanceOfPid().equals(pid) || manifest.getSubclassOfPid().equals(pid)) {
typeFound = true;
break;
}
}
if (!typeFound) {
QAWarning issue = new QAWarning(noTypeType, null, QAWarning.Severity.WARNING, 1);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.
the class StatementGroupEdit method contributeToStatementUpdate.
/**
* Given an existing statement group on the target entity, translate this edit
* into concrete changes of statements, by logging them into the supplied builder.
* @param builder
* the statement update builder in which to add the changes
* @param statementGroup
* the corresponding existing statement group on the entity, or null if there is no such statement yet
*/
public void contributeToStatementUpdate(StatementUpdateBuilder builder, StatementGroup statementGroup) {
List<Statement> statements = statementGroup == null ? Collections.emptyList() : statementGroup.getStatements();
for (StatementEdit edit : statementEdits) {
StatementMerger merger = edit.getMerger();
Stream<Statement> matchingStatements = statements.stream().filter(statement -> {
return merger.match(statement, edit.getStatement());
});
StatementEditingMode mode = edit.getMode();
switch(mode) {
case ADD:
Optional<Statement> anyMatching = matchingStatements.findAny();
if (anyMatching.isEmpty()) {
builder.add(edit.getStatement());
}
break;
case ADD_OR_MERGE:
Optional<Statement> firstMatching = matchingStatements.findFirst();
if (firstMatching.isEmpty()) {
builder.add(edit.getStatement());
} else {
builder.replace(merger.merge(firstMatching.get(), edit.getStatement()));
}
break;
case DELETE:
matchingStatements.forEach(matchingStatement -> {
builder.remove(matchingStatement.getStatementId());
});
break;
default:
throw new IllegalStateException("Unsupported statement editing mode " + mode);
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.
the class UnsourcedScrutinizer method scrutinize.
@Override
public void scrutinize(TermedStatementEntityEdit update) {
for (Statement statement : update.getAddedStatements()) {
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
List<Statement> constraintDefinitions = _fetcher.getConstraintsByType(pid, citationNeededConstraintQid);
List<Reference> referenceList = statement.getReferences();
if (referenceList.isEmpty()) {
if (!constraintDefinitions.isEmpty()) {
QAWarning issue = new QAWarning(constraintItemType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_entity", update.getEntityId());
addIssue(issue);
} else {
warning(generalType);
}
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.Statement in project OpenRefine by OpenRefine.
the class QuickStatementsExporterTest method testNoValue.
@Test
public void testNoValue() throws IOException {
PropertyIdValue pid = Datamodel.makeWikidataPropertyIdValue("P123");
Claim claim = Datamodel.makeClaim(qid1, Datamodel.makeNoValueSnak(pid), Collections.emptyList());
Statement statement = Datamodel.makeStatement(claim, Collections.emptyList(), StatementRank.NORMAL, "");
StatementEdit statementUpdate = new StatementEdit(statement, StatementMerger.FORMER_DEFAULT_STRATEGY, StatementEditingMode.ADD_OR_MERGE);
TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(qid1).addStatement(statementUpdate).build();
assertEquals("Q1377\tP123\tnovalue\n", export(update));
}
Aggregations