![]() |
rego-cpp 1.0.0
A C++ implementation of the Rego language and runtime
|
This class forms the main interface to the Rego library. More...
#include <rego.hh>
Public Member Functions | |
| Interpreter () | |
| Constructor. | |
| Node | add_module_file (const std::filesystem::path &path) |
| Adds a module (i.e. virtual document) file to the interpreter. | |
| Node | add_module (const std::string &name, const std::string &contents) |
| Adds a module (i.e. virtual document) to the interpreter. | |
| Node | add_data_json_file (const std::filesystem::path &path) |
| Adds a base document to the interpreter. | |
| Node | add_data_json (const std::string &json) |
| Adds a base document to the interpreter. | |
| Node | add_data (const Node &node) |
| Adds a base document to the interpreter. | |
| Node | set_input_json_file (const std::filesystem::path &path) |
| Sets the input document to the interpreter. | |
| Node | set_input_json (const std::string &json) |
| Sets the input document to the interpreter. | |
| Node | set_input_term (const std::string &term) |
| Sets the input term of the interpreter. | |
| Node | set_input (const Node &node) |
| Sets the input document to the interpreter. | |
| Node | set_query (const std::string &query) |
| Sets the query expression of the interpreter. | |
| Interpreter & | entrypoints (const std::initializer_list< std::string > &entrypoints) |
| Sets the entrypoints to include when building a bundle. | |
| Interpreter & | entrypoints (const std::vector< std::string > &entrypoints) |
| Sets the entrypoints to include when building a bundle. | |
| const std::vector< std::string > & | entrypoints () const |
| Gets the entrypoints to include when building a bundle. | |
| std::vector< std::string > & | entrypoints () |
| Gets the entrypoints to include when building a bundle. | |
| std::string | query () |
| Executes the documents against the interpreter. | |
| std::string | query (const std::string &query_expr) |
| Executes a query against the interpreter. | |
| Node | query_node (const std::string &query_expr) |
| Executes a query against the interpreter. | |
| Node | query_node () |
| Executes a query against the interpreter. | |
| Node | build () |
| Builds a bundle from the current state of the interpreter. | |
| Node | save_bundle (const std::filesystem::path &dir, const Node &bundle) |
| Saves a bundle to a directory in JSON format. | |
| Node | load_bundle (const std::filesystem::path &dir) |
| Loads a bundle in JSON format from a direction. | |
| Node | query_bundle (const Bundle &bundle) |
| Performs a query against a bundle. | |
| Node | query_bundle (const Bundle &bundle, const std::string &endpoint) |
| Performs a query against a bundle. | |
| Interpreter & | debug_path (const std::filesystem::path &prefix) |
| The path to the debug directory. | |
| const std::filesystem::path & | debug_path () const |
| Gets the debug path. | |
| Interpreter & | debug_enabled (bool enabled) |
| Sets whether debug mode is enabled. | |
| bool | debug_enabled () const |
| Checks if debug mode is enabled. | |
| Interpreter & | wf_check_enabled (bool enabled) |
| Sets whether well-formedness checks are enabled. | |
| bool | wf_check_enabled () const |
| Checks if well-formedness checks are enabled. | |
| BuiltIns | builtins () const |
| The built-ins used by the interpreter. | |
| std::string | output_to_string (const Node &output) const |
| Converts an output node into a human-readable string. | |
| LogLevel | log_level () const |
| Returns the current log level of this interpreter. | |
| Interpreter & | log_level (LogLevel level) |
| Sets the log level of the interpreter. | |
| Interpreter & | log_level (const std::string &level) |
| Sets the logging level for the interpreter from a string. | |
| const std::string & | c_error () const |
| Returns a string representing the most recent error message. | |
| Interpreter & | c_error (const std::string &error) |
| Sets the most recent error message. | |
This class forms the main interface to the Rego library.
You can use it to assemble and then execute queries, for example:
| Node rego::Interpreter::add_data | ( | const Node & | node | ) |
Adds a base document to the interpreter.
Adds a JSON AST node to the interpreter's data sequence.
| node | The contents of the document. |
| Node rego::Interpreter::add_data_json | ( | const std::string & | json | ) |
Adds a base document to the interpreter.
The document must contain a single JSON-encoded object, and will be parsed and added to the interpreter's data sequence.
| json | The contents of the document. |
| Node rego::Interpreter::add_data_json_file | ( | const std::filesystem::path & | path | ) |
Adds a base document to the interpreter.
This is the same as calling Interpreter::add_data_json with the contents of the file.
| path | The path to the data JSON file. |
| Node rego::Interpreter::add_module | ( | const std::string & | name, |
| const std::string & | contents ) |
Adds a module (i.e. virtual document) to the interpreter.
The module will be parsed and added to the interpreter's module sequence.
| name | The name of the module. |
| contents | The contents of the module. |
| Node rego::Interpreter::add_module_file | ( | const std::filesystem::path & | path | ) |
Adds a module (i.e. virtual document) file to the interpreter.
This is the same as calling Interpreter::add_module with the contents of the file.
| path | The path to the module file. |
| Node rego::Interpreter::build | ( | ) |
Builds a bundle from the current state of the interpreter.
The bundle will include all base and virtual documents, and will include execution plans for a query (if set) and zero or more entrypoints (if set). If no query or entrypoints are set, or if there is an error during bundle creation, an error node will be returned. The resulting bundle node can be used as an input to Bundle::from_node.
| BuiltIns rego::Interpreter::builtins | ( | ) | const |
The built-ins used by the interpreter.
This object can be used to register custom built-ins created using BuiltInDef::create.
| const std::string & rego::Interpreter::c_error | ( | ) | const |
Returns a string representing the most recent error message.
| Interpreter & rego::Interpreter::c_error | ( | const std::string & | error | ) |
Sets the most recent error message.
| error | the error message |
| bool rego::Interpreter::debug_enabled | ( | ) | const |
Checks if debug mode is enabled.
| Interpreter & rego::Interpreter::debug_enabled | ( | bool | enabled | ) |
Sets whether debug mode is enabled.
If true, then the interpreter will output intermediary ASTs after each compiler pass to the debug directory set via Interpreter::debug_path.
| enabled | whether debug is enabled |
| const std::filesystem::path & rego::Interpreter::debug_path | ( | ) | const |
Gets the debug path.
| Interpreter & rego::Interpreter::debug_path | ( | const std::filesystem::path & | prefix | ) |
The path to the debug directory.
If set, then (when in debug mode) the interpreter will output intermediary ASTs after each compiler pass to the debug directory. If the directory does not exist, it will be created.
| std::vector< std::string > & rego::Interpreter::entrypoints | ( | ) |
Gets the entrypoints to include when building a bundle.
| const std::vector< std::string > & rego::Interpreter::entrypoints | ( | ) | const |
Gets the entrypoints to include when building a bundle.
| Interpreter & rego::Interpreter::entrypoints | ( | const std::initializer_list< std::string > & | entrypoints | ) |
Sets the entrypoints to include when building a bundle.
| entrypoints | The entrypoints. |
| Interpreter & rego::Interpreter::entrypoints | ( | const std::vector< std::string > & | entrypoints | ) |
Sets the entrypoints to include when building a bundle.
| entrypoints | The entrypoints. |
| Node rego::Interpreter::load_bundle | ( | const std::filesystem::path & | dir | ) |
Loads a bundle in JSON format from a direction.
The directory is expected to contain a plan.json file, a data.json file, and zero or more .rego files. If there is an error when loading, an error node will be returned.
| dir | The direction to use when loading the bundle |
| LogLevel rego::Interpreter::log_level | ( | ) | const |
Returns the current log level of this interpreter.
| Interpreter & rego::Interpreter::log_level | ( | const std::string & | level | ) |
Sets the logging level for the interpreter from a string.
| level | The logging level. One of "None", "Error", "Output", "Warn", "Info", "Debug", or "Trace". |
| Interpreter & rego::Interpreter::log_level | ( | LogLevel | level | ) |
Sets the log level of the interpreter.
| level | the log level |
| std::string rego::Interpreter::output_to_string | ( | const Node & | output | ) | const |
Converts an output node into a human-readable string.
| output | An output node returned from Interpreter::query_node or Interpreter::query_bundle. |
| std::string rego::Interpreter::query | ( | ) |
Executes the documents against the interpreter.
This method calls Interpreter::query_node and then converts it into a human-readable string using Interpeter::output_to_string.
| std::string rego::Interpreter::query | ( | const std::string & | query_expr | ) |
Executes a query against the interpreter.
This method calls Interpreter::query_node and then converts it into a human-readable string using Interpreter::output_to_string.
| query_expr | The query expression. |
| Node rego::Interpreter::query_bundle | ( | const Bundle & | bundle | ) |
Performs a query against a bundle.
The interpreter will load this bundle into its virtual machine and then execute the stored query plan. If no query was compiled into the bundle, this will result in an error. Otherwise, the result will be an AST node representing the result, which will either be a list of bindings and terms, or an error sequence.
| bundle | The bundle to query |
| Node rego::Interpreter::query_bundle | ( | const Bundle & | bundle, |
| const std::string & | endpoint ) |
Performs a query against a bundle.
The interpreter will load this bundle into its virtual machine and then execute the stored entrypoint plan. If this entrypoint was not compiled into the bundle, this will result in an error. Otherwise, the result will be an AST node representing the result, which will either be a list of bindings and terms, or an error sequence.
| bundle | The bundle to query |
| endpoint | The entrypoint to execute |
| Node rego::Interpreter::query_node | ( | ) |
Executes a query against the interpreter.
The query executed will be the one provided to Interpreter::set_query. The result will be an AST node representing the result, which will either be a list of bindings and terms, or an error sequence.
| Node rego::Interpreter::query_node | ( | const std::string & | query_expr | ) |
Executes a query against the interpreter.
The query expression must be a valid Rego query expression. The result will be an AST node representing the result, which will either be a list of bindings and terms, or an error sequence.
| query_expr | The query expression. |
| Node rego::Interpreter::save_bundle | ( | const std::filesystem::path & | dir, |
| const Node & | bundle ) |
Saves a bundle to a directory in JSON format.
The bundle will be saved in a format compatible with OPA. The directory will be created if it does not exist. There will be a plan.json file, a data.json file, and zero or more .rego files (from the modules used during compilation). If there is an error during saving, an error node will be returned.
| dir | The directory to save the bundle to. |
| bundle | The bundle to save (as produced by Interpreter::build). |
| Node rego::Interpreter::set_input | ( | const Node & | node | ) |
Sets the input document to the interpreter.
Sets an AST node directly as the interpreter's input. Use with caution. The node can either be a JSON AST (which will be converted) or a Rego AST.
| node | The contents of the document. |
| Node rego::Interpreter::set_input_json | ( | const std::string & | json | ) |
Sets the input document to the interpreter.
The document must contain a single JSON-encoded object, and will be parsed and set as the interpreter's input.
| json | The contents of the document. |
| Node rego::Interpreter::set_input_json_file | ( | const std::filesystem::path & | path | ) |
Sets the input document to the interpreter.
This is the same as calling Interpreter::set_input_json with the contents of the file.
| path | The path to the input file. |
| Node rego::Interpreter::set_input_term | ( | const std::string & | term | ) |
Sets the input term of the interpreter.
The string must contain a single valid Rego data term.
| term | The contents of the term. |
| Node rego::Interpreter::set_query | ( | const std::string & | query | ) |
Sets the query expression of the interpreter.
This query will be included when building a bundle.
| query | The query expression. |
| bool rego::Interpreter::wf_check_enabled | ( | ) | const |
Checks if well-formedness checks are enabled.
| Interpreter & rego::Interpreter::wf_check_enabled | ( | bool | enabled | ) |
Sets whether well-formedness checks are enabled.
If true, then the interpreter will perform well-formedness checks after each compiler pass using the well-formedness definitions.
| enabled | Whether well-formedness checks are enabled |