mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 16:02:18 +00:00
feat: custom node (#138)
* feat: custom node * add zen template, expose $nodes and $root * add custom handler to go and nodejs * add support for python, improve bindings, add error to zen template * fix: correct binding exports for nodejs and python * fix benchmark * improve rust api, trim template in zen templates * update cargo action * update expression version * compile action * fix action format
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::custom_node::PyCustomNode;
|
||||
use crate::decision::PyZenDecision;
|
||||
use crate::loader::PyDecisionLoader;
|
||||
use crate::value::PyValue;
|
||||
@@ -13,7 +14,7 @@ use zen_engine::{DecisionEngine, EvaluationOptions};
|
||||
#[pyclass]
|
||||
#[pyo3(name = "ZenEngine")]
|
||||
pub struct PyZenEngine {
|
||||
graph: Arc<DecisionEngine<PyDecisionLoader>>,
|
||||
graph: Arc<DecisionEngine<PyDecisionLoader, PyCustomNode>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -34,7 +35,11 @@ impl Default for PyZenEvaluateOptions {
|
||||
impl Default for PyZenEngine {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
graph: DecisionEngine::new(PyDecisionLoader::default()).into(),
|
||||
graph: DecisionEngine::new(
|
||||
Arc::new(PyDecisionLoader::default()),
|
||||
Arc::new(PyCustomNode::default()),
|
||||
)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,13 +52,22 @@ impl PyZenEngine {
|
||||
return Ok(Default::default());
|
||||
};
|
||||
|
||||
let Some(loader_any) = options.get_item("loader")? else {
|
||||
return Ok(Default::default());
|
||||
let loader = match options.get_item("loader")? {
|
||||
Some(loader) => Some(Python::with_gil(|py| loader.to_object(py))),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let custom_node = match options.get_item("customHandler")? {
|
||||
Some(custom_node) => Some(Python::with_gil(|py| custom_node.to_object(py))),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let loader = Python::with_gil(|py| loader_any.to_object(py));
|
||||
Ok(Self {
|
||||
graph: DecisionEngine::new(PyDecisionLoader::from(loader)).into(),
|
||||
graph: DecisionEngine::new(
|
||||
Arc::new(PyDecisionLoader::from(loader)),
|
||||
Arc::new(PyCustomNode::from(custom_node)),
|
||||
)
|
||||
.into(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user