Exemplo n.º 1
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);
                    }
                }
            }
        }
Exemplo n.º 2
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);
        }