コード例 #1
0
        // Note: see -
        // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
        // slide 12 for where this test example was taken from.
        public static FOLKnowledgeBase createABCEqualityKnowledgeBase(
            InferenceProcedure infp, bool includeEqualityAxioms)
        {
            FOLDomain domain = new FOLDomain();

            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");

            FOLKnowledgeBase kb = new FOLKnowledgeBase(domain, infp);

            kb.tell("B = A");
            kb.tell("B = C");

            if (includeEqualityAxioms)
            {
                // Reflexivity Axiom
                kb.tell("x = x");
                // Symmetry Axiom
                kb.tell("(x = y => y = x)");
                // Transitivity Axiom
                kb.tell("((x = y AND y = z) => x = z)");
            }
            return(kb);
        }
コード例 #2
0
        private static void kingsDemo1(InferenceProcedure ip)
        {
            StandardizeApartIndexicalFactory.flush();

            FOLKnowledgeBase kb = FOLKnowledgeBaseFactory
                                  .createKingsKnowledgeBase(ip);

            String kbStr = kb.ToString();

            List <Term> terms = new List <Term>();

            terms.Add(new Constant("John"));
            Predicate query = new Predicate("Evil", terms);

            InferenceResult answer = kb.ask(query);

            System.Console.WriteLine("Kings Knowledge Base:");
            System.Console.WriteLine(kbStr);
            System.Console.WriteLine("Query: " + query);
            foreach (Proof p in answer.getProofs())
            {
                System.Console.Write(ProofPrinter.printProof(p));
                System.Console.WriteLine("");
            }
        }
コード例 #3
0
        protected void testEqualityAndSubstitutionNoAxiomsKBabcdPDSucceeds(InferenceProcedure infp, bool expectedToFail)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory.createABCDEqualityAndSubstitutionKnowledgeBase(infp, false);

            ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();

            terms.Add(new Constant("D"));
            Predicate query = new Predicate("P", terms);

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToFail)
            {
                Assert.IsTrue(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Size());
            }
            else
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsTrue(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(1 == answer.getProofs().Size());
                Assert.IsTrue(0 == answer.getProofs().Get(0)
                              .getAnswerBindings().Size());
            }
        }
コード例 #4
0
        protected void testEqualityAndSubstitutionAxiomsKBabcdFFASucceeds(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory
                                   .createABCDEqualityAndSubstitutionKnowledgeBase(infp, true);

            List <Term> terms = new List <Term>();

            terms.Add(new Constant("A"));
            Function fa = new Function("F", terms);

            terms = new List <Term>();
            terms.Add(fa);
            TermEquality query = new TermEquality(new Function("F", terms),
                                                  new Constant("A"));

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(1 == answer.getProofs().Count);
            Assert.IsTrue(0 == answer.getProofs()[0].getAnswerBindings()
                          .Count);
        }
コード例 #5
0
        // Note: see -
        // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
        // slide 16,17, and 18 for where this test example was taken from.
        public static FOLKnowledgeBase createABCDEqualityAndSubstitutionKnowledgeBase(
            InferenceProcedure infp, bool includeEqualityAxioms)
        {
            FOLDomain domain = new FOLDomain();

            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLKnowledgeBase kb = new FOLKnowledgeBase(domain, infp);

            kb.tell("F(A) = B");
            kb.tell("F(B) = A");
            kb.tell("C = D");
            kb.tell("P(A)");
            kb.tell("P(C)");

            if (includeEqualityAxioms)
            {
                // Reflexivity Axiom
                kb.tell("x = x");
                // Symmetry Axiom
                kb.tell("(x = y => y = x)");
                // Transitivity Axiom
                kb.tell("((x = y AND y = z) => x = z)");
                // Function F Substitution Axiom
                kb.tell("((x = y AND F(y) = z) => F(x) = z)");
                // Predicate P Substitution Axiom
                kb.tell("((x = y AND P(y)) => P(x))");
            }
            return(kb);
        }
