pub struct Engine { /* private fields */ }Expand description
Engine for evaluating Lemma rules.
Pure Rust implementation that evaluates Lemma specs directly from the AST. Uses pre-built execution plans that are self-contained and ready for evaluation.
The engine never performs network calls. External @... references must be
pre-resolved before calling add_lemma_files — either by including dep files
in the file map or by calling resolve_registry_references separately
(e.g. in a lemma fetch command).
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.
Sourcepub fn hash_pin(
&self,
spec_name: &str,
effective: &DateTimeValue,
) -> Option<&str>
pub fn hash_pin( &self, spec_name: &str, effective: &DateTimeValue, ) -> Option<&str>
Get the content hash (hash pin) for the temporal version active at effective.
Sourcepub fn hash_pin_for_spec(&self, spec: &Arc<LemmaSpec>) -> Option<&str>
pub fn hash_pin_for_spec(&self, spec: &Arc<LemmaSpec>) -> Option<&str>
Get the content hash for a specific spec (by arc). Used when the resolved spec is already known.
Sourcepub fn all_hash_pins(&self) -> Vec<(&str, Option<String>, &str)>
pub fn all_hash_pins(&self) -> Vec<(&str, Option<String>, &str)>
Get all hash pins as (spec_name, effective_from_display, hash) triples.
Sourcepub fn get_spec_by_hash_pin(
&self,
spec_name: &str,
hash_pin: &str,
) -> Option<Arc<LemmaSpec>>
pub fn get_spec_by_hash_pin( &self, spec_name: &str, hash_pin: &str, ) -> Option<Arc<LemmaSpec>>
Get the spec with the given name whose content hash matches hash_pin.
Returns None if no such spec exists or if multiple versions match (hash collision).
Sourcepub fn add_lemma_files(
&mut self,
files: HashMap<String, String>,
) -> Result<(), Vec<Error>>
pub fn add_lemma_files( &mut self, files: HashMap<String, String>, ) -> Result<(), Vec<Error>>
Add Lemma source files, parse them, and build execution plans.
External @... references must already be resolved: include dependency
.lemma files in the files map. The engine never
performs network calls. Use resolve_registry_references separately
(e.g. in lemma fetch) to download dependencies before calling this.
- Validates and resolves types once across all specs
- Collects all errors across all files (parse, 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_spec(&mut self, spec: Arc<LemmaSpec>)
Sourcepub fn list_specs(&self) -> Vec<Arc<LemmaSpec>>
pub fn list_specs(&self) -> Vec<Arc<LemmaSpec>>
All specs, all temporal versions, ordered by (name, effective_from).
Sourcepub fn list_specs_effective(
&self,
effective: &DateTimeValue,
) -> Vec<Arc<LemmaSpec>>
pub fn list_specs_effective( &self, effective: &DateTimeValue, ) -> Vec<Arc<LemmaSpec>>
Specs active at effective (one per name).
Sourcepub fn get_spec(
&self,
spec_name: &str,
effective: &DateTimeValue,
) -> Option<Arc<LemmaSpec>>
pub fn get_spec( &self, spec_name: &str, effective: &DateTimeValue, ) -> Option<Arc<LemmaSpec>>
Get spec by name at a specific time.
Sourcepub fn get_execution_plan(
&self,
spec_name: &str,
hash_pin: Option<&str>,
effective: &DateTimeValue,
) -> Option<&ExecutionPlan>
pub fn get_execution_plan( &self, spec_name: &str, hash_pin: Option<&str>, effective: &DateTimeValue, ) -> Option<&ExecutionPlan>
Get the execution plan for a spec.
When hash_pin is Some, resolves the spec by content hash for that name,
then returns the slice plan that covers effective. When hash_pin is None,
resolves the temporal version active at effective then finds the covering slice plan.
Returns None when the spec does not exist or has no matching plan.
pub fn get_spec_rules( &self, spec_name: &str, effective: &DateTimeValue, ) -> Result<Vec<LemmaRule>, Error>
Sourcepub fn evaluate_json(
&self,
spec_name: &str,
hash_pin: Option<&str>,
effective: &DateTimeValue,
rule_names: Vec<String>,
json: &[u8],
) -> Result<Response, Error>
pub fn evaluate_json( &self, spec_name: &str, hash_pin: Option<&str>, effective: &DateTimeValue, rule_names: Vec<String>, json: &[u8], ) -> Result<Response, Error>
Evaluate rules in a spec with JSON values for facts.
This is a convenience method that accepts JSON directly and converts it to typed values using the spec’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 spec schema.
When hash_pin is Some, the spec is resolved by that content hash; otherwise
by temporal resolution at effective. Evaluation uses the resolved plan.
Sourcepub fn evaluate(
&self,
spec_name: &str,
hash_pin: Option<&str>,
effective: &DateTimeValue,
rule_names: Vec<String>,
fact_values: HashMap<String, String>,
) -> Result<Response, Error>
pub fn evaluate( &self, spec_name: &str, hash_pin: Option<&str>, effective: &DateTimeValue, rule_names: Vec<String>, fact_values: HashMap<String, String>, ) -> Result<Response, Error>
Evaluate rules in a spec 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 spec’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 spec schema.
When hash_pin is Some, the spec is resolved by that content hash; otherwise
by temporal resolution at effective. Evaluation uses the resolved plan.
Sourcepub fn invert(
&self,
spec_name: &str,
effective: &DateTimeValue,
rule_name: &str,
target: Target,
values: HashMap<String, String>,
) -> Result<InversionResponse, Error>
pub fn invert( &self, spec_name: &str, effective: &DateTimeValue, rule_name: &str, target: Target, values: HashMap<String, String>, ) -> Result<InversionResponse, Error>
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 spec 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