1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::decision::Decision;
use crate::loader::{ClosureLoader, DecisionLoader, LoaderResponse, LoaderResult, NoopLoader};
use crate::model::DecisionContent;

use serde_json::Value;
use std::future::Future;

use crate::handler::graph::DecisionGraphResponse;
use crate::EvaluationError;
use std::sync::Arc;

/// Structure used for generating and evaluating JDM decisions
#[derive(Debug, Clone)]
pub struct DecisionEngine<L>
where
    L: DecisionLoader,
{
    loader: Arc<L>,
}

#[derive(Debug, Default)]
pub struct EvaluationOptions {
    pub trace: Option<bool>,
    pub max_depth: Option<u8>,
}

impl Default for DecisionEngine<NoopLoader> {
    fn default() -> Self {
        Self {
            loader: Arc::new(NoopLoader::default()),
        }
    }
}

impl<F, O> DecisionEngine<ClosureLoader<F>>
where
    F: Fn(&str) -> O + Sync + Send,
    O: Future<Output = LoaderResponse> + Send,
{
    pub fn async_loader(loader: F) -> Self {
        Self {
            loader: Arc::new(ClosureLoader::new(loader)),
        }
    }
}

impl<L: DecisionLoader> DecisionEngine<L> {
    pub fn new<Loader>(loader: Loader) -> Self
    where
        Loader: Into<Arc<L>>,
    {
        Self {
            loader: loader.into(),
        }
    }

    pub fn new_arc(loader: Arc<L>) -> Self {
        Self { loader }
    }

    /// Evaluates a decision through loader using a key
    pub async fn evaluate<K>(
        &self,
        key: K,
        context: &Value,
    ) -> Result<DecisionGraphResponse, Box<EvaluationError>>
    where
        K: AsRef<str>,
    {
        self.evaluate_with_opts(key, context, Default::default())
            .await
    }

    /// Evaluates a decision through loader using a key with advanced options
    pub async fn evaluate_with_opts<K>(
        &self,
        key: K,
        context: &Value,
        options: EvaluationOptions,
    ) -> Result<DecisionGraphResponse, Box<EvaluationError>>
    where
        K: AsRef<str>,
    {
        let content = self.loader.load(key.as_ref()).await?;
        let decision = self.create_decision(content);
        decision.evaluate_with_opts(context, options).await
    }

    /// Creates a decision from DecisionContent, exists for easier binding creation
    pub fn create_decision(&self, content: Arc<DecisionContent>) -> Decision<L> {
        Decision::from(content).with_loader(self.loader.clone())
    }

    /// Retrieves a decision based on the loader
    pub async fn get_decision(&self, key: &str) -> LoaderResult<Decision<L>> {
        let content = self.loader.load(key).await?;
        Ok(self.create_decision(content))
    }
}