Skip to main content

entrenar/integrity/behavioral/
builder.rs

1//! Builder pattern for BehavioralIntegrity
2//!
3//! Provides a fluent API for constructing BehavioralIntegrity instances.
4
5use super::metrics::BehavioralIntegrity;
6use super::violation::MetamorphicViolation;
7
8/// Builder for BehavioralIntegrity
9#[derive(Debug, Clone)]
10pub struct BehavioralIntegrityBuilder {
11    equivalence_score: f64,
12    syscall_match: f64,
13    timing_variance: f64,
14    semantic_equiv: f64,
15    violations: Vec<MetamorphicViolation>,
16    test_count: u32,
17    model_id: String,
18}
19
20impl BehavioralIntegrityBuilder {
21    /// Create a new builder
22    pub fn new(model_id: impl Into<String>) -> Self {
23        Self {
24            equivalence_score: 0.0,
25            syscall_match: 0.0,
26            timing_variance: 1.0,
27            semantic_equiv: 0.0,
28            violations: Vec::new(),
29            test_count: 0,
30            model_id: model_id.into(),
31        }
32    }
33
34    /// Set equivalence score
35    pub fn equivalence_score(mut self, score: f64) -> Self {
36        self.equivalence_score = score;
37        self
38    }
39
40    /// Set syscall match score
41    pub fn syscall_match(mut self, score: f64) -> Self {
42        self.syscall_match = score;
43        self
44    }
45
46    /// Set timing variance
47    pub fn timing_variance(mut self, variance: f64) -> Self {
48        self.timing_variance = variance;
49        self
50    }
51
52    /// Set semantic equivalence score
53    pub fn semantic_equiv(mut self, score: f64) -> Self {
54        self.semantic_equiv = score;
55        self
56    }
57
58    /// Add a violation
59    pub fn violation(mut self, violation: MetamorphicViolation) -> Self {
60        self.violations.push(violation);
61        self
62    }
63
64    /// Set test count
65    pub fn test_count(mut self, count: u32) -> Self {
66        self.test_count = count;
67        self
68    }
69
70    /// Build the BehavioralIntegrity instance
71    pub fn build(self) -> BehavioralIntegrity {
72        let mut integrity = BehavioralIntegrity::new(
73            self.equivalence_score,
74            self.syscall_match,
75            self.timing_variance,
76            self.semantic_equiv,
77            self.model_id,
78        );
79        integrity.violations = self.violations;
80        integrity.test_count = self.test_count;
81        integrity
82    }
83}