コード例 #1
0
        public void RemoveEffect(ChainNode toRemove)
        {
            int i = 0;

            while (i < _effect.Count)
            {
                if (toRemove.Identifier == _effect[i].Identifier)
                {
                    _effect.RemoveAt(i);
                }

                else
                {
                    i += 1;
                }
            }
        }
コード例 #2
0
        public void RemoveCause(ChainNode toRemove)
        {
            int i = 0;

            while (i < _cause.Count)
            {
                if (toRemove.Identifier == _cause[i].Identifier)
                {
                    _cause.RemoveAt(i);
                }

                else
                {
                    i += 1;
                }
            }
        }
コード例 #3
0
        public bool CheckCausedBy(ChainNode b)
        {
            if (this.IsOr)
            {
                foreach (ChainNode c in this.Causes)
                {
                    if (c == b)//if a cause node is b then this is caused by b
                    {
                        return(true);
                    }

                    else if (c.CheckCausedBy(b))
                    {
                        return(true);
                    }
                }

                return(false);
            }

            else
            {
                bool result = true;

                foreach (ChainNode c in this.Causes)
                {
                    if (c != b)//if a cause node is b then this is caused by b
                    {
                        return(true);
                    }

                    else if (c.CheckCausedBy(b))
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }
コード例 #4
0
 public void AddEffect(ChainNode toAdd)
 {
     _effect.Add(toAdd);
 }
コード例 #5
0
 public void AddCause(ChainNode toAdd)
 {
     _cause.Add(toAdd);
 }
コード例 #6
0
        public static void EstablishRelationship(ChainNode parent, ChainNode child)
        {
            parent.AddEffect(child);

            child.AddCause(parent);
        }
コード例 #7
0
ファイル: Chain.cs プロジェクト: mrpie95/AI_Assignment2
        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);
                    }
                }
            }
        }