public void testDefaultClauseSimplifier()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("ZERO");
            domain.addConstant("ONE");
            domain.addPredicate("P");
            domain.addFunction("Plus");
            domain.addFunction("Power");

            FOLParser parser = new FOLParser(domain);

            List<TermEquality> rewrites = new List<TermEquality>();
            rewrites.Add((TermEquality)parser.parse("Plus(x, ZERO) = x"));
            rewrites.Add((TermEquality)parser.parse("Plus(ZERO, x) = x"));
            rewrites.Add((TermEquality)parser.parse("Power(x, ONE) = x"));
            rewrites.Add((TermEquality)parser.parse("Power(x, ZERO) = ONE"));
            DefaultClauseSimplifier simplifier = new DefaultClauseSimplifier(
                    rewrites);

            Sentence s1 = parser
                    .parse("((P(Plus(y,ZERO),Plus(ZERO,y)) OR P(Power(y, ONE),Power(y,ZERO))) OR P(Power(y,ZERO),Plus(y,ZERO)))");

            CNFConverter cnfConverter = new CNFConverter(parser);

            CNF cnf = cnfConverter.convertToCNF(s1);

            Assert.AreEqual(1, cnf.getNumberOfClauses());

            Clause simplified = simplifier.simplify(cnf.getConjunctionOfClauses()
                    [0]);

            Assert.AreEqual("[P(y,y), P(y,ONE), P(ONE,y)]", simplified
                    .ToString());
        }
Пример #2
0
 public FOLKnowledgeBase(FOLDomain domain,
         InferenceProcedure inferenceProcedure, Unifier unifier)
 {
     this.parser = new FOLParser(new FOLDomain(domain));
     this.inferenceProcedure = inferenceProcedure;
     this.unifier = unifier;
     //
     this.substVisitor = new SubstVisitor();
     this.variableCollector = new VariableCollector();
     this._standardizeApart = new StandardizeApart(variableCollector,
             substVisitor);
     this.cnfConverter = new CNFConverter(parser);
 }
Пример #3
0
        public void testExamplePg295AIMA2e()
        {
            FOLDomain domain = DomainFactory.weaponsDomain();
            FOLParser parser = new FOLParser(domain);

            Sentence origSentence = parser
                    .parse("FORALL x ((((American(x) AND Weapon(y)) AND Sells(x, y, z)) AND Hostile(z)) => Criminal(x))");

            CNFConverter cnfConv = new CNFConverter(parser);

            CNF cnf = cnfConv.convertToCNF(origSentence);

            Assert
                    .AreEqual(
                            "[~American(x), ~Hostile(z), ~Sells(x,y,z), ~Weapon(y), Criminal(x)]",
                            cnf.ToString());
        }
