/// <summary> /// <para> /// Creates a map data used in stress tests. /// </para> /// </summary> /// /// <returns> /// the created <see cref="MapData"/> object. /// </returns> internal static MapData CreateMapData() { // the map data MapData mapdata = new MapData(); // create nodes for (int i = 0; i < 100; i++) { MapNode node = new MapNode(); node.Id = i + 1; node.Name = "node" + node.Id; mapdata.AddNode(node); } // create links MapLink link = new MapLink(); link.Id = 50; link.Name = "link1"; link.AddAttribute(CreatePathAttribute(1, 1, 2)); link.AddAttribute(CreatePathAttribute(2, 1, "node50")); mapdata.AddLink(link); // create ports MapPort port = new MapPort(); port.Id = 1; port.Name = "port1"; port.Node = mapdata.GetNodeById(1); port.Node.Ports.Add(port); link.AddPort(port); mapdata.AddPort(port); port = new MapPort(); port.Id = 50; port.Name = "port2"; port.Node = mapdata.GetNodeById(100); port.Node.Ports.Add(port); link.AddPort(port); mapdata.AddPort(port); mapdata.Header = new MapHeader(); mapdata.Header.Lifecycle = "xxx"; return(mapdata); }
/// <summary> /// This method performs the following logic: /// For each link in the mapdata, it gets the nodes defining the link from its 'path' attributes. /// For each such node, it creates a new synthetic node as a child of the node. /// For each such synthetic node, it creates a port for it. /// It then forms new links between the nodes of the original link and the new ports formed. /// Finally it adds all the new links formed to the mapdata and removes the original link. /// </summary> /// <param name="mapdata">MapData to be processed</param> private void AddNewLinksAndSyntheticNodes(MapData mapdata) { IList <MapLink> linksToDelete = new List <MapLink>(); IList <MapLink> linksToAdd = new List <MapLink>(); foreach (ILink link in mapdata.Links) { MapLink mapLink = (MapLink)link; //Get the path attributes IList <IAttribute> pathAttrs = mapLink.GetAttributesByName(Path); //Preserve the original link if no path attributes are found if (pathAttrs == null || pathAttrs.Count == 0) { continue; } //Get the nodes of the path IList <MapNode> pathNodes = new List <MapNode>(); foreach (IAttribute attr in pathAttrs) { MapAttribute pathAttr = (MapAttribute)attr; if (pathAttr.Type.Equals(AttrTypeInt)) { pathNodes.Add((MapNode)mapdata.GetNodeById(pathAttr.IntValue)); } else if (pathAttr.Type.Equals(AttrTypeString)) { pathNodes.Add((MapNode)mapdata.GetNodeByName(pathAttr.StringValue)); } } //Create the synthetic nodes for each path node. IList <MapNode> syntheticNodes = new List <MapNode>(); foreach (MapNode pathNode in pathNodes) { if (mapdata.Nodes.Contains(pathNode)) { //Get the unique id for the new synthetic node and set it long syntheticNodeId = GetUniqueId(typeof(MapNode), mapdata, 0); MapNode syntheticNode = new MapNode(); syntheticNode.Id = syntheticNodeId; syntheticNode.Name = MapNodeNamePrefix + syntheticNodeId; //Add it as child node of pathNode pathNode.AddOccupant(syntheticNode); syntheticNode.Container = pathNode; //Add it to the mapData mapdata.AddNode(syntheticNode); //Create and add port to synthetic node. long syntheticNodePortId = GetUniqueId(typeof(MapPort), mapdata, 0); MapPort syntheticNodePort = new MapPort(); syntheticNodePort.Id = syntheticNodePortId; syntheticNodePort.Name = MapPortNamePrefix + syntheticNodePortId; syntheticNode.AddPort(syntheticNodePort); //Add synthetic node to the port syntheticNodePort.Node = syntheticNode; //Add created port to the mapdata mapdata.AddPort(syntheticNodePort); //Save it further processing syntheticNodes.Add(syntheticNode); } } //None of the path nodes were found in the mapdata nodes. if (syntheticNodes.Count == 0) { continue; } //Create new links //1)Note that we first get port and then get the node from the port. //See http://forums.topcoder.com/?module=Thread&threadID=592050&start=0&mc=24#885358 //2)Links are guaranteed to have exactly 2 nodes MapPort startPort = (MapPort)mapLink.Ports[0]; MapNode startNode = (MapNode)startPort.Node; MapNode endNode = null; MapPort endPort = null; for (int i = 0; i < syntheticNodes.Count; i++) { endNode = syntheticNodes[i]; endPort = (MapPort)syntheticNodes[i].Ports[0]; //Create link and save it for adding later linksToAdd.Add(CreateNewLink(startNode, endNode, startPort, endPort, mapdata, mapLink, linksToAdd.Count)); startNode = endNode; startPort = endPort; } endPort = (MapPort)mapLink.Ports[1]; endNode = (MapNode)endPort.Node; //Create last link and save it for adding later linksToAdd.Add(CreateNewLink(startNode, endNode, startPort, endPort, mapdata, mapLink, linksToAdd.Count)); //Save original link for deleting it later linksToDelete.Add(mapLink); } //Remove the original links foreach (MapLink linkToDelete in linksToDelete) { //Remove link's attributes from mapdata foreach (IAttribute linkAttr in linkToDelete.Attributes) { mapdata.RemoveAttribute((MapAttribute)linkAttr); } //REmove the link itself from mapdata mapdata.RemoveLink(linkToDelete); } //Add the new links foreach (MapLink linkToAdd in linksToAdd) { mapdata.AddLink(linkToAdd); } }