use of org.projectnessie.cel.interpreter.Coster.Cost in project cel-java by projectnessie.
the class Interpretable method calExhaustiveBinaryOpsCost.
static Cost calExhaustiveBinaryOpsCost(Interpretable lhs, Interpretable rhs) {
Cost l = estimateCost(lhs);
Cost r = estimateCost(rhs);
return Cost.OneOne.add(l).add(r);
}
use of org.projectnessie.cel.interpreter.Coster.Cost in project cel-java by projectnessie.
the class CELTest method Cost.
@Test
void Cost() {
Env e = newEnv();
AstIssuesTuple astIss = e.compile("\"Hello, World!\"");
assertThat(astIss.hasIssues()).isFalse();
Cost wantedCost = Cost.None;
// Test standard evaluation cost.
Program prg = e.program(astIss.getAst());
Cost c = estimateCost(prg);
assertThat(c).isEqualTo(wantedCost);
// Test the factory-based evaluation cost.
prg = e.program(astIss.getAst(), evalOptions(OptExhaustiveEval));
c = estimateCost(prg);
assertThat(c).isEqualTo(wantedCost);
}
use of org.projectnessie.cel.interpreter.Coster.Cost in project cel-java by projectnessie.
the class Interpretable method sumOfCost.
static Cost sumOfCost(Interpretable[] interps) {
long min = 0L;
long max = 0L;
for (Interpretable in : interps) {
Cost t = estimateCost(in);
min += t.min;
max += t.max;
}
return costOf(min, max);
}
use of org.projectnessie.cel.interpreter.Coster.Cost in project cel-java by projectnessie.
the class Interpretable method calShortCircuitBinaryOpsCost.
static Cost calShortCircuitBinaryOpsCost(Interpretable lhs, Interpretable rhs) {
Cost l = estimateCost(lhs);
Cost r = estimateCost(rhs);
return costOf(l.min, l.max + r.max + 1);
}
use of org.projectnessie.cel.interpreter.Coster.Cost in project cel-java by projectnessie.
the class InterpreterTest method interpreter.
@ParameterizedTest
@MethodSource("testCases")
void interpreter(TestCase tc) {
Assumptions.assumeTrue(tc.disabled == null, tc.disabled);
Program prg = program(tc);
Val want = True;
if (tc.out != null) {
want = (Val) tc.out;
}
Val got = prg.interpretable.eval(prg.activation);
if (UnknownT.isUnknown(want)) {
assertThat(got).isEqualTo(want);
} else if (tc.err != null) {
assertThat(got).isInstanceOf(Err.class).extracting(Object::toString).isEqualTo(tc.err);
} else if (isError(want)) {
assertThat(got).isEqualTo(want);
assertThat(got.equal(want)).isSameAs(True);
} else {
if (isError(got) && ((Err) got).hasCause()) {
throw ((Err) got).toRuntimeException();
}
assertThat(got).isEqualTo(want);
assertThat(got.equal(want)).isSameAs(True);
}
if (tc.cost != null) {
Cost cost = estimateCost(prg.interpretable);
assertThat(cost).isEqualTo(tc.cost);
}
EvalState state = newEvalState();
Map<String, InterpretableDecorator> opts = new HashMap<>();
opts.put("optimize", optimize());
opts.put("exhaustive", exhaustiveEval(state));
opts.put("track", trackState(state));
for (Entry<String, InterpretableDecorator> en : opts.entrySet()) {
String mode = en.getKey();
InterpretableDecorator opt = en.getValue();
prg = program(tc, opt);
got = prg.interpretable.eval(prg.activation);
if (UnknownT.isUnknown(want)) {
assertThat(got).isEqualTo(want);
} else if (tc.err != null) {
assertThat(got).isInstanceOf(Err.class).extracting(Object::toString).asString().startsWith(tc.err);
} else {
assertThat(got).isEqualTo(want);
assertThat(got.equal(want)).isSameAs(True);
}
if ("exhaustive".equals(mode) && tc.cost != null) {
Cost wantedCost = tc.cost;
if (tc.exhaustiveCost != null) {
wantedCost = tc.exhaustiveCost;
}
Cost cost = estimateCost(prg.interpretable);
assertThat(cost).isEqualTo(wantedCost);
}
if ("optimize".equals(mode) && tc.cost != null) {
Cost wantedCost = tc.cost;
if (tc.optimizedCost != null) {
wantedCost = tc.optimizedCost;
}
Cost cost = estimateCost(prg.interpretable);
assertThat(cost).isEqualTo(wantedCost);
}
state.reset();
}
}
Aggregations