1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use std::collections::VecDeque;

use super::*;

#[derive(Clone)]
pub struct Problem {
    structures: Vec<Structure>,
    instances: Vec<Instance>,
    classes: Vec<Class>,
    variables: Vec<Variable>,
    functions: Vec<Function>,
    constraints: Vec<Constraint>,
    search: Search,
}

impl Problem {
    pub fn new() -> Self {
        Self {
            structures: vec![],
            classes: vec![],
            instances: vec![],
            variables: vec![],
            functions: vec![],
            constraints: vec![],
            search: Search::Solve,
        }
    }

    //---------- Structure ----------

    pub fn add_structure(&mut self, mut structure: Structure) -> StructureId {
        let id = StructureId(self.structures.len());
        structure.set_id(id);
        self.structures.push(structure);
        id
    }

    pub fn get_structure(&self, id: StructureId) -> Option<&Structure> {
        let StructureId(n) = id;
        self.structures.get(n)
    }

    pub fn structures(&self) -> &Vec<Structure> {
        &self.structures
    }

    pub fn find_structure(&self, name: &str) -> Option<&Structure> {
        self.structures.iter().find(|x| x.name() == name)
    }

    //---------- Instance ----------

    pub fn add_instance(&mut self, mut instance: Instance) -> InstanceId {
        let id = InstanceId(self.instances.len());
        instance.set_id(id);
        self.instances.push(instance);
        id
    }

    pub fn get_instance(&self, id: InstanceId) -> Option<&Instance> {
        let InstanceId(n) = id;
        self.instances.get(n)
    }

    pub fn instances(&self) -> &Vec<Instance> {
        &self.instances
    }

    pub fn find_instance(&self, name: &str) -> Option<&Instance> {
        self.instances.iter().find(|x| x.name() == name)
    }

    //---------- Class ----------

    pub fn add_class(&mut self, mut class: Class) -> ClassId {
        let id = ClassId(self.classes.len());
        class.set_id(id);
        self.classes.push(class);
        id
    }

    pub fn get_class(&self, id: ClassId) -> Option<&Class> {
        let ClassId(n) = id;
        self.classes.get(n)
    }

    pub fn classes(&self) -> &Vec<Class> {
        &self.classes
    }

    pub fn find_class(&self, name: &str) -> Option<&Class> {
        self.classes.iter().find(|x| x.name() == name)
    }

    pub fn classes_ordered(&self) -> Vec<ClassId> {
        let mut all = self.classes.iter().map(|c| c.id()).collect::<VecDeque<_>>();
        let mut v = Vec::new();
        while !all.is_empty() {
            let id = all.pop_front().unwrap();
            let c = self.get_class(id).unwrap();
            if let Some(super_id) = c.super_class() {
                if v.contains(&super_id) {
                    v.push(id);
                } else {
                    all.push_back(id);
                }
            } else {
                v.push(id);
            }
        }
        v
    }

    //---------- Variable ----------

    pub fn add_variable(&mut self, mut variable: Variable) -> VariableId {
        let id = VariableId(self.variables.len());
        variable.set_id(id);
        self.variables.push(variable);
        id
    }

    pub fn get_variable(&self, id: VariableId) -> Option<&Variable> {
        let VariableId(n) = id;
        self.variables.get(n)
    }

    pub fn variables(&self) -> &Vec<Variable> {
        &self.variables
    }

    pub fn find_variable(&self, name: &str) -> Option<&Variable> {
        self.variables.iter().find(|x| x.name() == name)
    }

    //---------- Function ----------

    pub fn add_function(&mut self, mut function: Function) -> FunctionId {
        let id = FunctionId(self.functions.len());
        function.set_id(id);
        self.functions.push(function);
        id
    }

    pub fn get_function(&self, id: FunctionId) -> Option<&Function> {
        let FunctionId(n) = id;
        self.functions.get(n)
    }

    pub fn functions(&self) -> &Vec<Function> {
        &self.functions
    }

