Search in sources :

Example 16 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method Abbrevs_Disambiguation.

@Test
void Abbrevs_Disambiguation() {
    Env env = newEnv(abbrevs("external.Expr"), container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance()), declarations(Decls.newVar("test", Decls.Bool), Decls.newVar("external.Expr", Decls.String)));
    // This expression will return either a string or a protobuf Expr value depending on the value
    // of the 'test' argument. The fully qualified type name is used indicate that the protobuf
    // typed 'Expr' should be used rather than the abbreviatation for 'external.Expr'.
    AstIssuesTuple astIss = env.compile("test ? dyn(Expr) : google.api.expr.v1alpha1.Expr{id: 1}");
    assertThat(astIss.hasIssues()).isFalse();
    Program prg = env.program(astIss.getAst());
    EvalResult out = prg.eval(mapOf("test", true, "external.Expr", "string expr"));
    assertThat(out.getVal().value()).isEqualTo("string expr");
    out = prg.eval(mapOf("test", false, "external.Expr", "wrong expr"));
    Expr want = Expr.newBuilder().setId(1).build();
    Expr got = out.getVal().convertToNative(Expr.class);
    assertThat(got).isEqualTo(want);
}
Also used : ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) EvalResult(org.projectnessie.cel.Program.EvalResult) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 17 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method CustomEnvError.

@Test
void CustomEnvError() {
    Env e = newCustomEnv(StdLib(), StdLib());
    AstIssuesTuple xIss = e.compile("a.b.c == true");
    assertThat(xIss.hasIssues()).isTrue();
}
Also used : Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 18 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method ParseAndCheckConcurrently.

@SuppressWarnings("rawtypes")
@Test
void ParseAndCheckConcurrently() throws Exception {
    Env e = newEnv(container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance()), declarations(Decls.newVar("expr", Decls.newObjectType("google.api.expr.v1alpha1.Expr"))));
    Consumer<String> parseAndCheck = expr -> {
        AstIssuesTuple xIss = e.compile(expr);
        assertThat(xIss.hasIssues()).isFalse();
    };
    int concurrency = 10;
    ExecutorService executor = Executors.newFixedThreadPool(concurrency);
    try {
        CompletableFuture[] futures = IntStream.range(0, concurrency).mapToObj(i -> CompletableFuture.runAsync(() -> parseAndCheck.accept(String.format("expr.id + %d", i)), executor)).toArray(CompletableFuture[]::new);
        CompletableFuture.allOf(futures).get(30, TimeUnit.SECONDS);
    } finally {
        executor.shutdown();
        assertThat(executor.awaitTermination(30, TimeUnit.SECONDS)).isTrue();
    }
}
Also used : BoolT(org.projectnessie.cel.common.types.BoolT) Interpretable(org.projectnessie.cel.interpreter.Interpretable) Macro(org.projectnessie.cel.parser.Macro) Call(com.google.api.expr.v1alpha1.Expr.Call) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PartialActivation(org.projectnessie.cel.interpreter.Activation.PartialActivation) EnvOption.container(org.projectnessie.cel.EnvOption.container) Macro.newReceiverMacro(org.projectnessie.cel.parser.Macro.newReceiverMacro) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Disabled(org.junit.jupiter.api.Disabled) InterpretableDecorator(org.projectnessie.cel.interpreter.InterpretableDecorator) Collections.singletonList(java.util.Collections.singletonList) True(org.projectnessie.cel.common.types.BoolT.True) Err.isError(org.projectnessie.cel.common.types.Err.isError) Container(org.projectnessie.cel.common.types.traits.Container) Arrays.asList(java.util.Arrays.asList) CEL.estimateCost(org.projectnessie.cel.CEL.estimateCost) OptPartialEval(org.projectnessie.cel.EvalOption.OptPartialEval) ProtoTypeRegistry.newEmptyRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) EnvOption.customTypeProvider(org.projectnessie.cel.EnvOption.customTypeProvider) Val(org.projectnessie.cel.common.types.ref.Val) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) InterpretableAttribute(org.projectnessie.cel.interpreter.Interpretable.InterpretableAttribute) Collections.emptyList(java.util.Collections.emptyList) Expr(com.google.api.expr.v1alpha1.Expr) Ident(com.google.api.expr.v1alpha1.Expr.Ident) InterpretableCall(org.projectnessie.cel.interpreter.Interpretable.InterpretableCall) OptExhaustiveEval(org.projectnessie.cel.EvalOption.OptExhaustiveEval) OptTrackState(org.projectnessie.cel.EvalOption.OptTrackState) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Decls(org.projectnessie.cel.checker.Decls) CEL.attributePattern(org.projectnessie.cel.CEL.attributePattern) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) ProgramOption.functions(org.projectnessie.cel.ProgramOption.functions) DefaultTypeAdapter(org.projectnessie.cel.common.types.pb.DefaultTypeAdapter) Overload(org.projectnessie.cel.interpreter.functions.Overload) EvalResult(org.projectnessie.cel.Program.EvalResult) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) IntOne(org.projectnessie.cel.common.types.IntT.IntOne) IntStream(java.util.stream.IntStream) Overloads(org.projectnessie.cel.common.types.Overloads) Env.newEnv(org.projectnessie.cel.Env.newEnv) StdLib(org.projectnessie.cel.Library.StdLib) IntT(org.projectnessie.cel.common.types.IntT) Trait(org.projectnessie.cel.common.types.traits.Trait) EnvOption.declarations(org.projectnessie.cel.EnvOption.declarations) CompletableFuture(java.util.concurrent.CompletableFuture) Err.valOrErr(org.projectnessie.cel.common.types.Err.valOrErr) CEL.partialVars(org.projectnessie.cel.CEL.partialVars) AtomicReference(java.util.concurrent.atomic.AtomicReference) EnvOption.customTypeAdapter(org.projectnessie.cel.EnvOption.customTypeAdapter) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Mapper(org.projectnessie.cel.common.types.traits.Mapper) ProgramOption.evalOptions(org.projectnessie.cel.ProgramOption.evalOptions) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Type(com.google.api.expr.v1alpha1.Type) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CEL.noVars(org.projectnessie.cel.CEL.noVars) ExecutorService(java.util.concurrent.ExecutorService) Collections.emptyMap(java.util.Collections.emptyMap) CEL.astToString(org.projectnessie.cel.CEL.astToString) ProgramOption.globals(org.projectnessie.cel.ProgramOption.globals) Operator(org.projectnessie.cel.common.operators.Operator) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) UnknownT(org.projectnessie.cel.common.types.UnknownT) Cost(org.projectnessie.cel.interpreter.Coster.Cost) EnvOption.types(org.projectnessie.cel.EnvOption.types) ProgramOption.customDecorator(org.projectnessie.cel.ProgramOption.customDecorator) EnvOption.macros(org.projectnessie.cel.EnvOption.macros) Interpretable.newConstValue(org.projectnessie.cel.interpreter.Interpretable.newConstValue) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) EvalState(org.projectnessie.cel.interpreter.EvalState) Util.mapOf(org.projectnessie.cel.Util.mapOf) StringT(org.projectnessie.cel.common.types.StringT) EnvOption.homogeneousAggregateLiterals(org.projectnessie.cel.EnvOption.homogeneousAggregateLiterals) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) EnvOption.abbrevs(org.projectnessie.cel.EnvOption.abbrevs) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) CEL.astToString(org.projectnessie.cel.CEL.astToString) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 19 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple 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 20 with AstIssuesTuple

