rego-cpp 1.2.0
A C++ implementation of the Rego language and runtime
Loading...
Searching...
No Matches
rego::Output Class Reference

Encapsulates the output of a Rego query. More...

#include <rego.hh>

Public Member Functions

 Output (Node node)
 Constructor.
 
bool ok () const
 Whether the Output is OK (i.e., has not resulted in one or more errors)
 
Node node () const
 Gets the underlying node.
 
size_t size () const
 Gets the number of results.
 
Node expressions_at (size_t index) const
 Gets the expressions for the result at the specified index.
 
Node expressions () const
 Gets the expressions for the first result.
 
Node binding_at (size_t index, const std::string &name) const
 Gets the binding for the specified name at the specified result index.
 
Node binding (const std::string &name) const
 Gets the binding for the specified name at the first result.
 
const std::string & json () const
 Gets the JSON representation of the output.
 
std::vector< std::string > errors () const
 Gets the errors in the output.
 

Detailed Description

Encapsulates the output of a Rego query.

The Output class encapsulates the output of a Rego query. A Rego query result can be either undefined (no results), one or more errors, or one or more results. The fact that a query can result in one or more results may seem surprising. The query a = [1, 2, 3, 4]; x = a[_]; x % 2 / == 0; x / 2 results in two distinct solutions: json {"expressions":[true, true, true, 1], "bindings":{"a":[1,2,3,4], "x":2}} {"expressions":[true, true, true, 2], "bindings":{"a":[1,2,3,4], "x":4}} Each result consists of a list of expressions (one for each statement in the query) and a lookup table of bindings, with assigned values for each named local. This class provides methods for accessing these value. Using the output above: cpp rego::Node expressions0 = output.expressions(); rego::Node expressions1 = output.expressions_at(1); rego::Node last0 = expressions0->back(); std::cout << rego::to_key(last0); // 1 rego::Node last1 = expressions1->back(); std::cout << rego::to_key(last1); // 2 Node a = output.binding("a"); std::cout << rego::to_key(a); // [1,2,3,4] auto x0 = rego::get_int(output.binding("x")); std::cout << x0; // 2 auto x1 = rego::get_int(output.binding_at(1, "x")); std::cout << x1; // 4

Constructor & Destructor Documentation

◆ Output()

rego::Output::Output ( Node node)

Constructor.

Parameters
nodeA Results or ErrorSeq node.

Member Function Documentation

◆ binding()

Node rego::Output::binding ( const std::string & name) const

Gets the binding for the specified name at the first result.

Parameters
nameThe name of the binding.
Returns
The binding for the specified name.

◆ binding_at()

Node rego::Output::binding_at ( size_t index,
const std::string & name ) const

Gets the binding for the specified name at the specified result index.

Parameters
indexThe index of the result.
nameThe name of the binding.
Returns
The binding for the specified name.

◆ errors()

std::vector< std::string > rego::Output::errors ( ) const

Gets the errors in the output.

If the output is not OK, this method can be used to retrieve the error messages.

Returns
The errors in the output.

◆ expressions()

Node rego::Output::expressions ( ) const

Gets the expressions for the first result.

Returns
The expressions for the first result.

◆ expressions_at()

Node rego::Output::expressions_at ( size_t index) const

Gets the expressions for the result at the specified index.

Parameters
indexThe index of the result.
Returns
The expressions for the result.

◆ json()

const std::string & rego::Output::json ( ) const

Gets the JSON representation of the output.

Returns
The JSON representation of the output.

◆ node()

Node rego::Output::node ( ) const

Gets the underlying node.

Returns
The underlying node.

◆ ok()

bool rego::Output::ok ( ) const

Whether the Output is OK (i.e., has not resulted in one or more errors)

Returns
True if the Output is OK, otherwise false.

◆ size()

size_t rego::Output::size ( ) const

Gets the number of results.

There will be one result per solution to the query.

Returns
The number of results.

The documentation for this class was generated from the following file: