smt_lang/problem/
structure.rs1use super::*;
2use crate::parser::Position;
3
4pub enum StructureElement {
5 Attribute(Attribute<StructureId>),
6 Method(Method<StructureId>),
7}
8
9#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
12pub struct StructureId(pub usize);
13
14impl Id for StructureId {
15 fn empty() -> Self {
16 Self(0)
17 }
18}
19
20#[derive(Clone)]
23pub struct Structure {
24 id: StructureId,
25 name: String,
26 typ: Type,
27 attributes: Vec<Attribute<StructureId>>,
28 methods: Vec<Method<StructureId>>,
29 position: Option<Position>,
30}
31
32impl Structure {
33 pub fn new<S: Into<String>>(name: S, position: Option<Position>) -> Self {
34 let id = StructureId::empty();
35 let name = name.into();
36 Self {
37 id,
38 name,
39 typ: Type::Structure(id),
40 attributes: vec![],
41 methods: vec![],
42 position,
43 }
44 }
45
46 pub fn add_attribute(
49 &mut self,
50 mut attribute: Attribute<StructureId>,
51 ) -> AttributeId<StructureId> {
52 let id = AttributeId(self.id(), self.attributes.len());
53 attribute.set_id(id);
54 self.attributes.push(attribute);
55 id
56 }
57
58 pub fn get_attribute(&self, id: AttributeId<StructureId>) -> Option<&Attribute<StructureId>> {
59 let AttributeId(structure_id, n) = id;
60 if self.id != structure_id {
61 None
62 } else {
63 self.attributes.get(n)
64 }
65 }
66
67 pub fn attributes(&self) -> &Vec<Attribute<StructureId>> {
68 &self.attributes
69 }
70
71 pub fn find_attribute(&self, name: &str) -> Option<&Attribute<StructureId>> {
72 self.attributes.iter().find(|x| x.name() == name)
73 }
74
75 pub fn add_method(&mut self, mut method: Method<StructureId>) -> MethodId<StructureId> {
78 let id = MethodId(self.id(), self.methods.len());
79 method.set_id(id);
80 self.methods.push(method);
81 id
82 }
83
84 pub fn get_method(&self, id: MethodId<StructureId>) -> Option<&Method<StructureId>> {
85 let MethodId(structure_id, n) = id;
86 if self.id != structure_id {
87 None
88 } else {
89 self.methods.get(n)
90 }
91 }
92
93 pub fn methods(&self) -> &Vec<Method<StructureId>> {
94 &self.methods
95 }
96
97 pub fn find_method(&self, name: &str) -> Option<&Method<StructureId>> {
98 self.methods.iter().find(|x| x.name() == name)
99 }
100
101 pub fn instances(&self, problem: &Problem) -> Vec<InstanceId> {
104 let mut v = Vec::new();
105 for inst in problem.instances().iter() {
106 match inst.typ() {
107 Type::Structure(id) => {
108 if *id == self.id() {
109 v.push(inst.id());
110 }
111 }
112 _ => {}
113 }
114 }
115 v
116 }
117
118 pub fn is_empty(&self, problem: &Problem) -> bool {
119 self.instances(problem).is_empty()
120 }
121
122 pub fn local_naming(&self) -> Vec<Naming> {
125 let mut v = vec![];
126 v.extend(self.attributes.iter().map(|x| x.naming()));
127 v.extend(self.methods.iter().map(|x| x.naming()));
128 v
129 }
130
131 pub fn duplicate(&self) -> Result<(), Error> {
132 check_duplicate(self.local_naming())?;
133 for x in self.methods.iter() {
134 x.duplicate()?;
135 }
136 Ok(())
137 }
138
139 pub fn resolve_type_expr(&mut self, entries: &TypeEntries) -> Result<(), Error> {
142 for x in self.attributes.iter_mut() {
144 x.resolve_type(entries)?;
145 x.resolve_type_expr(entries)?;
146 }
147 for x in self.methods.iter_mut() {
149 x.resolve_type(entries)?;
150 x.resolve_type_expr(entries)?;
151 }
152 Ok(())
154 }
155
156 pub fn resolve_expr(&self, problem: &Problem, entries: &Entries) -> Result<Structure, Error> {
157 let mut attributes = Vec::new();
159 for x in self.attributes.iter() {
160 let a = x.resolve_expr(problem, &entries)?;
161 attributes.push(a);
162 }
163 let mut methods = Vec::new();
165 for x in self.methods.iter() {
166 let m = x.resolve_expr(problem, &entries)?;
167 methods.push(m);
168 }
169 Ok(Structure {
171 id: self.id,
172 name: self.name.clone(),
173 typ: self.typ.clone(),
174 attributes,
175 methods,
176 position: self.position.clone(),
177 })
178 }
179
180 pub fn check_parameter_size(&self, problem: &Problem) -> Result<(), Error> {
183 for x in self.attributes.iter() {
184 x.check_parameter_size(problem)?;
185 }
186 for x in self.methods.iter() {
187 x.check_parameter_size(problem)?;
188 }
189 Ok(())
190 }
191
192 pub fn check_bounded(&self, problem: &Problem) -> Result<(), Error> {
195 for x in self.methods.iter() {
196 x.check_bounded(problem)?;
197 }
198 Ok(())
199 }
200
201 pub fn check_type(&self, problem: &Problem) -> Result<(), Error> {
204 for x in self.attributes.iter() {
205 x.check_type(problem)?;
206 }
207 for x in self.methods.iter() {
208 x.check_type(problem)?;
209 }
210 Ok(())
211 }
212}
213
214impl WithPosition for Structure {
217 fn position(&self) -> &Option<Position> {
218 &self.position
219 }
220}
221
222impl Named<StructureId> for Structure {
225 fn id(&self) -> StructureId {
226 self.id
227 }
228
229 fn set_id(&mut self, id: StructureId) {
230 self.id = id;
231 self.typ = Type::Structure(id);
232 for (n, x) in self.attributes.iter_mut().enumerate() {
233 let id = AttributeId(id, n);
234 x.set_id(id);
235 }
236 for (n, x) in self.methods.iter_mut().enumerate() {
237 let id = MethodId(id, n);
238 x.set_id(id);
239 }
240 }
241
242 fn name(&self) -> &str {
243 &self.name
244 }
245}
246
247impl ToLang for Structure {
250 fn to_lang(&self, problem: &Problem) -> String {
251 let mut s = format!("struct {} {{\n", self.name());
252 for x in self.attributes.iter() {
254 s.push_str(&format!("{}\n", &x.to_lang(problem)));
255 }
256 for x in self.methods.iter() {
258 s.push_str(&format!("{}", &x.to_lang(problem)));
259 s.push_str(&format!("// {:?}\n", x.id()));
260 }
261 s.push_str("}\n");
263 s
264 }
265}
266
267impl WithType for Structure {
270 fn typ(&self) -> &Type {
271 &self.typ
272 }
273
274 fn set_type(&mut self, _: Type) {}
275
276 fn resolve_type_children(&mut self, entries: &TypeEntries) -> Result<(), Error> {
277 for x in self.attributes.iter_mut() {
278 x.resolve_type(&entries)?;
279 }
280 for x in self.methods.iter_mut() {
281 x.resolve_type(&entries)?;
282 }
283 Ok(())
284 }
285
286 fn check_interval_children(&self, problem: &Problem) -> Result<(), Error> {
287 for x in self.attributes.iter() {
288 x.check_interval(problem)?;
289 }
290 for x in self.methods.iter() {
291 x.check_interval(problem)?;
292 }
293 Ok(())
294 }
295}
296
297impl GetFromId<AttributeId<StructureId>, Attribute<StructureId>> for Structure {
300 fn get(&self, id: AttributeId<StructureId>) -> Option<&Attribute<StructureId>> {
301 self.get_attribute(id)
302 }
303}
304
305impl GetFromId<MethodId<StructureId>, Method<StructureId>> for Structure {
306 fn get(&self, id: MethodId<StructureId>) -> Option<&Method<StructureId>> {
307 self.get_method(id)
308 }
309}