示例#1
0
        public String predict(Example e)
        {
            String prediction = "~" + e.targetValue();

            if (null != currentBestHypothesis)
            {
                FOLExample etp = new FOLExample(folDSDomain, e, 0);
                kb.clear();
                kb.tell(etp.getDescription());
                kb.tell(currentBestHypothesis.getHypothesis());
                InferenceResult ir = kb.ask(etp.getClassification());
                if (ir.isTrue())
                {
                    if (trueGoalValue.Equals(e.targetValue()))
                    {
                        prediction = e.targetValue();
                    }
                }
                else if (ir.isPossiblyFalse() || ir.isUnknownDueToTimeout())
                {
                    if (!trueGoalValue.Equals(e.targetValue()))
                    {
                        prediction = e.targetValue();
                    }
                }
            }

            return(prediction);
        }
示例#2
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);
            }
        }
        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());
            }
        }
        protected void testHornClauseKBRingOfThievesQuerySkisXReturnsNancyRedBertDrew(InferenceProcedure infp)
        {
            FOLKnowledgeBase   rotkb = FOLKnowledgeBaseFactory.createRingOfThievesKnowledgeBase(infp);
            ICollection <Term> terms = CollectionFactory.CreateQueue <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().Size());
            Assert.AreEqual(1, answer.getProofs().Get(0).getAnswerBindings().Size());
            Assert.AreEqual(1, answer.getProofs().Get(1).getAnswerBindings().Size());
            Assert.AreEqual(1, answer.getProofs().Get(2).getAnswerBindings().Size());
            Assert.AreEqual(1, answer.getProofs().Get(3).getAnswerBindings().Size());

            ICollection <Constant> expected = CollectionFactory.CreateQueue <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(p.getAnswerBindings().Get(new Variable("x")) as Constant);
            }
            Assert.AreEqual(0, expected.Size());
        }
示例#5
0
        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("");
            }
        }
        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());
            }
        }
        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());
            }
        }
示例#8
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);
        }
示例#9
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("");
            }
        }
        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);
        }
        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());
        }
        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")));
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
0
        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("");
            }
        }
示例#17
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")]);
        }
示例#18
0
        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("");
            }
        }
        protected override InferenceResult Execute(CodeActivityContext context)
        {
            FOLKnowledgeBase kb = context.GetValue <FOLKnowledgeBase>(KB);

            return(kb.ask(AskText));
        }