capability_grower_configuration/
weighted_node_variant_policy.rs

1// ---------------- [ File: capability-grower-configuration/src/weighted_node_variant_policy.rs ]
2crate::ix!();
3
4/// Probability-based selection among aggregator/dispatch/leaf, with *no* extra constraints.
5#[derive(
6    SaveLoad,Debug, Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate,
7    AiJsonTemplateWithJustification,
8    Builder, Default, Getters
9)]
10#[builder(pattern="owned", setter(into))]
11#[getset(get="pub")]
12pub struct WeightedNodeVariantPolicy {
13
14    /// Probability weight for aggregator selection
15    aggregator_weight: f32,
16
17    /// Probability weight for dispatch selection
18    dispatch_weight: f32,
19
20    /// Probability weight for leaf-holder selection
21    leaf_holder_weight: f32,
22}
23
24impl FuzzyFromJsonValue for JustifiedWeightedNodeVariantPolicy {
25    fn fuzzy_from_json_value(value: &serde_json::Value)
26        -> Result<Self, FuzzyFromJsonValueError>
27    {
28        trace!("(JustifiedWeightedNodeVariantPolicy) Entering fuzzy_from_json_value");
29
30        // ... if null => default checks, etc. ...
31        if value.is_null() {
32            debug!("(JustifiedWeightedNodeVariantPolicy) Null => aggregator_weight=0, etc.");
33            return Ok(Self {
34                aggregator_weight: 0.0,
35                aggregator_weight_confidence: 0.0,
36                aggregator_weight_justification: String::new(),
37                dispatch_weight: 0.0,
38                dispatch_weight_confidence: 0.0,
39                dispatch_weight_justification: String::new(),
40                leaf_holder_weight: 0.0,
41                leaf_holder_weight_confidence: 0.0,
42                leaf_holder_weight_justification: String::new(),
43            });
44        }
45
46        let mut obj = match value.as_object() {
47            Some(m) => {
48                trace!("(JustifiedWeightedNodeVariantPolicy) Found object => flattening if needed.");
49                m.clone()
50            }
51            None => {
52                error!("(JustifiedWeightedNodeVariantPolicy) Not an object => fail!");
53                return Err(FuzzyFromJsonValueError::NotAnObject {
54                    target_type: "JustifiedWeightedNodeVariantPolicy",
55                    actual: value.clone(),
56                });
57            }
58        };
59
60        flatten_all_fields(&mut obj);
61
62        // aggregator_weight => f32
63        let agg_f64 = get_f64_field(&obj, "aggregator_weight", "JustifiedWeightedNodeVariantPolicy")
64            .unwrap_or(0.0);
65        let aggregator_weight = agg_f64 as f32;
66        let agg_conf = get_f64_field(&obj, "aggregator_weight_confidence", "JustifiedWeightedNodeVariantPolicy")
67            .unwrap_or(0.0);
68        let agg_just = get_string_field(&obj, "aggregator_weight_justification", "JustifiedWeightedNodeVariantPolicy")
69            .unwrap_or_default();
70
71        // dispatch_weight => f32
72        let disp_f64 = get_f64_field(&obj, "dispatch_weight", "JustifiedWeightedNodeVariantPolicy")
73            .unwrap_or(0.0);
74        let dispatch_weight = disp_f64 as f32;
75        let disp_conf = get_f64_field(&obj, "dispatch_weight_confidence", "JustifiedWeightedNodeVariantPolicy")
76            .unwrap_or(0.0);
77        let disp_just = get_string_field(&obj, "dispatch_weight_justification", "JustifiedWeightedNodeVariantPolicy")
78            .unwrap_or_default();
79
80        // leaf_holder_weight => f32
81        let leaf_f64 = get_f64_field(&obj, "leaf_holder_weight", "JustifiedWeightedNodeVariantPolicy")
82            .unwrap_or(0.0);
83        let leaf_holder_weight = leaf_f64 as f32;
84        let leaf_conf = get_f64_field(&obj, "leaf_holder_weight_confidence", "JustifiedWeightedNodeVariantPolicy")
85            .unwrap_or(0.0);
86        let leaf_just = get_string_field(&obj, "leaf_holder_weight_justification", "JustifiedWeightedNodeVariantPolicy")
87            .unwrap_or_default();
88
89        trace!("(JustifiedWeightedNodeVariantPolicy) Successfully parsed => returning final struct.");
90        Ok(Self {
91            aggregator_weight,
92            aggregator_weight_confidence: agg_conf,
93            aggregator_weight_justification: agg_just,
94            dispatch_weight,
95            dispatch_weight_confidence: disp_conf,
96            dispatch_weight_justification: disp_just,
97            leaf_holder_weight,
98            leaf_holder_weight_confidence: leaf_conf,
99            leaf_holder_weight_justification: leaf_just,
100        })
101    }
102}