adf_bdd 0.3.1

Library to solve grounded, complete, and stable ADF-semantics by utilising OBDDs - ordered binary decision diagrams
Documentation
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
//! Collection of all nogood-related structures.

use std::{
    borrow::Borrow,
    fmt::{Debug, Display},
    ops::{BitAnd, BitOr, BitXor, BitXorAssign},
};

use crate::datatypes::Term;
use roaring::RoaringBitmap;

/// A [NoGood] and an [Interpretation] can be represented by the same structure.
/// Moreover this duality (i.e. an [Interpretation] becomes a [NoGood] is reflected by this type alias.
pub type Interpretation = NoGood;

/// Representation of a nogood by a pair of [Bitmaps][RoaringBitmap]
#[derive(Debug, Default, Clone)]
pub struct NoGood {
    active: RoaringBitmap,
    value: RoaringBitmap,
}

impl Eq for NoGood {}
impl PartialEq for NoGood {
    fn eq(&self, other: &Self) -> bool {
        self.active
            .borrow()
            .bitxor(other.active.borrow())
            .is_empty()
            && self.value.borrow().bitxor(other.value.borrow()).is_empty()
    }
}

impl NoGood {
    /// Creates an [Interpretation] from a given Vector of [Terms][Term].
    pub fn from_term_vec(term_vec: &[Term]) -> Interpretation {
        let mut result = Self::default();
        term_vec.iter().enumerate().for_each(|(idx, val)| {
	    let idx:u32 = idx.try_into().expect("no-good learner implementation is based on the assumption that only u32::MAX-many variables are in place");
            if val.is_truth_value() {
                result.active.insert(idx);
                if val.is_true() {
                    result.value.insert(idx);
                }
            }
        });
        result
    }

    /// Creates a [NoGood] representing an atomic assignment.
    pub fn new_single_nogood(pos: usize, val: bool) -> NoGood {
        let mut result = Self::default();
        let pos:u32 = pos.try_into().expect("nog-good learner implementation is based on the assumption that only u32::MAX-many variables are in place");
        result.active.insert(pos);
        if val {
            result.value.insert(pos);
        }
        result
    }

    /// Returns [None] if the pair contains inconsistent pairs.
    /// Otherwise it returns an [Interpretation] which represents the set values.
    pub fn try_from_pair_iter(
        pair_iter: &mut impl Iterator<Item = (usize, bool)>,
    ) -> Option<Interpretation> {
        let mut result = Self::default();
        let mut visit = false;
        for (idx, val) in pair_iter {
            visit = true;
            let idx:u32 = idx.try_into().expect("no-good learner implementation is based on the assumption that only u32::MAX-many variables are in place");
            let is_new = result.active.insert(idx);
            let upd = if val {
                result.value.insert(idx)
            } else {
                result.value.remove(idx)
            };
            // if the state is not new and the value is changed
            if !is_new && upd {
                return None;
            }
        }
        visit.then_some(result)
    }

    /// Creates an updated [`Vec<Term>`], based on the given [&[Term]] and the [NoGood].
    /// The parameter _update_ is set to [`true`] if there has been an update and to [`false`] otherwise
    pub fn update_term_vec(&self, term_vec: &[Term], update: &mut bool) -> Vec<Term> {
        *update = false;
        term_vec
            .iter()
            .enumerate()
            .map(|(idx, val)| {
                let idx: u32 = idx.try_into().expect(
                    "no-good learner implementation is based on the assumption \
                     that only u32::MAX-many variables are in place",
                );
                if self.active.contains(idx) {
                    if !val.is_truth_value() {
                        *update = true;
                    }
                    if self.value.contains(idx) {
                        Term::TOP
                    } else {
                        Term::BOT
                    }
                } else {
                    *val
                }
            })
            .collect()
    }

    /// Given a [NoGood] and another one, conclude a non-conflicting value which can be concluded on basis of the given one.
    pub fn conclude(&self, other: &NoGood) -> Option<(usize, bool)> {
        log::debug!("conclude: {:?} other {:?}", self, other);
        let implication = self
            .active
            .borrow()
            .bitxor(other.active.borrow())
            .bitand(self.active.borrow());

        let bothactive = self.active.borrow().bitand(other.active.borrow());
        let mut no_matches = bothactive.borrow().bitand(other.value.borrow());
        no_matches.bitxor_assign(bothactive.bitand(self.value.borrow()));

        if implication.len() == 1 && no_matches.is_empty() {
            let pos = implication
                .min()
                .expect("just checked that there is one element to be found");
            log::trace!(
                "Conclude {:?}",
                Some((pos as usize, !self.value.contains(pos)))
            );
            Some((pos as usize, !self.value.contains(pos)))
        } else {
            log::trace!("Nothing to Conclude");
            None
        }
    }