Пример #4
0
        public void testExamplePg296AIMA2e()
        {
            FOLDomain domain = DomainFactory.lovesAnimalDomain();
            FOLParser parser = new FOLParser(domain);

            Sentence origSentence = parser
                    .parse("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))");

            CNFConverter cnfConv = new CNFConverter(parser);

            CNF cnf = cnfConv.convertToCNF(origSentence);

            Assert
                    .AreEqual(
                            "[Animal(SF0(x)), Loves(SF1(x),x)],[~Loves(x,SF0(x)), Loves(SF1(x),x)]",
                            cnf.ToString());
        }
        public void testFindSubsumedClauses()
        {
            // Taken from AIMA2e pg 679
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("patrons");
            domain.addPredicate("hungry");
            domain.addPredicate("type");
            domain.addPredicate("fri_sat");
            domain.addPredicate("will_wait");
            domain.addConstant("Some");
            domain.addConstant("Full");
            domain.addConstant("French");
            domain.addConstant("Thai");
            domain.addConstant("Burger");
            FOLParser parser = new FOLParser(domain);

            String c1 = "patrons(v,Some)";
            String c2 = "patrons(v,Full) AND (hungry(v) AND type(v,French))";
            String c3 = "patrons(v,Full) AND (hungry(v) AND (type(v,Thai) AND fri_sat(v)))";
            String c4 = "patrons(v,Full) AND (hungry(v) AND type(v,Burger))";
            String sh = "FORALL v (will_wait(v) <=> (" + c1 + " OR (" + c2
                    + " OR (" + c3 + " OR (" + c4 + ")))))";

            Sentence s = parser.parse(sh);

            CNFConverter cnfConv = new CNFConverter(parser);

            CNF cnf = cnfConv.convertToCNF(s);

            // Contains 9 duplicates
            Assert.AreEqual(40, cnf.getNumberOfClauses());

            List<Clause> clauses = new List<Clause>(cnf.getConjunctionOfClauses());

            // duplicates removed
            Assert.AreEqual(31, clauses.Count);
            foreach (Clause remove in SubsumptionElimination.findSubsumedClauses(clauses))
            {
                clauses.Remove(remove);
            }

            // subsumed clauses removed
            Assert.AreEqual(8, clauses.Count);

            // Ensure only the 8 correct/expected clauses remain
            Clause cl1 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(will_wait(v)) OR (patrons(v,Full) OR patrons(v,Some)))"))
                    .getConjunctionOfClauses()[0];
            Clause cl2 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(will_wait(v)) OR (hungry(v) OR patrons(v,Some)))"))
                    .getConjunctionOfClauses()[0];
            Clause cl3 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(will_wait(v)) OR (patrons(v,Some) OR (type(v,Burger) OR (type(v,French) OR type(v,Thai)))))"))
                    .getConjunctionOfClauses()[0];
            Clause cl4 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(will_wait(v)) OR (fri_sat(v) OR (patrons(v,Some) OR (type(v,Burger) OR type(v,French)))))"))
                    .getConjunctionOfClauses()[0];
            Clause cl5 = cnfConv.convertToCNF(
                    parser.parse("(NOT(patrons(v,Some)) OR will_wait(v))"))
                    .getConjunctionOfClauses()[0];
            Clause cl6 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,French)) OR will_wait(v))))"))
                    .getConjunctionOfClauses()[0];
            Clause cl7 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(fri_sat(v)) OR (NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,Thai)) OR will_wait(v)))))"))
                    .getConjunctionOfClauses()[0];
            Clause cl8 = cnfConv
                    .convertToCNF(
                            parser
                                    .parse("(NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,Burger)) OR will_wait(v))))"))
                    .getConjunctionOfClauses()[0];

            Assert.IsTrue(clauses.Contains(cl1));
            Assert.IsTrue(clauses.Contains(cl2));
            Assert.IsTrue(clauses.Contains(cl3));
            Assert.IsTrue(clauses.Contains(cl4));
            Assert.IsTrue(clauses.Contains(cl5));
            Assert.IsTrue(clauses.Contains(cl6));
            Assert.IsTrue(clauses.Contains(cl7));
            Assert.IsTrue(clauses.Contains(cl8));
        }