    pub fn find_function(&self, name: &str) -> Option<&Function> {
        self.functions.iter().find(|x| x.name() == name)
    }

    //---------- Constraint ----------

    pub fn add_constraint(&mut self, mut constraint: Constraint) -> ConstraintId {
        let id = ConstraintId(self.constraints.len());
        constraint.set_id(id);
        self.constraints.push(constraint);
        id
    }

    pub fn get_constraint(&self, id: ConstraintId) -> Option<&Constraint> {
        let ConstraintId(n) = id;
        self.constraints.get(n)
    }

    pub fn constraints(&self) -> &Vec<Constraint> {
        &self.constraints
    }

    //---------- Search ----------

    pub fn search(&self) -> &Search {
        &self.search
    }

    pub fn set_search(&mut self, search: Search) {
        self.search = search
    }

    //---------- Entry ----------

    pub fn type_entries(&self) -> TypeEntries {
        let mut v = vec![];
        v.extend(
            self.structures
                .iter()
                .map(|x| TypeEntry::from_id(self, x.id())),
        );
        v.extend(
            self.classes
                .iter()
                .map(|x| TypeEntry::from_id(self, x.id())),
        );
        TypeEntries::new(v)
    }

    pub fn entries(&self) -> Entries {
        let mut v = vec![];
        v.extend(self.instances.iter().map(|x| Entry::from_id(self, x.id())));
        v.extend(self.variables.iter().map(|x| Entry::from_id(self, x.id())));
        Entries::new(v)
    }

    //---------- Duplicate ----------

    pub fn naming(&self) -> Vec<Naming> {
        let mut v = vec![];
        v.extend(self.structures.iter().map(|x| x.naming()));
        v.extend(self.classes.iter().map(|x| x.naming()));
        v.extend(self.instances.iter().map(|x| x.naming()));
        v.extend(self.variables.iter().map(|x| x.naming()));
        v.extend(self.functions.iter().map(|x| x.naming()));
        v.extend(self.constraints.iter().map(|x| x.naming()));
        v
    }

    pub fn duplicate(&self) -> Result<(), Error> {
        check_duplicate(self.naming())?;
        for x in self.structures.iter() {
            x.duplicate()?;
        }
        for x in self.classes.iter() {
            x.duplicate(self)?;
        }
        for x in self.functions.iter() {
            x.duplicate()?;
        }
        Ok(())
    }

    //---------- Resolve ----------

    pub fn resolve_type(&mut self) -> Result<(), Error> {
        let entries = self.type_entries();
        // Types
        for x in self.structures.iter_mut() {
            x.resolve_type(&entries)?;
        }
        for x in self.classes.iter_mut() {
            x.resolve_type(&entries)?;
        }
        for x in self.instances.iter_mut() {
            x.resolve_type(&entries)?;
        }
        for x in self.variables.iter_mut() {
            x.resolve_type(&entries)?;
        }
        for x in self.functions.iter_mut() {
            x.resolve_type(&entries)?;
        }
        // Expr
        for x in self.structures.iter_mut() {
            x.resolve_type_expr(&entries)?;
        }
        for x in self.classes.iter_mut() {
            x.resolve_type_expr(&entries)?;
        }
        for x in self.variables.iter_mut() {
            x.resolve_type_expr(&entries)?;
        }
        for x in self.functions.iter_mut() {
            x.resolve_type_expr(&entries)?;
        }
        for x in self.constraints.iter_mut() {
            x.resolve_type_expr(&entries)?;
        }
        // Optimize
        let s = self.search.resolve_type_expr(&entries)?;
        self.search = s;
        //
        Ok(())
    }