    /// Updates the [NoGood] and a second one in a disjunctive (bitor) manner.
    pub fn disjunction(&mut self, other: &NoGood) {
        self.active = self.active.borrow().bitor(&other.active);
        self.value = self.value.borrow().bitor(&other.value);
    }

    /// Returns [true] if the other [Interpretation] matches with all the assignments of the current [NoGood].
    pub fn is_violating(&self, other: &Interpretation) -> bool {
        let active = self.active.borrow().bitand(other.active.borrow());
        if self.active.len() == active.len() {
            let lhs = active.borrow().bitand(self.value.borrow());
            let rhs = active.borrow().bitand(other.value.borrow());
            if lhs.bitxor(rhs).is_empty() {
                return true;
            }
        }
        false
    }

    /// Returns the number of set (i.e. active) bits.
    pub fn len(&self) -> usize {
        self.active
            .len()
            .try_into()
            .expect("expecting to be on a 64 bit system")
    }

    #[must_use]
    /// Returns [true] if the [NoGood] does not set any value.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl From<&[Term]> for NoGood {
    fn from(term_vec: &[Term]) -> Self {
        Self::from_term_vec(term_vec)
    }
}

/// A structure to store [NoGoods][NoGood] and offer operations and deductions based on them.
#[derive(Debug)]
pub struct NoGoodStore {
    store: Vec<Vec<NoGood>>,
    duplicates: DuplicateElemination,
}

impl Display for NoGoodStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "NoGoodStats: [")?;
        for (arity, vec) in self.store.iter().enumerate() {
            writeln!(f, "{arity}: {}", vec.len())?;
            log::debug!("Nogoods:\n {:?}", vec);
        }
        write!(f, "]")
    }
}

impl NoGoodStore {
    /// Creates a new [NoGoodStore] and assumes a size compatible with the underlying [NoGood] implementation.
    pub fn new(size: u32) -> NoGoodStore {
        Self {
            store: vec![Vec::new(); size as usize],
            duplicates: DuplicateElemination::Equiv,
        }
    }

    /// Tries to create a new [NoGoodStore].
    /// Does not succeed if the size is too big for the underlying [NoGood] implementation.
    pub fn try_new(size: usize) -> Option<NoGoodStore> {
        Some(Self::new(size.try_into().ok()?))
    }

    /// Sets the behaviour when managing duplicates.
    pub fn set_dup_elem(&mut self, mode: DuplicateElemination) {
        self.duplicates = mode;
    }

    /// Adds a given [NoGood]
    pub fn add_ng(&mut self, nogood: NoGood) {
        let mut idx = nogood.len();
        if idx > 0 {
            idx -= 1;
            if match self.duplicates {
                DuplicateElemination::None => true,
                DuplicateElemination::Equiv => !self.store[idx].contains(&nogood),
                DuplicateElemination::Subsume => {
                    self.store
                        .iter_mut()
                        .enumerate()
                        .for_each(|(cur_idx, ng_vec)| {
                            if idx >= cur_idx {
                                ng_vec.retain(|ng| !ng.is_violating(&nogood));
                            }
                        });
                    true
                }
            } {
                self.store[idx].push(nogood);
            }
        }
    }

    /// Draws a (Conclusion)[NoGood], based on the [NoGoodStore] and the given [NoGood].
    /// *Returns* [None] if there is a conflict
    pub fn conclusions(&self, nogood: &NoGood) -> Option<NoGood> {
        let mut result = nogood.clone();
        log::trace!("ng-store: {:?}", self.store);
        self.store
            .iter()
            .enumerate()
            .filter(|(len, _vec)| *len <= nogood.len())
            .filter_map(|(_len, val)| {
                NoGood::try_from_pair_iter(&mut val.iter().filter_map(|ng| ng.conclude(nogood)))
            })
            .try_fold(&mut result, |acc, ng| {
                if ng.is_violating(acc) {
                    log::trace!("ng conclusion violating");
                    None
                } else {
                    acc.disjunction(&ng);
                    Some(acc)
                }
            })?;
        if self
            .store
            .iter()
            .enumerate()
            .filter(|(len, _vec)| *len <= nogood.len())
            .any(|(_, vec)| {
                vec.iter()
                    .any(|elem| elem.is_violating(&result) || elem.is_violating(nogood))
            })
        {
            return None;
        }
        Some(result)
    }

