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:
stefan-gorules
2024-04-03 16:09:51 +02:00
committed by GitHub
parent 685a0345f5
commit daecf901e6
65 changed files with 1729 additions and 235 deletions
+20 -6
View File
@@ -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(),
})
}