parse() public method

public parse ( String s ) : Sentence
s String
return Sentence
        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());
        }
        public void testSimpleAtomicExamples()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addPredicate("P");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addFunction("J");

            FOLParser parser = new FOLParser(domain);

            Predicate expression = (Predicate)parser
                    .parse("P(A,F(B,G(A,H(B)),C),D)");
            TermEquality assertion = (TermEquality)parser.parse("B = E");

            Predicate altExpression = (Predicate)demodulation.apply(assertion,
                    expression);

            Assert.IsFalse(expression.Equals(altExpression));
            Assert
                    .AreEqual("P(A,F(E,G(A,H(B)),C),D)", altExpression
                            .ToString());

            altExpression = (Predicate)demodulation
                    .apply(assertion, altExpression);

            Assert
                    .AreEqual("P(A,F(E,G(A,H(E)),C),D)", altExpression
                            .ToString());

            assertion = (TermEquality)parser.parse("G(x,y) = J(x)");

            altExpression = (Predicate)demodulation.apply(assertion, expression);

            Assert.AreEqual("P(A,F(B,J(A),C),D)", altExpression.ToString());
        }
        public void testSimpleExample()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            AtomicSentence a1 = (AtomicSentence)parser.parse("P(F(x,B),x)");
            AtomicSentence a2 = (AtomicSentence)parser.parse("Q(x)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));

            Clause c1 = new Clause(lits);

            lits.Clear();
            a1 = (AtomicSentence)parser.parse("F(A,y) = y");
            a2 = (AtomicSentence)parser.parse("R(y)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));

            Clause c2 = new Clause(lits);

            List<Clause> paras = paramodulation.apply(c1, c2);
            Assert.AreEqual(2, paras.Count);

            foreach (Clause c in paras)
            {
                Assert.AreEqual("[P(B,A), Q(A), R(B)]", c.ToString());
                Assert.AreEqual("[P(F(A,F(x,B)),x), Q(x), R(F(x,B))]", c
                        .ToString());
            }
        }
        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 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());
        }
Beispiel #6
0
        public void testAdditionalVariableMixtures()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addPredicate("P");

            FOLParser parser = new FOLParser(domain);

            // Test Cascade Substitutions handled correctly
            Sentence s1 = parser.parse("P(z, x)");
            Sentence s2 = parser.parse("P(x, a)");
            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.AreEqual("{z=a, x=a}", result.ToString());

            s1 = parser.parse("P(x, z)");
            s2 = parser.parse("P(a, x)");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{x=a, z=a}", result.ToString());

            s1 = parser.parse("P(w, w, w)");
            s2 = parser.parse("P(x, y, z)");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{w=z, x=z, y=z}", result.ToString());

            s1 = parser.parse("P(x, y, z)");
            s2 = parser.parse("P(w, w, w)");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{x=w, y=w, z=w}", result.ToString());

            s1 = parser.parse("P(x, B, F(y))");
            s2 = parser.parse("P(A, y, F(z))");
            result = unifier.unify(s1, s2);

            Assert.AreEqual("{x=A, y=B, z=B}", result.ToString());

            s1 = parser.parse("P(F(x,B), G(y),         F(z,A))");
            s2 = parser.parse("P(y,      G(F(G(w),w)), F(w,z))");
            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("P(F(G(A)), x,    F(H(z,z)), H(y,    G(w)))");
            s2 = parser.parse("P(y,       G(z), F(v     ), H(F(w), x   ))");
            result = unifier.unify(s1, s2);

            Assert.AreEqual(
                    "{y=F(G(A)), x=G(G(A)), v=H(G(A),G(A)), w=G(A), z=G(A)}",
                    result.ToString());
        }
