capability_grower_configuration/
node_variant_level_weights.rs

1// ---------------- [ File: capability-grower-configuration/src/node_variant_level_weights.rs ]
2crate::ix!();
3
4/// A single “probability triple” used in `ScriptedNodeVariantPolicy.levels`, specifying 
5/// aggregator/dispatch/leaf probabilities at that level.
6#[derive(
7    SaveLoad,Debug, Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate,
8    AiJsonTemplateWithJustification,
9    Builder, Default, Getters
10)]
11#[builder(pattern="owned", setter(into))]
12#[getset(get="pub")]
13pub struct NodeVariantLevelWeights {
14    /// Probability weight for aggregator selection in this phase
15    aggregator_chance: f32,
16    /// Probability weight for dispatch selection in this phase
17    dispatch_chance: f32,
18    /// Probability weight for leaf selection in this phase
19    leaf_chance: f32,
20}
21
22impl FuzzyFromJsonValue for JustifiedNodeVariantLevelWeights {
23    fn fuzzy_from_json_value(value: &JsonValue)
24        -> Result<Self, FuzzyFromJsonValueError>
25    {
26        trace!("(JustifiedNodeVariantLevelWeights) Entering fuzzy_from_json_value");
27
28        // 1) If null => default
29        if value.is_null() {
30            debug!("(JustifiedNodeVariantLevelWeights) Null => returning aggregator_chance=0, etc.");
31            return Ok(Self {
32                aggregator_chance: 0.0,
33                aggregator_chance_confidence: 0.0,
34                aggregator_chance_justification: String::new(),
35                dispatch_chance: 0.0,
36                dispatch_chance_confidence: 0.0,
37                dispatch_chance_justification: String::new(),
38                leaf_chance: 0.0,
39                leaf_chance_confidence: 0.0,
40                leaf_chance_justification: String::new(),
41            });
42        }
43
44        // 2) Must be object => parse
45        let mut obj = match value.as_object() {
46            Some(m) => {
47                trace!("(JustifiedNodeVariantLevelWeights) Found object => flattening if needed.");
48                m.clone()
49            }
50            None => {
51                error!("(JustifiedNodeVariantLevelWeights) Not an object => fail!");
52                return Err(FuzzyFromJsonValueError::NotAnObject {
53                    target_type: "JustifiedNodeVariantLevelWeights",
54                    actual: value.clone(),
55                });
56            }
57        };
58
59        flatten_all_fields(&mut obj);
60
61        // aggregator_chance => f32
62        trace!("(JustifiedNodeVariantLevelWeights) aggregator_chance => f32");
63        let agg_f64 = get_f64_field(&obj, "aggregator_chance", "JustifiedNodeVariantLevelWeights")
64            .unwrap_or(0.0);
65        let aggregator_chance = agg_f64 as f32;
66        let agg_conf = get_f64_field(&obj, "aggregator_chance_confidence", "JustifiedNodeVariantLevelWeights")
67            .unwrap_or(0.0);
68        let agg_just = get_string_field(&obj, "aggregator_chance_justification", "JustifiedNodeVariantLevelWeights")
69            .unwrap_or_default();
70
71        // dispatch_chance => f32
72        let disp_f64 = get_f64_field(&obj, "dispatch_chance", "JustifiedNodeVariantLevelWeights")
73            .unwrap_or(0.0);
74        let dispatch_chance = disp_f64 as f32;
75        let disp_conf = get_f64_field(&obj, "dispatch_chance_confidence", "JustifiedNodeVariantLevelWeights")
76            .unwrap_or(0.0);
77        let disp_just = get_string_field(&obj, "dispatch_chance_justification", "JustifiedNodeVariantLevelWeights")
78            .unwrap_or_default();
79
80        // leaf_chance => f32
81        trace!("(JustifiedNodeVariantLevelWeights) leaf_chance => f32");
82        let leaf_f64 = get_f64_field(&obj, "leaf_chance", "JustifiedNodeVariantLevelWeights")
83            .unwrap_or(0.0);
84        let leaf_chance = leaf_f64 as f32;
85        let leaf_conf = get_f64_field(&obj, "leaf_chance_confidence", "JustifiedNodeVariantLevelWeights")
86            .unwrap_or(0.0);
87        let leaf_just = get_string_field(&obj, "leaf_chance_justification", "JustifiedNodeVariantLevelWeights")
88            .unwrap_or_default();
89
90        trace!("(JustifiedNodeVariantLevelWeights) Successfully parsed => returning final struct.");
91        Ok(Self {
92            aggregator_chance,
93            aggregator_chance_confidence: agg_conf,
94            aggregator_chance_justification: agg_just,
95            dispatch_chance,
96            dispatch_chance_confidence: disp_conf,
97            dispatch_chance_justification: disp_just,
98            leaf_chance,
99            leaf_chance_confidence: leaf_conf,
100            leaf_chance_justification: leaf_just,
101        })
102    }
103}