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.
An optional Registry can be configured to resolve external @... references.
When a Registry is set, add_lemma_files will automatically resolve @...
references by fetching source text from the Registry, parsing it, and including
the resulting Lemma docs in the document set before planning.
Implementations§
Source§impl Engine
impl Engine
pub fn new() -> Self
Sourcepub fn with_limits(limits: ResourceLimits) -> Self
pub fn with_limits(limits: ResourceLimits) -> Self
Create an engine with custom resource limits.
Uses the default registry (LemmaBase when the registry feature is enabled).
Sourcepub fn with_registry(self, registry: Arc<dyn Registry>) -> Self
pub fn with_registry(self, registry: Arc<dyn Registry>) -> Self
Configure a Registry for resolving external @... references.
When set, add_lemma_files will resolve @... references automatically
by fetching source text from the Registry before planning.
Sourcepub async fn add_lemma_files(
&mut self,
files: HashMap<String, String>,
) -> Result<(), Vec<LemmaError>>
pub async fn add_lemma_files( &mut self, files: HashMap<String, String>, ) -> Result<(), Vec<LemmaError>>
Add Lemma source files and (when a registry is configured) resolve any @... references.
- Resolves registry references once for all documents
- Validates and resolves types once across all documents
- Collects all errors across all files (parse, registry, planning) instead of aborting on the first
files maps source identifiers (e.g. file paths) to source code.
For a single file, pass a one-entry HashMap.
pub fn remove_document(&mut self, doc_name: &str)
pub fn list_documents(&self) -> Vec<String>
pub fn get_document(&self, doc_name: &str) -> Option<&LemmaDoc>
Sourcepub fn get_execution_plan(&self, doc_name: &str) -> Option<&ExecutionPlan>
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.
pub fn get_document_rules(&self, doc_name: &str) -> Vec<&LemmaRule>
Sourcepub fn evaluate_json(
&self,
doc_name: &str,
rule_names: Vec<String>,
json: &[u8],
) -> LemmaResult<Response>
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.
Sourcepub fn evaluate(
&self,
doc_name: &str,
rule_names: Vec<String>,
fact_values: HashMap<String, String>,
) -> LemmaResult<Response>
pub fn evaluate( &self, doc_name: &str, rule_names: Vec<String>, fact_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).
Fact 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.
Sourcepub fn invert_json(
&self,
doc_name: &str,
rule_name: &str,
target: Target,
json: &[u8],
) -> LemmaResult<InversionResponse>
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.
Sourcepub fn invert(
&self,
doc_name: &str,
rule_name: &str,
target: Target,
values: HashMap<String, String>,
) -> LemmaResult<InversionResponse>
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§
Auto Trait Implementations§
impl Freeze for Engine
impl !RefUnwindSafe for Engine
impl Send for Engine
impl Sync for Engine
impl Unpin for Engine
impl UnsafeUnpin for Engine
impl !UnwindSafe for Engine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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