Beispiel #7
0
        public void testCascadedOccursCheck()
        {
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("P");
            domain.addFunction("F");
            domain.addFunction("SF0");
            domain.addFunction("SF1");
            FOLParser parser = new FOLParser(domain);

            Sentence s1 = parser.parse("P(SF1(v2),v2)");
            Sentence s2 = parser.parse("P(v3,SF0(v3))");
            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("P(v1,SF0(v1),SF0(v1),SF0(v1),SF0(v1))");
            s2 = parser.parse("P(v2,SF0(v2),v2,     v3,     v2)");
            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser
                    .parse("P(v1,   F(v2),F(v2),F(v2),v1,      F(F(v1)),F(F(F(v1))),v2)");
            s2 = parser
                    .parse("P(F(v3),v4,   v5,   v6,   F(F(v5)),v4,      F(v3),      F(F(v5)))");
            result = unifier.unify(s1, s2);

            Assert.IsNull(result);
        }
Beispiel #8
0
        public void testSubsumes()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addConstant("F");
            domain.addConstant("G");
            domain.addConstant("H");
            domain.addConstant("I");
            domain.addConstant("J");
            domain.addPredicate("P");
            domain.addPredicate("Q");

            FOLParser parser = new FOLParser(domain);

            // Example
            // {~p(a,b),q(c)}
            Clause psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(x,y)}
            Clause phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(x,y)"));

            Assert.IsTrue(phi.subsumes(psi));
            // Non-Example
            // {~p(x,b),q(x)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(x,B)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(x)"));
            // {~p(a,y)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,y)"));
            // Reason for Non-Example:
            // {p(b,b)}
            // {~q(b)}
            Assert.IsFalse(phi.subsumes(psi));

            //
            // Additional Examples

            // Non-Example
            // {~p(x,b),q(z)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(x,B)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(z)"));
            // {~p(a,y)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,y)"));

            Assert.IsFalse(phi.subsumes(psi));

            // Example
            // {~p(a,b),~p(w,z),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(w,z)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(x,y),~p(a,b)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(x,y)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));

            Assert.IsTrue(phi.subsumes(psi));

            // Non-Example
            // {~p(v,b),~p(w,z),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(v,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(w,z)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(x,y),~p(a,b)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(x,y)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));

            Assert.IsFalse(phi.subsumes(psi));

            // Example
            // {~p(a,b),~p(c,d),~p(e,f),~p(g,h),~p(i,j),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(G,H)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(i,j)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));

            Assert.IsTrue(phi.subsumes(psi));

            // Example
            // {~p(a,b),~p(c,d),~p(e,f),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(e,f),~p(a,b),~p(c,d)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));

            Assert.IsTrue(phi.subsumes(psi));

            // Example
            // {~p(a,b),~p(c,d),~p(e,f),~p(g,h),~p(i,j),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(G,H)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(i,j),~p(c,d)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));

            Assert.IsTrue(phi.subsumes(psi));

            // Non-Example
            // {~p(a,b),~p(x,d),~p(e,f),~p(g,h),~p(i,j),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(x,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(G,H)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(i,j),~p(c,d)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));

            Assert.IsFalse(phi.subsumes(psi));

            // Example
            // {~p(a,b),~p(c,d),~p(e,f),~p(g,h),~p(i,j),q(c)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(G,H)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C)"));
            // {~p(i,j),~p(a,x)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,x)"));

            Assert.IsTrue(phi.subsumes(psi));

            // Example
            // {~p(a,b),~p(c,d),~p(e,f),~p(g,h),~p(i,j),q(a,b),q(c,d),q(e,f)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(G,H)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C,D)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(E,F)"));
            // {~p(i,j),~p(a,b),q(e,f),q(a,b)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            phi.addPositiveLiteral((Predicate)parser.parse("Q(E,F)"));
            phi.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));

            Assert.IsTrue(phi.subsumes(psi));

            // Non-Example
            // {~p(a,b),~p(c,d),~p(e,f),~p(g,h),~p(i,j),q(a,b),q(c,d),q(e,f)}
            psi = new Clause();
            psi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(C,D)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(E,F)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(G,H)"));
            psi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(C,D)"));
            psi.addPositiveLiteral((Predicate)parser.parse("Q(E,F)"));
            // {~p(i,j),~p(a,b),q(e,f),q(a,b)}
            phi = new Clause();
            phi.addNegativeLiteral((Predicate)parser.parse("P(I,J)"));
            phi.addNegativeLiteral((Predicate)parser.parse("P(A,B)"));
            phi.addPositiveLiteral((Predicate)parser.parse("Q(E,A)"));
            phi.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));

            Assert.IsFalse(phi.subsumes(psi));
        }
