private int GetTopPostionIndex(List <InsertElement> ElementList, List <State> StateList) { MatchHelper.Assert(ElementList.Count != StateList.Count, @" FUCTION GetTopPostionIndex ERROR, ElementList's Length not equal to StateList's Length "); int index = -1; for (int i = 0; i < ElementList.Count; i++) { // is new place and index = 0, inserted node is root if ((ElementList[i].Mode & PlaceMask) == (UInt16)InsertMode.NewPlace) { index = i; break; } if ((ElementList[i].Mode & PlaceMask) == (UInt16)InsertMode.OldPlace) { if (StateList[i].NodeCount > 1) { throw new TrainException(ElementList[i], "Top Insert position is multi-matched, insert failed"); } else if (StateList[i].NodeCount == 0) { throw new TrainException(ElementList[i], "Top Insert position is non-matched, insert failed"); } else { index = i + 1; // continue loop continue; } } if ((ElementList[i].Mode & PlaceMask) == (UInt16)InsertMode.AutoPlace) { if (StateList[i].NodeCount == 1) { index = i + 1; continue; } else if (StateList[i].NodeCount > 1) { throw new TrainException(ElementList[i], "Insert element list is not sufficient, insert failed"); } else if (StateList[i].NodeCount == 0) { index = i; break; } } } MatchHelper.Assert(index == -1, @" FUCTION GetTopPostionIndex ERROR, judge position index error, skip all if statement "); return(index); }
public static void TestRef() { UInt16 a = 0x200; InsertElement e = new InsertElement("", LEVEL.Building, InsertMode.AutoLevel | InsertMode.AutoPlace); Console.WriteLine("a is = " + Convert.ToString(a, 2)); MatchHelper.Assert(true, "aaaa"); Console.ReadLine(); }
// TODO Construct transaction and support roll back? 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); }