Пример #6
0
        public void testComplexEquals()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addPredicate("Animal");
            domain.addPredicate("Kills");
            domain.addFunction("F");
            domain.addFunction("SF0");

            FOLParser parser = new FOLParser(domain);

            CNFConverter cnfConverter = new CNFConverter(parser);
            Sentence s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)");
            Sentence s2 = parser.parse("((x2 = y2 AND F(y2) = z2) => F(x2) = z2)");
            CNF cnf1 = cnfConverter.convertToCNF(s1);
            CNF cnf2 = cnfConverter.convertToCNF(s2);

            Clause c1 = cnf1.getConjunctionOfClauses()[0];
            Clause c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsFalse(c1.Equals(c2));

            s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)");
            s2 = parser.parse("((x2 = y2 AND y2 = z2) => x2 = z2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)");
            s2 = parser.parse("((y2 = z2 AND x2 = y2) => x2 = z2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)");
            s2 = parser.parse("(((x2 = y2 AND y2 = z2) AND z2 = r2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)");
            s2 = parser.parse("(((z2 = r2 AND y2 = z2) AND x2 = y2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)");
            s2 = parser.parse("(((x2 = y2 AND y2 = z2) AND z2 = y2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsFalse(c1.Equals(c2));

            s1 = parser
                    .parse("(((((x1 = y1 AND y1 = z1) AND z1 = r1) AND r1 = q1) AND q1 = s1) => x1 = r1)");
            s2 = parser
                    .parse("(((((x2 = y2 AND y2 = z2) AND z2 = r2) AND r2 = q2) AND q2 = s2) => x2 = r2)");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));

            s1 = parser
                    .parse("((((NOT(Animal(c1920)) OR NOT(Animal(c1921))) OR NOT(Kills(c1922,c1920))) OR NOT(Kills(c1919,c1921))) OR NOT(Kills(SF0(c1922),SF0(c1919))))");
            s2 = parser
                    .parse("((((NOT(Animal(c1929)) OR NOT(Animal(c1928))) OR NOT(Kills(c1927,c1929))) OR NOT(Kills(c1930,c1928))) OR NOT(Kills(SF0(c1930),SF0(c1927))))");
            cnf1 = cnfConverter.convertToCNF(s1);
            cnf2 = cnfConverter.convertToCNF(s2);

            c1 = cnf1.getConjunctionOfClauses()[0];
            c2 = cnf2.getConjunctionOfClauses()[0];

            Assert.IsTrue(c1.Equals(c2));
        }
Пример #7
0
        public void testExamplesPg299AIMA2e()
        {
            FOLDomain domain = DomainFactory.lovesAnimalDomain();
            FOLParser parser = new FOLParser(domain);

            // FOL A.
            Sentence origSentence = parser
                    .parse("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))");

            CNFConverter cnfConv = new CNFConverter(parser);

            CNF cnf = cnfConv.convertToCNF(origSentence);

            // CNF A1. and A2.
            Assert
                    .AreEqual(
                            "[Animal(SF0(x)), Loves(SF1(x),x)],[~Loves(x,SF0(x)), Loves(SF1(x),x)]",
                            cnf.ToString());

            // FOL B.
            origSentence = parser
                    .parse("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))");

            cnf = cnfConv.convertToCNF(origSentence);

            // CNF B.
            Assert.AreEqual("[~Animal(y), ~Kills(x,y), ~Loves(z,x)]", cnf
                    .ToString());

            // FOL C.
            origSentence = parser.parse("FORALL x (Animal(x) => Loves(Jack, x))");

            cnf = cnfConv.convertToCNF(origSentence);

            // CNF C.
            Assert.AreEqual("[~Animal(x), Loves(Jack,x)]", cnf.ToString());

            // FOL D.
            origSentence = parser
                    .parse("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))");

            cnf = cnfConv.convertToCNF(origSentence);

            // CNF D.
            Assert.AreEqual("[Kills(Curiosity,Tuna), Kills(Jack,Tuna)]", cnf
                    .ToString());

            // FOL E.
            origSentence = parser.parse("Cat(Tuna)");

            cnf = cnfConv.convertToCNF(origSentence);

            // CNF E.
            Assert.AreEqual("[Cat(Tuna)]", cnf.ToString());

            // FOL F.
            origSentence = parser.parse("FORALL x (Cat(x) => Animal(x))");

            cnf = cnfConv.convertToCNF(origSentence);

            // CNF F.
            Assert.AreEqual("[~Cat(x), Animal(x)]", cnf.ToString());

            // FOL G.
            origSentence = parser.parse("NOT(Kills(Curiosity, Tuna))");

            cnf = cnfConv.convertToCNF(origSentence);

            // CNF G.
            Assert.AreEqual("[~Kills(Curiosity,Tuna)]", cnf.ToString());
        }