Beispiel #9
0
        public void testIsTautology()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            // {p(f(a)),~p(f(a))}
            Clause c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(F(A))"));
            Assert.IsFalse(c.isTautology());
            c.addNegativeLiteral((Predicate)parser.parse("P(F(A))"));
            Assert.IsTrue(c.isTautology());

            // {p(x),q(y),~q(y),r(z)}
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x)"));
            Assert.IsFalse(c.isTautology());
            c.addPositiveLiteral((Predicate)parser.parse("Q(y)"));
            Assert.IsFalse(c.isTautology());
            c.addNegativeLiteral((Predicate)parser.parse("Q(y)"));
            Assert.IsTrue(c.isTautology());
            c.addPositiveLiteral((Predicate)parser.parse("R(z)"));
            Assert.IsTrue(c.isTautology());

            // {~p(a),p(x)}
            c = new Clause();
            c.addNegativeLiteral((Predicate)parser.parse("P(A)"));
            Assert.IsFalse(c.isTautology());
            c.addPositiveLiteral((Predicate)parser.parse("P(x)"));
            Assert.IsFalse(c.isTautology());
        }
        public void testMultipleTermEqualitiesInBothClausesExample()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("R");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            AtomicSentence a1 = (AtomicSentence)parser.parse("F(C,x) = D");
            AtomicSentence a2 = (AtomicSentence)parser.parse("A = D");
            AtomicSentence a3 = (AtomicSentence)parser.parse("P(F(x,B),x)");
            AtomicSentence a4 = (AtomicSentence)parser.parse("Q(x)");
            AtomicSentence a5 = (AtomicSentence)parser.parse("R(C)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));
            lits.Add(new Literal(a3));
            lits.Add(new Literal(a4));
            lits.Add(new Literal(a5));

            Clause c1 = new Clause(lits);

            lits.Clear();
            a1 = (AtomicSentence)parser.parse("F(A,y) = y");
            a2 = (AtomicSentence)parser.parse("F(B,y) = C");
            a3 = (AtomicSentence)parser.parse("R(y)");
            a4 = (AtomicSentence)parser.parse("R(A)");
            lits.Add(new Literal(a1));
            lits.Add(new Literal(a2));
            lits.Add(new Literal(a3));
            lits.Add(new Literal(a4));

            Clause c2 = new Clause(lits);

            List<Clause> paras = paramodulation.apply(c1, c2);
            Assert.AreEqual(5, paras.Count);

            List<Clause>.Enumerator it = paras.GetEnumerator();
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(B,B) = C, F(C,A) = D, A = D, P(B,A), Q(A), R(A), R(B), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(A,F(C,x)) = D, F(B,F(C,x)) = C, A = D, P(F(x,B),x), Q(x), R(F(C,x)), R(A), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(A,B) = B, F(C,B) = D, A = D, P(C,B), Q(B), R(A), R(B), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(F(B,y),x) = D, F(A,y) = y, A = D, P(F(x,B),x), Q(x), R(y), R(A), R(C)]",
                            it.Current.ToString());
            it.MoveNext();
            Assert
                    .AreEqual(
                            "[F(B,y) = C, F(C,x) = D, F(D,y) = y, P(F(x,B),x), Q(x), R(y), R(A), R(C)]",
                            it.Current.ToString());
        }
Beispiel #11
0
 public void testConnectedImplication()
 {
     parser = new FOLParser(DomainFactory.weaponsDomain());
     parser
             .parse("((Missile(m) AND Owns(Nono,m)) => Sells(West , m ,Nono))");
 }
Beispiel #12
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());
        }