コード例 #6
0
        protected void testFullFOLKBLovesAnimalQueryNotKillsJackTunaSucceeds(
            InferenceProcedure infp, bool expectedToTimeOut)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory
                                   .createLovesAnimalKnowledgeBase(infp);
            List <Term> terms = new List <Term>();

            terms.Add(new Constant("Jack"));
            terms.Add(new Constant("Tuna"));
            NotSentence query = new NotSentence(new Predicate("Kills", terms));

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToTimeOut)
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsTrue(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Count);
            }
            else
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsTrue(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(1 == answer.getProofs().Count);
                Assert.IsTrue(0 == answer.getProofs()[0]
                              .getAnswerBindings().Count);
            }
        }
コード例 #7
0
 public void setInferenceProcedure(InferenceProcedure inferenceProcedure)
 {
     if (null != inferenceProcedure)
     {
         this.inferenceProcedure = inferenceProcedure;
     }
 }
コード例 #8
0
ファイル: FolDemoBase.cs プロジェクト: tvn-cosine/aima.net
        protected static void lovesAnimalDemo(InferenceProcedure ip)
        {
            StandardizeApartIndexicalFactory.flush();

            FOLKnowledgeBase kb = FOLKnowledgeBaseFactory.createLovesAnimalKnowledgeBase(ip);

            string kbStr = kb.ToString();

            ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();

            terms.Add(new Constant("Curiosity"));
            terms.Add(new Constant("Tuna"));
            Predicate query = new Predicate("Kills", terms);

            InferenceResult answer = kb.ask(query);

            System.Console.WriteLine("Loves Animal Knowledge Base:");
            System.Console.WriteLine(kbStr);
            System.Console.WriteLine("Query: " + query);
            foreach (Proof p in answer.getProofs())
            {
                System.Console.Write(ProofPrinter.printProof(p));
                System.Console.WriteLine("");
            }
        }
コード例 #9
0
        protected void testFullFOLKBLovesAnimalQueryKillsJackTunaFalse(InferenceProcedure infp, bool expectedToTimeOut)
        {
            FOLKnowledgeBase   akb   = FOLKnowledgeBaseFactory.createLovesAnimalKnowledgeBase(infp);
            ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();

            terms.Add(new Constant("Jack"));
            terms.Add(new Constant("Tuna"));
            Predicate query = new Predicate("Kills", terms);

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToTimeOut)
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsTrue(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Size());
            }
            else
            {
                Assert.IsTrue(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Size());
            }
        }
コード例 #10
0
        protected void testEqualityNoAxiomsKBabcAEqualsCSucceeds(InferenceProcedure infp, bool expectedToFail)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory.createABCEqualityKnowledgeBase(infp, false);

            TermEquality query = new TermEquality(new Constant("A"), new Constant("C"));

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToFail)
            {
                Assert.IsTrue(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Size());
            }
            else
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsTrue(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(1 == answer.getProofs().Size());
                Assert.IsTrue(0 == answer.getProofs().Get(0)
                              .getAnswerBindings().Size());
            }
        }
コード例 #11
0
        public static FOLKnowledgeBase createKingsKnowledgeBase(InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.kingsDomain(), infp);

            kb.tell("((King(x) AND Greedy(x)) => Evil(x))");
            kb.tell("King(John)");
            kb.tell("King(Richard)");
            kb.tell("Greedy(John)");

            return(kb);
        }
コード例 #12
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);
 }
コード例 #13
0
        public static FOLKnowledgeBase createKingsKnowledgeBase(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.kingsDomain(),
                    infp);
            kb.tell("((King(x) AND Greedy(x)) => Evil(x))");
            kb.tell("King(John)");
            kb.tell("King(Richard)");
            kb.tell("Greedy(John)");

            return kb;
        }
