rl_model/model/
interrupt.rs

1use super::*;
2use crate::parser::{Position, RlError};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone)]
6pub struct Interrupt {
7    effects: Vec<Effect>,
8    postconditions: Vec<Postcondition>,
9    position: Option<Position>,
10}
11
12impl Interrupt {
13    pub fn new(
14        postconditions: Vec<Postcondition>,
15        effects: Vec<Effect>,
16        position: Option<Position>,
17    ) -> Self {
18        Self {
19            effects,
20            postconditions,
21            position,
22        }
23    }
24
25    pub fn postconditions(&self) -> &Vec<Postcondition> {
26        &self.postconditions
27    }
28
29    pub fn effects(&self) -> &Vec<Effect> {
30        &self.effects
31    }
32
33    pub fn position(&self) -> Option<Position> {
34        self.position.clone()
35    }
36
37    //---------- Resolve ----------
38
39    pub fn resolve_resource(&mut self, map: &HashMap<String, ResourceId>) -> Result<(), RlError> {
40        for x in self.effects.iter_mut() {
41            x.resolve_resource(map)?;
42        }
43        for x in self.postconditions.iter_mut() {
44            x.resolve_resource(map)?;
45        }
46        Ok(())
47    }
48
49    pub fn resolve_state(&mut self, map: &HashMap<String, StateId>) -> Result<(), RlError> {
50        for x in self.effects.iter_mut() {
51            x.resolve_state(map)?;
52        }
53        for x in self.postconditions.iter_mut() {
54            x.resolve_state(map)?;
55        }
56        Ok(())
57    }
58}
59
60impl ToLang for Interrupt {
61    fn to_lang(&self, skillset: &Skillset) -> String {
62        let mut s = String::from("\t\t\tinterrupt {\n");
63        // Postcondition
64        if !self.postconditions.is_empty() {
65            s.push_str("\t\t\t\tpostcondition {\n");
66            for x in self.postconditions() {
67                s.push_str(&format!("\t\t\t\t\t{}\n", x.to_lang(skillset)));
68            }
69            s.push_str("\t\t\t\t}\n");
70        }
71        // Effects
72        if !self.effects.is_empty() {
73            s.push_str("\t\t\t\teffect {\n");
74            for x in self.effects.iter() {
75                s.push_str(&format!("\t\t\t\t\t{}\n", x.to_lang(skillset)))
76            }
77            s.push_str("\t\t\t\t}\n");
78        }
79        //
80        s.push_str("\t\t\t}\n");
81        s
82    }
83}
84
85impl std::fmt::Display for Interrupt {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        write!(f, "interrupt")
88    }
89}