/// <summary>Check this proposition is negative normal form</summary> /// <returns>True if NNF</returns> public bool IsNNF() { if (!IsVaild()) { return(false); } if (op == Operator.Atom) { return(true); } else if (op == Operator.Imply || op == Operator.IfOnlyIf) { return(false); } else if (op == Operator.Not) { return(left.op == Operator.Atom); } else if (op == Operator.Or || op == Operator.And) { return(left.IsNNF() && right.IsNNF()); } else { return(false); } }
/// <summary>Convert given NNF proposition to CNF proposition</summary> /// <param name="expr">NNF proposition to convert</param> /// <exception cref="PropositionNotSupportException"></exception> /// <returns>CNF proposition</returns> public Proposition ConvertToCNF(Proposition expr) { if (expr == null || !expr.IsNNF()) { throw new PropositionNotSupportedException("NOT_NNF"); } if (expr.op == Operator.Atom) { return(expr); } else if (expr.op == Operator.And) // S(P&Q) => S(P)&S(Q) { return(new Proposition(Operator.And, ConvertToCNF(expr.left), ConvertToCNF(expr.right))); } else if (expr.op == Operator.Or) // S(P|Q) => { Proposition c1 = ConvertToCNF(expr.left); Proposition c2 = ConvertToCNF(expr.right); if (c1.op != Operator.And && c2.op != Operator.And) // S(P|Q) => S(P)|S(Q) { return(new Proposition(Operator.Or, c1, c2)); } if (c1.op != Operator.And) { Proposition tmp = c1; c1 = c2; c2 = tmp; // S((A&B)|C) => S(A|C)&S(B|C) } return(new Proposition(Operator.And, ConvertToCNF(new Proposition(Operator.Or, c1.left, c2)), ConvertToCNF(new Proposition(Operator.Or, c1.right, c2)))); } else if (expr.op == Operator.Not) // S(P) => S(P) { if (expr.left.op == Operator.Atom) // Must Be Unit According to Def. of NNF { return(expr); } } return(null); }