    /// Constructs the Closure of the conclusions drawn by the nogoods with respect to the given `interpretation`
    pub(crate) fn conclusion_closure(&self, interpretation: &[Term]) -> ClosureResult {
        let mut update = true;
        let mut result = match self.conclusions(&interpretation.into()) {
            Some(val) => {
                log::trace!(
                    "conclusion-closure step 1: val:{:?} -> {:?}",
                    val,
                    val.update_term_vec(interpretation, &mut update)
                );
                val.update_term_vec(interpretation, &mut update)
            }

            None => return ClosureResult::Inconsistent,
        };
        if !update {
            return ClosureResult::NoUpdate;
        }
        while update {
            match self.conclusions(&result.as_slice().into()) {
                Some(val) => result = val.update_term_vec(&result, &mut update),
                None => return ClosureResult::Inconsistent,
            }
        }
        ClosureResult::Update(result)
    }
}

/// Allows to define how costly the DuplicateElemination is done.
#[derive(Debug, Copy, Clone)]
pub enum DuplicateElemination {
    /// No Duplicate Detection
    None,
    /// Only check weak equivalence
    Equiv,
    /// Check for subsumptions
    Subsume,
}

/// If the closure had some issues, it is represented with this enum
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ClosureResult {
    Update(Vec<Term>),
    NoUpdate,
    Inconsistent,
}

impl ClosureResult {
    /// Dead_code due to (currently) unused utility function for the [ClosureResult] enum.
    #[allow(dead_code)]
    pub fn is_update(&self) -> bool {
        matches!(self, Self::Update(_))
    }
    /// Dead_code due to (currently) unused utility function for the [ClosureResult] enum.
    #[allow(dead_code)]
    pub fn is_no_update(&self) -> bool {
        matches!(self, Self::NoUpdate)
    }
    /// Dead_code due to (currently) unused utility function for the [ClosureResult] enum.
    #[allow(dead_code)]
    pub fn is_inconsistent(&self) -> bool {
        matches!(self, Self::Inconsistent)
    }
}

impl TryInto<Vec<Term>> for ClosureResult {
    type Error = &'static str;