コード例 #14
0
        public static FOLKnowledgeBase createLovesAnimalKnowledgeBase(InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.lovesAnimalDomain(), infp);

            kb.tell("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))");
            kb.tell("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))");
            kb.tell("FORALL x (Animal(x) => Loves(Jack, x))");
            kb.tell("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))");
            kb.tell("Cat(Tuna)");
            kb.tell("FORALL x (Cat(x) => Animal(x))");

            return(kb);
        }
コード例 #15
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);
 }
コード例 #16
0
        public static FOLKnowledgeBase createWeaponsKnowledgeBase(InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.weaponsDomain(), infp);

            kb.tell("( (((American(x) AND Weapon(y)) AND Sells(x,y,z)) AND Hostile(z)) => Criminal(x))");
            kb.tell(" Owns(Nono, M1)");
            kb.tell(" Missile(M1)");
            kb.tell("((Missile(x) AND Owns(Nono,x)) => Sells(West,x,Nono))");
            kb.tell("(Missile(x) => Weapon(x))");
            kb.tell("(Enemy(x,America) => Hostile(x))");
            kb.tell("American(West)");
            kb.tell("Enemy(Nono,America)");

            return(kb);
        }
コード例 #17
0
 protected void testDefiniteClauseKBKingsQueryRichardEvilFalse(
         InferenceProcedure infp)
 {
     FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory
             .createKingsKnowledgeBase(infp);
     List<Term> terms = new List<Term>();
     terms.Add(new Constant("Richard"));
     Predicate query = new Predicate("Evil", terms);
     InferenceResult answer = kkb.ask(query);
     Assert.IsTrue(null != answer);
     Assert.IsTrue(answer.isPossiblyFalse());
     Assert.IsFalse(answer.isTrue());
     Assert.IsFalse(answer.isUnknownDueToTimeout());
     Assert.IsFalse(answer.isPartialResultDueToTimeout());
     Assert.IsTrue(0 == answer.getProofs().Count);
 }
コード例 #18
0
        protected void testDefiniteClauseKBKingsQueryRichardEvilFalse(InferenceProcedure infp)
        {
            FOLKnowledgeBase   kkb   = FOLKnowledgeBaseFactory.createKingsKnowledgeBase(infp);
            ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();

            terms.Add(new Constant("Richard"));
            Predicate       query  = new Predicate("Evil", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsTrue(answer.isPossiblyFalse());
            Assert.IsFalse(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(0 == answer.getProofs().Size());
        }
コード例 #19
0
        public static FOLKnowledgeBase createLovesAnimalKnowledgeBase(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory
                    .lovesAnimalDomain(), infp);

            kb
                    .tell("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))");
            kb
                    .tell("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))");
            kb.tell("FORALL x (Animal(x) => Loves(Jack, x))");
            kb.tell("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))");
            kb.tell("Cat(Tuna)");
            kb.tell("FORALL x (Cat(x) => Animal(x))");

            return kb;
        }
コード例 #20
0
        public static FOLKnowledgeBase createWeaponsKnowledgeBase(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory
                    .weaponsDomain(), infp);
            kb
                    .tell("( (((American(x) AND Weapon(y)) AND Sells(x,y,z)) AND Hostile(z)) => Criminal(x))");
            kb.tell(" Owns(Nono, M1)");
            kb.tell(" Missile(M1)");
            kb.tell("((Missile(x) AND Owns(Nono,x)) => Sells(West,x,Nono))");
            kb.tell("(Missile(x) => Weapon(x))");
            kb.tell("(Enemy(x,America) => Hostile(x))");
            kb.tell("American(West)");
            kb.tell("Enemy(Nono,America)");

            return kb;
        }
