Search in sources :

Example 1 with ExpressionParams

use of org.hisp.dhis.expression.ExpressionParams in project dhis2-core by dhis2.

the class VectorFunction method visitSampledPeriods.

/**
 * Visits each of the sample periods and compiles a list of the double
 * values produced.
 */
private List<Double> visitSampledPeriods(ExprContext ctx, CommonExpressionVisitor visitor) {
    ExpressionParams params = visitor.getParams();
    ExpressionState state = visitor.getState();
    List<Double> values = new ArrayList<>();
    for (Period p : params.getSamplePeriods()) {
        state.setItemsFound(0);
        state.setItemValuesFound(0);
        Map<DimensionalItemObject, Object> valueMap = firstNonNull(params.getPeriodValueMap().get(p), Collections.emptyMap());
        Double value = visitWithValueMap(ctx, visitor, valueMap);
        if ((params.getMissingValueStrategy() == SKIP_IF_ANY_VALUE_MISSING && state.getItemValuesFound() < state.getItemsFound()) || (params.getMissingValueStrategy() == SKIP_IF_ALL_VALUES_MISSING && state.getItemsFound() != 0 && state.getItemValuesFound() == 0)) {
            value = null;
        }
        if (value != null) {
            values.add(value);
        }
    }
    return values;
}
Also used : ExpressionParams(org.hisp.dhis.expression.ExpressionParams) ExpressionState(org.hisp.dhis.parser.expression.ExpressionState) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) AntlrParserUtils.castDouble(org.hisp.dhis.antlr.AntlrParserUtils.castDouble)

Example 2 with ExpressionParams

use of org.hisp.dhis.expression.ExpressionParams in project dhis2-core by dhis2.

the class DataValidationTaskTest method mockExpressionService.

private void mockExpressionService(Expression expression, Map<DimensionalItemObject, Object> vals, Double val) {
    ExpressionParams params = ExpressionParams.builder().expression(expression.getExpression()).parseType(VALIDATION_RULE_EXPRESSION).valueMap(vals).missingValueStrategy(expression.getMissingValueStrategy()).orgUnit(ouA).build();
    when(expressionService.getExpressionValue(params.toBuilder().days(p1.getDaysInPeriod()).build())).thenReturn(val);
    when(expressionService.getExpressionValue(params.toBuilder().days(p2.getDaysInPeriod()).build())).thenReturn(val);
    when(expressionService.getExpressionValue(params.toBuilder().days(p3.getDaysInPeriod()).build())).thenReturn(val);
}
Also used : ExpressionParams(org.hisp.dhis.expression.ExpressionParams)

Example 3 with ExpressionParams

use of org.hisp.dhis.expression.ExpressionParams in project dhis2-core by dhis2.

the class VectorFunction method visitWithValueMap.

/**
 * Visits a subtree with an explicit valueMap.
 */
private Double visitWithValueMap(ExprContext ctx, CommonExpressionVisitor visitor, Map<DimensionalItemObject, Object> valueMap) {
    ExpressionParams savedParams = visitor.getParams();
    ExpressionParams params = visitor.getParams().toBuilder().valueMap(valueMap).build();
    visitor.setParams(params);
    Double value = castDouble(visitor.visit(ctx));
    visitor.setParams(savedParams);
    return value;
}
Also used : ExpressionParams(org.hisp.dhis.expression.ExpressionParams) AntlrParserUtils.castDouble(org.hisp.dhis.antlr.AntlrParserUtils.castDouble)

Aggregations

ExpressionParams (org.hisp.dhis.expression.ExpressionParams)3 AntlrParserUtils.castDouble (org.hisp.dhis.antlr.AntlrParserUtils.castDouble)2 ArrayList (java.util.ArrayList)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 ExpressionState (org.hisp.dhis.parser.expression.ExpressionState)1 Period (org.hisp.dhis.period.Period)1