Beispiel #13
0
        public void testBypassReflexivityAxiom()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            Predicate p1 = (Predicate)parser.parse("P(y, F(A,y))");
            lits.Add(new Literal(p1));

            Clause clExpression = new Clause(lits);

            TermEquality assertion = (TermEquality)parser.parse("x = x");

            Clause altClExpression = demodulation.apply(assertion, clExpression);

            Assert.IsNull(altClExpression);
        }
Beispiel #14
0
        public void testQuantifiedSentence()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addFunction("Plus");
            domain.addPredicate("P");

            FOLParser parser = new FOLParser(domain);

            Sentence s1 = parser
                    .parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            Sentence s2 = parser
                    .parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");

            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            s1 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("FORALL x   ((P(x) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(B) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("FORALL x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("FORALL x,y ((P(A) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(new Constant("A"), result[new Variable("x")]);

            //
            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x   ((P(x) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(B) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("EXISTS x,y ((P(x) AND P(A)) OR (P(A) => P(y)))");
            s2 = parser.parse("EXISTS x,y ((P(A) AND P(A)) OR (P(A) => P(y)))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(new Constant("A"), result[new Variable("x")]);
        }
Beispiel #15
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());
        }
Beispiel #16
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());
        }
Beispiel #17
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());
        }
Beispiel #18
0
        public void testTermEquality()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addFunction("Plus");

            FOLParser parser = new FOLParser(domain);

            TermEquality te1 = (TermEquality)parser.parse("x = x");
            TermEquality te2 = (TermEquality)parser.parse("x = x");

            // Both term equalities the same,
            // should unify but no substitutions.
            Dictionary<Variable, Term> result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            // Different variable names but should unify.
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("x2 = x2");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("{x1=x2}", result.ToString());

            // Test simple unification with reflexivity axiom
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("Plus(A,B) = Plus(A,B)");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("{x1=Plus(A,B)}", result.ToString());

            // Test more complex unification with reflexivity axiom
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("Plus(A,B) = Plus(A,z1)");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("{x1=Plus(A,B), z1=B}", result.ToString());

            // Test reverse of previous unification with reflexivity axiom
            // Should still be the same.
            te1 = (TermEquality)parser.parse("x1 = x1");
            te2 = (TermEquality)parser.parse("Plus(A,z1) = Plus(A,B)");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("{x1=Plus(A,B), z1=B}", result.ToString());

            // Test with nested terms
            te1 = (TermEquality)parser
                    .parse("Plus(Plus(Plus(A,B),B, A)) = Plus(Plus(Plus(A,B),B, A))");
            te2 = (TermEquality)parser
                    .parse("Plus(Plus(Plus(A,B),B, A)) = Plus(Plus(Plus(A,B),B, A))");

            result = unifier.unify(te1, te2);

            Assert.IsNotNull(result);

            Assert.AreEqual(0, result.Count);

            // Simple term equality unification fails
            te1 = (TermEquality)parser.parse("Plus(A,B) = Plus(B,A)");
            te2 = (TermEquality)parser.parse("Plus(A,B) = Plus(A,B)");

            result = unifier.unify(te1, te2);

            Assert.IsNull(result);
        }
Beispiel #19
0
        public void testSimpleAtomicNonExample()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addPredicate("P");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addFunction("J");

            FOLParser parser = new FOLParser(domain);

            Predicate expression = (Predicate)parser.parse("P(A,G(x,B),C)");
            TermEquality assertion = (TermEquality)parser.parse("G(A,y) = J(y)");

            Predicate altExpression = (Predicate)demodulation.apply(assertion,
                    expression);

            Assert.IsNull(altExpression);
        }
Beispiel #20
0
        public void testNOTSentence()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addFunction("Plus");
            domain.addPredicate("P");

            FOLParser parser = new FOLParser(domain);

            Sentence s1 = parser.parse("NOT(P(A))");
            Sentence s2 = parser.parse("NOT(P(A))");

            Dictionary<Variable, Term> result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count);

            s1 = parser.parse("NOT(P(A))");
            s2 = parser.parse("NOT(P(B))");

            result = unifier.unify(s1, s2);

            Assert.IsNull(result);

            s1 = parser.parse("NOT(P(A))");
            s2 = parser.parse("NOT(P(x))");

            result = unifier.unify(s1, s2);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(new Constant("A"), result[new Variable("x")]);
        }
