use of org.hisp.dhis.parser.expression.ExpressionState in project dhis2-core by dhis2.
the class PeriodOffset method evaluate.
@Override
public Object evaluate(ExprContext ctx, CommonExpressionVisitor visitor) {
ExpressionState state = visitor.getState();
int existingPeriodOffset = (state.getQueryMods() == null) ? 0 : state.getQueryMods().getPeriodOffset();
int parsedPeriodOffset = (ctx.period == null) ? 0 : firstNonNull(parseInt(ctx.period.getText()), 0);
int periodOffset = existingPeriodOffset + parsedPeriodOffset;
QueryModifiers queryMods = state.getQueryModsBuilder().periodOffset(periodOffset).build();
return visitor.visitWithQueryMods(ctx.expr(0), queryMods);
}
use of org.hisp.dhis.parser.expression.ExpressionState in project dhis2-core by dhis2.
the class RepeatableProgramStageOffset method next.
private Object next(ExpressionParser.ExprContext ctx, CommonExpressionVisitor visitor) {
ExpressionState state = visitor.getState();
int oldStageOffset = state.getStageOffset();
state.setStageOffset(Integer.parseInt(ctx.stage.getText()));
Object ret = visitor.visit(ctx.expr(0));
state.setStageOffset(oldStageOffset);
return ret;
}
use of org.hisp.dhis.parser.expression.ExpressionState 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;
}
use of org.hisp.dhis.parser.expression.ExpressionState in project dhis2-core by dhis2.
the class DefaultExpressionService method getExpressionValue.
// -------------------------------------------------------------------------
// Compute the value of the expression
// -------------------------------------------------------------------------
@Override
public Object getExpressionValue(ExpressionParams params) {
if (isEmpty(params.getExpression())) {
return null;
}
CommonExpressionVisitor visitor = newVisitor(ITEM_EVALUATE, params);
Object value = visit(params.getExpression(), params.getDataType(), visitor, true);
ExpressionState state = visitor.getState();
int itemsFound = state.getItemsFound();
int itemValuesFound = state.getItemValuesFound();
if (state.isUnprotectedNullDateFound()) {
return null;
}
switch(params.getMissingValueStrategy()) {
case SKIP_IF_ANY_VALUE_MISSING:
if (itemValuesFound < itemsFound) {
return null;
}
case SKIP_IF_ALL_VALUES_MISSING:
if (itemsFound != 0 && itemValuesFound == 0) {
return null;
}
case NEVER_SKIP:
if (value == null) {
switch(params.getDataType()) {
case NUMERIC:
return 0d;
case BOOLEAN:
return FALSE;
case TEXT:
return "";
}
}
}
return value;
}
use of org.hisp.dhis.parser.expression.ExpressionState in project dhis2-core by dhis2.
the class VectorFunction method getSampleValues.
/**
* Gets a list of sample values to aggregate.
* <p>
* The missingValueStrategy is handled as follows: for each sample
* expression inside the aggregation function, if there are any sample
* values missing and the strategy is SKIP_IF_ANY_VALUE_MISSING, then that
* sample is skipped. Also if all the values are missing and the strategy is
* SKIP_IF_ALL_VALUES_MISSING, then that sample is skipped.
* <p>
* Finally, if there were any items in the sample expression, the count of
* items in the main expression is incremented. And if there was at least
* one sample value, the count of item values in the main expression is
* incremented. This means that if the vector is empty, it counts as a
* missing value in the main expression.
*/
private List<Double> getSampleValues(ExprContext ctx, CommonExpressionVisitor visitor) {
ExpressionState state = visitor.getState();
int savedItemsFound = state.getItemsFound();
int savedItemValuesFound = state.getItemValuesFound();
List<Double> values = visitSampledPeriods(ctx, visitor);
if (state.getItemsFound() > 0) {
savedItemsFound++;
if (!values.isEmpty()) {
savedItemValuesFound++;
}
}
state.setItemsFound(savedItemsFound);
state.setItemValuesFound(savedItemValuesFound);
return values;
}
Aggregations