Пример #8
0
        public void testTermEquality()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addFunction("Plus");
            domain.addConstant("ONE");
            domain.addConstant("ZERO");

            FOLParser parser = new FOLParser(domain);
            CNFConverter cnfConv = new CNFConverter(parser);

            // x=y
            Sentence sent = parser.parse("x = y");
            CNF cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[x = y]", cnf.ToString());

            // x!=y
            sent = parser.parse("NOT(x = y)");
            cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[~x = y]", cnf.ToString());

            // A=B
            sent = parser.parse("A = B");
            cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[A = B]", cnf.ToString());

            // A!=B
            sent = parser.parse("NOT(A = B)");
            cnf = cnfConv.convertToCNF(sent);

            Assert.AreEqual("[~A = B]", cnf.ToString());

            // ~(((~A=B or ~D=C) => ~(A=B or D=C)) => A=D)
            sent = parser
                    .parse("NOT(((((NOT(A = B) OR NOT(D = C))) => NOT((A = B OR D = C))) => A = D))");
            cnf = cnfConv.convertToCNF(sent);

            Assert
                    .AreEqual(
                            "[~A = B, A = B],[~A = B, D = C],[~D = C, A = B],[~D = C, D = C],[~A = D]",
                            cnf.ToString());

            //
            // Induction Axiom Schema using Term Equality

            // Base Case:
            sent = parser
                    .parse("NOT(FORALL x (FORALL y (Plus(Plus(x,y),ZERO) = Plus(x,Plus(y,ZERO)))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Plus(Plus(SC0,SC1),ZERO) = Plus(SC0,Plus(SC1,ZERO))]", cnf
                            .ToString());

            // Instance of Induction Axion Scmema
            sent = parser.parse("(("
                    + "Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO))" + " AND "
                    + "(FORALL x (FORALL y (FORALL z("
                    + "Plus(Plus(x,y),z) = Plus(x,Plus(y,z))" + " => "
                    + "Plus(Plus(x,y),Plus(z,ONE)) = Plus(x,Plus(y,Plus(z,ONE)))"
                    + "))))" + ")" + " => " + "FORALL x (FORALL y (FORALL z("
                    + "Plus(Plus(x,y),z) = Plus(x,Plus(y,z))" + "))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert
                    .AreEqual(
                            "[~Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO)), Plus(Plus(q0,q1),q2) = Plus(q0,Plus(q1,q2)), Plus(Plus(SC2,SC3),SC4) = Plus(SC2,Plus(SC3,SC4))],[~Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO)), ~Plus(Plus(SC2,SC3),Plus(SC4,ONE)) = Plus(SC2,Plus(SC3,Plus(SC4,ONE))), Plus(Plus(q0,q1),q2) = Plus(q0,Plus(q1,q2))]",
                            cnf.ToString());

            // Goal
            sent = parser
                    .parse("NOT(FORALL x (FORALL y (FORALL z (Plus(Plus(x,y),z) = Plus(x,Plus(y,z))))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Plus(Plus(SC5,SC6),SC7) = Plus(SC5,Plus(SC6,SC7))]", cnf
                            .ToString());
        }
