internal IEnumerable <PartitonTree <PRED> > GetLeaves() { if (left == null && right == null) { yield return(this); } else if (right == null) { foreach (var leaf in left.GetLeaves()) { yield return(leaf); } } else if (left == null) { foreach (var leaf in right.GetLeaves()) { yield return(leaf); } } else { foreach (var leaf in left.GetLeaves()) { yield return(leaf); } foreach (var leaf in right.GetLeaves()) { yield return(leaf); } } }
internal IEnumerable <PartitonTree <PRED> > GenerateMintermsHelper(PRED[] preds) { var tree = new PartitonTree <PRED>(ba); foreach (var psi in preds) { tree.Refine(psi); } return(tree.GetLeaves()); }
/// <summary> /// Given an array of predidates {p_1, p_2, ..., p_n} where n>=0. /// Enumerate all satisfiable Boolean combinations Tuple({b_1, b_2, ..., b_n}, p) /// where p is satisfiable and equivalent to p'_1 & p'_2 & ... & p'_n, /// where p'_i = p_i if b_i = true and p'_i is Not(p_i) otherwise. /// If n=0 return Tuple({},True). /// </summary> /// <param name="preds">array of predicates</param> /// <param name="useEquivalenceChecking">optimization flag: if true, uses equivalence checking to cluster equivalent predicates; otherwise does not use equivalence checking</param> /// <returns>all minterms of the given predicate sequence</returns> public IEnumerable <Tuple <bool[], PRED> > GenerateMinterms(bool useEquivalenceChecking, params PRED[] preds) { if (preds.Length == 0) { yield return(new Tuple <bool[], PRED>(new bool[] { }, ba.True)); } else { var count = preds.Length; List <PRED> nonequivalentSets = new List <PRED>(); //work only with nonequivalent sets as distinct elements var indexLookup = new Dictionary <int, int>(); var newIndexMap = new Dictionary <EquivClass, int>(); var equivs = new List <List <int> >(); for (int i = 0; i < count; i++) { int newIndex; EquivClass equiv = CreateEquivalenceClass(useEquivalenceChecking, preds[i]); if (!newIndexMap.TryGetValue(equiv, out newIndex)) { newIndex = newIndexMap.Count; newIndexMap[equiv] = newIndex; nonequivalentSets.Add(preds[i]); equivs.Add(new List <int>()); } indexLookup[i] = newIndex; equivs[newIndex].Add(i); } //var pairs = new List<Tuple<IntSet, PRED>>(GenerateMinterms1(nonequivalentSets.ToArray())); //foreach (var pair in pairs) //{ // var characteristic = new bool[preds.Length]; // for (int i = 0; i < count; i++) // if (pair.First.Contains(indexLookup[i])) // characteristic[i] = true; // yield return // new Tuple<bool[], PRED>(characteristic, pair.Second); //} var tree = new PartitonTree <PRED>(ba); foreach (var psi in nonequivalentSets) { tree.Refine(psi); } foreach (var leaf in tree.GetLeaves()) { var characteristic = new bool[preds.Length]; foreach (var k in leaf.GetPath()) { foreach (var n in equivs[k]) { characteristic[n] = true; } } yield return (new Tuple <bool[], PRED>(characteristic, leaf.phi)); } } }