コード例 #1
0
        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);
                    }
                }
            }
        }
コード例 #2
0
        //generates knowledge base and truth table
        private void generateData()
        {
            KB = new KnowledgeBase();
            //KB.Interpret("p2=> p3; p3 => p1; c => e; b&e => f; f&g => h; p1=>d; p1&p3 => c; a; b; p2;");

            KB.Interpret(attributesString, AssertionEnum.Assertion);

            tt = new TruthTable(KB);
            printTable();
        }
コード例 #3
0
 public ForwardChain(KnowledgeBase kB)
 {
     _kB = kB;
 }
コード例 #4
0
ファイル: TruthTable.cs プロジェクト: mrpie95/AI_Assignment2
        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();
        }
コード例 #5
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());
                }
            }

            if (created == null)
            {
                //Or section
                deconstruction = KnowledgeBase.DelimitString(input, new string[] { Or.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 Or(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);
        }