Search in sources :

Example 6 with Expr

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

the class Macro method makeMap.

static Expr makeMap(ExprHelper eh, Expr target, List<Expr> args) {
    String v = extractIdent(args.get(0));
    if (v == null) {
        throw new ErrorWithLocation(null, "argument is not an identifier");
    }
    Expr fn;
    Expr filter;
    if (args.size() == 3) {
        filter = args.get(1);
        fn = args.get(2);
    } else {
        filter = null;
        fn = args.get(1);
    }
    Expr accuExpr = eh.ident(AccumulatorName);
    Expr init = eh.newList();
    Expr condition = eh.literalBool(true);
    Expr step = eh.globalCall(Operator.Add.id, accuExpr, eh.newList(fn));
    if (filter != null) {
        step = eh.globalCall(Operator.Conditional.id, filter, step, accuExpr);
    }
    return eh.fold(v, target, AccumulatorName, init, condition, step, accuExpr);
}
Also used : ErrorWithLocation(org.projectnessie.cel.common.ErrorWithLocation) Expr(com.google.api.expr.v1alpha1.Expr)

Example 7 with Expr

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

the class Parser method parse.

ParseResult parse(Source source) {
    StringCharStream charStream = new StringCharStream(source.content(), source.description());
    CELLexer lexer = new CELLexer(charStream);
    CELParser parser = new CELParser(new CommonTokenStream(lexer, 0));
    RecursionListener parserListener = new RecursionListener(options.getMaxRecursionDepth());
    parser.addParseListener(parserListener);
    parser.setErrorHandler(new RecoveryLimitErrorStrategy(options.getErrorRecoveryLimit()));
    Helper helper = new Helper(source);
    Errors errors = new Errors(source);
    InnerParser inner = new InnerParser(helper, errors);
    lexer.addErrorListener(inner);
    parser.addErrorListener(inner);
    Expr expr = null;
    try {
        if (charStream.size() > options.getExpressionSizeCodePointLimit()) {
            errors.reportError(Location.NoLocation, "expression code point size exceeds limit: size: %d, limit %d", charStream.size(), options.getExpressionSizeCodePointLimit());
        } else {
            expr = inner.exprVisit(parser.start());
        }
    } catch (RecoveryLimitError | RecursionError e) {
        errors.reportError(Location.NoLocation, "%s", e.getMessage());
    }
    if (errors.hasErrors()) {
        expr = null;
    }
    return new ParseResult(expr, errors, helper.getSourceInfo());
}
Also used : CommonTokenStream(org.projectnessie.cel.shaded.org.antlr.v4.runtime.CommonTokenStream) CELLexer(org.projectnessie.cel.parser.gen.CELLexer) CELParser(org.projectnessie.cel.parser.gen.CELParser) Errors(org.projectnessie.cel.common.Errors) Expr(com.google.api.expr.v1alpha1.Expr)

Example 8 with Expr

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

the class AstPruner method maybePruneConditional.