Beispiel #21
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());
        }
Beispiel #22
0
        public void testSimpleClauseExamples()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addConstant("E");
            domain.addPredicate("P");
            domain.addPredicate("Q");
            domain.addPredicate("W");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addFunction("J");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            Predicate p1 = (Predicate)parser.parse("Q(z, G(D,B))");
            Predicate p2 = (Predicate)parser.parse("P(x, G(A,C))");
            Predicate p3 = (Predicate)parser.parse("W(z,x,u,w,y)");
            lits.Add(new Literal(p1));
            lits.Add(new Literal(p2));
            lits.Add(new Literal(p3));

            Clause clExpression = new Clause(lits);

            TermEquality assertion = (TermEquality)parser.parse("G(x,y) = x");

            Clause altClExpression = demodulation.apply(assertion, clExpression);

            Assert.AreEqual("[P(x,G(A,C)), Q(z,D), W(z,x,u,w,y)]",
                    altClExpression.ToString());

            altClExpression = demodulation.apply(assertion, altClExpression);

            Assert.AreEqual("[P(x,A), Q(z,D), W(z,x,u,w,y)]", altClExpression
                    .ToString());
        }
Beispiel #23
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());
        }
Beispiel #24
0
        public void testEqualityBinaryResolvents()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");

            FOLParser parser = new FOLParser(domain);

            // B = A
            Clause c1 = new Clause();
            c1.addPositiveLiteral((AtomicSentence)parser.parse("B = A"));

            Clause c2 = new Clause();
            c2.addNegativeLiteral((AtomicSentence)parser.parse("B = A"));
            c2.addPositiveLiteral((AtomicSentence)parser.parse("B = A"));

            List<Clause> resolvents = c1.binaryResolvents(c2);

            Assert.AreEqual(1, resolvents.Count);
            Assert.AreEqual("[[B = A]]", resolvents.ToString());
        }
Beispiel #25
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));
        }
        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));
        }
