Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any example about how to add user-defined macros or functions in cel-cpp #213

Open
lshmouse opened this issue Jun 6, 2023 · 1 comment

Comments

@lshmouse
Copy link

lshmouse commented Jun 6, 2023

We use cel-cpp to write some rules for trigger the action when some condition matched. For some sophisticated rules,we need use some user-defined macros or functions, for example: compute the 3D IOU for two 3D objects(proto message), compute the included angle of two 3D vector and so on.

After search the codebase, we can not find examples about how to add user-defined macros or functions in cel-cpp.

@keshav861
Copy link

I think this example is good to understand and to add user defined macros or functions in cel cpp for 3D IOU prototype.
example :-

#include "eval/eval/evaluator.h"
#include "eval/public/cel_function.h"
#include "eval/public/cel_function_registry.h"
#include "eval/public/cel_options.h"

// Custom function to compute 3D IOU
cel::Status Compute3DIoU(cel::CelValue* result, const std::vectorcel::CelValue& args) {
// Implement the logic to compute 3D IOU
return cel::Status::OK;
}

// Custom function to compute included angle of two 3D vectors
cel::Status ComputeIncludedAngle(cel::CelValue* result, const std::vectorcel::CelValue& args) {
// Implement the logic to compute included angle
return cel::Status::OK;
}

int main() {
// Create a CEL function registry
auto registry = std::make_sharedcel::Registry();

// Register custom functions with the registry
registry->RegisterFunction("Compute3DIoU", Compute3DIoU);
registry->RegisterFunction("ComputeIncludedAngle", ComputeIncludedAngle);

// Create a CEL evaluator with custom function registry
cel::InterpreterOptions options;
options.mutable_function_registry()->Register(std::move(registry));
cel::Interpreter interpreter(options);

// Example expression with custom functions
std::string expression = "Compute3DIoU(obj1, obj2) > 0.5 && ComputeIncludedAngle(vec1, vec2) < 90";

// Evaluate the expression
cel::Activation activation;
auto status_or = interpreter.Evaluate(expression, activation);
if (status_or.ok()) {
    // Handle result
} else {
    // Handle error
}

return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants