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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use crate::core::*;
use hashbrown::HashMap;
use indexmap::IndexSet;
use lazy_static::lazy_static;
use log::info;
use regex::Regex;
use std::fmt::Display;
use std::fmt::Write;
use tokio::time::Instant;
use ustr::Ustr;
#[derive(Clone, Debug)]
pub enum AssertName {
    Config,
    Constraint,
    Attribute,
    Group,
    GroupMember,
    GroupMin,
    GroupMax,
    RootFeature,
}
impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}
impl Display for AssertName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Attribute => write!(f, "attribute value"),
            Self::Constraint => write!(f, "constraint"),
            Self::Config => write!(f, "configuration value"),
            Self::Group => write!(f, "group"),
            Self::GroupMin => write!(f, "lower bound"),
            Self::GroupMax => write!(f, "upper bound"),
            Self::GroupMember => write!(f, "group member"),
            Self::RootFeature => write!(f, "root feature are always required"),
        }
    }
}
#[derive(Clone, Debug)]
pub struct AssertInfo(pub ModuleSymbol, pub AssertName);
#[derive(Clone, Debug)]
pub struct Assert(pub Option<AssertInfo>, pub Expr);
#[derive(Clone, Debug)]
pub enum Expr {
    Bool(bool),
    Real(f64),
    String(String),
    Var(usize),
    //Logic
    And(Vec<Expr>),
    Or(Vec<Expr>),
    Not(Box<Expr>),
    Implies(Vec<Expr>),
    Greater(Vec<Expr>),
    Less(Vec<Expr>),
    Equal(Vec<Expr>),
    AtLeast(usize, Vec<Expr>),
    AtMost(usize, Vec<Expr>),
    //Arithmetic
    Add(Vec<Expr>),
    Sub(Vec<Expr>),
    Mul(Vec<Expr>),
    Div(Vec<Expr>),
    //Integer Arithmetic
    Ceil(Box<Expr>),
    Floor(Box<Expr>),
    //String ops
    Strlen(Box<Expr>),
    StrLess(Vec<Expr>),
    StrLessEq(Vec<Expr>),
    StrConcat(Box<Expr>, Box<Expr>),
    //IfThenElse
    Ite(Box<Expr>, Box<Expr>, Box<Expr>),
}
/// This is a helper module, to use SMT (and in that sense Z3) for every UVL Module
/// A smt-lib module equivalent to some UVL source module.
pub struct SMTModule {
    pub variables: IndexSet<ModuleSymbol>,
    pub asserts: Vec<Assert>,
}
impl SMTModule {
    #[allow(dead_code)]
    pub fn parse_model<'a>(
        &'a self,
        model: &'a str,
    ) -> impl Iterator<Item = (ModuleSymbol, ConfigValue)> + 'a {
        lazy_static! {
            static ref RE: Regex =
                Regex::new(r#"\(\s*define-fun\s+v(\d+)\s+\(\)\s+(Bool|String|Real)\s+(true|false|"[^"]*"|-?[0-9]*\.[0-9]*)\s*\)"#)
                    .unwrap();
        };

        RE.captures_iter(model).map(|i| {
            let idx: usize = i[1].parse().unwrap();
            let var = *self.variables.get_index(idx).unwrap();
            (
                var,
                match &i[2] {
                    "Bool" => ConfigValue::Bool(match &i[3] {
                        "true" => true,
                        _ => false,
                    }),
                    "Real" => ConfigValue::Number(i[3].parse().unwrap()),
                    _ => {
                        let s = &i[3];
                        ConfigValue::String(s[1..s.len() - 1].into())
                    }
                },
            )
        })
    }
    /// extract values from a response string
    pub fn parse_values<'a>(
        &'a self,
        values: &'a str,
        module: &'a Module,
    ) -> impl Iterator<Item = (ModuleSymbol, ConfigValue)> + 'a {
        super::parse::iter_values(self, module, values)
        /*
        //This abomination parses a identifier with a bool, negative number, positive number or a string
        //The problem is that z3 encodes numbers using nested expressions instead of simple floating point
        //values. Hopefully all cases are covered here...
        //TODO replace this with a true eval parser(best use NOM)

        lazy_static! {
            static ref RE: Regex = Regex::new(
                r#"\(\s*v(\d+)\s+(?:(true|false)|\(- ((?:[0-9]*\.)?[0-9]+)\??|((?:[0-9]*\.)?[0-9]+)\??|"([^"]*)")\s*\)"#
            )
            .unwrap();
        };
        //info!("{values}");
        RE.captures_iter(values).map(|i| {
            let idx: usize = i[1].parse().unwrap();
            let var = *self.variables.get_index(idx).unwrap();
            (
                var,
                match (i.get(2), i.get(3), i.get(4), i.get(5)) {
                    (Some(b), _, _, _) => ConfigValue::Bool(match b.as_str() {
                        "true" => true,
                        _ => false,
                    }),
                    (_, Some(num), _, _) => {
                        ConfigValue::Number(-(num.as_str().parse::<f64>().unwrap()))
                    }
                    (_, _, Some(num), _) => ConfigValue::Number(num.as_str().parse().unwrap()),
                    (_, _, _, Some(s)) => ConfigValue::String(s.as_str().into()),
                    _ => unreachable!(),
                },
            )
        })
        */
    }
    pub fn parse_unsat_core<'a>(&'a self, core: &'a str) -> impl Iterator<Item = AssertInfo> + 'a {
        lazy_static! {
            static ref RE: Regex = Regex::new(r"a(\d+)").unwrap();
        };
        RE.captures_iter(core).filter_map(|i| {
            let idx: usize = i[1].parse().unwrap();
            self.asserts[idx].0.clone()
        })
    }
    /// create source to config z3 Solver
    pub fn config_to_source(&self) -> String {
        let out = "(set-option :produce-unsat-cores true)
        (define-fun smooth_div ((x Real) (y Real)) Real(if (not (= y 0.0))(/ x y)0.0))
        (define-fun floor ((x Real)) Int (to_int x))
        (define-fun ceil ((x Real)) Int (ite (= (to_int x) x) (to_int x) (to_int (+ x 1)) ))
        (set-option :smt.core.minimize true)\n"
            .to_string();
        out
    }
    /// create with all Variable the source for the SMTSolver
    pub fn variable_to_source(&self, module: &Module) -> String {
        let mut out = "".to_string();
        for (i, v) in self.variables.iter().enumerate() {
            let ty = module.type_of(*v);
            let _ = writeln!(out, "(declare-const v{i} {ty})");
        }
        out
    }

    /// create the source for an assert, if the assert should be negated the variable neg must be true
    pub fn assert_to_source(
        &self,
        i: usize,
        info: &Option<AssertInfo>,
        expr: &Expr,
        not: bool,
    ) -> String {
        let mut out = "".to_string();
        let _ = write!(out, "(assert");

        if info.is_some() {
            let _ = write!(out, "(! ");
        }
        if not {
            let _ = write!(out, "( not ");
        }
        #[derive(Debug)]
        enum CExpr<'a> {
            Expr(&'a Expr),
            End,
        }
        let mut stack = vec![CExpr::Expr(expr)];
        while let Some(e) = stack.pop() {
            match e {
                CExpr::End => {
                    let _ = write!(out, ")");
                }
                //Head
                CExpr::Expr(e) => {
                    match e {
                        Expr::Bool(b) => {
                            let _ = write!(out, " {b}");
                        }
                        Expr::Real(r) => {
                            let _ = write!(out, " {r:?}");
                        }
                        Expr::String(val) => {
                            let _ = write!(out, " \"{val}\"");
                        }
                        Expr::Var(off) => {
                            let _ = write!(out, " v{off}");
                        }
                        Expr::Add(..) => {
                            let _ = write!(out, "(+");
                        }
                        Expr::Sub(..) => {
                            let _ = write!(out, "(-");
                        }
                        Expr::Mul(..) => {
                            let _ = write!(out, "(*");
                        }
                        Expr::Div(..) => {
                            let _ = write!(out, "(smooth_div");
                        }
                        Expr::Ceil(..) => {
                            let _ = write!(out, "(ceil");
                        }
                        Expr::Floor(..) => {
                            let _ = write!(out, "(floor");
                        }
                        Expr::And(..) => {
                            let _ = write!(out, "(and");
                        }
                        Expr::Or(..) => {
                            let _ = write!(out, "(or");
                        }
                        Expr::Implies(..) => {
                            let _ = write!(out, "(=>");
                        }
                        Expr::Not(..) => {
                            let _ = write!(out, "(not");
                        }
                        Expr::Ite(..) => {
                            let _ = write!(out, "(ite");
                        }
                        Expr::Equal(..) => {
                            let _ = write!(out, "(=");
                        }
                        Expr::Greater(..) => {
                            let _ = write!(out, "(>");
                        }
                        Expr::Less(..) => {
                            let _ = write!(out, "(<");
                        }
                        Expr::Strlen(..) => {
                            let _ = write!(out, "(str.len");
                        }
                        Expr::AtLeast(min, ..) => {
                            let _ = write!(out, "((_ at-least {min})");
                        }
                        Expr::AtMost(max, ..) => {
                            let _ = write!(out, "((_ at-most {max})");
                        }
                        Expr::StrConcat(..) => {
                            let _ = write!(out, "(str.++");
                        }
                        Expr::StrLess(..) => {
                            let _ = write!(out, "(str.<");
                        }
                        Expr::StrLessEq(..) => {
                            let _ = write!(out, "(str.<=");
                        }
                    }
                    //Args
                    match e {
                        Expr::Add(v)
                        | Expr::Sub(v)
                        | Expr::Mul(v)
                        | Expr::Div(v)
                        | Expr::Or(v)
                        | Expr::And(v)
                        | Expr::Implies(v)
                        | Expr::AtLeast(_, v)
                        | Expr::AtMost(_, v)
                        | Expr::Greater(v)
                        | Expr::Less(v)
                        | Expr::StrLess(v)
                        | Expr::StrLessEq(v)
                        | Expr::Equal(v) => {
                            stack.push(CExpr::End);
                            for i in v.iter().rev() {
                                stack.push(CExpr::Expr(i));
                            }
                        }
                        Expr::Strlen(e) | Expr::Ceil(e) | Expr::Floor(e) | Expr::Not(e) => {
                            stack.push(CExpr::End);
                            stack.push(CExpr::Expr(e));
                        }
                        Expr::StrConcat(rhs, lhs) => {
                            stack.push(CExpr::End);
                            stack.push(CExpr::Expr(rhs));
                            stack.push(CExpr::Expr(lhs));
                        }
                        Expr::Ite(cond, lhs, rhs) => {
                            stack.push(CExpr::End);
                            stack.push(CExpr::Expr(rhs));
                            stack.push(CExpr::Expr(lhs));
                            stack.push(CExpr::Expr(cond));
                        }
                        Expr::Bool(..) | Expr::String(..) | Expr::Real(..) | Expr::Var(..) => {}
                    }
                }
            }
        }
        if not {
            let _ = write!(out, " )");
        }
        //name tag
        if info.is_some() {
            let _ = write!(out, " :named a{i})");
        }

        let _ = write!(out, ")\n");
        out
    }

    /// tree to source
    pub fn to_source(&self, module: &Module) -> String {
        let time = Instant::now();
        //
        let mut out = self.config_to_source();
        let _ = writeln!(out, "{}", self.variable_to_source(module));
        for (i, Assert(info, expr)) in self.asserts.iter().enumerate() {
            let _ = writeln!(out, "{}", self.assert_to_source(i, info, expr, false));
        }
        info!("model to string  in {:?}", time.elapsed());
        out
    }
    pub fn var(&self, ms: ModuleSymbol) -> usize {
        self.variables.get_index_of(&ms).unwrap()
    }
    pub fn pseudo_bool(&self, ms: ModuleSymbol, module: &Module) -> String {
        let ms = module.resolve_value(ms);
        match module.type_of(ms) {
            Type::Bool => format!("v{}", self.var(ms)),
            Type::Real => format!("(not(= v{} 0.0))", self.var(ms)),
            Type::String => format!(r#"(not(= v{} ""))"#, self.var(ms)),
            _ => unimplemented!(),
        }
    }
}
struct SMTBuilder<'a> {
    /// Each variable is encoded as v{n} where n is an index into sym2var using an IndexSet
    /// enables us to lookup a variable both by index and ModuleSymbol
    sym2var: IndexSet<ModuleSymbol>,
    assert: Vec<Assert>,
    module: &'a Module,
}
impl<'a> SMTBuilder<'a> {
    //Variable to index
    fn var(&self, ms: ModuleSymbol) -> Expr {
        Expr::Var(
            self.sym2var
                .get_index_of(&self.module.resolve_value(ms))
                .unwrap_or(0),
        )
    }
    //The language allows non boolean variables as attribute and feature parents
    //So we treat them as boolean expressions in those contexts
    fn pseudo_bool(&self, ms: ModuleSymbol) -> Expr {
        let ms = self.module.resolve_value(ms);
        match self.module.type_of(ms) {
            Type::Bool => self.var(ms),
            Type::Real => Expr::Not(Expr::Equal(vec![self.var(ms), Expr::Real(0.0)]).into()),
            Type::String => {
                Expr::Not(Expr::Equal(vec![self.var(ms), Expr::String("".into())]).into())
            }
            _ => unimplemented!(),
        }
    }
    fn clause(&self, g: ModuleSymbol) -> Vec<Expr> {
        let file = self
            .module
            .instances()
            .filter(|(id, _)| id == &g.instance)
            .map(|(_, f)| f)
            .collect::<Vec<&AstDocument>>()[0];

        let mut cardinalitys: HashMap<Ustr, (Cardinality, Vec<Symbol>)> = HashMap::from([]);
        // Cardinality
        let card_expressions = self
            .module
            .file(g.instance)
            .direct_children(g.sym)
            .filter(|i| {
                if let Symbol::Feature(index) = i {
                    let feature = file.get_feature(index.clone()).unwrap();
                    match feature.cardinality {
                        Some(Cardinality::Range(_, _)) => {
                            if !cardinalitys.contains_key(&feature.name.name) {
                                cardinalitys.insert(
                                    feature.name.name,
                                    (
                                        feature.clone().cardinality.unwrap(),
                                        vec![Symbol::Feature(index.clone())],
                                    ),
                                );
                            } else {
                                cardinalitys
                                    .get_mut(&feature.name.name)
                                    .unwrap()
                                    .1
                                    .push(Symbol::Feature(index.clone()));
                            }
                            return true;
                        }
                        _ => return false,
                    }
                }
                return false;
            })
            .collect::<Vec<Symbol>>();

        let mut result = self
            .module
            .file(g.instance)
            .direct_children(g.sym)
            .filter(|s| !card_expressions.contains(s))
            .map(|i| self.pseudo_bool(g.instance.sym(i)))
            .collect::<Vec<Expr>>();

        result.append(
            cardinalitys
                .values()
                .into_iter()
                .map(|(card, syms)| {
                    let list = syms
                        .into_iter()
                        .map(|i| self.pseudo_bool(g.instance.sym(i.clone())))
                        .collect::<Vec<Expr>>();
                    match card {
                        Cardinality::Range(min, max) => Expr::And(vec![
                            Expr::AtLeast(min.clone(), list.clone()),
                            Expr::AtMost(max.clone(), list),
                        ]),
                        _ => panic!(),
                    }
                })
                .collect::<Vec<Expr>>()
                .as_mut(),
        );

        return result;
    }
    fn min_assert(&mut self, min: usize, p_bind: &Expr, g: ModuleSymbol) {
        let clause = self.clause(g);
        self.assert.push(Assert(
            Some(AssertInfo(g, AssertName::GroupMin)),
            Expr::Implies(vec![p_bind.clone(), Expr::AtLeast(min, clause)]),
        ));
    }
    fn max_assert(&mut self, max: usize, p_bind: &Expr, g: ModuleSymbol) {
        let clause = self.clause(g);
        self.assert.push(Assert(
            Some(AssertInfo(g, AssertName::GroupMax)),
            Expr::Implies(vec![p_bind.clone(), Expr::AtMost(max, clause)]),
        ));
    }
    fn push_var(&mut self, ms: ModuleSymbol) -> Expr {
        self.sym2var.insert(ms);
        Expr::Var(self.sym2var.len() - 1)
    }
}
impl Into<Expr> for ConfigValue {
    fn into(self) -> Expr {
        match self {
            Self::Bool(b) => Expr::Bool(b),
            Self::Number(n) => Expr::Real(n),
            Self::String(s) => Expr::String(s),
            Self::Cardinality(_) => Expr::Bool(true),
        }
    }
}
/// Converts a UVL Module into a SMT Module
pub fn uvl2smt(module: &Module, config: &HashMap<ModuleSymbol, ConfigValue>) -> SMTModule {
    assert!(module.ok);
    let mut builder = SMTBuilder {
        module,
        sym2var: IndexSet::new(),
        assert: Vec::new(),
    };
    //encode features
    for (m, file) in module.instances() {
        for f in file.all_features() {
            builder.push_var(m.sym(f));
        }

        for sym_feature in file.all_features() {
            if let Symbol::Feature(id) = sym_feature {
                let feature = file.get_feature(id).unwrap().clone();
                if let Cardinality::Range(min, _) =
                    feature.cardinality.unwrap_or_else(|| Cardinality::Fixed)
                {
                    // Make AtLeast assertion for cardinality feature
                    if feature.first_cardinality_child {
                        let mut list = vec![];
                        let parent = file.parent(sym_feature, false).unwrap();

                        for child in file.direct_children(parent) {
                            if let Symbol::Feature(sibling_id) = child {
                                let sibling = file.get_feature(sibling_id);
                                if sibling.unwrap().name.name.as_str() == feature.name.name.as_str()
                                {
                                    list.push(builder.var(m.sym(Symbol::Feature(sibling_id))));
                                }
                            }
                        }

                        match file.parent(Symbol::Feature(id), false) {
                            Some(Symbol::Group(_)) => {
                                // if parent is group, a cardinality can be atleast 0 or min, since it can not be selected at all.
                                builder.assert.push(Assert(
                                    Some(AssertInfo(m.sym(sym_feature), AssertName::GroupMin)),
                                    Expr::Or(vec![
                                        Expr::AtLeast(min, list.clone()),
                                        Expr::AtMost(0, list),
                                    ]),
                                ));
                            }
                            _ => {
                                builder.assert.push(Assert(
                                    Some(AssertInfo(m.sym(sym_feature), AssertName::GroupMin)),
                                    Expr::AtLeast(min, list),
                                ));
                            }
                        }
                    }
                }
            }
        }
    }
    //set config features
    for (&ms, val) in config
        .iter()
        .filter(|i| matches!(i.0.sym, Symbol::Feature(..)))
    {
        let var = builder.var(ms);
        builder.assert.push(Assert(
            Some(AssertInfo(ms, AssertName::Config)),
            Expr::Equal(vec![var, val.clone().into()]),
        ));
    }
    //encode attributes
    for (m, file) in module.instances() {
        for f in file.all_features() {
            file.visit_named_children(f, true, |a, _| {
                if !matches!(a, Symbol::Attribute(..)) {
                    return true;
                }
                let ms = m.sym(a);
                let Some((val, n)) = config
                    .get(&ms)
                    .map(|v| (v.clone().into(), AssertName::Config))
                    .or_else(|| {
                        file.value(a)
                            .and_then(|v| match v {
                                ast::Value::Bool(x) => Some(Expr::Bool(*x)),
                                ast::Value::Number(x) => Some(Expr::Real(*x)),
                                ast::Value::String(x) => Some(Expr::String(x.clone())),
                                _ => None,
                            })
                            .map(|v| (v, AssertName::Attribute))
                    })
                else {
                    return true;
                };
                let zero = match val {
                    Expr::Bool(..) => Expr::Bool(false),
                    Expr::Real(..) => Expr::Real(0.0),
                    Expr::String(..) => Expr::String("".into()),
                    _ => unreachable!(),
                };
                let attrib_var = builder.push_var(ms);
                let feat_var = builder.pseudo_bool(m.sym(f));
                builder.assert.push(Assert(
                    Some(AssertInfo(ms, n)),
                    Expr::Equal(vec![
                        Expr::Ite(feat_var.into(), val.into(), zero.into()),
                        attrib_var,
                    ]),
                ));
                true
            });
        }
    }
    //encode groups
    for (m, file) in module.instances() {
        for p in file.all_features() {
            for g in file
                .direct_children(p)
                .filter(|sym| matches!(sym, Symbol::Group(..)))
            {
                if file.direct_children(g).next().is_none() {
                    continue;
                }
                let p_bind = builder.pseudo_bool(m.sym(p));
                for c in file.direct_children(g) {
                    let c_bind = builder.pseudo_bool(m.sym(c));
                    builder.assert.push(Assert(
                        None,
                        Expr::Implies(vec![c_bind.into(), p_bind.clone().into()]),
                    ));
                }
                match file.group_mode(g).unwrap() {
                    GroupMode::Or => {
                        let clause = builder.clause(m.sym(g));
                        builder.assert.push(Assert(
                            Some(AssertInfo(m.sym(g), AssertName::Group)),
                            Expr::Implies(vec![p_bind.into(), Expr::Or(clause).into()]),
                        ));
                    }
                    GroupMode::Alternative => {
                        builder.min_assert(1, &p_bind, m.sym(g));
                        builder.max_assert(1, &p_bind, m.sym(g));
                    }
                    GroupMode::Mandatory => {
                        builder.clause(m.sym(g)).into_iter().for_each(|expr| {
                            info!("expr {:?}", expr);
                            builder.assert.push(Assert(
                                Some(AssertInfo(m.sym(p), AssertName::GroupMember)),
                                Expr::Equal(vec![expr.into(), p_bind.clone().into()]),
                            ))
                        });
                    }
                    GroupMode::Optional | GroupMode::Cardinality(Cardinality::Fixed) => {}
                    GroupMode::Cardinality(Cardinality::Range(min, max)) => {
                        builder.min_assert(min, &p_bind, m.sym(g));
                        builder.max_assert(max, &p_bind, m.sym(g));
                    }
                }
            }
        }
    }
    //assert root features are always on
    for f in module
        .file(InstanceID(0))
        .direct_children(Symbol::Root)
        .filter(|f| matches!(f, Symbol::Feature(..)))
    {
        builder.assert.push(Assert(
            Some(AssertInfo(InstanceID(0).sym(f), AssertName::RootFeature)),
            builder.pseudo_bool(InstanceID(0).sym(f)),
        ));
    }
    //encode constraints
    for (m, file) in module.instances() {
        for c in file.all_constraints() {
            let expr = translate_constraint(file.constraint(c).unwrap(), m, &mut builder, file);
            builder.assert.push(Assert(
                Some(AssertInfo(m.sym(c), AssertName::Constraint)),
                expr,
            ));
        }
    }

    SMTModule {
        variables: builder.sym2var,
        asserts: builder.assert,
    }
}

