public CompoundFormula RemoveNestedConjunction(out bool bChanged) { bChanged = false; CompoundFormula cfAnd = new CompoundFormula("and"); if (Operator == "or") { List<CompoundFormula> lCompoundOperands = new List<CompoundFormula>(); List<PredicateFormula> lPredicateOperands = new List<PredicateFormula>(); foreach (Formula f in Operands) { if (f is CompoundFormula) { CompoundFormula cf = (CompoundFormula)f; cf = cf.RemoveNestedConjunction(out bChanged); lCompoundOperands.Add(cf); } else lPredicateOperands.Add((PredicateFormula)f); } if (lCompoundOperands.Count == 0) { return this; } List<List<Formula>> lAllCombinations = GetAllCombinations(lPredicateOperands, lCompoundOperands); foreach (List<Formula> lCombination in lAllCombinations) { CompoundFormula cfOr = new CompoundFormula("or"); foreach (Formula f in lCombination) cfOr.AddOperand(f); foreach (PredicateFormula f in lPredicateOperands) cfOr.AddOperand(f); if (!cfOr.IsTrue(new List<Predicate>())) cfAnd.AddOperand(cfOr); } /* else if (lCompoundOperands.Count == 1) { CompoundFormula cfNestedAnd = lCompoundOperands[0]; if (cfNestedAnd.Operator != "and") throw new NotImplementedException(); foreach (Formula f in cfNestedAnd.Operands) { CompoundFormula cfOr = new CompoundFormula("or"); cfOr.AddOperand(f); foreach (PredicateFormula pfOrg in lPredicateOperands) cfOr.AddOperand(pfOrg); cfAnd.AddOperand(cfOr); } } else throw new NotImplementedException(); * */ bChanged = true; } else { foreach (Formula f in Operands) { if (f is CompoundFormula) { CompoundFormula cf = (CompoundFormula)f; cf = cf.RemoveNestedConjunction(out bChanged); if (cf.Operands.Count > 0) { if (cf.Operator == "and") foreach (Formula fSub in cf.Operands) cfAnd.AddOperand(fSub); else cfAnd.AddOperand(cf); } } else cfAnd.AddOperand(f); } } return cfAnd; }
private List<Formula> GetAllCombinations(List<Formula> lConverted, int idx) { List<Formula> lCombinations = new List<Formula>(); if (idx == lConverted.Count - 1) { if (lConverted[idx] is CompoundFormula) { CompoundFormula cf = (CompoundFormula)lConverted[idx]; foreach (Formula fSub in cf.Operands) lCombinations.Add(fSub); } else lCombinations.Add(lConverted[idx]); } else { List<Formula> lRec = GetAllCombinations(lConverted, idx + 1); if (lConverted[idx] is CompoundFormula) { CompoundFormula cf1 = (CompoundFormula)lConverted[idx]; foreach (Formula fSub1 in cf1.Operands) { foreach (Formula f2 in lRec) { CompoundFormula cfOr = new CompoundFormula("or"); cfOr.AddOperand(fSub1); cfOr.AddOperand(f2); if(!cfOr.IsTrue(null)) lCombinations.Add(cfOr); } } } else { foreach (Formula f2 in lRec) { CompoundFormula cfOr = new CompoundFormula("or"); cfOr.AddOperand(lConverted[idx]); cfOr.AddOperand(f2); if (!cfOr.IsTrue(null)) lCombinations.Add(cfOr); } } } return lCombinations; }