capability_grower_configuration/
depth_based_node_variant_policy.rs

1// ---------------- [ File: capability-grower-configuration/src/depth_based_node_variant_policy.rs ]
2crate::ix!();
3
4/// This picks which variant to use based on current depth.  
5/// - levels < aggregator_start_level => dispatch
6/// - aggregator_start_level..(leaf_start_level) => aggregator
7/// - leaf_start_level.. => leaf
8#[derive(
9    SaveLoad,Debug, Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate,
10    AiJsonTemplateWithJustification,
11    Builder, Getters
12)]
13#[builder(pattern="owned", setter(into))]
14#[getset(get="pub")]
15pub struct DepthBasedNodeVariantPolicy {
16
17    /// This field specifies the level of our tree at which aggregator nodes *start*
18    #[serde(deserialize_with = "fuzzy_u8")]
19    aggregator_start_level: u8,
20
21    /// This field specifies the level of our tree at which leaf nodes *start*
22    #[serde(deserialize_with = "fuzzy_u8")]
23    leaf_start_level: u8,
24}
25
26impl Default for DepthBasedNodeVariantPolicy {
27    fn default() -> Self {
28        DepthBasedNodeVariantPolicy {
29            aggregator_start_level: 2,
30            leaf_start_level: 4,
31        }
32    }
33}
34
35impl FuzzyFromJsonValue for JustifiedDepthBasedNodeVariantPolicy {
36    fn fuzzy_from_json_value(value: &JsonValue)
37        -> Result<Self, FuzzyFromJsonValueError>
38    {
39        trace!("(JustifiedDepthBasedNodeVariantPolicy) Entering fuzzy_from_json_value");
40
41        // 1) If null => default
42        if value.is_null() {
43            debug!("(JustifiedDepthBasedNodeVariantPolicy) Null => aggregator_start_level=0, leaf_start_level=0");
44            return Ok(Self {
45                aggregator_start_level: 0,
46                aggregator_start_level_confidence: 0.0,
47                aggregator_start_level_justification: String::new(),
48                leaf_start_level: 0,
49                leaf_start_level_confidence: 0.0,
50                leaf_start_level_justification: String::new(),
51            });
52        }
53
54        // 2) Must be object => parse
55        let mut obj = match value.as_object() {
56            Some(m) => {
57                trace!("(JustifiedDepthBasedNodeVariantPolicy) Found object => flattening if needed.");
58                m.clone()
59            }
60            None => {
61                error!("(JustifiedDepthBasedNodeVariantPolicy) Not an object => fail!");
62                return Err(FuzzyFromJsonValueError::NotAnObject {
63                    target_type: "JustifiedDepthBasedNodeVariantPolicy",
64                    actual: value.clone(),
65                });
66            }
67        };
68
69        flatten_all_fields(&mut obj);
70
71        // aggregator_start_level => fuzzy_u8
72        trace!("(JustifiedDepthBasedNodeVariantPolicy) aggregator_start_level => fuzzy_u8");
73        let agg_val = match obj.get("aggregator_start_level") {
74            Some(x) => {
75                match fuzzy_u8(x) {
76                    Ok(u) => {
77                        trace!("(JustifiedDepthBasedNodeVariantPolicy) aggregator_start_level => {}", u);
78                        u
79                    }
80                    Err(e) => {
81                        error!("(JustifiedDepthBasedNodeVariantPolicy) aggregator_start_level parse error => {:?}", e);
82                        return Err(FuzzyFromJsonValueError::Other {
83                            target_type: "JustifiedDepthBasedNodeVariantPolicy",
84                            detail: format!("Error aggregator_start_level: {:?}", e),
85                        });
86                    }
87                }
88            }
89            None => 0,
90        };
91        let agg_conf = get_f64_field(&obj, "aggregator_start_level_confidence", "JustifiedDepthBasedNodeVariantPolicy")
92            .unwrap_or(0.0);
93        let agg_just = get_string_field(&obj, "aggregator_start_level_justification", "JustifiedDepthBasedNodeVariantPolicy")
94            .unwrap_or_default();
95
96        // leaf_start_level => fuzzy_u8
97        trace!("(JustifiedDepthBasedNodeVariantPolicy) leaf_start_level => fuzzy_u8");
98        let leaf_val = match obj.get("leaf_start_level") {
99            Some(x) => {
100                match fuzzy_u8(x) {
101                    Ok(u) => {
102                        trace!("(JustifiedDepthBasedNodeVariantPolicy) leaf_start_level => {}", u);
103                        u
104                    }
105                    Err(e) => {
106                        error!("(JustifiedDepthBasedNodeVariantPolicy) leaf_start_level parse error => {:?}", e);
107                        return Err(FuzzyFromJsonValueError::Other {
108                            target_type: "JustifiedDepthBasedNodeVariantPolicy",
109                            detail: format!("Error leaf_start_level: {:?}", e),
110                        });
111                    }
112                }
113            }
114            None => 0,
115        };
116        let leaf_conf = get_f64_field(&obj, "leaf_start_level_confidence", "JustifiedDepthBasedNodeVariantPolicy")
117            .unwrap_or(0.0);
118        let leaf_just = get_string_field(&obj, "leaf_start_level_justification", "JustifiedDepthBasedNodeVariantPolicy")
119            .unwrap_or_default();
120
121        trace!("(JustifiedDepthBasedNodeVariantPolicy) Successfully parsed => returning final struct.");
122        Ok(Self {
123            aggregator_start_level: agg_val,
124            aggregator_start_level_confidence: agg_conf,
125            aggregator_start_level_justification: agg_just,
126            leaf_start_level: leaf_val,
127            leaf_start_level_confidence: leaf_conf,
128            leaf_start_level_justification: leaf_just,
129        })
130    }
131}