use of org.projectnessie.cel.Env.AstIssuesTuple in project cel-java by projectnessie.

the class CELTest method exampleWithBuiltins.

@Test
void exampleWithBuiltins() {
    // Variables used within this expression environment.
    EnvOption decls = declarations(Decls.newVar("i", Decls.String), Decls.newVar("you", Decls.String));
    Env env = newEnv(decls);
    // Compile the expression.
    AstIssuesTuple astIss = env.compile("\"Hello \" + you + \"! I'm \" + i + \".\"");
    assertThat(astIss.hasIssues()).isFalse();
    // Create the program, and evaluate it against some input.
    Program prg = env.program(astIss.getAst());
    // If the Eval() call were provided with cel.evalOptions(OptTrackState) the details response
    // (2nd return) would be non-nil.
    EvalResult out = prg.eval(mapOf("i", "CEL", "you", "world"));
    assertThat(out.getVal().equal(stringOf("Hello world! I'm CEL."))).isSameAs(True);
}
Also used : EvalResult(org.projectnessie.cel.Program.EvalResult) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Aggregations

AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)26 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)26 Env.newEnv (org.projectnessie.cel.Env.newEnv)26 Test (org.junit.jupiter.api.Test)24 EvalResult (org.projectnessie.cel.Program.EvalResult)17 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)12 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)12 CEL.astToString (org.projectnessie.cel.CEL.astToString)11 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)10 CEL.astToCheckedExpr (org.projectnessie.cel.CEL.astToCheckedExpr)10 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)8 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)8 Expr (com.google.api.expr.v1alpha1.Expr)7 Type (com.google.api.expr.v1alpha1.Type)6 CEL.estimateCost (org.projectnessie.cel.CEL.estimateCost)6 Call (com.google.api.expr.v1alpha1.Expr.Call)5 Ident (com.google.api.expr.v1alpha1.Expr.Ident)5 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.emptyMap (java.util.Collections.emptyMap)5