Пример #1
0
        static void TTable(KnowledgeBase KB)
        {
            TruthTable tt = new TruthTable(KB);


            Console.Write(tt.Solution());
        }
Пример #2
0
        //Interprets text file
        public void Interpret(string input, AssertionEnum isAssertion)
        {
            List <string> stats = new List <string>(KnowledgeBase.DelimitString(input, new string[] { KnowledgeBase.Delimiter }, new string[] { " " }));

            if (isAssertion == AssertionEnum.Assertion)
            {
                foreach (string s in stats)
                {
                    Statement stat = this.GenerateStatement(s);

                    if ((stat as Variable) != null)
                    {
                        (stat as Variable).SetValue(true);
                        (stat as Variable).Defined = true;
                    }

                    bool found = false;

                    foreach (Statement statments in _assertions)
                    {
                        if (statments.Identifier == stat.Identifier)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        _assertions.Add(stat);
                    }
                }
            }

            else if (isAssertion == AssertionEnum.Query)
            {
                foreach (string s in stats)
                {
                    Statement stat = this.GenerateStatement(s);

                    //can't set query variables

                    bool found = false;

                    foreach (Statement statments in _queries)
                    {
                        if (statments.Identifier == stat.Identifier)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        _queries.Add(stat);
                    }
                }
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Arguments invalid please enter <file name> and <mode: TT, FC, BC>");
                return;
            }

            KnowledgeBase KB = new KnowledgeBase();

            KB.Load(args[1]);

            if (args[0].ToLower() == "fc".ToLower())
            {
                ChainF(KB);
            }

            else if (args[0].ToLower() == "tt".ToLower())
            {
                TTable(KB);
            }

            else if (args[0].ToLower() == "bc".ToLower())
            {
                ChainB(KB);
            }

            else
            {
                Console.WriteLine(args[1] + " was not recognised");
            }
        }
Пример #4
0
        public Chain(KnowledgeBase kB)
        {
            _kB = kB;

            this.Populate();

            this.InitialiseFrontier();
        }
Пример #5
0
 public BackwardChain(KnowledgeBase kB) : base(kB)
 {
 }
Пример #6
0
        public TruthTable(KnowledgeBase kB)
        {
            _kB = kB;

            //add in assertions specifically
            foreach (Statement s in _kB.Assertions)
            {
                bool found = false;
                foreach (Statement stat in _labels)
                {
                    if (stat.Identifier == s.Identifier)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    _labels.Add(s);
                    _values.Add(new List <bool>());
                }
            }

            //load in queries specifically
            foreach (Statement s in kB.Queries)
            {
                bool found = false;
                foreach (Statement stat in _labels)
                {
                    if (stat.Identifier == s.Identifier)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    _labels.Add(s);
                    _values.Add(new List <bool>());
                }
            }

            foreach (Statement s in kB.Universe)
            {
                if ((s as Variable) != null)
                {
                    bool found = false;
                    foreach (Statement stat in _labels)
                    {
                        if (stat.Identifier == s.Identifier)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        _labels.Add(s);
                        _values.Add(new List <bool>());
                    }
                }
            }

            this.OrderVariables();
            this.OrderDependancies();

            this.Populate();

            this.Clean();
        }
Пример #7
0
        private Statement GenerateStatement(string input)
        {
            //The order these sections are placed implies the order of logical operations

            string[]  deconstruction;
            Statement created = null;

            if (created == null)
            {
                //Implication section
                deconstruction = KnowledgeBase.DelimitString(input, new string[] { Implication.Symbol }, new string[] { });

                //if 1 string is returned the string did not contain the delimitor
                if (deconstruction.Length > 1)
                {
                    if (deconstruction.Length > 2)
                    {
                        throw new Exception("Implication format failure");
                    }

                    List <Statement> localWorld = new List <Statement>();
                    foreach (string s in deconstruction)
                    {
                        localWorld.Add(this.GenerateStatement(s));
                    }

                    created = new Implication(localWorld[0], localWorld[1]);
                }
            }

            if (created == null)
            {
                //And section
                deconstruction = KnowledgeBase.DelimitString(input, new string[] { And.Symbol }, new string[] { });

                if (deconstruction.Length > 1)
                {
                    List <Statement> localWorld = new List <Statement>();
                    foreach (string s in deconstruction)
                    {
                        localWorld.Add(this.GenerateStatement(s));
                    }

                    created = new And(localWorld.ToArray());
                }
            }

            //Variables section

            if (created == null)
            {
                created = new Variable(input, false);
            }

            //search for statements with the same identifier

            if (created == null)
            {
                throw new Exception(input + " not understood");
            }

            bool found = false;

            foreach (Statement s in _universe)
            {
                if (s.Identifier == created.Identifier)
                {
                    created = s;
                    found   = true;
                    break;
                }
            }

            if (!found)
            {
                _universe.Add(created);
            }

            return(created);
        }
Пример #8
0
        static void ChainB(KnowledgeBase KB)
        {
            BackwardChain b = new BackwardChain(KB);

            Console.WriteLine(b.Solution());
        }
Пример #9
0
        static void ChainF(KnowledgeBase KB)
        {
            ForwardChain f = new ForwardChain(KB);

            Console.WriteLine(f.Solution());
        }
Пример #10
0
 public ForwardChain(KnowledgeBase kB) : base(kB)
 {
 }