Пример #9
0
        public void testInductionAxiomSchema()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("Equal");
            domain.addFunction("Plus");
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("N");
            domain.addConstant("ONE");
            domain.addConstant("ZERO");

            FOLParser parser = new FOLParser(domain);
            CNFConverter cnfConv = new CNFConverter(parser);

            // Base Case:
            Sentence sent = parser
                    .parse("NOT(FORALL x (FORALL y (Equal(Plus(Plus(x,y),ZERO), Plus(x,Plus(y,ZERO))))))");
            CNF cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Equal(Plus(Plus(SC0,SC1),ZERO),Plus(SC0,Plus(SC1,ZERO)))]",
                    cnf.ToString());

            // Instance of Induction Axion Scmema
            sent = parser
                    .parse("(("
                            + "Equal(Plus(Plus(A,B),ZERO), Plus(A,Plus(B,ZERO)))"
                            + " AND "
                            + "(FORALL x (FORALL y (FORALL z("
                            + "Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))"
                            + " => "
                            + "Equal(Plus(Plus(x,y),Plus(z,ONE)), Plus(x,Plus(y,Plus(z,ONE))))"
                            + "))))" + ")" + " => "
                            + "FORALL x (FORALL y (FORALL z("
                            + "Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))"
                            + "))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert
                    .AreEqual(
                            "[~Equal(Plus(Plus(A,B),ZERO),Plus(A,Plus(B,ZERO))), Equal(Plus(Plus(q0,q1),q2),Plus(q0,Plus(q1,q2))), Equal(Plus(Plus(SC2,SC3),SC4),Plus(SC2,Plus(SC3,SC4)))],[~Equal(Plus(Plus(A,B),ZERO),Plus(A,Plus(B,ZERO))), ~Equal(Plus(Plus(SC2,SC3),Plus(SC4,ONE)),Plus(SC2,Plus(SC3,Plus(SC4,ONE)))), Equal(Plus(Plus(q0,q1),q2),Plus(q0,Plus(q1,q2)))]",
                            cnf.ToString());

            // Goal
            sent = parser
                    .parse("NOT(FORALL x (FORALL y (FORALL z (Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))))))");
            cnf = cnfConv.convertToCNF(sent);
            Assert.AreEqual(
                    "[~Equal(Plus(Plus(SC5,SC6),SC7),Plus(SC5,Plus(SC6,SC7)))]",
                    cnf.ToString());
        }
Пример #10
0
        public void testNegationsAndNestedImplications()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addConstant("A");

            FOLParser parser = new FOLParser(domain);
            CNFConverter cnfConv = new CNFConverter(parser);

            // ~(((~p or ~q) => ~(p or q)) => r)
            Sentence sent = parser
                    .parse("NOT(((((NOT(P(A)) OR NOT(Q(A)))) => NOT((P(A) OR Q(A)))) => R(A)))");
            CNF cnf = cnfConv.convertToCNF(sent);

            Assert
                    .AreEqual(
                            "[~P(A), P(A)],[~P(A), Q(A)],[~Q(A), P(A)],[~Q(A), Q(A)],[~R(A)]",
                            cnf.ToString());
        }
Пример #11
0
        public void testImplicationsAndExtendedAndsOrs()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("Cheat");
            domain.addPredicate("Extra");
            domain.addPredicate("Knows");
            domain.addPredicate("Diff");
            domain.addPredicate("F");
            domain.addPredicate("A");
            domain.addPredicate("Probation");
            domain.addPredicate("Award");

            FOLParser parser = new FOLParser(domain);
            CNFConverter cnfConv = new CNFConverter(parser);

            // cheat(x,y) => f(x,y)
            Sentence def1 = parser.parse("(Cheat(x,y) => F(x,y))");
            CNF cnfDef1 = cnfConv.convertToCNF(def1);

            Assert.AreEqual("[~Cheat(x,y), F(x,y)]", cnfDef1.ToString());

            // extra(x,y) | knows(x) => a(x,y)
            Sentence def2 = parser.parse("((Extra(x,y) OR Knows(x)) => A(x,y))");
            CNF cnfDef2 = cnfConv.convertToCNF(def2);

            Assert.AreEqual("[~Extra(x,y), A(x,y)],[~Knows(x), A(x,y)]",
                    cnfDef2.ToString());

            // f(x,y) & f(x,z) & diff(y,z) <=> probation(x)
            Sentence def3 = parser
                    .parse("(((NOT(((F(x,y) AND F(x,z)) AND Diff(y,z)))) OR Probation(x)) AND (((F(x,y) AND F(x,z)) AND Diff(y,z)) OR NOT(Probation(x))))");
            CNF cnfDef3 = cnfConv.convertToCNF(def3);

            Assert
                    .AreEqual(
                            "[~Diff(y,z), ~F(x,y), ~F(x,z), Probation(x)],[~Probation(x), F(x,y)],[~Probation(x), F(x,z)],[~Probation(x), Diff(y,z)]",
                            cnfDef3.ToString());

            // a(x,y) & a(x,z) & diff(y,z) <=> award(x)
            Sentence def4 = parser
                    .parse("(((NOT(((A(x,y) AND A(x,z)) AND Diff(y,z)))) OR Award(x)) AND (((A(x,y) AND A(x,z)) AND Diff(y,z)) OR NOT(Award(x))))");
            CNF cnfDef4 = cnfConv.convertToCNF(def4);

            Assert
                    .AreEqual(
                            "[~A(x,y), ~A(x,z), ~Diff(y,z), Award(x)],[~Award(x), A(x,y)],[~Award(x), A(x,z)],[~Award(x), Diff(y,z)]",
                            cnfDef4.ToString());

            // f(x,y) <=> ~a(x,y)
            Sentence def5 = parser
                    .parse("( ( NOT(F(x,y)) OR NOT(A(x,y))) AND ( F(x,y) OR NOT(NOT(A(x,y))) ) )");
            CNF cnfDef5 = cnfConv.convertToCNF(def5);

            Assert.AreEqual("[~A(x,y), ~F(x,y)],[A(x,y), F(x,y)]", cnfDef5
                    .ToString());
        }
