Skip to main content

qualia_core_db/research/
enquiry.rs

1//! Research enquiry — purpose, scope, constraints, questions.
2
3use std::collections::BTreeMap;
4
5/// A research constraint (e.g. temporal, geographic, methodological).
6#[derive(Debug, Clone)]
7pub struct ResearchConstraint {
8    pub constraint_type: String,
9    pub value: String,
10    pub description: String,
11}
12
13/// A research question linked to the enquiry.
14#[derive(Debug, Clone)]
15pub struct ResearchQuestion {
16    pub id: String,
17    pub text: String,
18    pub linked_questions: Vec<String>,
19    pub answered: bool,
20}
21
22/// A research enquiry with scope, constraints, and questions.
23#[derive(Debug, Clone)]
24pub struct ResearchEnquiry {
25    pub id: String,
26    pub purpose: String,
27    pub scope: Vec<String>,
28    pub constraints: Vec<ResearchConstraint>,
29    pub questions: Vec<ResearchQuestion>,
30    pub metadata: BTreeMap<String, String>,
31}
32
33impl ResearchEnquiry {
34    /// Creates an enquiry with `id` and `purpose`.
35    pub fn new(id: &str, purpose: &str) -> Self {
36        Self {
37            id: id.to_string(),
38            purpose: purpose.to_string(),
39            scope: Vec::new(),
40            constraints: Vec::new(),
41            questions: Vec::new(),
42            metadata: BTreeMap::new(),
43        }
44    }
45
46    /// Updates the enquiry purpose.
47    pub fn set_purpose(&mut self, purpose: &str) {
48        self.purpose = purpose.to_string();
49    }
50
51    /// Sets the topical scope.
52    pub fn define_scope(&mut self, scope: Vec<String>) {
53        self.scope = scope;
54    }
55
56    /// Adds a research constraint.
57    pub fn add_constraint(&mut self, constraint: ResearchConstraint) {
58        self.constraints.push(constraint);
59    }
60
61    /// Adds a research question.
62    pub fn add_question(&mut self, question: ResearchQuestion) {
63        self.questions.push(question);
64    }
65
66    /// Links `q1`→`q2` as related.
67    pub fn link_questions(&mut self, q1: &str, q2: &str) {
68        for q in &mut self.questions {
69            if q.id == q1 && !q.linked_questions.contains(&q2.to_string()) {
70                q.linked_questions.push(q2.to_string());
71            }
72            if q.id == q2 && !q.linked_questions.contains(&q1.to_string()) {
73                q.linked_questions.push(q1.to_string());
74            }
75        }
76    }
77
78    /// Total questions.
79    pub fn question_count(&self) -> usize {
80        self.questions.len()
81    }
82
83    /// Questions with recorded answers.
84    pub fn answered_count(&self) -> usize {
85        self.questions.iter().filter(|q| q.answered).count()
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn enquiry_creation() {
95        let eq = ResearchEnquiry::new("r1", "Investigate causal links");
96        assert_eq!(eq.id, "r1");
97        assert_eq!(eq.purpose, "Investigate causal links");
98        assert!(eq.questions.is_empty());
99    }
100
101    #[test]
102    fn enquiry_add_question() {
103        let mut eq = ResearchEnquiry::new("r1", "Test");
104        eq.add_question(ResearchQuestion {
105            id: "q1".into(),
106            text: "What happened?".into(),
107            linked_questions: vec![],
108            answered: false,
109        });
110        assert_eq!(eq.question_count(), 1);
111    }
112
113    #[test]
114    fn enquiry_link_questions() {
115        let mut eq = ResearchEnquiry::new("r1", "Test");
116        eq.add_question(ResearchQuestion {
117            id: "q1".into(),
118            text: "Why?".into(),
119            linked_questions: vec![],
120            answered: false,
121        });
122        eq.add_question(ResearchQuestion {
123            id: "q2".into(),
124            text: "How?".into(),
125            linked_questions: vec![],
126            answered: false,
127        });
128        eq.link_questions("q1", "q2");
129        assert!(eq.questions[0].linked_questions.contains(&"q2".to_string()));
130        assert!(eq.questions[1].linked_questions.contains(&"q1".to_string()));
131    }
132
133    #[test]
134    fn enquiry_scope_and_constraints() {
135        let mut eq = ResearchEnquiry::new("r1", "Test");
136        eq.define_scope(vec!["2020-2024".into(), "EU".into()]);
137        eq.add_constraint(ResearchConstraint {
138            constraint_type: "temporal".into(),
139            value: "2020-2024".into(),
140            description: "Year range".into(),
141        });
142        assert_eq!(eq.scope.len(), 2);
143        assert_eq!(eq.constraints.len(), 1);
144    }
145}