/// <summary> /// Parse source road map XML document /// </summary> /// <param name="sourceXml">road map XML document</param> /// <returns>parsed RoadMap object</returns> public static RoadMap ParseRoadMapXml(XmlDocument sourceXml) { if (sourceXml == null) { return(null); } RoadMap roadMap = new RoadMap(); if (ValidateSchema(sourceXml)) { XmlNode graph = sourceXml.SelectNodes("/graph").Item(0); XmlNodeList nodes = graph.SelectNodes("node"); foreach (XmlNode node in nodes) { RoadNodeRole nodeRole = RoadNodeRole.Normal; if (node.Attributes["role"] != null && node.Attributes["role"].Value == "start") { nodeRole = RoadNodeRole.Start; } else if (node.Attributes["role"] != null && node.Attributes["role"].Value == "finish") { nodeRole = RoadNodeRole.Finish; } RoadNodeState nodeState = RoadNodeState.Ok; if (node.Attributes["status"] != null && node.Attributes["status"].Value == "crash") { nodeState = RoadNodeState.Crash; } int nodeId = Convert.ToInt32(node.Attributes["id"].Value); RoadNode roadNode = new RoadNode(nodeId, nodeRole, nodeState); XmlNodeList links = node.SelectNodes("link"); foreach (XmlNode link in links) { int linkRef = Convert.ToInt32(link.Attributes["ref"].Value); int linkWeight = Convert.ToInt32(link.Attributes["weight"].Value); RoadLink roadLink = new RoadLink(linkWeight, linkRef); roadNode.Links.Add(roadLink); } roadMap.Nodes.Add(roadNode); } } return(roadMap); }
/// <summary> /// Parse source road map XML document /// </summary> /// <param name="sourceXml">road map XML document</param> /// <returns>parsed RoadMap object</returns> public static RoadMap ParseRoadMapXml(XmlDocument sourceXml) { if (sourceXml == null) return null; RoadMap roadMap = new RoadMap(); if (ValidateSchema(sourceXml)) { XmlNode graph = sourceXml.SelectNodes("/graph").Item(0); XmlNodeList nodes = graph.SelectNodes("node"); foreach (XmlNode node in nodes) { RoadNodeRole nodeRole = RoadNodeRole.Normal; if (node.Attributes["role"] != null && node.Attributes["role"].Value == "start") { nodeRole = RoadNodeRole.Start; } else if (node.Attributes["role"] != null &&node.Attributes["role"].Value == "finish") { nodeRole = RoadNodeRole.Finish; } RoadNodeState nodeState = RoadNodeState.Ok; if (node.Attributes["status"] != null && node.Attributes["status"].Value == "crash") { nodeState = RoadNodeState.Crash; } int nodeId = Convert.ToInt32(node.Attributes["id"].Value); RoadNode roadNode = new RoadNode(nodeId, nodeRole, nodeState); XmlNodeList links = node.SelectNodes("link"); foreach (XmlNode link in links) { int linkRef = Convert.ToInt32(link.Attributes["ref"].Value); int linkWeight = Convert.ToInt32(link.Attributes["weight"].Value); RoadLink roadLink = new RoadLink(linkWeight, linkRef); roadNode.Links.Add(roadLink); } roadMap.Nodes.Add(roadNode); } } return roadMap; }
/// <summary> /// Check if all links between nodes are correct. /// </summary> /// <param name="roadMap">road map to check</param> /// <returns>String with errors descriptions or empty string if no errors found</returns> private string CheckLinks(RoadMap roadMap) { string errors = string.Empty; foreach (var node in roadMap.Nodes) { foreach (var link in node.Links) { try { //check if exactly one node exists by link ref RoadNode neighbourByLink = roadMap.Nodes.Where(n => n.Id == link.RefNodeId).Single(); //check if exactly one back link exists and has the same weight as direct link RoadLink backLink = neighbourByLink.Links.Where(l => l.RefNodeId == node.Id && l.Weight == link.Weight).Single(); } catch (InvalidOperationException) { errors += string.Format("Node {0} has wrong link\n", node.Id); } } } return(errors); }