use of com.google.api.expr.v1alpha1.EvalRequest in project cel-java by projectnessie.
the class ConformanceServerTest method fullPipeline.
/**
* fullPipeline parses, checks, and evaluates the CEL expression in source and returns the result
* from the Eval call.
*/
FullPipelineResult fullPipeline(String source) {
// Parse
ParseRequest preq = ParseRequest.newBuilder().setCelSource(source).build();
ParseResponse pres = stub.parse(preq);
assertThat(pres.isInitialized()).isTrue();
ParsedExpr parsedExpr = pres.getParsedExpr();
assertThat(parsedExpr.isInitialized()).isTrue();
assertThat(parsedExpr.getExpr().isInitialized()).isTrue();
// Check
CheckRequest creq = CheckRequest.newBuilder().setParsedExpr(parsedExpr).build();
CheckResponse cres = stub.check(creq);
assertThat(cres.isInitialized()).isTrue();
CheckedExpr checkedExpr = cres.getCheckedExpr();
assertThat(checkedExpr.isInitialized()).isTrue();
// Eval
EvalRequest ereq = EvalRequest.newBuilder().setCheckedExpr(checkedExpr).build();
EvalResponse eres = stub.eval(ereq);
assertThat(eres.isInitialized()).isTrue();
assertThat(eres.getResult().isInitialized()).isTrue();
return new FullPipelineResult(pres, cres, eres);
}
use of com.google.api.expr.v1alpha1.EvalRequest in project cel-java by projectnessie.
the class ConformanceServiceImpl method eval.
@Override
public void eval(EvalRequest request, io.grpc.stub.StreamObserver<EvalResponse> responseObserver) {
try {
Env env = newEnv(container(request.getContainer()), types(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance()));
Program prg;
Ast ast;
switch(request.getExprKindCase()) {
case PARSED_EXPR:
ast = parsedExprToAst(request.getParsedExpr());
break;
case CHECKED_EXPR:
ast = checkedExprToAst(request.getCheckedExpr());
break;
default:
throw new IllegalArgumentException("No expression.");
}
prg = env.program(ast);
Map<String, Object> args = new HashMap<>();
request.getBindingsMap().forEach((name, exprValue) -> {
Val refVal = exprValueToRefValue(env.getTypeAdapter(), exprValue);
args.put(name, refVal);
});
// NOTE: the EvalState is currently discarded
EvalResult res = prg.eval(args);
ExprValue resultExprVal;
if (!isError(res.getVal())) {
resultExprVal = refValueToExprValue(res.getVal());
} else {
Err err = (Err) res.getVal();
if (verboseEvalErrors) {
System.err.printf("%n" + "Eval error (not necessarily a bug!!!):%n" + " error: %s%n" + "%s", err, err.hasCause() ? (stacktrace(err.toRuntimeException()) + "\n") : "");
}
resultExprVal = ExprValue.newBuilder().setError(ErrorSet.newBuilder().addErrors(Status.newBuilder().setMessage(err.toString()))).build();
}
EvalResponse.Builder resp = EvalResponse.newBuilder().setResult(resultExprVal);
responseObserver.onNext(resp.build());
responseObserver.onCompleted();
} catch (Exception e) {
responseObserver.onError(io.grpc.Status.fromCode(io.grpc.Status.Code.UNKNOWN).withDescription(stacktrace(e)).asException());
}
}
use of com.google.api.expr.v1alpha1.EvalRequest in project cel-java by projectnessie.
the class ConformanceServerTest method FullUp.
/**
* TestFullUp tests Parse, Check, and Eval back-to-back.
*/
@Test
void FullUp() {
ParseRequest preq = ParseRequest.newBuilder().setCelSource("x + y").build();
ParseResponse pres = stub.parse(preq);
assertThat(pres.isInitialized()).isTrue();
ParsedExpr parsedExpr = pres.getParsedExpr();
assertThat(parsedExpr.isInitialized()).isTrue();
CheckRequest creq = CheckRequest.newBuilder().setParsedExpr(parsedExpr).addTypeEnv(Decls.newVar("x", Decls.Int)).addTypeEnv(Decls.newVar("y", Decls.Int)).build();
CheckResponse cres = stub.check(creq);
assertThat(cres.isInitialized()).isTrue();
CheckedExpr checkedExpr = cres.getCheckedExpr();
assertThat(checkedExpr.isInitialized()).isTrue();
Type tp = checkedExpr.getTypeMapMap().get(1L);
assertThat(tp).isNotNull();
assertThat(tp.getTypeKindCase()).isSameAs(TypeKindCase.PRIMITIVE);
assertThat(tp.getPrimitive()).isSameAs(PrimitiveType.INT64);
EvalRequest ereq = EvalRequest.newBuilder().setCheckedExpr(checkedExpr).putBindings("x", exprValueInt64(1)).putBindings("y", exprValueInt64(2)).build();
EvalResponse eres = stub.eval(ereq);
assertThat(eres.isInitialized()).isTrue();
assertThat(eres.getResult().isInitialized()).isTrue();
assertThat(eres.getResult().getKindCase()).isSameAs(KindCase.VALUE);
assertThat(eres.getResult().getValue().getKindCase()).isSameAs(Value.KindCase.INT64_VALUE);
assertThat(eres.getResult().getValue().getInt64Value()).isEqualTo(3L);
}
use of com.google.api.expr.v1alpha1.EvalRequest in project cel-java by projectnessie.
the class ConformanceServerTest method Eval.
/**
* TestEval tests the Eval method.
*/
@Test
void Eval() {
EvalRequest req = EvalRequest.newBuilder().setParsedExpr(parsed).build();
EvalResponse res = stub.eval(req);
assertThat(res.isInitialized()).isTrue();
assertThat(res.getResult().isInitialized()).isTrue();
assertThat(res.getResult().getKindCase()).isSameAs(KindCase.VALUE);
assertThat(res.getResult().getValue().getKindCase()).isSameAs(Value.KindCase.INT64_VALUE);
assertThat(res.getResult().getValue().getInt64Value()).isEqualTo(2L);
}
Aggregations