Search in sources :

Example 1 with Cost

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);
}
Also used : Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Cost(org.projectnessie.cel.interpreter.Coster.Cost)

Example 2 with Cost

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);
}
Also used : Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) CEL.estimateCost(org.projectnessie.cel.CEL.estimateCost) Cost(org.projectnessie.cel.interpreter.Coster.Cost) Test(org.junit.jupiter.api.Test)

Example 3 with Cost

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);
}
Also used : Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Cost(org.projectnessie.cel.interpreter.Coster.Cost)

Example 4 with Cost

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);
}
Also used : Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Cost(org.projectnessie.cel.interpreter.Coster.Cost)

Example 5 with Cost

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();
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Err(org.projectnessie.cel.common.types.Err) HashMap(java.util.HashMap) EvalState.newEvalState(org.projectnessie.cel.interpreter.EvalState.newEvalState) ByteString(com.google.protobuf.ByteString) Cost.estimateCost(org.projectnessie.cel.interpreter.Coster.Cost.estimateCost) Cost(org.projectnessie.cel.interpreter.Coster.Cost) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Cost (org.projectnessie.cel.interpreter.Coster.Cost)5 Cost.estimateCost (org.projectnessie.cel.interpreter.Coster.Cost.estimateCost)4 ByteString (com.google.protobuf.ByteString)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 CEL.estimateCost (org.projectnessie.cel.CEL.estimateCost)1 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)1 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)1 Env.newEnv (org.projectnessie.cel.Env.newEnv)1 Err (org.projectnessie.cel.common.types.Err)1 Val (org.projectnessie.cel.common.types.ref.Val)1 EvalState.newEvalState (org.projectnessie.cel.interpreter.EvalState.newEvalState)1