Esempio n. 1
0
        public bool Ask(KnowledgeBase kb, string predicate)
        {
            switch (Procedure)
            {
            case PLInferenceProcedure.TTEntails:
                return(kb.askWithTTEntails(predicate));

            case PLInferenceProcedure.PLFCEntails:
                return(new PLFCEntails().plfcEntails(kb, new PropositionSymbol(predicate)));

            case PLInferenceProcedure.Resolve:
                return(new PLResolution().plResolution(kb, (Sentence)parser.parse(predicate)));

            case PLInferenceProcedure.DPLLEntails:
                return(new DPLLSatisfiable().isEntailed(kb, (Sentence)parser.parse(predicate)));

            case PLInferenceProcedure.DPLLSatisfiable:
                return(new DPLLSatisfiable().dpllSatisfiable((Sentence)parser.parse(predicate)));

            case PLInferenceProcedure.WalkSatPredicate:
                return(new WalkSAT().walkSAT(ConvertToConjunctionOfClauses.convert((Sentence)parser.parse(predicate)).getClauses(), WalkSatProbability, WalkSatMaxFlips) != null);

            case PLInferenceProcedure.WalkSatKb:
                return(new WalkSAT().walkSAT(kb.asCNF(), WalkSatProbability, WalkSatMaxFlips) != null);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 2
0
        public void testCollectSymbolsFromComplexSentence()
        {
            Sentence sentence          = (Sentence)parser.parse("(~B11 | P12 | P21) & (B11 | ~P12) & (B11 | ~P21)");
            ISet <PropositionSymbol> s = SymbolCollector.getSymbolsFrom(sentence);

            Assert.AreEqual(3, s.Size());
            Sentence b11 = parser.parse("B11");
            Sentence p21 = parser.parse("P21");
            Sentence p12 = parser.parse("P12");

            Assert.IsTrue(s.Contains(b11 as PropositionSymbol));
            Assert.IsTrue(s.Contains(p21 as PropositionSymbol));
            Assert.IsTrue(s.Contains(p12 as PropositionSymbol));
        }
Esempio n. 3
0
        public void testAIMAExample()
        {
            KnowledgeBase kb = new KnowledgeBase();

            kb.tell("P => Q");
            kb.tell("L & M => P");
            kb.tell("B & L => M");
            kb.tell("A & P => L");
            kb.tell("A & B => L");
            kb.tell("A");
            kb.tell("B");
            PropositionSymbol q = (PropositionSymbol)parser.parse("Q");

            Assert.AreEqual(true, plfce.plfcEntails(kb, q));
        }
Esempio n. 4
0
        public void testWalkSat()
        {
            WalkSAT walkSAT = new WalkSAT();
            Model   m       = walkSAT.walkSAT(ConvertToConjunctionOfClauses.convert(parser.parse("A & B"))
                                              .getClauses(), 0.5, 1000);

            if (m == null)
            {
                System.Console.WriteLine("failure");
            }
            else
            {
                m.print();
            }
        }
Esempio n. 5
0
        public void testComplexSentence()
        {
            string p = "P";
            string q = "Q";

            m = m.union(new PropositionSymbol(p), true);
            m = m.union(new PropositionSymbol(q), false);
            Sentence sent = (Sentence)parser.parse("((P | Q) &  (P => Q))");

            Assert.IsFalse(m.isTrue(sent));
            Assert.IsTrue(m.isFalse(sent));
            Sentence sent2 = (Sentence)parser.parse("((P | Q) & (Q))");

            Assert.IsFalse(m.isTrue(sent2));
            Assert.IsTrue(m.isFalse(sent2));
        }
Esempio n. 6
0
        /**
         * Returns the answer to the specified question using the TT-Entails
         * algorithm.
         *
         * @param queryString
         *            a question to ASK the knowledge base
         *
         * @return the answer to the specified question using the TT-Entails
         *         algorithm.
         */
        public bool askWithTTEntails(string queryString)
        {
            PLParser parser = new PLParser();

            Sentence alpha = parser.parse(queryString);

            return(new TTEntails().ttEntails(this, alpha));
        }
Esempio n. 7
0
        static void displayResolutionResults(KnowledgeBase kb, string query)
        {
            PLParser parser = new PLParser();

            System.Console.WriteLine("Running plResolution of query "
                                     + query
                                     + " on knowledgeBase  gives "
                                     + plr.plResolution(kb, parser.parse(query)));
        }
Esempio n. 8
0
        public static void displayDPLLSatisfiableStatus(string query)
        {
            PLParser parser = new PLParser();

            if (dpll.dpllSatisfiable(parser.parse(query)))
            {
                System.Console.WriteLine(query + " is  (DPLL) satisfiable");
            }
            else
            {
                System.Console.WriteLine(query + " is NOT (DPLL)  satisfiable");
            }
        }
Esempio n. 9
0
 public void setUp()
 {
     parser                = new PLParser();
     trueSentence          = (Sentence)parser.parse("true");
     falseSentence         = (Sentence)parser.parse("false");
     andSentence           = (Sentence)parser.parse("(P  &  Q)");
     orSentence            = (Sentence)parser.parse("(P  |  Q)");
     impliedSentence       = (Sentence)parser.parse("(P  =>  Q)");
     biConditionalSentence = (Sentence)parser.parse("(P  <=>  Q)");
     m = new Model();
 }
Esempio n. 10
0
        public void testAtomicSentenceTrueParse()
        {
            sentence = parser.parse("true");
            expected = prettyPrintF("True");
            Assert.IsTrue(sentence.isPropositionSymbol());
            Assert.AreEqual(expected, sentence.ToString());

            sentence = parser.parse("(true)");
            expected = prettyPrintF("True");
            Assert.IsTrue(sentence.isPropositionSymbol());
            Assert.AreEqual(expected, sentence.ToString());

            sentence = parser.parse("((true))");
            expected = prettyPrintF("True");
            Assert.IsTrue(sentence.isPropositionSymbol());
            Assert.AreEqual(expected, sentence.ToString());
        }
Esempio n. 11
0
 /**
  * Adds the specified sentence to the knowledge base.
  *
  * @param aSentence
  *            a fact to be added to the knowledge base.
  */
 public void tell(string aSentence)
 {
     tell((Sentence)parser.parse(aSentence));
 }
Esempio n. 12
0
        public void testDPLLReturnsTrueWhenAllClausesTrueInModel()
        {
            Model model = new Model();

            model = model.union(new PropositionSymbol("A"), true).union(
                new PropositionSymbol("B"), true);
            Sentence      sentence = parser.parse("A & B & (A | B)");
            ISet <Clause> clauses  = ConvertToConjunctionOfClauses.convert(sentence)
                                     .getClauses();
            ICollection <PropositionSymbol> symbols = CollectionFactory.CreateQueue <PropositionSymbol>(
                SymbolCollector.getSymbolsFrom(sentence));

            bool satisfiable = dpll.dpll(clauses, symbols, model);

            Assert.AreEqual(true, satisfiable);
        }
Esempio n. 13
0
        public void testSymbolTransform()
        {
            Sentence             symbol      = parser.parse("A");
            ConjunctionOfClauses transformed = ConvertToConjunctionOfClauses.convert(symbol);

            Assert.AreEqual("{{A}}", transformed.ToString());
        }
Esempio n. 14
0
        public void testPLResolveWithOneLiteralMatching()
        {
            Clause one      = Util.first(ConvertToConjunctionOfClauses.convert(parser.parse("A | B")).getClauses());
            Clause two      = Util.first(ConvertToConjunctionOfClauses.convert(parser.parse("~B | C")).getClauses());
            Clause expected = Util.first(ConvertToConjunctionOfClauses.convert(parser.parse("A | C")).getClauses());

            ISet <Clause> resolvents = resolution.plResolve(one, two);

            Assert.AreEqual(1, resolvents.Size());
            Assert.IsTrue(resolvents.Contains(expected));
        }
Esempio n. 15
0
        public void testSymbolTransform()
        {
            Sentence symbol      = parser.parse("A");
            Sentence transformed = ConvertToCNF.convert(symbol);

            Assert.AreEqual("A", transformed.ToString());
        }