コード例 #21
0
        protected void testDefiniteClauseKBKingsQueryEvilXReturnsJohnSucceeds(InferenceProcedure infp)
        {
            FOLKnowledgeBase   kkb   = FOLKnowledgeBaseFactory.createKingsKnowledgeBase(infp);
            ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();

            terms.Add(new Variable("x"));
            Predicate       query  = new Predicate("Evil", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(1 == answer.getProofs().Size());
            Assert.IsTrue(1 == answer.getProofs().Get(0).getAnswerBindings().Size());
            Assert.AreEqual(new Constant("John"), answer.getProofs().Get(0).getAnswerBindings().Get(new Variable("x")));
        }
コード例 #22
0
        //
        // Protected Methods
        //
        protected void testDefiniteClauseKBKingsQueryCriminalXFalse(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory
                                   .createKingsKnowledgeBase(infp);
            List <Term> terms = new List <Term>();

            terms.Add(new Variable("x"));
            Predicate       query  = new Predicate("Criminal", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsTrue(answer.isPossiblyFalse());
            Assert.IsFalse(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(0 == answer.getProofs().Count);
        }
コード例 #23
0
        protected void testDefiniteClauseKBKingsQueryKingXReturnsJohnAndRichardSucceeds(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory
                                   .createKingsKnowledgeBase(infp);
            List <Term> terms = new List <Term>();

            terms.Add(new Variable("x"));
            Predicate       query  = new Predicate("King", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(2 == answer.getProofs().Count);
            Assert.IsTrue(1 == answer.getProofs()[0].getAnswerBindings()
                          .Count);
            Assert.IsTrue(1 == answer.getProofs()[1].getAnswerBindings()
                          .Count);

            bool gotJohn, gotRichard;

            gotJohn = gotRichard = false;
            Constant cJohn    = new Constant("John");
            Constant cRichard = new Constant("Richard");

            foreach (Proof p in answer.getProofs())
            {
                Dictionary <Variable, Term> ans = p.getAnswerBindings();
                Assert.AreEqual(1, ans.Count);
                if (cJohn.Equals(ans[new Variable("x")]))
                {
                    gotJohn = true;
                }
                if (cRichard.Equals(ans[new Variable("x")]))
                {
                    gotRichard = true;
                }
            }
            Assert.IsTrue(gotJohn);
            Assert.IsTrue(gotRichard);
        }
コード例 #24
0
        protected void testDefiniteClauseKBKingsQueryJohnEvilSucceeds(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory
                                   .createKingsKnowledgeBase(infp);
            List <Term> terms = new List <Term>();

            terms.Add(new Constant("John"));
            Predicate       query  = new Predicate("Evil", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(1 == answer.getProofs().Count);
            Assert.IsTrue(0 == answer.getProofs()[0].getAnswerBindings()
                          .Count);
        }
コード例 #25
0
        protected void testDefiniteClauseKBKingsQueryEvilXReturnsJohnSucceeds(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory
                    .createKingsKnowledgeBase(infp);
            List<Term> terms = new List<Term>();
            terms.Add(new Variable("x"));
            Predicate query = new Predicate("Evil", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(1 == answer.getProofs().Count);
            Assert.IsTrue(1 == answer.getProofs()[0].getAnswerBindings()
                    .Count);
            Assert.AreEqual(new Constant("John"), answer.getProofs()[0]
                    .getAnswerBindings()[new Variable("x")]);
        }
コード例 #26
0
ファイル: FolDemoBase.cs プロジェクト: tvn-cosine/aima.net
        protected static void abcEqualityNoAxiomDemo(InferenceProcedure ip)
        {
            StandardizeApartIndexicalFactory.flush();

            FOLKnowledgeBase kb = FOLKnowledgeBaseFactory.createABCEqualityKnowledgeBase(ip, false);

            string kbStr = kb.ToString();

            TermEquality query = new TermEquality(new Constant("A"), new Constant("C"));

            InferenceResult answer = kb.ask(query);

            System.Console.WriteLine("ABC Equality No Axiom Knowledge Base:");
            System.Console.WriteLine(kbStr);
            System.Console.WriteLine("Query: " + query);
            foreach (Proof p in answer.getProofs())
            {
                System.Console.Write(ProofPrinter.printProof(p));
                System.Console.WriteLine("");
            }
        }
コード例 #27
0
        public static FOLKnowledgeBase createRingOfThievesKnowledgeBase(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory
                    .ringOfThievesDomain(), infp);

            // s(x) => ~c(x) One who skis never gets caught
            kb.tell("(Skis(x) => NOT(Caught(x)))");
            // c(x) => ~s(x) Those who are caught don't ever ski
            kb.tell("(Caught(x) => NOT(Skis(x)))");
            // p(x,y) & c(y) => s(x) Jailbird parents have skiing kids
            kb.tell("((Parent(x,y) AND Caught(y)) => Skis(x))");
            // s(x) & f(x,y) => s(y) All friends ski together
            kb.tell("(Skis(x) AND Friend(x,y) => Skis(y))");
            // f(x,y) => f(y,x) Friendship is symmetric
            kb.tell("(Friend(x,y) => Friend(y,x))");
            // FACTS
            // 1. { p(Mike,Joe) } Premise
            kb.tell("Parent(Mike, Joe)");
            // 2. { p(Janet,Joe) } Premise
            kb.tell("Parent(Janet,Joe)");
            // 3. { p(Nancy,Mike) } Premise
            kb.tell("Parent(Nancy,Mike)");
            // 4. { p(Ernie,Janet) } Premise
            kb.tell("Parent(Ernie,Janet)");
            // 5. { p(Bert,Nancy) } Premise
            kb.tell("Parent(Bert,Nancy)");
            // 6. { p(Red,Ernie) } Premise
            kb.tell("Parent(Red,Ernie)");
            // 7. { f(Red,Bert) } Premise
            kb.tell("Friend(Red,Bert)");
            // 8. { f(Drew,Nancy) } Premise
            kb.tell("Friend(Drew,Nancy)");
            // 9. { c(Mike) } Premise
            kb.tell("Caught(Mike)");
            // 10. { c(Ernie) } Premise
            kb.tell("Caught(Ernie)");

            return kb;
        }
コード例 #28
0
        protected void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase rotkb = FOLKnowledgeBaseFactory
                                     .createRingOfThievesKnowledgeBase(infp);
            List <Term> terms = new List <Term>();

            terms.Add(new Variable("x"));
            Predicate query = new Predicate("Skis", terms);

            InferenceResult answer = rotkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            // DB can expand infinitely so is only partial.
            Assert.IsTrue(answer.isPartialResultDueToTimeout());
            Assert.AreEqual(4, answer.getProofs().Count);
            Assert.AreEqual(1, answer.getProofs()[0].getAnswerBindings()
                            .Count);
            Assert.AreEqual(1, answer.getProofs()[1].getAnswerBindings()
                            .Count);
            Assert.AreEqual(1, answer.getProofs()[2].getAnswerBindings()
                            .Count);
            Assert.AreEqual(1, answer.getProofs()[3].getAnswerBindings()
                            .Count);

            List <Constant> expected = new List <Constant>();

            expected.Add(new Constant("Nancy"));
            expected.Add(new Constant("Red"));
            expected.Add(new Constant("Bert"));
            expected.Add(new Constant("Drew"));
            foreach (Proof p in answer.getProofs())
            {
                expected.Remove((Constant)p.getAnswerBindings()[new Variable("x")]);
            }
            Assert.AreEqual(0, expected.Count);
        }
コード例 #29
0
        public static FOLKnowledgeBase createRingOfThievesKnowledgeBase(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory
                                                       .ringOfThievesDomain(), infp);

            // s(x) => ~c(x) One who skis never gets caught
            kb.tell("(Skis(x) => NOT(Caught(x)))");
            // c(x) => ~s(x) Those who are caught don't ever ski
            kb.tell("(Caught(x) => NOT(Skis(x)))");
            // p(x,y) & c(y) => s(x) Jailbird parents have skiing kids
            kb.tell("((Parent(x,y) AND Caught(y)) => Skis(x))");
            // s(x) & f(x,y) => s(y) All friends ski together
            kb.tell("(Skis(x) AND Friend(x,y) => Skis(y))");
            // f(x,y) => f(y,x) Friendship is symmetric
            kb.tell("(Friend(x,y) => Friend(y,x))");
            // FACTS
            // 1. { p(Mike,Joe) } Premise
            kb.tell("Parent(Mike, Joe)");
            // 2. { p(Janet,Joe) } Premise
            kb.tell("Parent(Janet,Joe)");
            // 3. { p(Nancy,Mike) } Premise
            kb.tell("Parent(Nancy,Mike)");
            // 4. { p(Ernie,Janet) } Premise
            kb.tell("Parent(Ernie,Janet)");
            // 5. { p(Bert,Nancy) } Premise
            kb.tell("Parent(Bert,Nancy)");
            // 6. { p(Red,Ernie) } Premise
            kb.tell("Parent(Red,Ernie)");
            // 7. { f(Red,Bert) } Premise
            kb.tell("Friend(Red,Bert)");
            // 8. { f(Drew,Nancy) } Premise
            kb.tell("Friend(Drew,Nancy)");
            // 9. { c(Mike) } Premise
            kb.tell("Caught(Mike)");
            // 10. { c(Ernie) } Premise
            kb.tell("Caught(Ernie)");

            return(kb);
        }
コード例 #30
0
        protected void testDefiniteClauseKBWeaponsQueryCriminalXReturnsWestSucceeds(
            InferenceProcedure infp)
        {
            FOLKnowledgeBase wkb = FOLKnowledgeBaseFactory
                                   .createWeaponsKnowledgeBase(infp);
            List <Term> terms = new List <Term>();

            terms.Add(new Variable("x"));
            Predicate query = new Predicate("Criminal", terms);

            InferenceResult answer = wkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(1 == answer.getProofs().Count);
            Assert.IsTrue(1 == answer.getProofs()[0].getAnswerBindings()
                          .Count);
            Assert.AreEqual(new Constant("West"), answer.getProofs()[0]
                            .getAnswerBindings()[new Variable("x")]);
        }
コード例 #31
0
ファイル: FolDemoBase.cs プロジェクト: tvn-cosine/aima.net
        protected static void weaponsDemo(InferenceProcedure ip)
        {
            StandardizeApartIndexicalFactory.flush();

            FOLKnowledgeBase kb = FOLKnowledgeBaseFactory.createWeaponsKnowledgeBase(ip);

            string kbStr = kb.ToString();

            ICollection <Term> terms = CollectionFactory.CreateQueue <Term>();

            terms.Add(new Variable("x"));
            Predicate query = new Predicate("Criminal", terms);

            InferenceResult answer = kb.ask(query);

            System.Console.WriteLine("Weapons Knowledge Base:");
            System.Console.WriteLine(kbStr);
            System.Console.WriteLine("Query: " + query);
            foreach (Proof p in answer.getProofs())
            {
                System.Console.Write(ProofPrinter.printProof(p));
                System.Console.WriteLine("");
            }
        }
コード例 #32
0
 public FOLKnowledgeBase(FOLDomain domain,
         InferenceProcedure inferenceProcedure) : this(domain, inferenceProcedure, new Unifier())
 {
    
 }
コード例 #33
0
 public void setInferenceProcedure(InferenceProcedure inferenceProcedure)
 {
     if (null != inferenceProcedure)
     {
         this.inferenceProcedure = inferenceProcedure;
     }
 }
コード例 #34
0
 public FOLKnowledgeBase(FOLDomain domain, InferenceProcedure inferenceProcedure)
     : this(domain, inferenceProcedure, new Unifier())
 {
 }
コード例 #35
0
        protected void testFullFOLKBLovesAnimalQueryKillsJackTunaFalse(
                InferenceProcedure infp, bool expectedToTimeOut)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory
                    .createLovesAnimalKnowledgeBase(infp);
            List<Term> terms = new List<Term>();
            terms.Add(new Constant("Jack"));
            terms.Add(new Constant("Tuna"));
            Predicate query = new Predicate("Kills", terms);

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToTimeOut)
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsTrue(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Count);
            }
            else
            {
                Assert.IsTrue(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Count);
            }
        }