    pub fn resolve_expr(&mut self) -> Result<(), Error> {
        let entries = self.entries();
        // Structure
        let mut structures = Vec::new();
        for x in self.structures.iter() {
            let s = x.resolve_expr(self, &entries)?;
            structures.push(s);
        }
        self.structures = structures;
        // Class
        let mut classes = Vec::new();
        for x in self.classes.iter() {
            let c = x.resolve_expr(self, &entries)?;
            classes.push(c);
        }
        self.classes = classes;
        // Variables
        let mut variables = Vec::new();
        for x in self.variables.iter() {
            let v = x.resolve_expr(self, &entries)?;
            variables.push(v);
        }
        self.variables = variables;
        // Function
        let mut functions = Vec::new();
        for x in self.functions.iter() {
            let f = x.resolve_expr(self, &entries)?;
            functions.push(f);
        }
        self.functions = functions;
        // Constraint
        let mut constraints = Vec::new();
        for x in self.constraints.iter() {
            let c = x.resolve_expr(self, &entries)?;
            constraints.push(c);
        }
        self.constraints = constraints;
        // Search
        self.search = self.search.resolve_expr(self, &entries)?;
        //
        Ok(())
    }

    //---------- Interval ----------

    pub fn check_interval(&self) -> Result<(), Error> {
        for x in self.structures.iter() {
            x.check_interval(self)?;
        }
        for x in self.classes.iter() {
            x.check_interval(self)?;
        }
        for x in self.variables.iter() {
            x.check_interval(self)?;
        }
        for x in self.functions.iter() {
            x.check_interval(self)?;
        }
        Ok(())
    }

    //---------- Parameter Size ----------

    pub fn check_parameter_size(&self) -> Result<(), Error> {
        for x in self.structures.iter() {
            x.check_parameter_size(self)?;
        }
        for x in self.classes.iter() {
            x.check_parameter_size(self)?;
        }
        for x in self.variables.iter() {
            x.check_parameter_size(self)?;
        }
        for x in self.functions.iter() {
            x.check_parameter_size(self)?;
        }
        for x in self.constraints.iter() {
            x.check_parameter_size(self)?;
        }
        self.search.check_parameter_size(self)?;
        Ok(())
    }

    //---------- Bounded ----------

    pub fn check_bounded(&self) -> Result<(), Error> {
        for x in self.structures.iter() {
            x.check_bounded(self)?;
        }
        for x in self.classes.iter() {
            x.check_bounded(self)?;
        }
        for x in self.functions.iter() {
            x.check_bounded(self)?;
        }
        Ok(())
    }

    //---------- Typing ----------

    pub fn check_type(&self) -> Result<(), Error> {
        for x in self.structures.iter() {
            x.check_type(self)?;
        }
        for x in self.classes.iter() {
            x.check_type(self)?;
        }
        for x in self.variables.iter() {
            x.check_type(self)?;
        }
        for x in self.functions.iter() {
            x.check_type(self)?;
        }
        for x in self.constraints.iter() {
            x.check_type(self)?;
        }
        self.search.check_type(self)?;
        Ok(())
    }

    //---------- Empty ----------

    pub fn check_empty(&self) -> Result<(), Error> {
        for x in self.structures.iter() {
            if x.is_empty(self) {
                return Err(Error::Empty {
                    name: x.name().to_string(),
                    category: "Structure".to_string(),
                });
            }
        }
        for x in self.classes.iter() {
            if x.is_empty(self) {
                return Err(Error::Empty {
                    name: x.name().to_string(),
                    category: "Class".to_string(),
                });
            }
        }
        Ok(())
    }

    //---------- Cycle ----------

    pub fn check_cycle(&self) -> Result<(), Error> {
        for x in self.classes.iter() {
            x.check_cycle(self)?;
        }
        Ok(())
    }

    //---------- To Entry ----------

    pub fn to_entry(&self) -> d_stuff::Entry {
        d_stuff::Entry::new(
            d_stuff::Status::Info,
            d_stuff::Text::new(
                "Problem",
                termion::style::Bold.to_string(),
                termion::color::Blue.fg_str(),
            ),
            None,
            vec![d_stuff::Message::new(
                None,
                d_stuff::Text::new(
                    format!("{}", self),
                    termion::style::Reset.to_string(),
                    termion::color::White.fg_str(),
                ),
            )],
        )
    }
}