/// create an SMTModule, but the asserts are only constraints
pub fn uvl2smt_constraints(module: &Module) -> SMTModule {
    assert!(module.ok);
    let mut builder = SMTBuilder {
        module,
        sym2var: IndexSet::new(),
        assert: Vec::new(),
    };
    //encode features
    for (m, file) in module.instances() {
        for f in file.all_features() {
            builder.push_var(m.sym(f));
        }
    }
    //encode attributes
    for (m, file) in module.instances() {
        for f in file.all_features() {
            file.visit_named_children(f, true, |a, _| {
                if !matches!(a, Symbol::Attribute(..)) {
                    return true;
                }
                let ms = m.sym(a);
                builder.push_var(ms);
                true
            });
        }
    }
    //encode constraints
    for (m, file) in module.instances() {
        for c in file.all_constraints() {
            let expr = translate_constraint(file.constraint(c).unwrap(), m, &mut builder, file);
            builder.assert.push(Assert(
                Some(AssertInfo(m.sym(c), AssertName::Constraint)),
                expr,
            ));
        }
    }
    SMTModule {
        variables: builder.sym2var,
        asserts: builder.assert,
    }
}

/// Translates the constraints into Expressions which can be converted to Z3 Statements
fn translate_constraint(
    decl: &ast::ConstraintDecl,
    m: InstanceID,
    builder: &mut SMTBuilder,
    ast: &AstDocument,
) -> Expr {
    match &decl.content {
        ast::Constraint::Ref(sym) => {
            let module_symbol: ModuleSymbol = builder.module.resolve_value(m.sym(*sym));
            let resolved_ast = builder.module.file(module_symbol.instance);
            let all_of = resolved_ast
                .find_all_of(resolved_ast.name(module_symbol.sym).unwrap())
                .into_iter()
                .map(|feature| builder.var(module_symbol.instance.sym(feature)))
                .collect::<Vec<Expr>>();
            return Expr::Or(all_of);
        }
        ast::Constraint::Not(lhs) => stacker::maybe_grow(32 * 1024, 1024 * 1024, || {
            Expr::Not(translate_constraint(lhs, m, builder, ast).into())
        }),
        ast::Constraint::Constant(b) => Expr::Bool(*b),
        ast::Constraint::Logic { op, lhs, rhs } => {
            let lhs = stacker::maybe_grow(32 * 1024, 1024 * 1024, || {
                translate_constraint(lhs, m, builder, ast)
            });
            let rhs = translate_constraint(rhs, m, builder, ast);
            match op {
                ast::LogicOP::Or => Expr::Or(vec![lhs, rhs]),
                ast::LogicOP::And => Expr::And(vec![lhs, rhs]),
                ast::LogicOP::Equiv => Expr::Equal(vec![lhs, rhs]),
                ast::LogicOP::Implies => Expr::Implies(vec![lhs, rhs]),
            }
        }
        ast::Constraint::Equation { op, lhs, rhs } => {
            let (lhs, lty) =
                stacker::maybe_grow(32 * 1024, 1024 * 1024, || translate_expr(lhs, m, builder));
            let (rhs, rty) = translate_expr(rhs, m, builder);
            debug_assert!(rty == lty);
            if lty == Type::String {
                match op {
                    ast::EquationOP::Equal => Expr::Equal(vec![lhs, rhs]),
                    ast::EquationOP::Greater => Expr::StrLess(vec![lhs, rhs]),
                    ast::EquationOP::Smaller => Expr::Not(Expr::StrLessEq(vec![lhs, rhs]).into()),
                }
            } else {
                match op {
                    ast::EquationOP::Equal => Expr::Equal(vec![lhs, rhs]),
                    ast::EquationOP::Greater => Expr::Greater(vec![lhs, rhs]),
                    ast::EquationOP::Smaller => Expr::Less(vec![lhs, rhs]),
                }
            }
        }
    }
}
/// Translates expr into Expressions which can be converted to Z3 Statements
fn translate_expr(decl: &ast::ExprDecl, m: InstanceID, builder: &mut SMTBuilder) -> (Expr, Type) {
    match &decl.content {
        ast::Expr::Number(n) => (Expr::Real(*n), Type::Real),
        ast::Expr::String(s) => (Expr::String(s.clone()), Type::String),
        ast::Expr::Ref(sym) => (
            builder.var(m.sym(*sym)),
            builder.module.type_of(m.sym(*sym)),
        ),
        ast::Expr::Len(lhs) => (
            Expr::Strlen(translate_expr(lhs, m, builder).0.into()),
            Type::Real,
        ),
        ast::Expr::Binary { lhs, rhs, op } => {
            let (lhs, lty) =
                stacker::maybe_grow(32 * 1024, 1024 * 1024, || translate_expr(lhs, m, builder));
            let (rhs, rty) = translate_expr(rhs, m, builder);
            debug_assert!(rty == lty);
            if rty == Type::String {
                debug_assert!(*op == NumericOP::Add);
                (Expr::StrConcat(rhs.into(), lhs.into()), Type::String)
            } else {
                let expr = match op {
                    ast::NumericOP::Add => Expr::Add(vec![lhs, rhs]),
                    ast::NumericOP::Sub => Expr::Sub(vec![lhs, rhs]),
                    ast::NumericOP::Mul => Expr::Mul(vec![lhs, rhs]),
                    ast::NumericOP::Div => Expr::Div(vec![lhs, rhs]),
                };
                (expr, Type::Real)
            }
        }
        ast::Expr::Aggregate { op, context, query } => {
            let mut all_attributes = Vec::new();
            let mut count_features = Vec::new();
            let tgt = context
                .map(|sym| builder.module.resolve_value(m.sym(sym)))
                .unwrap_or(m.sym(Symbol::Root));
            let tgt_file = builder.module.file(tgt.instance);
            tgt_file.visit_attributes(tgt.sym, |feature, attrib, prefix| {
                if prefix == query.names.as_slice()
                    && tgt_file.type_of(attrib).unwrap() == Type::Real
                {
                    count_features.push(Expr::Ite(
                        builder.pseudo_bool(tgt.instance.sym(feature)).into(),
                        Expr::Real(1.0).into(),
                        Expr::Real(0.0).into(),
                    ));
                    all_attributes.push(builder.var(tgt.instance.sym(attrib)));
                }
            });
            if all_attributes.is_empty() {
                (Expr::Real(0.0), Type::Real)
            } else {
                (
                    match op {
                        ast::AggregateOP::Sum => Expr::Add(all_attributes),
                        ast::AggregateOP::Avg => {
                            Expr::Div(vec![Expr::Add(all_attributes), Expr::Add(count_features)])
                        }
                    },
                    Type::Real,
                )
            }
        }
        ast::Expr::Integer { op, n } => (
            match op {
                ast::IntegerOP::Ceil => Expr::Ceil(translate_expr(n, m, builder).0.into()),
                ast::IntegerOP::Floor => Expr::Floor(translate_expr(n, m, builder).0.into()),
            },
            Type::Real,
        ),
    }
}