Search in sources :

Example 76 with Expr

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

the class Unparser method visitStructMsg.

void visitStructMsg(CreateStruct expr) {
    List<Entry> entries = expr.getEntriesList();
    str.append(expr.getMessageName());
    str.append("{");
    for (int i = 0; i < entries.size(); i++) {
        if (i > 0) {
            str.append(", ");
        }
        Entry entry = entries.get(i);
        String f = entry.getFieldKey();
        str.append(f);
        str.append(": ");
        Expr v = entry.getValue();
        visit(v);
    }
    str.append("}");
}
Also used : Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr)

Example 77 with Expr

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

the class AstPruner method pruneAst.

public static Expr pruneAst(Expr expr, EvalState state) {
    AstPruner pruner = new AstPruner(expr, state, 1);
    Expr newExpr = pruner.prune(expr);
    return newExpr;
}
Also used : Expr(com.google.api.expr.v1alpha1.Expr)

Example 78 with Expr

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

the class AstPruner method maybeCreateLiteral.

Expr maybeCreateLiteral(long id, Val v) {
    Type t = v.type();
    switch(t.typeEnum()) {
        case Bool:
            return createLiteral(id, Constant.newBuilder().setBoolValue((Boolean) v.value()).build());
        case Int:
            return createLiteral(id, Constant.newBuilder().setInt64Value(((Number) v.value()).longValue()).build());
        case Uint:
            return createLiteral(id, Constant.newBuilder().setUint64Value(((Number) v.value()).longValue()).build());
        case String:
            return createLiteral(id, Constant.newBuilder().setStringValue(v.value().toString()).build());
        case Double:
            return createLiteral(id, Constant.newBuilder().setDoubleValue(((Number) v.value()).doubleValue()).build());
        case Bytes:
            return createLiteral(id, Constant.newBuilder().setBytesValue(ByteString.copyFrom((byte[]) v.value())).build());
        case Null:
            return createLiteral(id, Constant.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
    }
    // Attempt to build a list literal.
    if (v instanceof Lister) {
        Lister list = (Lister) v;
        int sz = (int) list.size().intValue();
        List<Expr> elemExprs = new ArrayList<>(sz);
        for (int i = 0; i < sz; i++) {
            Val elem = list.get(intOf(i));
            if (isUnknownOrError(elem)) {
                return null;
            }
            Expr elemExpr = maybeCreateLiteral(nextID(), elem);
            if (elemExpr == null) {
                return null;
            }
            elemExprs.add(elemExpr);
        }
        return Expr.newBuilder().setId(id).setListExpr(CreateList.newBuilder().addAllElements(elemExprs).build()).build();
    }
    // Create a map literal if possible.
    if (v instanceof Mapper) {
        Mapper mp = (Mapper) v;
        IteratorT it = mp.iterator();
        List<Entry> entries = new ArrayList<>((int) mp.size().intValue());
        while (it.hasNext() == True) {
            Val key = it.next();
            Val val = mp.get(key);
            if (isUnknownOrError(key) || isUnknownOrError(val)) {
                return null;
            }
            Expr keyExpr = maybeCreateLiteral(nextID(), key);
            if (keyExpr == null) {
                return null;
            }
            Expr valExpr = maybeCreateLiteral(nextID(), val);
            if (valExpr == null) {
                return null;
            }
            Entry entry = Entry.newBuilder().setId(nextID()).setMapKey(keyExpr).setValue(valExpr).build();
            entries.add(entry);
        }
        return Expr.newBuilder().setId(id).setStructExpr(CreateStruct.newBuilder().addAllEntries(entries)).build();
    }
    // the enumeration the fields for a given message.
    return null;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Mapper(org.projectnessie.cel.common.types.traits.Mapper) IteratorT(org.projectnessie.cel.common.types.IteratorT) Type(org.projectnessie.cel.common.types.ref.Type) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) Lister(org.projectnessie.cel.common.types.traits.Lister) ArrayList(java.util.ArrayList)

Example 79 with Expr

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

the class Macro method makeFilter.

static Expr makeFilter(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 filter = 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(args.get(0)));
    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 80 with Expr

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

the class Macro method makeQuantifier.

static Expr makeQuantifier(QuantifierKind kind, ExprHelper eh, Expr target, List<Expr> args) {
    String v = extractIdent(args.get(0));
    if (v == null) {
        Location location = eh.offsetLocation(args.get(0).getId());
        throw new ErrorWithLocation(location, "argument must be a simple name");
    }
    Supplier<Expr> accuIdent = () -> eh.ident(AccumulatorName);
    Expr init;
    Expr condition;
    Expr step;
    Expr result;
    switch(kind) {
        case quantifierAll:
            init = eh.literalBool(true);
            condition = eh.globalCall(Operator.NotStrictlyFalse.id, accuIdent.get());
            step = eh.globalCall(Operator.LogicalAnd.id, accuIdent.get(), args.get(1));
            result = accuIdent.get();
            break;
        case quantifierExists:
            init = eh.literalBool(false);
            condition = eh.globalCall(Operator.NotStrictlyFalse.id, eh.globalCall(Operator.LogicalNot.id, accuIdent.get()));
            step = eh.globalCall(Operator.LogicalOr.id, accuIdent.get(), args.get(1));
            result = accuIdent.get();
            break;
        case quantifierExistsOne:
            Expr zeroExpr = eh.literalInt(0);
            Expr oneExpr = eh.literalInt(1);
            init = zeroExpr;
            condition = eh.literalBool(true);
            step = eh.globalCall(Operator.Conditional.id, args.get(1), eh.globalCall(Operator.Add.id, accuIdent.get(), oneExpr), accuIdent.get());
            result = eh.globalCall(Operator.Equals.id, accuIdent.get(), oneExpr);
            break;
        default:
            throw new ErrorWithLocation(null, String.format("unrecognized quantifier '%s'", kind));
    }
    return eh.fold(v, target, AccumulatorName, init, condition, step, result);
}
Also used : ErrorWithLocation(org.projectnessie.cel.common.ErrorWithLocation) Expr(com.google.api.expr.v1alpha1.Expr) Location(org.projectnessie.cel.common.Location) ErrorWithLocation(org.projectnessie.cel.common.ErrorWithLocation)

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