Infer precision from engineering notation of quantity values

This commit is contained in:
Antonin Delpeuch 2018-03-20 18:17:42 +00:00
parent ec6d0eec64
commit b1e368e7a8
2 changed files with 18 additions and 3 deletions

View File

@ -64,8 +64,21 @@ public class WbQuantityExpr implements WbExpression<QuantityValue> {
// we know the amount is nonnull, nonempty here
BigDecimal parsedAmount = null;
BigDecimal lowerBound = null;
BigDecimal upperBound = null;
try {
parsedAmount = new BigDecimal(amount.getString());
String originalAmount = amount.getString().toUpperCase();
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) {
throw new SkipSchemaExpressionException();
@ -73,10 +86,10 @@ public class WbQuantityExpr implements WbExpression<QuantityValue> {
if (getUnitExpr() != null) {
ItemIdValue unit = getUnitExpr().evaluate(ctxt);
return Datamodel.makeQuantityValue(parsedAmount, unit.getIri());
return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound, unit.getIri());
}
return Datamodel.makeQuantityValue(parsedAmount);
return Datamodel.makeQuantityValue(parsedAmount, lowerBound, upperBound);
}
@JsonProperty("amount")

View File

@ -83,6 +83,8 @@ public class WbQuantityExprTest extends WbExpressionTest<QuantityValue> {
setRow("38.4E+3", recon("Q42"));
QuantityValue val = exprWithUnit.evaluate(ctxt);
assertEquals("38400", val.getNumericValue().toString());
assertEquals("38350", val.getLowerBound().toString());
assertEquals("38450", val.getUpperBound().toString());
}
}