Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN apt-get update && apt-get install -y \
# Copy manifests
COPY Cargo.toml Cargo.lock ./
COPY crates ./crates
COPY examples ./examples

# Build release with NATS sync support
RUN --mount=type=cache,id=ordo-cargo-registry,target=/usr/local/cargo/registry,sharing=locked \
Expand Down
112 changes: 108 additions & 4 deletions crates/ordo-core/src/rule/compiled_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ impl CompiledRuleExecutor {
let mut child_data = std::collections::HashMap::with_capacity(bindings.len());
for binding in bindings {
let name = ruleset.get_string(binding.name)?;
child_data.insert(
name.to_string(),
self.evaluate_expr(ruleset, binding.expr, parent_ctx)?,
);
if let Some(value) =
self.evaluate_sub_rule_binding(ruleset, binding.expr, parent_ctx)?
{
child_data.insert(name.to_string(), value);
}
}

self.execute_sub_graph(
Expand All @@ -303,6 +304,23 @@ impl CompiledRuleExecutor {
)
}

fn evaluate_sub_rule_binding(
&self,
ruleset: &CompiledRuleSet,
expr_idx: u32,
ctx: &Context,
) -> Result<Option<Value>> {
match self.evaluate_expr(ruleset, expr_idx, ctx) {
Ok(value) => Ok(Some(value)),
Err(OrdoError::FieldNotFound { .. })
if ruleset.metadata.field_missing == FIELD_MISSING_LENIENT =>
{
Ok(None)
}
Err(error) => Err(error),
}
}

fn copy_sub_rule_outputs(
&self,
ruleset: &CompiledRuleSet,
Expand Down Expand Up @@ -891,6 +909,92 @@ mod tests {
assert_eq!(result.output.get_path("tier"), Some(&Value::string("gold")));
}

#[test]
fn compiled_sub_rule_bindings_follow_lenient_missing_field_behavior() {
let mut sub_steps = hashbrown::HashMap::new();
sub_steps.insert(
"check_score".to_string(),
Step::decision("check_score", "Check Score")
.branch(
crate::rule::Condition::from_string("score >= 90"),
"tier_gold",
)
.default("tier_silver")
.build(),
);
sub_steps.insert(
"tier_gold".to_string(),
Step::action(
"tier_gold",
"Gold",
vec![Action {
kind: ActionKind::SetVariable {
name: "tier".to_string(),
value: Expr::literal("gold"),
},
description: String::new(),
}],
"done",
),
);
sub_steps.insert(
"tier_silver".to_string(),
Step::action(
"tier_silver",
"Silver",
vec![Action {
kind: ActionKind::SetVariable {
name: "tier".to_string(),
value: Expr::literal("silver"),
},
description: String::new(),
}],
"done",
),
);
sub_steps.insert(
"done".to_string(),
Step::terminal("done", "Done", TerminalResult::new("OK")),
);

let mut ruleset = RuleSet::new("compiled_sub_rule_lenient", "start");
ruleset.add_sub_rule(
"classify",
SubRuleGraph {
entry_step: "check_score".to_string(),
steps: sub_steps,
},
);
ruleset.add_step(Step {
id: "start".to_string(),
name: "Start".to_string(),
kind: StepKind::SubRule {
ref_name: "classify".to_string(),
bindings: vec![("score".to_string(), Expr::field("score"))],
outputs: vec![("result_tier".to_string(), "tier".to_string())],
next_step: "done".to_string(),
},
});
ruleset.add_step(Step::terminal(
"done",
"Done",
TerminalResult::new("DONE").with_output("tier", Expr::field("$result_tier")),
));

ruleset.validate().unwrap();
let compiled = RuleSetCompiler::compile(&ruleset).unwrap();
let executor = CompiledRuleExecutor::new();

let input = serde_json::from_str(r#"{}"#).unwrap();
let result = executor.execute(&compiled, input).unwrap();

assert_eq!(result.code, "DONE");
assert_eq!(
result.output.get_path("tier"),
Some(&Value::string("silver"))
);
}

#[test]
fn compiled_executor_preserves_object_payloads_for_external_calls() {
let mut ruleset = RuleSet::new("compiled_external_payload_test", "invoke");
Expand Down
Loading
Loading