예제 #1
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);
        }
예제 #2
0
        public void Populate()
        {
            _nodes.Clear();

            foreach (Statement stat in _kB.Universe) // populate with all (includes assertions and queieries)
            {
                if ((stat as Implication) != null)   //implications express relationships not nodes
                {
                    //don't add implications
                }

                else
                {
                    ChainNode toAdd = new ChainNode(stat);

                    if ((stat as Variable) != null)
                    {
                        toAdd.IsOr = true;
                    }

                    _nodes.Add(toAdd);
                }
            }

            foreach (Statement stat in _kB.Assertions)// set assertions
            {
                foreach (ChainNode chan in _nodes)
                {
                    if (chan.Identifier == stat.Identifier)
                    {
                        chan.Asserted = true;
                    }
                }
            }

            foreach (Statement stat in _kB.Universe)// set relationships
            {
                if ((stat as Variable) != null)
                {
                    //no self evident causes
                }

                else if ((stat as And) != null)
                {
                    And a = (stat as And);

                    int toFind = a.Stats.Length;

                    int         index   = 0;
                    ChainNode[] parents = new ChainNode[toFind];

                    ChainNode child = null;

                    foreach (ChainNode n in _nodes)
                    {
                        if (n.Stat.Identifier != a.Identifier)
                        {
                            if (a.ComprisedOf(n.Identifier))
                            {
                                parents[index] = n;

                                index += 1;
                            }
                        }

                        else
                        {
                            child = n;
                        }
                    }

                    foreach (ChainNode p in parents)
                    {
                        ChainNode.EstablishRelationship(p, child);
                    }
                }

                else if ((stat as Implication) != null)
                {
                    Implication i = (stat as Implication);

                    ChainNode parent = null;
                    ChainNode child  = null;

                    foreach (ChainNode n in _nodes)
                    {
                        if (i.ComprisedOf(n.Identifier))
                        {
                            //relationship always established after here
                            if (i.CausedBy(n.Identifier))
                            {
                                parent = n;
                            }

                            else
                            {
                                child = n;
                            }
                        }
                    }

                    ChainNode.EstablishRelationship(parent, child);
                }

                else//exaustive list
                {
                    throw new Exception("Unknown statement: " + stat.Identifier);
                }
            }

            /*int j = 0;
             * while (j < _nodes.Count)//remove non-variables
             * {
             *  List<ChainNode> causes = new List<ChainNode>();
             *  List<ChainNode> effects = new List<ChainNode>();
             *
             *  if ((_nodes[j].Stat as Variable) == null)//non variable
             *  {
             *      foreach (ChainNode c in _nodes[j].Causes)
             *      {
             *          causes.Add(c);
             *
             *          c.RemoveEffect(_nodes[j]);//disassociate
             *      }
             *
             *      foreach (ChainNode e in _nodes[j].Effects)
             *      {
             *          effects.Add(e);
             *
             *          e.RemoveCause(_nodes[j]);//disassociate
             *      }
             *
             *      _nodes.RemoveAt(j);
             *
             *      foreach (ChainNode c in causes)
             *      {
             *          foreach (ChainNode e in effects)
             *          {
             *              e.AddCause(c);//associate
             *              c.AddEffect(e);
             *          }
             *      }
             *  }
             *
             *  else
             *  {
             *      j += 1;
             *  }
             * }*/

            foreach (ChainNode n in _nodes) // fill queries
            {
                foreach (Statement q in _kB.Queries)
                {
                    if (q.Identifier == n.Identifier)
                    {
                        _queries.Add(n);
                    }
                }
            }
        }