コード例 #36
0
        // Note: see -
        // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
        // slide 12 for where this test example was taken from.
        public static FOLKnowledgeBase createABCEqualityKnowledgeBase(
                InferenceProcedure infp, bool includeEqualityAxioms)
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");

            FOLKnowledgeBase kb = new FOLKnowledgeBase(domain, infp);

            kb.tell("B = A");
            kb.tell("B = C");

            if (includeEqualityAxioms)
            {
                // Reflexivity Axiom
                kb.tell("x = x");
                // Symmetry Axiom
                kb.tell("(x = y => y = x)");
                // Transitivity Axiom
                kb.tell("((x = y AND y = z) => x = z)");
            }

            return kb;
        }
コード例 #37
0
        // Note: see -
        // http://logic.stanford.edu/classes/cs157/2008/lectures/lecture15.pdf
        // slide 16,17, and 18 for where this test example was taken from.
        public static FOLKnowledgeBase createABCDEqualityAndSubstitutionKnowledgeBase(
                InferenceProcedure infp, bool includeEqualityAxioms)
        {
            FOLDomain domain = new FOLDomain();
            domain.addConstant("A");
            domain.addConstant("B");
            domain.addConstant("C");
            domain.addConstant("D");
            domain.addPredicate("P");
            domain.addFunction("F");

            FOLKnowledgeBase kb = new FOLKnowledgeBase(domain, infp);

            kb.tell("F(A) = B");
            kb.tell("F(B) = A");
            kb.tell("C = D");
            kb.tell("P(A)");
            kb.tell("P(C)");

            if (includeEqualityAxioms)
            {
                // Reflexivity Axiom
                kb.tell("x = x");
                // Symmetry Axiom
                kb.tell("(x = y => y = x)");
                // Transitivity Axiom
                kb.tell("((x = y AND y = z) => x = z)");
                // Function F Substitution Axiom
                kb.tell("((x = y AND F(y) = z) => F(x) = z)");
                // Predicate P Substitution Axiom
                kb.tell("((x = y AND P(y)) => P(x))");
            }

            return kb;
        }
