private ForIteration[] initializeContexts(EvaluationContext ctx, List<IterationContextCompiled> iterationContexts) { ForIteration[] ictx = new ForIteration[iterationContexts.size()]; int i = 0; for (IterationContextCompiled icn : iterationContexts) { ictx[i] = createQuantifiedExpressionIterationContext(ctx, icn); if (i < iterationContexts.size() - 1 && ictx[i].hasNextValue()) { ForExpressionNode.setValueIntoContext(ctx, ictx[i]); } i++; } return ictx; }
public Object rreturn(Function<EvaluationContext, Object> expression) { try { ctx.enterFrame(); List results = new ArrayList(); ctx.setValue("partial", results); ForIteration[] ictx = initializeContexts(ctx, iterationContexts); while (ForExpressionNode.nextIteration(ctx, ictx)) { Object result = expression.apply(ctx); results.add(result); } return results; } catch (EndpointOfRangeNotOfNumberException e) { // ast error already reported return null; } finally { ctx.exitFrame(); } }
private ForIteration createQuantifiedExpressionIterationContext(EvaluationContext ctx, IterationContextCompiled icn) { ForIteration fi = null; String name = (String) icn.getName().apply(ctx); Object result = icn.getExpression().apply(ctx); Object rangeEnd = icn.getRangeEndExpr().apply(ctx); if (rangeEnd == null) { Iterable values = result instanceof Iterable ? (Iterable) result : Collections.singletonList(result); fi = new ForIteration(name, values); } else { valueMustBeANumber(ctx, result); BigDecimal start = (BigDecimal) result; valueMustBeANumber(ctx, rangeEnd); BigDecimal end = (BigDecimal) rangeEnd; fi = new ForIteration(name, start, end); } return fi; }
public static ForBuilder ffor(EvaluationContext ctx) { return new ForBuilder(ctx); }
/** FEEL: for x in [ 10, 20, 30 ], y in [ 1, 2, 3 ] return x * y */ @Override public Object apply(EvaluationContext feelExprCtx) { return CompiledFEELSupport.ffor(feelExprCtx) .with(c -> "x", c -> Arrays.asList(K_10, K_20, K_30)) .with(c -> "y", c -> Arrays.asList(K_1, K_2, K_3)) .rreturn(c -> ((BigDecimal) feelExprCtx.getValue("x")).multiply((BigDecimal) feelExprCtx.getValue("y"))); }