use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.
the class QSValuePrinter method visit.
@Override
public String visit(QuantityValue value) {
ItemIdValue unit = value.getUnitItemId();
String unitRepresentation = "", boundsRepresentation = "";
if (unit != null) {
unitRepresentation = "U" + unit.getId().substring(1);
}
if (value.getLowerBound() != null) {
// bounds are always null at the same time so we know they are both not null
BigDecimal lowerBound = value.getLowerBound();
BigDecimal upperBound = value.getUpperBound();
boundsRepresentation = String.format(Locale.US, "[%s,%s]", lowerBound.toString(), upperBound.toString());
}
return String.format(Locale.US, "%s%s%s", value.getNumericValue().toString(), boundsRepresentation, unitRepresentation);
}
use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.
the class QuantityScrutinizer method scrutinize.
@Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
if (!added) {
return;
}
if (snak instanceof ValueSnak && ((ValueSnak) snak).getValue() instanceof QuantityValue && added) {
PropertyIdValue pid = snak.getPropertyId();
QuantityValue value = (QuantityValue) ((ValueSnak) snak).getValue();
if (!_fetcher.getConstraintsByType(pid, noBoundsConstraintQid).isEmpty() && (value.getUpperBound() != null || value.getLowerBound() != null)) {
QAWarning issue = new QAWarning(boundsDisallowedType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value", value.getNumericValue().toString());
issue.setProperty("example_item_entity", entityId);
addIssue(issue);
}
if (!_fetcher.getConstraintsByType(pid, integerValuedConstraintQid).isEmpty() && value.getNumericValue().scale() > 0) {
QAWarning issue = new QAWarning(integerConstraintType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value", value.getNumericValue().toString());
issue.setProperty("example_item_entity", entityId);
addIssue(issue);
}
List<Statement> statementList = _fetcher.getConstraintsByType(pid, allowedUnitsConstraintQid);
Set<ItemIdValue> allowedUnits = null;
if (!statementList.isEmpty()) {
AllowedUnitsConstraint allowedUnitsConstraint = new AllowedUnitsConstraint(statementList.get(0));
allowedUnits = allowedUnitsConstraint.allowedUnits;
}
ItemIdValue currentUnit = null;
if (value.getUnitItemId() != null) {
currentUnit = value.getUnitItemId();
}
if (allowedUnits != null && !allowedUnits.contains(currentUnit)) {
String issueType = currentUnit == null ? noUnitProvidedType : invalidUnitType;
QAWarning issue = new QAWarning(issueType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_value", value.getNumericValue().toString());
issue.setProperty("example_item_entity", entityId);
if (currentUnit != null) {
issue.setProperty("unit_entity", value.getUnitItemId());
}
addIssue(issue);
}
}
}
use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.
the class WbQuantityExpr method evaluate.
@Override
public QuantityValue evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException {
StringValue amount = getAmountExpr().evaluate(ctxt);
// we know the amount is nonnull, nonempty here
BigDecimal parsedAmount = null;
BigDecimal lowerBound = null;
BigDecimal upperBound = null;
String originalAmount = amount.getString().toUpperCase();
try {
parsedAmount = new BigDecimal(originalAmount);
if (originalAmount.contains("E")) {
// engineering notation: we derive the precision from
// the expression (feature!)
BigDecimal uncertainty = new BigDecimal("0.5").scaleByPowerOfTen(-parsedAmount.scale());
lowerBound = new BigDecimal(parsedAmount.subtract(uncertainty).toPlainString());
upperBound = new BigDecimal(parsedAmount.add(uncertainty).toPlainString());
}
// workaround for https://github.com/Wikidata/Wikidata-Toolkit/issues/341
parsedAmount = new BigDecimal(parsedAmount.toPlainString());
} catch (NumberFormatException e) {
if (!originalAmount.isEmpty()) {
QAWarning issue = new QAWarning("ignored-amount", null, QAWarning.Severity.WARNING, 1);
issue.setProperty("example_value", originalAmount);
ctxt.addWarning(issue);
}
throw new SkipSchemaExpressionException();
}
if (getUnitExpr() != null) {
ItemIdValue unit = getUnitExpr().evaluate(ctxt);
return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound, unit);
}
return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound);
}
use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.
the class EditBatchProcessorTest method testMultipleBatches.
@Test
public void testMultipleBatches() throws MediaWikiApiErrorException, InterruptedException, IOException {
// Prepare test data
MonolingualTextValue description = Datamodel.makeMonolingualTextValue("village in Nepal", "en");
List<String> ids = new ArrayList<>();
for (int i = 124; i < 190; i++) {
ids.add("Q" + String.valueOf(i));
}
List<ItemIdValue> qids = ids.stream().map(e -> Datamodel.makeWikidataItemIdValue(e)).collect(Collectors.toList());
List<TermedStatementEntityEdit> batch = qids.stream().map(qid -> new TermedStatementEntityEditBuilder(qid).addDescription(description, true).build()).collect(Collectors.toList());
int batchSize = 50;
List<ItemDocument> fullBatch = qids.stream().map(qid -> ItemDocumentBuilder.forItemId(qid).withStatement(TestingData.generateStatement(qid, TestingData.existingId)).build()).collect(Collectors.toList());
List<ItemDocument> firstBatch = fullBatch.subList(0, batchSize);
List<ItemDocument> secondBatch = fullBatch.subList(batchSize, fullBatch.size());
when(fetcher.getEntityDocuments(toQids(firstBatch))).thenReturn(toMap(firstBatch));
when(fetcher.getEntityDocuments(toQids(secondBatch))).thenReturn(toMap(secondBatch));
// Run edits
EditBatchProcessor processor = new EditBatchProcessor(fetcher, editor, batch, library, summary, maxlag, tags, batchSize);
assertEquals(0, processor.progress());
for (int i = 124; i < 190; i++) {
assertEquals(processor.remainingEdits(), 190 - i);
processor.performEdit();
}
assertEquals(0, processor.remainingEdits());
assertEquals(100, processor.progress());
// Check result
assertEquals(new NewEntityLibrary(), library);
verify(fetcher, times(1)).getEntityDocuments(toQids(firstBatch));
verify(fetcher, times(1)).getEntityDocuments(toQids(secondBatch));
for (ItemDocument doc : fullBatch) {
verify(editor, times(1)).editEntityDocument(Datamodel.makeItemUpdate(doc.getEntityId(), doc.getRevisionId(), Datamodel.makeTermUpdate(Collections.emptyList(), Collections.emptyList()), Datamodel.makeTermUpdate(Collections.singletonList(description), Collections.emptyList()), Collections.emptyMap(), Datamodel.makeStatementUpdate(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()), Collections.emptyList(), Collections.emptyList()), false, summary, tags);
}
}
use of org.wikidata.wdtk.datamodel.interfaces.ItemIdValue in project OpenRefine by OpenRefine.
the class ReconEntityRewriterTest method testRewriteCreate.
@Test
public void testRewriteCreate() throws NewEntityNotCreatedYetException {
ItemIdValue subject = TestingData.newIdA;
rewriter = new ReconEntityRewriter(library, subject);
library.setId(4567L, "Q1234");
TermedStatementEntityEdit update = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, TestingData.newIdB)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
TermedStatementEntityEdit rewritten = rewriter.rewrite(update);
TermedStatementEntityEdit expected = new TermedStatementEntityEditBuilder(subject).addStatement(TestingData.generateStatementAddition(subject, newlyCreated)).addStatement(TestingData.generateStatementDeletion(subject, TestingData.existingId)).addLabel(Datamodel.makeMonolingualTextValue("label", "de"), true).addDescription(Datamodel.makeMonolingualTextValue("beschreibung", "de"), false).addAlias(Datamodel.makeMonolingualTextValue("darstellung", "de")).build();
assertEquals(rewritten, expected);
}
Aggregations