Search in sources :

Example 1 with DecimalLiteralImpl

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;
                }
            }
        }
    }
}
Also used : Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) BigInteger(java.math.BigInteger) IntegerLiteralImpl(org.openrdf.model.impl.IntegerLiteralImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) DecimalLiteralImpl(org.openrdf.model.impl.DecimalLiteralImpl) BigDecimal(java.math.BigDecimal) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)

Example 2 with DecimalLiteralImpl

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);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) DecimalLiteralImpl(org.openrdf.model.impl.DecimalLiteralImpl) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with DecimalLiteralImpl

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);
}
Also used : BindingSet(org.openrdf.query.BindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) DecimalLiteralImpl(org.openrdf.model.impl.DecimalLiteralImpl) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

BigDecimal (java.math.BigDecimal)3 DecimalLiteralImpl (org.openrdf.model.impl.DecimalLiteralImpl)3 MapBindingSet (org.openrdf.query.impl.MapBindingSet)3 Test (org.junit.Test)2 BigInteger (java.math.BigInteger)1 Literal (org.openrdf.model.Literal)1 Value (org.openrdf.model.Value)1 IntegerLiteralImpl (org.openrdf.model.impl.IntegerLiteralImpl)1 BindingSet (org.openrdf.query.BindingSet)1 ValueExprEvaluationException (org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)1