Skip to main content

rust_lstar/
query.rs

1use crate::word::Word;
2
3/// Represents a query to be executed against the system under learning (SUL)
4#[derive(Clone, Debug)]
5pub struct OutputQuery {
6    pub input_word: Word,
7    pub output_word: Option<Word>,
8    pub queried: bool,
9}
10
11impl OutputQuery {
12    /// Create a new output query
13    pub fn new(input_word: Word) -> Self {
14        OutputQuery {
15            input_word,
16            output_word: None,
17            queried: false,
18        }
19    }
20
21    /// Check if the query has been executed
22    pub fn is_queried(&self) -> bool {
23        self.queried
24    }
25
26    /// Set the result of the query
27    pub fn set_result(&mut self, output_word: Word) {
28        self.output_word = Some(output_word);
29        self.queried = true;
30    }
31
32    /// Get the output word (if query has been executed)
33    pub fn output_word(&self) -> Option<&Word> {
34        self.output_word.as_ref()
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use crate::letter::Letter;
42
43    #[test]
44    fn test_query_creation() {
45        let input = Word::from_letters(vec![Letter::new("a")]);
46        let query = OutputQuery::new(input);
47        assert!(!query.is_queried());
48    }
49
50    #[test]
51    fn test_query_execution() {
52        let input = Word::from_letters(vec![Letter::new("a")]);
53        let output = Word::from_letters(vec![Letter::new("0")]);
54        let mut query = OutputQuery::new(input);
55        query.set_result(output);
56        assert!(query.is_queried());
57    }
58}