1use std::collections::VecDeque;
2
3use super::*;
4
5#[derive(Clone)]
6pub struct Problem {
7 structures: Vec<Structure>,
8 instances: Vec<Instance>,
9 classes: Vec<Class>,
10 variables: Vec<Variable>,
11 functions: Vec<Function>,
12 constraints: Vec<Constraint>,
13 search: Search,
14}
15
16impl Problem {
17 pub fn new() -> Self {
18 Self {
19 structures: vec![],
20 classes: vec![],
21 instances: vec![],
22 variables: vec![],
23 functions: vec![],
24 constraints: vec![],
25 search: Search::Solve,
26 }
27 }
28
29 pub fn add_structure(&mut self, mut structure: Structure) -> StructureId {
32 let id = StructureId(self.structures.len());
33 structure.set_id(id);
34 self.structures.push(structure);
35 id
36 }
37
38 pub fn get_structure(&self, id: StructureId) -> Option<&Structure> {
39 let StructureId(n) = id;
40 self.structures.get(n)
41 }
42
43 pub fn structures(&self) -> &Vec<Structure> {
44 &self.structures
45 }
46
47 pub fn find_structure(&self, name: &str) -> Option<&Structure> {
48 self.structures.iter().find(|x| x.name() == name)
49 }
50
51 pub fn add_instance(&mut self, mut instance: Instance) -> InstanceId {
54 let id = InstanceId(self.instances.len());
55 instance.set_id(id);
56 self.instances.push(instance);
57 id
58 }
59
60 pub fn get_instance(&self, id: InstanceId) -> Option<&Instance> {
61 let InstanceId(n) = id;
62 self.instances.get(n)
63 }
64
65 pub fn instances(&self) -> &Vec<Instance> {
66 &self.instances
67 }
68
69 pub fn find_instance(&self, name: &str) -> Option<&Instance> {
70 self.instances.iter().find(|x| x.name() == name)
71 }
72
73 pub fn add_class(&mut self, mut class: Class) -> ClassId {
76 let id = ClassId(self.classes.len());
77 class.set_id(id);
78 self.classes.push(class);
79 id
80 }
81
82 pub fn get_class(&self, id: ClassId) -> Option<&Class> {
83 let ClassId(n) = id;
84 self.classes.get(n)
85 }
86
87 pub fn classes(&self) -> &Vec<Class> {
88 &self.classes
89 }
90
91 pub fn find_class(&self, name: &str) -> Option<&Class> {
92 self.classes.iter().find(|x| x.name() == name)
93 }
94
95 pub fn classes_ordered(&self) -> Vec<ClassId> {
96 let mut all = self.classes.iter().map(|c| c.id()).collect::<VecDeque<_>>();
97 let mut v = Vec::new();
98 while !all.is_empty() {
99 let id = all.pop_front().unwrap();
100 let c = self.get_class(id).unwrap();
101 if let Some(super_id) = c.super_class() {
102 if v.contains(&super_id) {
103 v.push(id);
104 } else {
105 all.push_back(id);
106 }
107 } else {
108 v.push(id);
109 }
110 }
111 v
112 }
113
114 pub fn add_variable(&mut self, mut variable: Variable) -> VariableId {
117 let id = VariableId(self.variables.len());
118 variable.set_id(id);
119 self.variables.push(variable);
120 id
121 }
122
123 pub fn get_variable(&self, id: VariableId) -> Option<&Variable> {
124 let VariableId(n) = id;
125 self.variables.get(n)
126 }
127
128 pub fn variables(&self) -> &Vec<Variable> {
129 &self.variables
130 }
131
132 pub fn find_variable(&self, name: &str) -> Option<&Variable> {
133 self.variables.iter().find(|x| x.name() == name)
134 }
135
136 pub fn add_function(&mut self, mut function: Function) -> FunctionId {
139 let id = FunctionId(self.functions.len());
140 function.set_id(id);
141 self.functions.push(function);
142 id
143 }
144
145 pub fn get_function(&self, id: FunctionId) -> Option<&Function> {
146 let FunctionId(n) = id;
147 self.functions.get(n)
148 }
149
150 pub fn functions(&self) -> &Vec<Function> {
151 &self.functions
152 }
153
154 pub fn find_function(&self, name: &str) -> Option<&Function> {
155 self.functions.iter().find(|x| x.name() == name)
156 }
157
158 pub fn add_constraint(&mut self, mut constraint: Constraint) -> ConstraintId {
161 let id = ConstraintId(self.constraints.len());
162 constraint.set_id(id);
163 self.constraints.push(constraint);
164 id
165 }
166
167 pub fn get_constraint(&self, id: ConstraintId) -> Option<&Constraint> {
168 let ConstraintId(n) = id;
169 self.constraints.get(n)
170 }
171
172 pub fn constraints(&self) -> &Vec<Constraint> {
173 &self.constraints
174 }
175
176 pub fn search(&self) -> &Search {
179 &self.search
180 }
181
182 pub fn set_search(&mut self, search: Search) {
183 self.search = search
184 }
185
186 pub fn type_entries(&self) -> TypeEntries {
189 let mut v = vec![];
190 v.extend(
191 self.structures
192 .iter()
193 .map(|x| TypeEntry::from_id(self, x.id())),
194 );
195 v.extend(
196 self.classes
197 .iter()
198 .map(|x| TypeEntry::from_id(self, x.id())),
199 );
200 TypeEntries::new(v)
201 }
202
203 pub fn entries(&self) -> Entries {
204 let mut v = vec![];
205 v.extend(self.instances.iter().map(|x| Entry::from_id(self, x.id())));
206 v.extend(self.variables.iter().map(|x| Entry::from_id(self, x.id())));
207 Entries::new(v)
208 }
209
210 pub fn naming(&self) -> Vec<Naming> {
213 let mut v = vec![];
214 v.extend(self.structures.iter().map(|x| x.naming()));
215 v.extend(self.classes.iter().map(|x| x.naming()));
216 v.extend(self.instances.iter().map(|x| x.naming()));
217 v.extend(self.variables.iter().map(|x| x.naming()));
218 v.extend(self.functions.iter().map(|x| x.naming()));
219 v.extend(self.constraints.iter().map(|x| x.naming()));
220 v
221 }
222
223 pub fn duplicate(&self) -> Result<(), Error> {
224 check_duplicate(self.naming())?;
225 for x in self.structures.iter() {
226 x.duplicate()?;
227 }
228 for x in self.classes.iter() {
229 x.duplicate(self)?;
230 }
231 for x in self.functions.iter() {
232 x.duplicate()?;
233 }
234 Ok(())
235 }
236
237 pub fn resolve_type(&mut self) -> Result<(), Error> {
240 let entries = self.type_entries();
241 for x in self.structures.iter_mut() {
243 x.resolve_type(&entries)?;
244 }
245 for x in self.classes.iter_mut() {
246 x.resolve_type(&entries)?;
247 }
248 for x in self.instances.iter_mut() {
249 x.resolve_type(&entries)?;
250 }
251 for x in self.variables.iter_mut() {
252 x.resolve_type(&entries)?;
253 }
254 for x in self.functions.iter_mut() {
255 x.resolve_type(&entries)?;
256 }
257 for x in self.structures.iter_mut() {
259 x.resolve_type_expr(&entries)?;
260 }
261 for x in self.classes.iter_mut() {
262 x.resolve_type_expr(&entries)?;
263 }
264 for x in self.variables.iter_mut() {
265 x.resolve_type_expr(&entries)?;
266 }
267 for x in self.functions.iter_mut() {
268 x.resolve_type_expr(&entries)?;
269 }
270 for x in self.constraints.iter_mut() {
271 x.resolve_type_expr(&entries)?;
272 }
273 let s = self.search.resolve_type_expr(&entries)?;
275 self.search = s;
276 Ok(())
278 }
279
280 pub fn resolve_expr(&mut self) -> Result<(), Error> {
281 let entries = self.entries();
282 let mut structures = Vec::new();
284 for x in self.structures.iter() {
285 let s = x.resolve_expr(self, &entries)?;
286 structures.push(s);
287 }
288 self.structures = structures;
289 let mut classes = Vec::new();
291 for x in self.classes.iter() {
292 let c = x.resolve_expr(self, &entries)?;
293 classes.push(c);
294 }
295 self.classes = classes;
296 let mut variables = Vec::new();
298 for x in self.variables.iter() {
299 let v = x.resolve_expr(self, &entries)?;
300 variables.push(v);
301 }
302 self.variables = variables;
303 let mut functions = Vec::new();
305 for x in self.functions.iter() {
306 let f = x.resolve_expr(self, &entries)?;
307 functions.push(f);
308 }
309 self.functions = functions;
310 let mut constraints = Vec::new();
312 for x in self.constraints.iter() {
313 let c = x.resolve_expr(self, &entries)?;
314 constraints.push(c);
315 }
316 self.constraints = constraints;
317 self.search = self.search.resolve_expr(self, &entries)?;
319 Ok(())
321 }
322
323 pub fn check_interval(&self) -> Result<(), Error> {
326 for x in self.structures.iter() {
327 x.check_interval(self)?;
328 }
329 for x in self.classes.iter() {
330 x.check_interval(self)?;
331 }
332 for x in self.variables.iter() {
333 x.check_interval(self)?;
334 }
335 for x in self.functions.iter() {
336 x.check_interval(self)?;
337 }
338 Ok(())
339 }
340
341 pub fn check_parameter_size(&self) -> Result<(), Error> {
344 for x in self.structures.iter() {
345 x.check_parameter_size(self)?;
346 }
347 for x in self.classes.iter() {
348 x.check_parameter_size(self)?;
349 }
350 for x in self.variables.iter() {
351 x.check_parameter_size(self)?;
352 }
353 for x in self.functions.iter() {
354 x.check_parameter_size(self)?;
355 }
356 for x in self.constraints.iter() {
357 x.check_parameter_size(self)?;
358 }
359 self.search.check_parameter_size(self)?;
360 Ok(())
361 }
362
363 pub fn check_bounded(&self) -> Result<(), Error> {
366 for x in self.structures.iter() {
367 x.check_bounded(self)?;
368 }
369 for x in self.classes.iter() {
370 x.check_bounded(self)?;
371 }
372 for x in self.functions.iter() {
373 x.check_bounded(self)?;
374 }
375 Ok(())
376 }
377
378 pub fn check_type(&self) -> Result<(), Error> {
381 for x in self.structures.iter() {
382 x.check_type(self)?;
383 }
384 for x in self.classes.iter() {
385 x.check_type(self)?;
386 }
387 for x in self.variables.iter() {
388 x.check_type(self)?;
389 }
390 for x in self.functions.iter() {
391 x.check_type(self)?;
392 }
393 for x in self.constraints.iter() {
394 x.check_type(self)?;
395 }
396 self.search.check_type(self)?;
397 Ok(())
398 }
399
400 pub fn check_empty(&self) -> Result<(), Error> {
403 for x in self.structures.iter() {
404 if x.is_empty(self) {
405 return Err(Error::Empty {
406 name: x.name().to_string(),
407 category: "Structure".to_string(),
408 });
409 }
410 }
411 for x in self.classes.iter() {
412 if x.is_empty(self) {
413 return Err(Error::Empty {
414 name: x.name().to_string(),
415 category: "Class".to_string(),
416 });
417 }
418 }
419 Ok(())
420 }
421
422 pub fn check_cycle(&self) -> Result<(), Error> {
425 for x in self.classes.iter() {
426 x.check_cycle(self)?;
427 }
428 Ok(())
429 }
430
431 pub fn to_entry(&self) -> d_stuff::Entry {
434 d_stuff::Entry::new(
435 d_stuff::Status::Info,
436 d_stuff::Text::new(
437 "Problem",
438 termion::style::Bold.to_string(),
439 termion::color::Blue.fg_str(),
440 ),
441 None,
442 vec![d_stuff::Message::new(
443 None,
444 d_stuff::Text::new(
445 format!("{}", self),
446 termion::style::Reset.to_string(),
447 termion::color::White.fg_str(),
448 ),
449 )],
450 )
451 }
452}
453
454impl std::fmt::Display for Problem {
457 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
458 for x in self.structures.iter() {
459 write!(f, "// {:?}\n", x.id())?;
460 write!(f, "{}\n", x.to_lang(self))?;
461 let instances = x.instances(self);
462 if let Some((first, others)) = instances.split_first() {
463 write!(f, "inst {}", self.get(*first).unwrap().name())?;
464 for i in others.iter() {
465 write!(f, ", {}", self.get(*i).unwrap().name())?;
466 }
467 write!(f, ": {}\n", x.name())?;
468 }
469 }
470 for x in self.classes.iter() {
471 write!(f, "// {:?}\n", x.id())?;
472 write!(f, "{}\n", x.to_lang(self))?;
473 let instances = x.instances(self);
474 if let Some((first, others)) = instances.split_first() {
475 write!(f, "inst {}", self.get(*first).unwrap().name())?;
476 for i in others.iter() {
477 write!(f, ", {}", self.get(*i).unwrap().name())?;
478 }
479 write!(f, ": {}\n", x.name())?;
480 }
481 }
482 for x in self.variables.iter() {
483 write!(f, "{}\n", x.to_lang(self))?;
484 }
485 for x in self.functions.iter() {
486 write!(f, "{}\n", x.to_lang(self))?;
487 }
488 for x in self.constraints.iter() {
489 write!(f, "{}\n", x.to_lang(self))?;
490 }
491 write!(f, "{}\n", self.search.to_lang(self))?;
492 Ok(())
493 }
494}
495
496impl GetFromId<StructureId, Structure> for Problem {
499 fn get(&self, id: StructureId) -> Option<&Structure> {
500 self.get_structure(id)
501 }
502}
503impl GetFromId<AttributeId<StructureId>, Attribute<StructureId>> for Problem {
504 fn get(&self, id: AttributeId<StructureId>) -> Option<&Attribute<StructureId>> {
505 let AttributeId(structure_id, _) = id;
506 if let Some(structure) = self.get(structure_id) {
507 structure.get(id)
508 } else {
509 None
510 }
511 }
512}
513impl GetFromId<MethodId<StructureId>, Method<StructureId>> for Problem {
514 fn get(&self, id: MethodId<StructureId>) -> Option<&Method<StructureId>> {
515 let MethodId(structure_id, _) = id;
516 if let Some(structure) = self.get(structure_id) {
517 structure.get(id)
518 } else {
519 None
520 }
521 }
522}
523
524impl GetFromId<ClassId, Class> for Problem {
525 fn get(&self, id: ClassId) -> Option<&Class> {
526 self.get_class(id)
527 }
528}
529impl GetFromId<AttributeId<ClassId>, Attribute<ClassId>> for Problem {
530 fn get(&self, id: AttributeId<ClassId>) -> Option<&Attribute<ClassId>> {
531 let AttributeId(class_id, _) = id;
532 if let Some(class) = self.get(class_id) {
533 class.get(id)
534 } else {
535 None
536 }
537 }
538}
539impl GetFromId<MethodId<ClassId>, Method<ClassId>> for Problem {
540 fn get(&self, id: MethodId<ClassId>) -> Option<&Method<ClassId>> {
541 let MethodId(class_id, _) = id;
542 if let Some(class) = self.get(class_id) {
543 class.get(id)
544 } else {
545 None
546 }
547 }
548}
549
550impl GetFromId<InstanceId, Instance> for Problem {
551 fn get(&self, id: InstanceId) -> Option<&Instance> {
552 self.get_instance(id)
553 }
554}
555
556impl GetFromId<VariableId, Variable> for Problem {
557 fn get(&self, id: VariableId) -> Option<&Variable> {
558 self.get_variable(id)
559 }
560}
561
562impl GetFromId<FunctionId, Function> for Problem {
563 fn get(&self, id: FunctionId) -> Option<&Function> {
564 self.get_function(id)
565 }
566}
567
568impl GetFromId<ConstraintId, Constraint> for Problem {
569 fn get(&self, id: ConstraintId) -> Option<&Constraint> {
570 self.get_constraint(id)
571 }
572}