|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.runtime.planner; |
| 16 | + |
| 17 | +import com.google.common.collect.ImmutableList; |
| 18 | +import com.google.errorprone.annotations.Immutable; |
| 19 | +import dev.cel.runtime.CelEvaluationException; |
| 20 | +import dev.cel.runtime.CelEvaluationListener; |
| 21 | +import dev.cel.runtime.CelFunctionResolver; |
| 22 | +import dev.cel.runtime.ConcatenatedListView; |
| 23 | +import dev.cel.runtime.GlobalResolver; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Map; |
| 26 | +import org.jspecify.annotations.Nullable; |
| 27 | + |
| 28 | +@Immutable |
| 29 | +final class EvalFold extends PlannedInterpretable { |
| 30 | + |
| 31 | + private final String accuVar; |
| 32 | + private final PlannedInterpretable accuInit; |
| 33 | + private final String iterVar; |
| 34 | + private final String iterVar2; |
| 35 | + private final PlannedInterpretable iterRange; |
| 36 | + private final PlannedInterpretable condition; |
| 37 | + private final PlannedInterpretable loopStep; |
| 38 | + private final PlannedInterpretable result; |
| 39 | + |
| 40 | + static EvalFold create( |
| 41 | + long exprId, |
| 42 | + String accuVar, |
| 43 | + PlannedInterpretable accuInit, |
| 44 | + String iterVar, |
| 45 | + String iterVar2, |
| 46 | + PlannedInterpretable iterRange, |
| 47 | + PlannedInterpretable loopCondition, |
| 48 | + PlannedInterpretable loopStep, |
| 49 | + PlannedInterpretable result) { |
| 50 | + return new EvalFold( |
| 51 | + exprId, accuVar, accuInit, iterVar, iterVar2, iterRange, loopCondition, loopStep, result); |
| 52 | + } |
| 53 | + |
| 54 | + private EvalFold( |
| 55 | + long exprId, |
| 56 | + String accuVar, |
| 57 | + PlannedInterpretable accuInit, |
| 58 | + String iterVar, |
| 59 | + String iterVar2, |
| 60 | + PlannedInterpretable iterRange, |
| 61 | + PlannedInterpretable condition, |
| 62 | + PlannedInterpretable loopStep, |
| 63 | + PlannedInterpretable result) { |
| 64 | + super(exprId); |
| 65 | + this.accuVar = accuVar; |
| 66 | + this.accuInit = accuInit; |
| 67 | + this.iterVar = iterVar; |
| 68 | + this.iterVar2 = iterVar2; |
| 69 | + this.iterRange = iterRange; |
| 70 | + this.condition = condition; |
| 71 | + this.loopStep = loopStep; |
| 72 | + this.result = result; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Object eval(GlobalResolver resolver) throws CelEvaluationException { |
| 77 | + Object iterRangeRaw = iterRange.eval(resolver); |
| 78 | + Folder folder = new Folder(resolver, accuVar, iterVar, iterVar2); |
| 79 | + folder.accuVal = maybeWrapAccumulator(accuInit.eval(folder)); |
| 80 | + |
| 81 | + Object result; |
| 82 | + if (iterRangeRaw instanceof Map) { |
| 83 | + result = evalMap((Map<?, ?>) iterRangeRaw, folder); |
| 84 | + } else if (iterRangeRaw instanceof Collection) { |
| 85 | + result = evalList((Collection<?>) iterRangeRaw, folder); |
| 86 | + } else { |
| 87 | + throw new IllegalArgumentException("Unexpected iter_range type: " + iterRangeRaw.getClass()); |
| 88 | + } |
| 89 | + |
| 90 | + return maybeUnwrapAccumulator(result); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Object eval(GlobalResolver resolver, CelEvaluationListener listener) { |
| 95 | + // TODO: Implement support |
| 96 | + throw new UnsupportedOperationException("Not yet supported"); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Object eval(GlobalResolver resolver, CelFunctionResolver lateBoundFunctionResolver) { |
| 101 | + // TODO: Implement support |
| 102 | + throw new UnsupportedOperationException("Not yet supported"); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Object eval( |
| 107 | + GlobalResolver resolver, |
| 108 | + CelFunctionResolver lateBoundFunctionResolver, |
| 109 | + CelEvaluationListener listener) { |
| 110 | + // TODO: Implement support |
| 111 | + throw new UnsupportedOperationException("Not yet supported"); |
| 112 | + } |
| 113 | + |
| 114 | + private Object evalMap(Map<?, ?> iterRange, Folder folder) throws CelEvaluationException { |
| 115 | + for (Map.Entry<?, ?> entry : iterRange.entrySet()) { |
| 116 | + folder.iterVarVal = entry.getKey(); |
| 117 | + if (!iterVar2.isEmpty()) { |
| 118 | + folder.iterVar2Val = entry.getValue(); |
| 119 | + } |
| 120 | + |
| 121 | + boolean cond = (boolean) condition.eval(folder); |
| 122 | + if (!cond) { |
| 123 | + return result.eval(folder); |
| 124 | + } |
| 125 | + |
| 126 | + // TODO: Introduce comprehension safety controls, such as iteration limit. |
| 127 | + folder.accuVal = loopStep.eval(folder); |
| 128 | + } |
| 129 | + return result.eval(folder); |
| 130 | + } |
| 131 | + |
| 132 | + private Object evalList(Collection<?> iterRange, Folder folder) throws CelEvaluationException { |
| 133 | + int index = 0; |
| 134 | + for (Object item : iterRange) { |
| 135 | + if (iterVar2.isEmpty()) { |
| 136 | + folder.iterVarVal = item; |
| 137 | + } else { |
| 138 | + folder.iterVarVal = (long) index; |
| 139 | + folder.iterVar2Val = item; |
| 140 | + } |
| 141 | + |
| 142 | + boolean cond = (boolean) condition.eval(folder); |
| 143 | + if (!cond) { |
| 144 | + return result.eval(folder); |
| 145 | + } |
| 146 | + |
| 147 | + folder.accuVal = loopStep.eval(folder); |
| 148 | + index++; |
| 149 | + } |
| 150 | + return result.eval(folder); |
| 151 | + } |
| 152 | + |
| 153 | + private static Object maybeWrapAccumulator(Object val) { |
| 154 | + if (val instanceof Collection) { |
| 155 | + return new ConcatenatedListView<>((Collection<?>) val); |
| 156 | + } |
| 157 | + // TODO: Introduce mutable map support (for comp v2) |
| 158 | + return val; |
| 159 | + } |
| 160 | + |
| 161 | + private static Object maybeUnwrapAccumulator(Object val) { |
| 162 | + if (val instanceof ConcatenatedListView) { |
| 163 | + return ImmutableList.copyOf((ConcatenatedListView<?>) val); |
| 164 | + } |
| 165 | + |
| 166 | + // TODO: Introduce mutable map support (for comp v2) |
| 167 | + return val; |
| 168 | + } |
| 169 | + |
| 170 | + private static class Folder implements GlobalResolver { |
| 171 | + private final GlobalResolver resolver; |
| 172 | + private final String accuVar; |
| 173 | + private final String iterVar; |
| 174 | + private final String iterVar2; |
| 175 | + |
| 176 | + private Object iterVarVal; |
| 177 | + private Object iterVar2Val; |
| 178 | + private Object accuVal; |
| 179 | + |
| 180 | + private Folder(GlobalResolver resolver, String accuVar, String iterVar, String iterVar2) { |
| 181 | + this.resolver = resolver; |
| 182 | + this.accuVar = accuVar; |
| 183 | + this.iterVar = iterVar; |
| 184 | + this.iterVar2 = iterVar2; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public @Nullable Object resolve(String name) { |
| 189 | + if (name.equals(accuVar)) { |
| 190 | + return accuVal; |
| 191 | + } |
| 192 | + |
| 193 | + if (name.equals(iterVar)) { |
| 194 | + return this.iterVarVal; |
| 195 | + } |
| 196 | + |
| 197 | + if (name.equals(iterVar2)) { |
| 198 | + return this.iterVar2Val; |
| 199 | + } |
| 200 | + |
| 201 | + return resolver.resolve(name); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments