Search in sources :

Example 11 with Expr

use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.

the class CELTest method Customtypes.

@Test
void Customtypes() {
    Type exprType = Decls.newObjectType("google.api.expr.v1alpha1.Expr");
    TypeRegistry reg = newEmptyRegistry();
    Env e = newEnv(customTypeAdapter(reg), customTypeProvider(reg), container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance(), BoolT.BoolType, IntT.IntType, StringT.StringType), declarations(Decls.newVar("expr", exprType)));
    AstIssuesTuple astIss = e.compile("expr == Expr{id: 2,\n" + "\t\t\tcall_expr: Expr.Call{\n" + "\t\t\t\tfunction: \"_==_\",\n" + "\t\t\t\targs: [\n" + "\t\t\t\t\tExpr{id: 1, ident_expr: Expr.Ident{ name: \"a\" }},\n" + "\t\t\t\t\tExpr{id: 3, ident_expr: Expr.Ident{ name: \"b\" }}]\n" + "\t\t\t}}");
    assertThat(astIss.getAst().getResultType()).isEqualTo(Decls.Bool);
    Program prg = e.program(astIss.getAst());
    Object vars = mapOf("expr", Expr.newBuilder().setId(2).setCallExpr(Call.newBuilder().setFunction("_==_").addAllArgs(asList(Expr.newBuilder().setId(1).setIdentExpr(Ident.newBuilder().setName("a")).build(), Expr.newBuilder().setId(3).setIdentExpr(Ident.newBuilder().setName("b")).build()))).build());
    EvalResult out = prg.eval(vars);
    assertThat(out.getVal()).isSameAs(True);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) 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) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 12 with Expr

use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.

the class ContainerTest method ToQualifiedName.

@Test
void ToQualifiedName() {
    Expr ident = Expr.newBuilder().setId(0).setIdentExpr(Ident.newBuilder().setName("var")).build();
    String idName = toQualifiedName(ident);
    assertThat(idName).isEqualTo("var");
    Expr sel = Expr.newBuilder().setId(0).setSelectExpr(Select.newBuilder().setOperand(ident).setField("qualifier")).build();
    String qualName = toQualifiedName(sel);
    assertThat(qualName).isEqualTo("var.qualifier");
    sel = Expr.newBuilder().setId(0).setSelectExpr(Select.newBuilder().setOperand(ident).setField("qualifier").setTestOnly(true)).build();
    assertThat(toQualifiedName(sel)).isNull();
    Expr unary = Expr.newBuilder().setId(0).setCallExpr(Call.newBuilder().setFunction("!_").addArgs(ident)).build();
    sel = Expr.newBuilder().setId(0).setSelectExpr(Select.newBuilder().setOperand(unary).setField("qualifier")).build();
    assertThat(toQualifiedName(sel)).isNull();
}
Also used : Expr(com.google.api.expr.v1alpha1.Expr) Test(org.junit.jupiter.api.Test)

Example 13 with Expr

use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.

the class Checker method Check.

/**
 * Check performs type checking, giving a typed AST. The input is a ParsedExpr proto and an env
 * which encapsulates type binding of variables, declarations of built-in functions, descriptions
 * of protocol buffers, and a registry for errors. Returns a CheckedExpr proto, which might not be
 * usable if there are errors in the error registry.
 */
public static CheckResult Check(ParseResult parsedExpr, Source source, CheckerEnv env) {
    TypeErrors errors = new TypeErrors(source);
    Checker c = new Checker(env, errors, newMapping(), 0, parsedExpr.getSourceInfo());
    Expr.Builder b = parsedExpr.getExpr().toBuilder();
    c.check(b);
    Expr e = b.build();
    // Walk over the final type map substituting any type parameters either by their bound value or
    // by DYN.
    Map<Long, Type> m = new HashMap<>();
    c.types.forEach((k, v) -> m.put(k, substitute(c.mappings, v, true)));
    CheckedExpr checkedExpr = CheckedExpr.newBuilder().setExpr(e).setSourceInfo(parsedExpr.getSourceInfo()).putAllTypeMap(m).putAllReferenceMap(c.references).build();
    return new CheckResult(checkedExpr, errors);
}
Also used : CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) HashMap(java.util.HashMap) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr)

Example 14 with Expr

use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.

the class PbObjectTest method protoObjectIsSet.

@Test
void protoObjectIsSet() {
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
    Val obj = reg.nativeToValue(msg);
    assertThat(obj).isInstanceOf(ObjectT.class);
    ObjectT objVal = (ObjectT) obj;
    assertThat(objVal.isSet(stringOf("source_info"))).isSameAs(True);
    assertThat(objVal.isSet(stringOf("expr"))).isSameAs(False);
    assertThat(objVal.isSet(stringOf("bad_field"))).matches(Err::isError);
    assertThat(objVal.isSet(IntZero)).matches(Err::isError);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Err(org.projectnessie.cel.common.types.Err) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 15 with Expr

use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.

the class UnparserTest method unparseIdentical.

@ParameterizedTest
@MethodSource("unparseIdenticalSource")
void unparseIdentical(String name, String in) {
    Parser parser = new Parser(Options.builder().build());
    ParseResult p = parser.parse(Source.newTextSource(in));
    if (p.hasErrors()) {
        fail(p.getErrors().toDisplayString());
    }
    String out = Unparser.unparse(p.getExpr(), p.getSourceInfo());
    assertThat(out).isEqualTo(in);
    ParseResult p2 = parser.parse(Source.newTextSource(out));
    if (p2.hasErrors()) {
        fail(p2.getErrors().toDisplayString());
    }
    Expr before = p.getExpr();
    Expr after = p2.getExpr();
    assertThat(before).isEqualTo(after);
}
Also used : ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) Expr(com.google.api.expr.v1alpha1.Expr) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Expr (edu.stanford.CVC4.Expr)57 Test (org.junit.Test)55 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)49 SExpr (edu.stanford.CVC4.SExpr)42 Expr (com.microsoft.z3.Expr)32 Result (edu.stanford.CVC4.Result)32 Rational (edu.stanford.CVC4.Rational)28 Expr (com.google.api.expr.v1alpha1.Expr)23 BoolExpr (com.microsoft.z3.BoolExpr)22 ArrayType (edu.stanford.CVC4.ArrayType)12 BitVectorType (edu.stanford.CVC4.BitVectorType)12 Status (com.microsoft.z3.Status)11 Type (edu.stanford.CVC4.Type)11 HashMap (java.util.HashMap)11 Test (org.junit.jupiter.api.Test)11 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)10 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)9 ArithExpr (com.microsoft.z3.ArithExpr)8 BitVecExpr (com.microsoft.z3.BitVecExpr)8 Val (org.projectnessie.cel.common.types.ref.Val)7