コード例 #38
0
        protected void testEqualityNoAxiomsKBabcAEqualsCSucceeds(
                InferenceProcedure infp, bool expectedToFail)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory
                    .createABCEqualityKnowledgeBase(infp, false);

            TermEquality query = new TermEquality(new Constant("A"), new Constant(
                    "C"));

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToFail)
            {
                Assert.IsTrue(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Count);
            }
            else
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsTrue(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(1 == answer.getProofs().Count);
                Assert.IsTrue(0 == answer.getProofs()[0]
                        .getAnswerBindings().Count);
            }
        }
コード例 #39
0
        protected void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase rotkb = FOLKnowledgeBaseFactory
                    .createRingOfThievesKnowledgeBase(infp);
            List<Term> terms = new List<Term>();
            terms.Add(new Variable("x"));
            Predicate query = new Predicate("Skis", terms);

            InferenceResult answer = rotkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            // DB can expand infinitely so is only partial.
            Assert.IsTrue(answer.isPartialResultDueToTimeout());
            Assert.AreEqual(4, answer.getProofs().Count);
            Assert.AreEqual(1, answer.getProofs()[0].getAnswerBindings()
                    .Count);
            Assert.AreEqual(1, answer.getProofs()[1].getAnswerBindings()
                    .Count);
            Assert.AreEqual(1, answer.getProofs()[2].getAnswerBindings()
                    .Count);
            Assert.AreEqual(1, answer.getProofs()[3].getAnswerBindings()
                    .Count);

            List<Constant> expected = new List<Constant>();
            expected.Add(new Constant("Nancy"));
            expected.Add(new Constant("Red"));
            expected.Add(new Constant("Bert"));
            expected.Add(new Constant("Drew"));
            foreach (Proof p in answer.getProofs())
            {
                expected.Remove((Constant)p.getAnswerBindings()[new Variable("x")]);
            }
            Assert.AreEqual(0, expected.Count);
        }
