use of org.openrdf.model.impl.DecimalLiteralImpl in project incubator-rya by apache.
the class AverageFunction method update.
@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
checkArgument(aggregation.getAggregationType() == AggregationType.AVERAGE, "The AverageFunction only accepts AVERAGE AggregationElements.");
requireNonNull(state);
requireNonNull(childBindingSet);
// Only update the average if the child contains the binding that we are averaging.
final String aggregatedName = aggregation.getAggregatedBindingName();
if (childBindingSet.hasBinding(aggregatedName)) {
final MapBindingSet result = state.getBindingSet();
final String resultName = aggregation.getResultBindingName();
final boolean newBinding = !result.hasBinding(resultName);
// Get the state of the average.
final Map<String, AverageState> averageStates = state.getAverageStates();
AverageState averageState = newBinding ? new AverageState() : averageStates.get(resultName);
// Update the state of the average.
final Value childValue = childBindingSet.getValue(aggregatedName);
if (childValue instanceof Literal) {
final Literal childLiteral = (Literal) childValue;
if (childLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(childLiteral.getDatatype())) {
try {
// Update the sum.
final Literal oldSum = new DecimalLiteralImpl(averageState.getSum());
final BigDecimal sum = MathUtil.compute(oldSum, childLiteral, MathOp.PLUS).decimalValue();
// Update the count.
final BigInteger count = averageState.getCount().add(BigInteger.ONE);
// Update the BindingSet to include the new average.
final Literal sumLiteral = new DecimalLiteralImpl(sum);
final Literal countLiteral = new IntegerLiteralImpl(count);
final Literal average = MathUtil.compute(sumLiteral, countLiteral, MathOp.DIVIDE);
result.addBinding(resultName, average);
// Update the average state that is stored.
averageState = new AverageState(sum, count);
averageStates.put(resultName, averageState);
} catch (final ValueExprEvaluationException e) {
log.error("A problem was encountered while updating an Average Aggregation. This binding set will be ignored: " + childBindingSet);
return;
}
}
}
}
}
use of org.openrdf.model.impl.DecimalLiteralImpl in project incubator-rya by apache.
the class BindingSetStringConverterTest method toString_Decimal.
@Test
public void toString_Decimal() throws BindingSetConversionException {
// Setup the binding set that will be converted.
final MapBindingSet originalBindingSet = new MapBindingSet();
originalBindingSet.addBinding("x", new DecimalLiteralImpl(new BigDecimal(2.5)));
// Convert it to a String.
final VariableOrder varOrder = new VariableOrder("x");
final BindingSetConverter<String> converter = new BindingSetStringConverter();
final String bindingSetString = converter.convert(originalBindingSet, varOrder);
// Ensure it converted to the expected result.
final String expected = "2.5<<~>>http://www.w3.org/2001/XMLSchema#decimal";
assertEquals(expected, bindingSetString);
}
use of org.openrdf.model.impl.DecimalLiteralImpl in project incubator-rya by apache.
the class BindingSetStringConverterTest method fromString_Decimal.
@Test
public void fromString_Decimal() throws BindingSetConversionException {
// Setup the String that will be converted.
final String bindingSetString = "2.5<<~>>http://www.w3.org/2001/XMLSchema#decimal";
// Convert it to a BindingSet
final BindingSetConverter<String> converter = new BindingSetStringConverter();
final BindingSet bindingSet = converter.convert(bindingSetString, new VariableOrder("x"));
// Ensure it converted to the expected result.
final MapBindingSet expected = new MapBindingSet();
expected.addBinding("x", new DecimalLiteralImpl(new BigDecimal(2.5)));
assertEquals(expected, bindingSet);
}
Aggregations