// for every f->g replace f with g public CompoundFormula AddRemoveConditionalEffects(Dictionary<Formula, List<Predicate>> dEffects) { CompoundFormula cfNew = new CompoundFormula(Operator); if (Operator == "and") //bugbug - not updating more complex structures for now { List<Formula> lReplaceKeys = new List<Formula>(); List<Formula> lReplaceNegates = new List<Formula>(); foreach (Formula fOperand in Operands) { if (dEffects.ContainsKey(fOperand)) { lReplaceKeys.Add(fOperand); } } if (lReplaceKeys.Count == 0 && lReplaceNegates.Count == 0) return this; List<Predicate> lEffects = new List<Predicate>(); foreach (Formula fReplace in lReplaceKeys) { lEffects.AddRange(dEffects[fReplace]); } foreach (Formula fOperand in Operands) { if (fOperand is PredicateFormula) { PredicateFormula pf = (PredicateFormula)fOperand; if (!lReplaceNegates.Contains(fOperand)) { if (!lEffects.Contains(pf.Predicate)) { if (!lEffects.Contains(pf.Predicate.Negate())) cfNew.AddOperand(fOperand); } } } else { CompoundFormula cf = (CompoundFormula)fOperand; CompoundFormula cfRevised = cf.AddRemoveConditionalEffects(dEffects); cfNew.AddOperand(cfRevised); } } foreach (Predicate p in lEffects) cfNew.AddOperand(new PredicateFormula(p)); } else { foreach (Formula fOperand in Operands) { if (fOperand is PredicateFormula) { bool bContainsKey = dEffects.ContainsKey(fOperand); Formula fNegate = fOperand.Negate(); bool bContainsNegate = dEffects.ContainsKey(fNegate); if (bContainsKey || bContainsNegate) { CompoundFormula cfAnd = new CompoundFormula("and"); List<Predicate> lEffects = null; if (bContainsKey) lEffects = dEffects[fOperand]; if (bContainsNegate) lEffects = dEffects[fNegate]; if (!lEffects.Contains(((PredicateFormula)fOperand).Predicate.Negate())) cfAnd.AddOperand(fOperand); foreach (Predicate p in lEffects) { cfAnd.AddOperand(new PredicateFormula(p)); } if (bContainsNegate) cfAnd = (CompoundFormula)cfAnd.Negate(); cfNew.AddOperand(cfAnd); } else cfNew.AddOperand(fOperand); } else cfNew.AddOperand(((CompoundFormula)fOperand).AddRemoveConditionalEffects(dEffects)); } } return cfNew; }