Beispiel #27
0
        public void testNonTrivialFactors()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addFunction("F");
            domain.addFunction("G");
            domain.addFunction("H");
            domain.addPredicate("P");
            domain.addPredicate("Q");

            FOLParser parser = new FOLParser(domain);

            // p(x,y), q(a,b), p(b,a), q(y,x)
            Clause c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x,y)"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));
            c.addNegativeLiteral((Predicate)parser.parse("P(B,A)"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(y,x)"));

            Assert.AreEqual("[[~P(B,A), P(B,A), Q(A,B)]]", c
                    .getNonTrivialFactors().ToString());

            // p(x,y), q(a,b), p(b,a), q(y,x)
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x,y)"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(A,B)"));
            c.addNegativeLiteral((Predicate)parser.parse("P(B,A)"));
            c.addNegativeLiteral((Predicate)parser.parse("Q(y,x)"));

            Assert.AreEqual("[]", c.getNonTrivialFactors().ToString());

            // p(x,f(y)), p(g(u),x), p(f(y),u)
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(x,F(y))"));
            c.addPositiveLiteral((Predicate)parser.parse("P(G(u),x)"));
            c.addPositiveLiteral((Predicate)parser.parse("P(F(y),u)"));

            // Should be: [{P(F(c#),F(c#)),P(G(F(c#)),F(c#))}]
            c = c.getNonTrivialFactors().First();
            Literal p = c.getPositiveLiterals()[0];
            Assert.AreEqual("P", p.getAtomicSentence().getSymbolicName());
            Function f = (Function)p.getAtomicSentence().getArgs()[0];
            Assert.AreEqual("F", f.getFunctionName());
            Variable v = (Variable)f.getTerms()[0];
            f = (Function)p.getAtomicSentence().getArgs()[1];
            Assert.AreEqual("F", f.getFunctionName());
            Assert.AreEqual(v, f.getTerms()[0]);

            //
            p = c.getPositiveLiterals()[1];
            f = (Function)p.getAtomicSentence().getArgs()[0];
            Assert.AreEqual("G", f.getFunctionName());
            f = (Function)f.getTerms()[0];
            Assert.AreEqual("F", f.getFunctionName());
            Assert.AreEqual(v, f.getTerms()[0]);
            f = (Function)p.getAtomicSentence().getArgs()[1];
            Assert.AreEqual("F", f.getFunctionName());
            Assert.AreEqual(v, f.getTerms()[0]);

            // p(g(x)), q(x), p(f(a)), p(x), p(g(f(x))), q(f(a))
            c = new Clause();
            c.addPositiveLiteral((Predicate)parser.parse("P(G(x))"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(x)"));
            c.addPositiveLiteral((Predicate)parser.parse("P(F(A))"));
            c.addPositiveLiteral((Predicate)parser.parse("P(x)"));
            c.addPositiveLiteral((Predicate)parser.parse("P(G(F(x)))"));
            c.addPositiveLiteral((Predicate)parser.parse("Q(F(A))"));

            Assert.AreEqual("[[P(F(A)), P(G(F(F(A)))), P(G(F(A))), Q(F(A))]]",
                    c.getNonTrivialFactors().ToString());
        }
        public void testExhaustsSearchSpace()
        {
            // Taken from AIMA pg 679
            FOLDomain domain = new FOLDomain();
            domain.addPredicate("alternate");
            domain.addPredicate("bar");
            domain.addPredicate("fri_sat");
            domain.addPredicate("hungry");
            domain.addPredicate("patrons");
            domain.addPredicate("price");
            domain.addPredicate("raining");
            domain.addPredicate("reservation");
            domain.addPredicate("type");
            domain.addPredicate("wait_estimate");
            domain.addPredicate("will_wait");
            domain.addConstant("Some");
            domain.addConstant("Full");
            domain.addConstant("French");
            domain.addConstant("Thai");
            domain.addConstant("Burger");
            domain.addConstant("$");
            domain.addConstant("_30_60");
            domain.addConstant("X0");
            FOLParser parser = new FOLParser(domain);

            // The hypothesis
            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 hypothesis = parser.parse(sh);
            Sentence desc = parser
                    .parse("(((((((((alternate(X0) AND NOT(bar(X0))) AND NOT(fri_sat(X0))) AND hungry(X0)) AND patrons(X0,Full)) AND price(X0,$)) AND NOT(raining(X0))) AND NOT(reservation(X0))) AND type(X0,Thai)) AND wait_estimate(X0,_30_60))");
            Sentence classification = parser.parse("will_wait(X0)");

            FOLKnowledgeBase kb = new FOLKnowledgeBase(domain,
                    new FOLOTTERLikeTheoremProver(false));

            kb.tell(hypothesis);
            kb.tell(desc);

            InferenceResult ir = kb.ask(classification);

            Assert.IsFalse(ir.isTrue());
            Assert.IsTrue(ir.isPossiblyFalse());
            Assert.IsFalse(ir.isUnknownDueToTimeout());
            Assert.IsFalse(ir.isPartialResultDueToTimeout());
            Assert.AreEqual(0, ir.getProofs().Count);
        }
        public void testNegativeTermEquality()
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLParser parser = new FOLParser(domain);

            List<Literal> lits = new List<Literal>();
            AtomicSentence a1 = (AtomicSentence)parser.parse("P(y, F(A,y))");
            lits.Add(new Literal(a1));

            Clause c1 = new Clause(lits);

            lits.Clear();
            a1 = (AtomicSentence)parser.parse("F(x,B) = x");
            lits.Add(new Literal(a1, true));

            Clause c2 = new Clause(lits);

            List<Clause> paras = paramodulation.apply(c1, c2);
            Assert.AreEqual(0, paras.Count);
        }