capability_grower_configuration/
phased_node_variant_policy.rs

1// ---------------- [ File: capability-grower-configuration/src/phased_node_variant_policy.rs ]
2crate::ix!();
3
4/// A list of “phases,” each specifying a start depth plus aggregator/dispatch/leaf weights.
5/// Once you pass a phase’s `start_level`, you use those weights until the next phase’s
6/// `start_level` or the end of the tree.
7#[derive(
8    SaveLoad,Debug, Clone, PartialEq, Serialize, Deserialize, AiJsonTemplate,
9    AiJsonTemplateWithJustification,
10    Builder, Default, Getters
11)]
12#[builder(pattern="owned", setter(into))]
13#[getset(get="pub")]
14pub struct PhasedNodeVariantPolicy {
15
16    /// Each phase should be determined taking into account relevant characteristics of our target
17    /// domain
18    #[justify=true]
19    phases: Vec<NodeVariantPhaseRange>,
20}
21
22impl FuzzyFromJsonValue for JustifiedPhasedNodeVariantPolicy {
23    fn fuzzy_from_json_value(value: &JsonValue)
24        -> Result<Self, FuzzyFromJsonValueError>
25    {
26        trace!("(JustifiedPhasedNodeVariantPolicy) Entering fuzzy_from_json_value");
27
28        if value.is_null() {
29            debug!("(JustifiedPhasedNodeVariantPolicy) Null => returning empty phases + defaults");
30            return Ok(Self {
31                phases: vec![],
32                phases_confidence: 0.0,
33                phases_justification: String::new(),
34            });
35        }
36
37        let mut obj = match value.as_object() {
38            Some(m) => {
39                trace!("(JustifiedPhasedNodeVariantPolicy) Found object => flattening.");
40                m.clone()
41            }
42            None => {
43                error!("(JustifiedPhasedNodeVariantPolicy) Not an object => fail!");
44                return Err(FuzzyFromJsonValueError::NotAnObject {
45                    target_type: "JustifiedPhasedNodeVariantPolicy",
46                    actual: value.clone(),
47                });
48            }
49        };
50
51        flatten_all_fields(&mut obj);
52
53        // phases => array
54        let phases_val = match obj.get("phases") {
55            Some(JsonValue::Array(arr)) => {
56                let mut out = Vec::with_capacity(arr.len());
57                for elem in arr {
58                    let parsed = JustifiedNodeVariantPhaseRange::fuzzy_from_json_value(elem)?;
59                    out.push(parsed);
60                }
61                out
62            }
63            _ => {
64                debug!("(JustifiedPhasedNodeVariantPolicy) 'phases' missing or not an array => empty");
65                vec![]
66            }
67        };
68        let pc = get_f64_field(&obj, "phases_confidence", "JustifiedPhasedNodeVariantPolicy")
69            .unwrap_or(0.0);
70        let pj = get_string_field(&obj, "phases_justification", "JustifiedPhasedNodeVariantPolicy")
71            .unwrap_or_default();
72
73        trace!("(JustifiedPhasedNodeVariantPolicy) returning final struct with {} phases", phases_val.len());
74        Ok(Self {
75            phases: phases_val,
76            phases_confidence: pc,
77            phases_justification: pj,
78        })
79    }
80}