Skip to main content

Engine

Struct Engine 

Source
pub struct Engine { /* private fields */ }
Expand description

Engine for evaluating Lemma rules

Pure Rust implementation that evaluates Lemma docs directly from the AST. Uses pre-built execution plans that are self-contained and ready for evaluation.

Implementations§

Source§

impl Engine

Source

pub fn new() -> Self

Source

pub fn with_limits(limits: ResourceLimits) -> Self

Create an engine with custom resource limits

Source

pub fn add_lemma_code( &mut self, lemma_code: &str, source: &str, ) -> LemmaResult<()>

Source

pub fn remove_document(&mut self, doc_name: &str)

Source

pub fn list_documents(&self) -> Vec<String>

Source

pub fn get_document(&self, doc_name: &str) -> Option<&LemmaDoc>

Source

pub fn get_execution_plan(&self, doc_name: &str) -> Option<&ExecutionPlan>

Get the execution plan for a document.

The execution plan contains the resolved fact schema, default values, and topologically sorted rules ready for evaluation.

Source

pub fn get_document_rules(&self, doc_name: &str) -> Vec<&LemmaRule>

Source

pub fn get_facts( &self, doc_name: &str, rule_names: &[String], ) -> LemmaResult<HashMap<FactPath, LemmaType>>

Get the facts (with types) required to evaluate a document’s rules.

  • If rule_names is empty, returns facts for all local rules in the document.
  • Otherwise, returns facts for the specified rules (by name).

Returns a map from FactPath to resolved LemmaType. This is the authoritative API for determining what inputs a rule needs.

Source

pub fn evaluate_json( &self, doc_name: &str, rule_names: Vec<String>, json: &[u8], ) -> LemmaResult<Response>

Evaluate rules in a document with JSON values for facts.

This is a convenience method that accepts JSON directly and converts it to typed values using the document’s fact type declarations.

If rule_names is empty, evaluates all rules. Otherwise, only returns results for the specified rules (dependencies still computed).

Values are provided as JSON bytes (e.g., b"{\"quantity\": 5, \"is_member\": true}"). They are automatically parsed to the expected type based on the document schema.

Source

pub fn evaluate( &self, doc_name: &str, rule_names: Vec<String>, values: HashMap<String, String>, ) -> LemmaResult<Response>

Evaluate rules in a document with string values for facts.

This is the user-friendly API that accepts raw string values and parses them to the appropriate types based on the document’s fact type declarations. Use this for CLI, HTTP APIs, and other user-facing interfaces.

If rule_names is empty, evaluates all rules. Otherwise, only returns results for the specified rules (dependencies still computed).

Values are provided as name -> value string pairs (e.g., “type” -> “latte”). They are automatically parsed to the expected type based on the document schema.

Source

pub fn invert_json( &self, doc_name: &str, rule_name: &str, target: Target, json: &[u8], ) -> LemmaResult<InversionResponse>

Invert a rule to find input domains that produce a desired outcome with JSON values.

Values are provided as JSON bytes (e.g., b"{\"quantity\": 5, \"is_member\": true}"). They are automatically parsed to the expected type based on the document schema.

Source

pub fn invert( &self, doc_name: &str, rule_name: &str, target: Target, values: HashMap<String, String>, ) -> LemmaResult<InversionResponse>

Invert a rule to find input domains that produce a desired outcome.

Values are provided as name -> value string pairs (e.g., “quantity” -> “5”). They are automatically parsed to the expected type based on the document schema.

Trait Implementations§

Source§

impl Default for Engine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Engine

§

impl RefUnwindSafe for Engine

§

impl Send for Engine

§

impl Sync for Engine

§

impl Unpin for Engine

§

impl UnwindSafe for Engine

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.