Expr maybePruneConditional(Expr node) {
    if (!existsWithUnknownValue(node.getId())) {
        return null;
    }
    Call call = node.getCallExpr();
    Val condVal = value(call.getArgs(0).getId());
    if (condVal == null || isUnknownOrError(condVal)) {
        return null;
    }
    if (condVal == True) {
        return call.getArgs(1);
    }
    return call.getArgs(2);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Call(com.google.api.expr.v1alpha1.Expr.Call)

Example 9 with Expr

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

the class AstPruner method prune.

Expr prune(Expr node) {
    if (node == null) {
        return null;
    }
    Val val = value(node.getId());
    if (val != null && !isUnknownOrError(val)) {
        Expr newNode = maybeCreateLiteral(node.getId(), val);
        if (newNode != null) {
            return newNode;
        }
    }
    switch(node.getExprKindCase()) {
        case SELECT_EXPR:
            Select select = node.getSelectExpr();
            Expr operand = prune(select.getOperand());
            if (operand != null && operand != select.getOperand()) {
                return Expr.newBuilder().setId(node.getId()).setSelectExpr(Select.newBuilder().setOperand(operand).setField(select.getField()).setTestOnly(select.getTestOnly())).build();
            }
            break;
        case CALL_EXPR:
            Call call = node.getCallExpr();
            Expr newExpr = maybePruneFunction(node);
            if (newExpr != null) {
                newExpr = prune(newExpr);
                return newExpr;
            }
            boolean prunedCall = false;
            List<Expr> args = call.getArgsList();
            List<Expr> newArgs = new ArrayList<>(args.size());
            for (int i = 0; i < args.size(); i++) {
                Expr arg = args.get(i);
                newArgs.add(arg);
                Expr newArg = prune(arg);
                if (newArg != null && newArg != arg) {
                    prunedCall = true;
                    newArgs.set(i, newArg);
                }
            }
            Call newCall = Call.newBuilder().setFunction(call.getFunction()).setTarget(call.getTarget()).addAllArgs(newArgs).build();
            Expr newTarget = prune(call.getTarget());
            if (newTarget != null && newTarget != call.getTarget()) {
                prunedCall = true;
                newCall = Call.newBuilder().setFunction(call.getFunction()).setTarget(newTarget).addAllArgs(newArgs).build();
            }
            if (prunedCall) {
                return Expr.newBuilder().setId(node.getId()).setCallExpr(newCall).build();
            }
            break;
        case LIST_EXPR:
            CreateList list = node.getListExpr();
            List<Expr> elems = list.getElementsList();
            List<Expr> newElems = new ArrayList<>(elems.size());
            boolean prunedList = false;
            for (int i = 0; i < elems.size(); i++) {
                Expr elem = elems.get(i);
                newElems.add(elem);
                Expr newElem = prune(elem);
                if (newElem != null && newElem != elem) {
                    newElems.set(i, newElem);
                    prunedList = true;
                }
            }
            if (prunedList) {
                return Expr.newBuilder().setId(node.getId()).setListExpr(CreateList.newBuilder().addAllElements(newElems)).build();
            }
            break;
        case STRUCT_EXPR:
            boolean prunedStruct = false;
            CreateStruct struct = node.getStructExpr();
            List<Entry> entries = struct.getEntriesList();
            String messageType = struct.getMessageName();
            List<Entry> newEntries = new ArrayList<>(entries.size());
            for (int i = 0; i < entries.size(); i++) {
                Entry entry = entries.get(i);
                newEntries.add(entry);
                Expr mapKey = entry.getMapKey();
                Expr newKey = mapKey != Entry.getDefaultInstance().getMapKey() ? prune(mapKey) : null;
                Expr newValue = prune(entry.getValue());
                if ((newKey == null || newKey == mapKey) && (newValue == null || newValue == entry.getValue())) {
                    continue;
                }
                prunedStruct = true;
                Entry newEntry;
                if (!messageType.isEmpty()) {
                    newEntry = Entry.newBuilder().setFieldKey(entry.getFieldKey()).setValue(newValue).build();
                } else {
                    newEntry = Entry.newBuilder().setMapKey(newKey).setValue(newValue).build();
                }
                newEntries.set(i, newEntry);
            }
            if (prunedStruct) {
                return Expr.newBuilder().setId(node.getId()).setStructExpr(CreateStruct.newBuilder().setMessageName(messageType).addAllEntries(entries)).build();
            }
            break;
        case COMPREHENSION_EXPR:
            Comprehension compre = node.getComprehensionExpr();
            // Only the range of the comprehension is pruned since the state tracking only records
            // the last iteration of the comprehension and not each step in the evaluation which
            // means that the any residuals computed in between might be inaccurate.
            Expr newRange = prune(compre.getIterRange());
            if (newRange != null && newRange != compre.getIterRange()) {
                return Expr.newBuilder().setId(node.getId()).setComprehensionExpr(Comprehension.newBuilder().setIterVar(compre.getIterVar()).setIterRange(newRange).setAccuVar(compre.getAccuVar()).setAccuInit(compre.getAccuInit()).setLoopCondition(compre.getLoopCondition()).setLoopStep(compre.getLoopStep()).setResult(compre.getResult())).build();
            }
    }
    // allocation cost at another point. So go with the simple approach - at least for now.
    return node;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Call(com.google.api.expr.v1alpha1.Expr.Call) CreateStruct(com.google.api.expr.v1alpha1.Expr.CreateStruct) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Comprehension(com.google.api.expr.v1alpha1.Expr.Comprehension) CreateList(com.google.api.expr.v1alpha1.Expr.CreateList) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) Select(com.google.api.expr.v1alpha1.Expr.Select)

Example 10 with Expr

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

the class CELTest method CheckedExprToAst_ConstantExpr.

@Test
void CheckedExprToAst_ConstantExpr() {
    Env stdEnv = newEnv();
    String in = "10";
    AstIssuesTuple astIss = stdEnv.compile(in);
    assertThat(astIss.hasIssues()).isFalse();
    CheckedExpr expr = astToCheckedExpr(astIss.getAst());
    Ast ast2 = checkedExprToAst(expr);
    assertThat(ast2.getExpr()).isEqualTo(astIss.getAst().getExpr());
}
Also used : CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) 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)

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