//------------------------- Display -------------------------

impl std::fmt::Display for Problem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for x in self.structures.iter() {
            write!(f, "// {:?}\n", x.id())?;
            write!(f, "{}\n", x.to_lang(self))?;
            let instances = x.instances(self);
            if let Some((first, others)) = instances.split_first() {
                write!(f, "inst {}", self.get(*first).unwrap().name())?;
                for i in others.iter() {
                    write!(f, ", {}", self.get(*i).unwrap().name())?;
                }
                write!(f, ": {}\n", x.name())?;
            }
        }
        for x in self.classes.iter() {
            write!(f, "// {:?}\n", x.id())?;
            write!(f, "{}\n", x.to_lang(self))?;
            let instances = x.instances(self);
            if let Some((first, others)) = instances.split_first() {
                write!(f, "inst {}", self.get(*first).unwrap().name())?;
                for i in others.iter() {
                    write!(f, ", {}", self.get(*i).unwrap().name())?;
                }
                write!(f, ": {}\n", x.name())?;
            }
        }
        for x in self.variables.iter() {
            write!(f, "{}\n", x.to_lang(self))?;
        }
        for x in self.functions.iter() {
            write!(f, "{}\n", x.to_lang(self))?;
        }
        for x in self.constraints.iter() {
            write!(f, "{}\n", x.to_lang(self))?;
        }
        write!(f, "{}\n", self.search.to_lang(self))?;
        Ok(())
    }
}

//------------------------- Get From Id -------------------------

impl GetFromId<StructureId, Structure> for Problem {
    fn get(&self, id: StructureId) -> Option<&Structure> {
        self.get_structure(id)
    }
}
impl GetFromId<AttributeId<StructureId>, Attribute<StructureId>> for Problem {
    fn get(&self, id: AttributeId<StructureId>) -> Option<&Attribute<StructureId>> {
        let AttributeId(structure_id, _) = id;
        if let Some(structure) = self.get(structure_id) {
            structure.get(id)
        } else {
            None
        }
    }
}
impl GetFromId<MethodId<StructureId>, Method<StructureId>> for Problem {
    fn get(&self, id: MethodId<StructureId>) -> Option<&Method<StructureId>> {
        let MethodId(structure_id, _) = id;
        if let Some(structure) = self.get(structure_id) {
            structure.get(id)
        } else {
            None
        }
    }
}

impl GetFromId<ClassId, Class> for Problem {
    fn get(&self, id: ClassId) -> Option<&Class> {
        self.get_class(id)
    }
}
impl GetFromId<AttributeId<ClassId>, Attribute<ClassId>> for Problem {
    fn get(&self, id: AttributeId<ClassId>) -> Option<&Attribute<ClassId>> {
        let AttributeId(class_id, _) = id;
        if let Some(class) = self.get(class_id) {
            class.get(id)
        } else {
            None
        }
    }
}
impl GetFromId<MethodId<ClassId>, Method<ClassId>> for Problem {
    fn get(&self, id: MethodId<ClassId>) -> Option<&Method<ClassId>> {
        let MethodId(class_id, _) = id;
        if let Some(class) = self.get(class_id) {
            class.get(id)
        } else {
            None
        }
    }
}

impl GetFromId<InstanceId, Instance> for Problem {
    fn get(&self, id: InstanceId) -> Option<&Instance> {
        self.get_instance(id)
    }
}

impl GetFromId<VariableId, Variable> for Problem {
    fn get(&self, id: VariableId) -> Option<&Variable> {
        self.get_variable(id)
    }
}

impl GetFromId<FunctionId, Function> for Problem {
    fn get(&self, id: FunctionId) -> Option<&Function> {
        self.get_function(id)
    }
}

impl GetFromId<ConstraintId, Constraint> for Problem {
    fn get(&self, id: ConstraintId) -> Option<&Constraint> {
        self.get_constraint(id)
    }
}