AddressMatch.Training.TrainMachine._insert C# (CSharp) Method

_insert() private method

private _insert ( List ElementList, List StateList ) : bool
ElementList List
StateList List
return bool
        private bool _insert(List<InsertElement> ElementList,List<State> StateList)
        {
            int start = GetTopPostionIndex(ElementList, StateList);

            GraphNode fathernode = null;
            //if linked to root?
            if (start == 0 && StateList[0].NodeCount == 0)
            {
                GraphNode node = new GraphNode(ElementList[0].Name, ElementList[0].Level);
                fathernode = node;
                _addrset.Insert(node, AddrSet.AddrGraph.root);
                start++;
            }
            //TODO  need test more
            fathernode = fathernode != null ? fathernode : StateList[start-1].NodeList.Single();

            for (; start < ElementList.Count; start++)
            {
                #region OldPlace
                if ((ElementList[start].Mode & PlaceMask) == (UInt16)InsertMode.OldPlace)
                {
                    if (StateList[start].NodeCount == 1)
                    {
                        fathernode = fathernode != null ? fathernode : StateList[start].NodeList.Single();
                    }
                    else if (StateList[start].NodeCount > 1)
                    {

                        List<GraphNode> searchresult = _addrset.ForwardSearchNode(delegate(GraphNode node)
                        {
                            return node.Name == ElementList[start].Name
                                        && node.NodeLEVEL == ElementList[start].Level;
                        }, fathernode);

                        if (searchresult.Count > 1)
                        {
                            throw new TrainException(ElementList[start], "Existed Node is Multi-matched");
                        }
                        if (searchresult.Count == 0)
                        {
                            throw new TrainException(ElementList[start], "Existed Node is not founded");
                        }
                        if (searchresult.Count == 1)
                        {
                            fathernode = searchresult.Single();
                        }
                    }

                }
                #endregion

                #region NewPalce
                if ((ElementList[start].Mode & PlaceMask) == (UInt16)InsertMode.NewPlace)
                {
                    MatchHelper.Assert(fathernode == null,
                                                 @" FUCTION _insert ERROR,
                                                         father node is null, bug may be exist in Func GetTopPostionIndex  ");

                    GraphNode node = new GraphNode(ElementList[start].Name, ElementList[start].Level);

                    //judge the Level
                    if ((ElementList[start].Mode & LevelMask) == (UInt16)InsertMode.ExactlyLevel)
                    {
                        if (fathernode.NodeLEVEL > ElementList[start].Level)
                        {
                            throw new TrainException(ElementList[start], @"LEVEL smaller than father node, insert failed");
                        }
                    }
                    if ((ElementList[start].Mode & LevelMask) == (UInt16)InsertMode.DegradeLevel
                                     || (ElementList[start].Mode & LevelMask) == (UInt16)InsertMode.AutoLevel)
                    {
                        if (fathernode.NodeLEVEL > ElementList[start].Level)
                        {
                            node.NodeLEVEL = fathernode.NodeLEVEL != LEVEL.Uncertainty ? fathernode.NodeLEVEL + 1 : LEVEL.Uncertainty;
                        }
                    }
                    _addrset.Insert(node, fathernode);

                    fathernode = node;

                }
                #endregion

                #region AutoPlace
                if ((ElementList[start].Mode & PlaceMask) == (UInt16)InsertMode.AutoPlace)
                {
                    if ((ElementList[start].Mode & LevelMask) == (UInt16)InsertMode.ExactlyLevel)
                    {
                        GraphNode node = new GraphNode(ElementList[start].Name, ElementList[start].Level);

                        List<GraphNode> searchresult = _addrset.ForwardSearchNode(delegate(GraphNode gn)
                        {
                            return gn.Name == ElementList[start].Name
                                && gn.NodeLEVEL == ElementList[start].Level;
                        }, fathernode);

                        if (searchresult.Count >= 1)
                        {
                            fathernode = searchresult.First();
                        }
                        if (searchresult.Count == 0)
                        {

                            _addrset.Insert(node, fathernode);

                            fathernode = node;
                        }
                    }

                    if ((ElementList[start].Mode & LevelMask) == (UInt16)InsertMode.DegradeLevel
                                || (ElementList[start].Mode & LevelMask) == (UInt16)InsertMode.AutoLevel)
                    {
                        GraphNode node = new GraphNode(ElementList[start].Name, ElementList[start].Level);

                        List<GraphNode> searchresult = _addrset.ForwardSearchNode(delegate(GraphNode gn)
                        {
                            return gn.Name == ElementList[start].Name;
                        }, fathernode);

                        if (searchresult.Count >= 1)
                        {
                            fathernode = searchresult.First();
                        }

                        if (searchresult.Count == 0)
                        {
                            node.NodeLEVEL = fathernode.NodeLEVEL != LEVEL.Uncertainty ? fathernode.NodeLEVEL + 1 : LEVEL.Uncertainty;

                            _addrset.Insert(node, fathernode);

                            fathernode = node;
                        }
                    }
                }
                #endregion
            }

            return true;
        }