    fn try_into(self) -> Result<Vec<Term>, Self::Error> {
        match self {
            ClosureResult::Update(val) => Ok(val),
            ClosureResult::NoUpdate => Err("No update occurred, use the old value instead"),
            ClosureResult::Inconsistent => Err("Inconsistency occurred"),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use test_log::test;

    #[test]
    fn create_ng() {
        let terms = vec![Term::TOP, Term(22), Term(13232), Term::BOT, Term::TOP];
        let ng = NoGood::from_term_vec(&terms);

        assert_eq!(ng.active.len(), 3);
        assert_eq!(ng.value.len(), 2);
        assert!(ng.active.contains(0));
        assert!(!ng.active.contains(1));
        assert!(!ng.active.contains(2));
        assert!(ng.active.contains(3));
        assert!(ng.active.contains(4));

        assert!(ng.value.contains(0));
        assert!(!ng.value.contains(1));
        assert!(!ng.value.contains(2));
        assert!(!ng.value.contains(3));
        assert!(ng.value.contains(4));
    }

    #[test]
    fn conclude() {
        let ng1 = NoGood::from_term_vec(&[Term::TOP, Term(22), Term::TOP, Term::BOT, Term::TOP]);
        let ng2 = NoGood::from_term_vec(&[Term::TOP, Term(22), Term(13232), Term::BOT, Term::TOP]);
        let ng3 = NoGood::from_term_vec(&[
            Term::TOP,
            Term(22),
            Term(13232),
            Term::BOT,
            Term::TOP,
            Term::BOT,
        ]);

        assert_eq!(ng1.conclude(&ng2), Some((2, false)));
        assert_eq!(ng1.conclude(&ng1), None);
        assert_eq!(ng2.conclude(&ng1), None);
        assert_eq!(ng1.conclude(&ng3), Some((2, false)));
        assert_eq!(ng3.conclude(&ng1), Some((5, true)));
        assert_eq!(ng3.conclude(&ng2), Some((5, true)));

        // conclusions on empty knowledge
        let ng4 = NoGood::from_term_vec(&[Term::TOP]);
        let ng5 = NoGood::from_term_vec(&[Term::BOT]);
        let ng6 = NoGood::from_term_vec(&[]);

        assert_eq!(ng4.conclude(&ng6), Some((0, false)));
        assert_eq!(ng5.conclude(&ng6), Some((0, true)));
        assert_eq!(ng6.conclude(&ng5), None);
        assert_eq!(ng4.conclude(&ng5), None);

        let ng_a = NoGood::from_term_vec(&[Term::BOT, Term(22)]);
        let ng_b = NoGood::from_term_vec(&[Term(22), Term::TOP]);

        assert_eq!(ng_a.conclude(&ng_b), Some((0, true)));
    }

    #[test]
    fn violate() {
        let ng1 = NoGood::from_term_vec(&[Term::TOP, Term(22), Term::TOP, Term::BOT, Term::TOP]);
        let ng2 = NoGood::from_term_vec(&[Term::TOP, Term(22), Term(13232), Term::BOT, Term::TOP]);
        let ng3 = NoGood::from_term_vec(&[
            Term::TOP,
            Term(22),
            Term(13232),
            Term::BOT,
            Term::TOP,
            Term::BOT,
        ]);
        let ng4 = NoGood::from_term_vec(&[Term::TOP]);

        assert!(ng4.is_violating(&ng1));
        assert!(!ng1.is_violating(&ng4));
        assert!(ng2.is_violating(&ng3));
        assert!(!ng3.is_violating(&ng2));

        assert_eq!(ng4, NoGood::new_single_nogood(0, true));
    }

    #[test]
    fn add_ng() {
        let mut ngs = NoGoodStore::new(5);
        let ng1 = NoGood::from_term_vec(&[Term::TOP]);
        let ng2 = NoGood::from_term_vec(&[Term(22), Term::TOP]);
        let ng3 = NoGood::from_term_vec(&[Term(22), Term(22), Term::TOP]);
        let ng4 = NoGood::from_term_vec(&[Term(22), Term(22), Term(22), Term::TOP]);
        let ng5 = NoGood::from_term_vec(&[Term::BOT]);

        assert!(!ng1.is_violating(&ng5));
        assert!(ng1.is_violating(&ng1));

        ngs.add_ng(ng1.clone());
        ngs.add_ng(ng2.clone());
        ngs.add_ng(ng3.clone());
        ngs.add_ng(ng4.clone());
        ngs.add_ng(ng5.clone());

        assert_eq!(
            ngs.store
                .iter()
                .fold(0, |acc, ng_vec| { acc + ng_vec.len() }),
            5
        );

        ngs.set_dup_elem(DuplicateElemination::Equiv);

        ngs.add_ng(ng1.clone());
        ngs.add_ng(ng2.clone());
        ngs.add_ng(ng3.clone());
        ngs.add_ng(ng4.clone());
        ngs.add_ng(ng5.clone());

        assert_eq!(
            ngs.store
                .iter()
                .fold(0, |acc, ng_vec| { acc + ng_vec.len() }),
            5
        );
        ngs.set_dup_elem(DuplicateElemination::Subsume);
        ngs.add_ng(ng1);
        ngs.add_ng(ng2);
        ngs.add_ng(ng3);
        ngs.add_ng(ng4);
        ngs.add_ng(ng5);

        assert_eq!(
            ngs.store
                .iter()
                .fold(0, |acc, ng_vec| { acc + ng_vec.len() }),
            5
        );

        ngs.add_ng(NoGood::from_term_vec(&[Term(22), Term::BOT, Term(22)]));

        assert_eq!(
            ngs.store
                .iter()
                .fold(0, |acc, ng_vec| { acc + ng_vec.len() }),
            6
        );

        ngs.add_ng(NoGood::from_term_vec(&[Term(22), Term::BOT, Term::BOT]));

        assert_eq!(
            ngs.store
                .iter()
                .fold(0, |acc, ng_vec| { acc + ng_vec.len() }),
            6
        );

        assert!(NoGood::from_term_vec(&[Term(22), Term::BOT, Term(22)])
            .is_violating(&NoGood::from_term_vec(&[Term(22), Term::BOT, Term::BOT])));
    }

    #[test]
    fn ng_store_conclusions() {
        let mut ngs = NoGoodStore::new(5);

        let ng1 = NoGood::from_term_vec(&[Term::BOT]);

        ngs.add_ng(ng1.clone());
        assert_eq!(ng1.conclude(&ng1), None);
        assert_eq!(
            ng1.conclude(&NoGood::from_term_vec(&[Term(33)])),
            Some((0, true))
        );
        assert_eq!(ngs.conclusions(&ng1), None);
        assert_ne!(ngs.conclusions(&NoGood::from_term_vec(&[Term(33)])), None);
        assert_eq!(
            ngs.conclusions(&NoGood::from_term_vec(&[Term(33)]))
                .expect("just checked with prev assertion")
                .update_term_vec(&[Term(33)], &mut false),
            vec![Term::TOP]
        );

        let ng2 = NoGood::from_term_vec(&[Term(123), Term::TOP, Term(234), Term(345)]);
        let ng3 = NoGood::from_term_vec(&[Term::TOP, Term::BOT, Term::TOP, Term(345)]);

        ngs.add_ng(ng2);
        ngs.add_ng(ng3);

        log::debug!("issues start here");
        assert!(ngs
            .conclusions(&NoGood::from_term_vec(&[Term::TOP]))
            .is_some());
        assert_eq!(
            ngs.conclusions(&[Term::TOP].as_slice().into())
                .expect("just checked with prev assertion")
                .update_term_vec(&[Term::TOP, Term(4), Term(5), Term(6), Term(7)], &mut false),
            vec![Term::TOP, Term::BOT, Term(5), Term(6), Term(7)]
        );
        assert!(ngs
            .conclusions(&NoGood::from_term_vec(&[
                Term::TOP,
                Term::BOT,
                Term(5),
                Term(6),
                Term(7)
            ]))
            .is_some());

        ngs = NoGoodStore::new(10);
        ngs.add_ng([Term::BOT].as_slice().into());
        ngs.add_ng(
            [Term::TOP, Term::BOT, Term(33), Term::TOP]
                .as_slice()
                .into(),
        );
        ngs.add_ng(
            [Term::TOP, Term::BOT, Term(33), Term(33), Term::BOT]
                .as_slice()
                .into(),
        );
        ngs.add_ng([Term::TOP, Term::TOP].as_slice().into());

        let interpr: Vec<Term> = vec![
            Term(123),
            Term(233),
            Term(345),
            Term(456),
            Term(567),
            Term(678),
            Term(789),
            Term(899),
            Term(999),
            Term(1000),
        ];
        let concl = ngs.conclusions(&interpr.as_slice().into());
        assert_eq!(concl, Some(NoGood::from_term_vec(&[Term::TOP])));
        let mut update = false;
        let new_interpr = concl
            .expect("just tested in assert")
            .update_term_vec(&interpr, &mut update);
        assert_eq!(
            new_interpr,
            vec![
                Term::TOP,
                Term(233),
                Term(345),
                Term(456),
                Term(567),
                Term(678),
                Term(789),
                Term(899),
                Term(999),
                Term(1000)
            ]
        );
        assert!(update);

        let new_int_2 = ngs
            .conclusions(&new_interpr.as_slice().into())
            .map(|val| val.update_term_vec(&new_interpr, &mut update))
            .expect("Should return a value");
        assert_eq!(
            new_int_2,
            vec![
                Term::TOP,
                Term::BOT,
                Term(345),
                Term(456),
                Term(567),
                Term(678),
                Term(789),
                Term(899),
                Term(999),
                Term(1000)
            ]
        );
        assert!(update);

        let new_int_3 = ngs
            .conclusions(&new_int_2.as_slice().into())
            .map(|val| val.update_term_vec(&new_int_2, &mut update))
            .expect("Should return a value");

        assert_eq!(
            new_int_3,
            vec![
                Term::TOP,
                Term::BOT,
                Term(345),
                Term::BOT,
                Term::TOP,
                Term(678),
                Term(789),
                Term(899),
                Term(999),
                Term(1000)
            ]
        );
        assert!(update);

        let concl4 = ngs.conclusions(&new_int_3.as_slice().into());
        assert_ne!(concl4, None);

        let new_int_4 = ngs
            .conclusions(&new_int_3.as_slice().into())
            .map(|val| val.update_term_vec(&new_int_3, &mut update))
            .expect("Should return a value");

        assert_eq!(
            new_int_4,
            vec![
                Term::TOP,
                Term::BOT,
                Term(345),
                Term::BOT,
                Term::TOP,
                Term(678),
                Term(789),
                Term(899),
                Term(999),
                Term(1000)
            ]
        );
        assert!(!update);

        // inconsistence
        let interpr = vec![
            Term::TOP,
            Term::TOP,
            Term::BOT,
            Term::BOT,
            Term(111),
            Term(678),
            Term(789),
            Term(899),
            Term(999),
            Term(1000),
        ];

        assert_eq!(ngs.conclusions(&interpr.as_slice().into()), None);

        ngs = NoGoodStore::new(6);
        ngs.add_ng(
            [Term(1), Term(1), Term(1), Term(0), Term(0), Term(1)]
                .as_slice()
                .into(),
        );
        ngs.add_ng(
            [Term(1), Term(1), Term(8), Term(0), Term(0), Term(11)]
                .as_slice()
                .into(),
        );
        ngs.add_ng([Term(22), Term(1)].as_slice().into());

        assert_eq!(
            ngs.conclusions(
                &[Term(1), Term(3), Term(3), Term(9), Term(0), Term(1)]
                    .as_slice()
                    .into(),
            ),
            Some(NoGood::from_term_vec(&[
                Term(1),
                Term(0),
                Term(3),
                Term(9),
                Term(0),
                Term(1)
            ]))
        );
    }

    #[test]
    fn conclusion_closure() {
        let mut ngs = NoGoodStore::new(10);
        ngs.add_ng([Term::BOT].as_slice().into());
        ngs.add_ng(
            [Term::TOP, Term::BOT, Term(33), Term::TOP]
                .as_slice()
                .into(),
        );
        ngs.add_ng(
            [Term::TOP, Term::BOT, Term(33), Term(33), Term::BOT]
                .as_slice()
                .into(),
        );
        ngs.add_ng([Term::TOP, Term::TOP].as_slice().into());

        let interpr: Vec<Term> = vec![
            Term(123),
            Term(233),
            Term(345),
            Term(456),
            Term(567),
            Term(678),
            Term(789),
            Term(899),
            Term(999),
            Term(1000),
        ];

        let result = ngs.conclusion_closure(&interpr);
        assert!(result.is_update());
        let resultint: Vec<Term> = result.try_into().expect("just checked conversion");
        assert_eq!(
            resultint,
            vec![
                Term::TOP,
                Term::BOT,
                Term(345),
                Term::BOT,
                Term::TOP,
                Term(678),
                Term(789),
                Term(899),
                Term(999),
                Term(1000)
            ]
        );
        let result_no_upd = ngs.conclusion_closure(&resultint);

        assert!(result_no_upd.is_no_update());
        assert_eq!(
            <ClosureResult as TryInto<Vec<Term>>>::try_into(result_no_upd)
                .expect_err("just checked that it is an error"),
            "No update occurred, use the old value instead"
        );

        let inconsistent_interpr = vec![
            Term::TOP,
            Term::TOP,
            Term::BOT,
            Term::BOT,
            Term(111),
            Term(678),
            Term(789),
            Term(899),
            Term(999),
            Term(1000),
        ];
        let result_inconsistent = ngs.conclusion_closure(&inconsistent_interpr);

        assert!(result_inconsistent.is_inconsistent());
        assert_eq!(
            <ClosureResult as TryInto<Vec<Term>>>::try_into(result_inconsistent)
                .expect_err("just checked that it is an error"),
            "Inconsistency occurred"
        );

        ngs = NoGoodStore::new(6);
        ngs.add_ng(
            [Term(1), Term(1), Term(1), Term(0), Term(0), Term(1)]
                .as_slice()
                .into(),
        );
        ngs.add_ng(
            [Term(1), Term(1), Term(8), Term(0), Term(0), Term(11)]
                .as_slice()
                .into(),
        );
        ngs.add_ng([Term(22), Term(1)].as_slice().into());

        assert_eq!(
            ngs.conclusion_closure(&[Term(1), Term(3), Term(3), Term(9), Term(0), Term(1)]),
            ClosureResult::Update(vec![Term(1), Term(0), Term(3), Term(9), Term(0), Term(1)])
        );
    }
}