/// <summary> /// A utility function for displaying the properties of the MapData /// </summary> /// <param name="md">The mapdata to display</param> private static void DisplayMapdataInfo(MapData md) { Console.WriteLine("MapData header lifecycle: " + md.Header.Lifecycle); foreach (ILink link in md.Links) { MapLink ml = (MapLink)link; Console.WriteLine("Link : "); Console.WriteLine(" Name: " + ml.Name); Console.WriteLine(" Id: " + ml.Id); } foreach (IPort port in md.Ports) { MapPort mp = (MapPort)port; Console.WriteLine("Port : "); Console.WriteLine(" Name: " + mp.Name); Console.WriteLine(" Id: " + mp.Id); } foreach (INode node in md.Nodes) { MapNode mn = node as MapNode; if (mn != null) { Console.WriteLine("Node : "); Console.WriteLine(" Name: " + mn.Name); Console.WriteLine(" Id: " + mn.Id); } } Console.WriteLine(); }
/// <summary> /// This method sets the minimal size of each port in the mapdata. /// </summary> /// <param name="mapdata">MapData to be processed</param> private void CalculateMinimalSizeForPorts(MapData mapdata) { //calculate the minimal size for each port foreach (IPort port in mapdata.Ports) { MapPort mapPort = (MapPort)port; //Set its minimal size mapPort.MinimalSize = new Dimension(MinimumPortHeight, MinimumPortWidth); } }
/// <summary> /// Test that the PreProcess method sets the correct minimal sizes for all ports /// </summary> /// <param name="md">The MapDat inputted to the PreProcess method.</param> public void TestPreProcessPortSizes(MapData md) { MapData ret = PreProcess(md); foreach (IPort port in md.Ports) { MapPort mapPort = (MapPort)port; Assert.AreEqual(mapPort.MinimalSize.Height, MinimumPortHeight, "Incorrect minimal size for port."); Assert.AreEqual(mapPort.MinimalSize.Width, MinimumPortWidth, "Incorrect minimal size for port."); } }
/// <summary> /// Test that the PreProcess method creates the new synthetic nodes correctly. /// </summary> /// <param name="md">The MapDat inputted to the PreProcess method.</param> public void TestPreProcessNewSyntheticNodes(MapData md) { MapData ret = PreProcess(md); //Node X MapNode nodeX = (MapNode)ret.GetNodeById(666); //The synthetic node for nodeX MapNode synX = (MapNode)ret.GetNodeById(-669); Assert.IsNotNull(synX, "Synthetic node for nodeX must be created."); Assert.AreEqual(nodeX, synX.Container, "The synthetic node must be child of the nodeX."); //Port for nodeX MapPort portX = (MapPort)ret.GetPortById(-1); Assert.IsNotNull(portX, "The new port created must be added to mapdata."); Assert.AreEqual(synX, portX.Node, "The synthetic node for X must be the node of portX"); Assert.AreEqual(portX, synX.Ports[0], "The new portX must be added to synthetic node X's ports."); //Node Y MapNode nodeY = (MapNode)ret.GetNodeById(667); //The synthetic node for nodeY MapNode synY = (MapNode)ret.GetNodeById(-670); Assert.IsNotNull(synY, "Synthetic node for nodeY must be created."); Assert.AreEqual(nodeY, synY.Container, "The synthetic node must be child of the nodeY."); //Port for nodeY MapPort portY = (MapPort)ret.GetPortById(-2); Assert.IsNotNull(portY, "The new port created must be added to mapdata."); Assert.AreEqual(synY, portY.Node, "The synthetic node for Y must be the node of portY"); Assert.AreEqual(portY, synY.Ports[0], "The new portY must be added to synthetic node Y's ports."); //Node Z MapNode nodeZ = (MapNode)ret.GetNodeById(668); //The synthetic node for nodeZ MapNode synZ = (MapNode)ret.GetNodeById(-671); Assert.IsNotNull(synZ, "Synthetic node for nodeZ must be created."); Assert.AreEqual(nodeZ, synZ.Container, "The synthetic node must be child of the nodeZ."); //Port for nodeZ MapPort portZ = (MapPort)ret.GetPortById(-3); Assert.IsNotNull(portZ, "The new port created must be added to mapdata."); Assert.AreEqual(synZ, portZ.Node, "The synthetic node for Z must be the node of portZ"); Assert.AreEqual(portZ, synZ.Ports[0], "The new portZ must be added to synthetic node Z's ports."); }
public void TestPreProcess_PortSizes_Accuracy() { MapData ret = instance.MockPreProcess(mapData); foreach (IPort port in ret.Ports) { MapPort mapPort = (MapPort)port; Assert.AreEqual(mapPort.MinimalSize.Height, instance.MinimumPortHeight, "Incorrect minimal size for port."); Assert.AreEqual(mapPort.MinimalSize.Width, instance.MinimumPortWidth, "Incorrect minimal size for port."); } }
/// <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> /// Creates a new link between the startNode and the endNode. /// The startPort and endPort are the ports for the start and end nodes. /// </summary> /// <param name="startNode">The starting node</param> /// <param name="endNode">The ending node</param> /// <param name="startPort">The starting port</param> /// <param name="endPort">the ending port</param> /// <param name="mapdata">The Mapdata to use for generating the unique id of the link.</param> /// <param name="origLink">The original link</param> /// <param name="toBeAddedLinks">The number of links yet to be added.</param> /// <returns>The new link created.</returns> private MapLink CreateNewLink( MapNode startNode, MapNode endNode, MapPort startPort, MapPort endPort, MapData mapdata, MapLink origLink, int toBeAddedLinks) { //Create new link long newLinkId = GetUniqueId(typeof(MapLink), mapdata, toBeAddedLinks); MapLink newLink = new MapLink(); newLink.Id = newLinkId; newLink.Name = MapLinkNamePrefix + newLinkId; //Add the nodes newLink.AddNode(startNode); newLink.AddNode(endNode); //Add the ports newLink.AddPort(startPort); newLink.AddPort(endPort); //Copy attributes of original link and set owner id of the new attributes. Also add to mapdata CopyAttributes(origLink, newLink, newLinkId, mapdata); //create a new attribute to store the original link id MapAttribute origLinkIdAttr = new MapAttribute(); origLinkIdAttr.Name = "origLinkId"; origLinkIdAttr.StringValue = origLink.Id.ToString(); long newAttrId = GetUniqueId(typeof(MapAttribute), mapdata, 0); origLinkIdAttr.Id = newAttrId; origLinkIdAttr.OwnerType = "link"; origLinkIdAttr.OwnerId = newLink.Id; origLinkIdAttr.Type = AttrTypeString; //Add the new attribute to the new link and the map data newLink.Attributes.Add(origLinkIdAttr); mapdata.AddAttribute(origLinkIdAttr); //Set new link's element type from original link newLink.ElementType = origLink.ElementType; return(newLink); }
/// <summary> /// Creates and adds a port to a node. Also sets the Node property of the port. /// </summary> /// <param name="node">The node to process.</param> /// <param name="link">Link, if linked port is required</param> private static void AddMockPort(INode node, ILink link) { MapPort mapPort = new MapPort(); MapNode mapNode = (MapNode)node; mapPort.Id = mapNode.Id; mapPort.Name = mapNode.Name; //Add port to node mapNode.AddPort(mapPort); //Add node to port mapPort.Node = mapNode; //Linked port is required if (link != null) { mapPort.Links.Add(link); } }
public void ReadPortIdMapCsv() { try { if (string.IsNullOrWhiteSpace(PortIdMapPath)) { return; } //foreach (var address in Vehicle.Mapinfo.addressMap.Values) //{ // address.PortIdMap.Clear(); //} string[] allRows = File.ReadAllLines(PortIdMapPath); if (allRows == null || allRows.Length < 2) { return; } string[] titleRow = allRows[0].Split(','); allRows = allRows.Skip(1).ToArray(); int nRows = allRows.Length; int nColumns = titleRow.Length; Dictionary <string, int> dicHeaderIndexes = new Dictionary <string, int>(); for (int i = 0; i < nColumns; i++) { var keyword = titleRow[i].Trim(); if (!string.IsNullOrWhiteSpace(keyword)) { dicHeaderIndexes.Add(keyword, i); } } for (int i = 0; i < nRows; i++) { string[] getThisRow = allRows[i].Split(','); try { string portId = getThisRow[dicHeaderIndexes["Id"]]; lastReadPortId = portId; string addressId = getThisRow[dicHeaderIndexes["AddressId"]]; string portNumber = getThisRow[dicHeaderIndexes["PortNumber"]]; bool isVitualPort = false; if (dicHeaderIndexes.ContainsKey("IsVitualPort")) { isVitualPort = bool.Parse(getThisRow[dicHeaderIndexes["IsVitualPort"]]); } MapPort port = new MapPort() { ID = portId, ReferenceAddressId = addressId, Number = portNumber, IsVitualPort = isVitualPort }; if (!Vehicle.MapInfo.portMap.ContainsKey(port.ID)) { Vehicle.MapInfo.portMap.Add(port.ID, port); } //if (Vehicle.Mapinfo.addressMap.ContainsKey(addressId)) //{ // Vehicle.Mapinfo.addressMap[addressId].PortIdMap.Add(port.ID, port); //} } catch (Exception ex) { LogException(GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, $"lastReadPortId=[{lastReadPortId}]" + ex.Message); } } } catch (Exception ex) { LogException(GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, $"lastReadPortId=[{lastReadPortId}]" + ex.Message); } }
/// <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); } }