Пример #12
0
        public void testNestedExistsAndOrs()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("P");
            domain.addPredicate("R");
            domain.addPredicate("Q");

            FOLParser parser = new FOLParser(domain);

            Sentence origSentence = parser
                    .parse("EXISTS w (FORALL x ( (EXISTS z (Q(w, z))) => (EXISTS y (NOT(P(x, y)) AND R(y))) ) )");

            CNFConverter cnfConv = new CNFConverter(parser);

            CNF cnf = cnfConv.convertToCNF(origSentence);

            Assert.AreEqual("[~P(x,SF0(x)), ~Q(SC0,z)],[~Q(SC0,z), R(SF0(x))]",
                    cnf.ToString());

            // Ax.Ay.(p(x,y) => Ez.(q(x,y,z)))
            origSentence = parser
                    .parse("FORALL x1 (FORALL y1 (P(x1, y1) => EXISTS z1 (Q(x1, y1, z1))))");

            cnf = cnfConv.convertToCNF(origSentence);

            Assert.AreEqual("[~P(x1,y1), Q(x1,y1,SF1(x1,y1))]", cnf.ToString());

            // Ex.Ay.Az.(r(y,z) <=> q(x,y,z))
            origSentence = parser
                    .parse("EXISTS x2 (FORALL y2 (FORALL z2 (R(y2, z2) <=> Q(x2, y2, z2))))");

            cnf = cnfConv.convertToCNF(origSentence);

            Assert.AreEqual(
                    "[~R(y2,z2), Q(SC1,y2,z2)],[~Q(SC1,y2,z2), R(y2,z2)]", cnf
                            .ToString());

            // Ax.Ey.(~p(x,y) => Az.(q(x,y,z)))
            origSentence = parser
                    .parse("FORALL x3 (EXISTS y3 (NOT(P(x3, y3)) => FORALL z3 (Q(x3, y3, z3))))");

            cnf = cnfConv.convertToCNF(origSentence);

            Assert
                    .AreEqual("[P(x3,SF2(x3)), Q(x3,SF2(x3),z3)]", cnf
                            .ToString());

            // Ew.Ex.Ey.Ez.(r(x,y) & q(x,w,z))
            origSentence = parser
                    .parse("NOT(EXISTS w4 (EXISTS x4 (EXISTS y4 ( EXISTS z4 (R(x4, y4) AND Q(x4, w4, z4))))))");

            cnf = cnfConv.convertToCNF(origSentence);

            Assert.AreEqual("[~Q(x4,w4,z4), ~R(x4,y4)]", cnf.ToString());
        }