コード例 #40
0
        protected void testEqualityAndSubstitutionNoAxiomsKBabcdPFFASucceeds(
                InferenceProcedure infp, bool expectedToFail)
        {
            FOLKnowledgeBase akb = FOLKnowledgeBaseFactory
                    .createABCDEqualityAndSubstitutionKnowledgeBase(infp, false);

            List<Term> terms = new List<Term>();
            terms.Add(new Constant("A"));
            Function fa = new Function("F", terms);
            terms = new List<Term>();
            terms.Add(fa);
            Function ffa = new Function("F", terms);
            terms = new List<Term>();
            terms.Add(ffa);
            Predicate query = new Predicate("P", terms);

            InferenceResult answer = akb.ask(query);

            Assert.IsTrue(null != answer);
            if (expectedToFail)
            {
                Assert.IsTrue(answer.isPossiblyFalse());
                Assert.IsFalse(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(0 == answer.getProofs().Count);
            }
            else
            {
                Assert.IsFalse(answer.isPossiblyFalse());
                Assert.IsTrue(answer.isTrue());
                Assert.IsFalse(answer.isUnknownDueToTimeout());
                Assert.IsFalse(answer.isPartialResultDueToTimeout());
                Assert.IsTrue(1 == answer.getProofs().Count);
                Assert.IsTrue(0 == answer.getProofs()[0]
                        .getAnswerBindings().Count);
            }
        }
コード例 #41
0
        protected void testDefiniteClauseKBKingsQueryKingXReturnsJohnAndRichardSucceeds(
                InferenceProcedure infp)
        {
            FOLKnowledgeBase kkb = FOLKnowledgeBaseFactory
                    .createKingsKnowledgeBase(infp);
            List<Term> terms = new List<Term>();
            terms.Add(new Variable("x"));
            Predicate query = new Predicate("King", terms);
            InferenceResult answer = kkb.ask(query);

            Assert.IsTrue(null != answer);
            Assert.IsFalse(answer.isPossiblyFalse());
            Assert.IsTrue(answer.isTrue());
            Assert.IsFalse(answer.isUnknownDueToTimeout());
            Assert.IsFalse(answer.isPartialResultDueToTimeout());
            Assert.IsTrue(2 == answer.getProofs().Count);
            Assert.IsTrue(1 == answer.getProofs()[0].getAnswerBindings()
                    .Count);
            Assert.IsTrue(1 == answer.getProofs()[1].getAnswerBindings()
                    .Count);

            bool gotJohn, gotRichard;
            gotJohn = gotRichard = false;
            Constant cJohn = new Constant("John");
            Constant cRichard = new Constant("Richard");
            foreach (Proof p in answer.getProofs())
            {
                Dictionary<Variable, Term> ans = p.getAnswerBindings();
                Assert.AreEqual(1, ans.Count);
                if (cJohn.Equals(ans[new Variable("x")]))
                {
                    gotJohn = true;
                }
                if (cRichard.Equals(ans[new Variable("x")]))
                {
                    gotRichard = true;
                }
            }
            Assert.IsTrue(gotJohn);
            Assert.IsTrue(gotRichard);
        }