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
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
pub fn add_lemma_code( &mut self, lemma_code: &str, source: &str, ) -> LemmaResult<()>
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>
pub fn get_document_facts(&self, doc_name: &str) -> Vec<&LemmaFact>
pub fn get_document_rules(&self, doc_name: &str) -> Vec<&LemmaRule>
Sourcepub fn get_fact_schema_type(
&self,
doc_name: &str,
fact_name: &str,
) -> Option<LemmaType>
pub fn get_fact_schema_type( &self, doc_name: &str, fact_name: &str, ) -> Option<LemmaType>
Get the resolved schema type for a fact in a document (including imported/custom types).
This uses the document’s compiled execution plan, so it reflects the authoritative schema after type resolution and overrides.
Sourcepub fn get_required_fact_names(
&self,
doc_name: &str,
rule_names: &[String],
) -> Option<HashSet<String>>
pub fn get_required_fact_names( &self, doc_name: &str, rule_names: &[String], ) -> Option<HashSet<String>>
Get the set of fact names required to evaluate a document’s rules.
- If
rule_namesis empty, returns required facts for all rules in the document. - Otherwise, returns required facts for the specified local rules (by name).
Returned names match FactReference::to_string() / FactPath::to_string() (e.g. “age”,
“order.price”, etc.), so they can be used by UIs to decide what to prompt for.
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>,
values: HashMap<String, String>,
) -> LemmaResult<Response>
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.
Sourcepub fn evaluate_strict(
&self,
doc_name: &str,
rule_names: Vec<String>,
values: HashMap<String, LiteralValue>,
) -> LemmaResult<Response>
pub fn evaluate_strict( &self, doc_name: &str, rule_names: Vec<String>, values: HashMap<String, LiteralValue>, ) -> LemmaResult<Response>
Evaluate rules in a document with typed values for facts.
This is the strict API that accepts pre-typed LiteralValue values. Use this for programmatic APIs, protobuf, msgpack, FFI, and other strongly-typed interfaces where values are already parsed.
If rule_names is empty, evaluates all rules.
Otherwise, only returns results for the specified rules (dependencies still computed).
Values are provided as name -> LiteralValue pairs (e.g., “age” -> Number(25)).
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.
This is a convenience method that accepts JSON directly and converts it to typed values using the document’s fact type declarations.
Returns an InversionResponse containing:
solutions: Concrete domain constraints for each free variableundetermined_facts: Facts that are not fully determinedis_determined: Whether all facts have concrete 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.
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.
Returns an InversionResponse containing:
solutions: Concrete domain constraints for each free variableundetermined_facts: Facts that are not fully determinedis_determined: Whether all facts have concrete values
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.
Sourcepub fn invert_strict(
&self,
doc_name: &str,
rule_name: &str,
target: Target,
values: HashMap<String, LiteralValue>,
) -> LemmaResult<InversionResponse>
pub fn invert_strict( &self, doc_name: &str, rule_name: &str, target: Target, values: HashMap<String, LiteralValue>, ) -> LemmaResult<InversionResponse>
Invert a rule to find input domains that produce a desired outcome.
This is the strict API that accepts pre-typed LiteralValue values. Use this for programmatic APIs, protobuf, msgpack, FFI, and other strongly-typed interfaces where values are already parsed.
Returns an InversionResponse containing:
solutions: Concrete domain constraints for each free variableundetermined_facts: Facts that are not fully determinedis_determined: Whether all facts have concrete values
Values are provided as name -> LiteralValue pairs (e.g., “quantity” -> Number(5)).
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 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> 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