public void AStarSameOriginAndDestination() { bool error = false; try { var list = CreatePointList(); Graph.Graph <GameObject> graph = new Graph.Graph <GameObject>(); graph.AddNodes(list); int[] indices = new int[graph.Nodes.Count * 2]; for (int i = 0; i < graph.Nodes.Count * 2; i += 2) { indices[i] = Random.Range(0, graph.Nodes.Count); indices[i + 1] = Random.Range(0, graph.Nodes.Count); } graph.CreateEdges(indices, (c, d) => Vector3.Distance(c.transform.position, d.transform.position)); graph.CalculateHeuristics(graph.Nodes[0].Item, (c, d) => Vector3.Distance(c.transform.position, d.transform.position)); var path = graph.AStar(0, 0); } catch { error = true; } Assert.IsFalse(error); }
public void DoesNotAllowDuplicates() { var list = CreatePointList(); list.Add(list[0]); Graph.Graph <GameObject> graph = new Graph.Graph <GameObject>(); graph.AddNodes(list); Assert.IsTrue(graph.FindAllNodes(n => n.Item == list[0]).Count == 1); }
public void DoesNotAllowDuplicatesSameCoordinate() { var list = CreatePointList(); var g = new GameObject(); var g2 = new GameObject(); var x = Random.Range(-5.0f, 5.0f); var y = Random.Range(-5.0f, 5.0f); g.transform.position = new Vector3(x, y, 0.0f); g2.transform.position = new Vector3(x, y, 0.0f); list.Add(g); list.Add(g2); Graph.Graph <GameObject> graph = new Graph.Graph <GameObject>(); graph.AddNodes(list); Assert.IsTrue(graph.FindAllNodes(n => n.Item.transform.position == g.transform.position).Count == 1); }
public static Graph.Graph GetGraphFromFile(String FileName) { Graph.Graph retGraph = new Graph.Graph(); try { String Results = ""; /*using (StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open))) * { * Results = sr.ReadToEnd(); * sr.Close(); * }*/ XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(Results); // Get elements XmlNodeList Nodes = xmlDoc.GetElementsByTagName("node"); XmlNodeList Edges = xmlDoc.GetElementsByTagName("edge"); foreach (XmlNode Node in Nodes) { var nextNode = retGraph.AddNodes(Node.Attributes["id"].Value, null); foreach (System.Xml.XmlAttribute a in Node.Attributes) { switch (a.Name) { case "id": continue; case "Weight": nextNode.Weight = Convert.ToInt32(a.Value); continue; default: nextNode.Properties.Add(a.Name, a.Value); continue; } } foreach (XmlNode Edge in Edges) { Graph.Node sNode = null, tNode = null; //oops edge with no node created. if (Edge.Attributes["source"] == null) { sNode = retGraph.AddNodes(Edge.Attributes["id"].Value, null); } else { sNode = retGraph.GetNode(Edge.Attributes["source"].Value); } //oops edge with no node created. if (Edge.Attributes["target"] == null) { tNode = retGraph.AddNodes(Edge.Attributes["id"].Value, null); } else { tNode = retGraph.GetNode(Edge.Attributes["target"].Value); } var nextEdge = retGraph.AddEdges(sNode, tNode); foreach (System.Xml.XmlAttribute a in Edge.Attributes) { switch (a.Name) { case "id": nextEdge.Label = a.Value; continue; case "Weight": nextEdge.Weight = Convert.ToInt32(a.Value); continue; default: nextEdge.Properties.Add(a.Name, a.Value); continue; } } } } } catch (Exception exp) { return(null); } return(retGraph); }
public static Graph.Graph GetGraphFromURL(String URL) { Graph.Graph retGraph = new Graph.Graph(); try { string result = ""; using (var client = new WebClient()) { result = client.DownloadString(URL); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(result); // Get elements XmlNodeList Nodes = xmlDoc.GetElementsByTagName("node"); XmlNodeList Edges = xmlDoc.GetElementsByTagName("edge"); foreach (XmlNode Node in Nodes) { var nextNode = retGraph.AddNodes(Node.Attributes["id"].Value, null); foreach (System.Xml.XmlAttribute a in Node.Attributes) { switch (a.Name) { case "id": continue; case "Weight": nextNode.Weight = Convert.ToInt32(a.Value); continue; default: nextNode.Properties.Add(a.Name, a.Value); continue; } } } foreach (XmlNode Edge in Edges) { Graph.Node sNode = null, tNode = null; //oops edge with no node created. if (Edge.Attributes["source"] == null) { sNode = retGraph.AddNodes(Edge.Attributes["id"].Value, null); } else { sNode = retGraph.GetNode(Edge.Attributes["source"].Value); } //oops edge with no node created. if (Edge.Attributes["target"] == null) { tNode = retGraph.AddNodes(Edge.Attributes["id"].Value, null); } else { tNode = retGraph.GetNode(Edge.Attributes["target"].Value); } var nextEdge = retGraph.AddEdges(sNode, tNode); foreach (System.Xml.XmlAttribute a in Edge.Attributes) { switch (a.Name) { case "id": nextEdge.Label = a.Value; continue; case "Weight": nextEdge.Weight = Convert.ToInt32(a.Value); continue; default: nextEdge.Properties.Add(a.Name, a.Value); continue; } } } } catch (Exception exp) { return(null); } return(retGraph); }
public static void GetGraphFromURL(Graph.Graph retGraph, String URL) { var myLogs = AR.Core.Logging.DBLogger.getInstance(); myLogs.LogMessage(ARTypes.LoggingLevels.Verbose, "Init GetGraphFromURL", Module: "GraphMLGraphFactory.GetGraphFromURL", Version: "ALPHA"); try { UnityWebRequest www = UnityWebRequest.Get(URL); www.Send(); while (!www.isDone) { //intentionally blank } string result = www.downloadHandler.text; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(result); // Get elements XmlNodeList Nodes = xmlDoc.GetElementsByTagName("node"); XmlNodeList Edges = xmlDoc.GetElementsByTagName("edge"); foreach (XmlNode Node in Nodes) { var nextNode = retGraph.AddNodes(Node.Attributes["id"].Value, null); foreach (System.Xml.XmlAttribute a in Node.Attributes) { switch (a.Name) { case "id": continue; case "Weight": nextNode.Weight = Convert.ToInt32(a.Value); continue; default: nextNode.Properties.Add(a.Name, a.Value); continue; } } try { List <String> Results = new List <string>(); Results.Add(Node["data"].InnerText); var myNextInnerNode = Node["data"].NextSibling; while (myNextInnerNode != null) { Results.Add(myNextInnerNode.InnerText); myNextInnerNode = myNextInnerNode.NextSibling; } var i = 0; nextNode.Properties.Add("PhysicianNames", Results[i].Substring(0, Results[i].Length < 250 ? Results[i].Length : 250)); ++i; nextNode.Properties.Add("countMale", Results[i]); ++i; nextNode.Properties.Add("ClinicCount", Results[i]); ++i; nextNode.Properties.Add("countFemale", Results[i]); ++i; nextNode.Properties.Add("ClinicNames", Results[i].Substring(0, Results[i].Length < 250 ? Results[i].Length : 250)); ++i; nextNode.Properties.Add("PhysicianCount", Results[i]); ++i; nextNode.Properties.Add("NodeCount", Results[i]); ++i; } catch (Exception exp) { myLogs.LogMessage(ARTypes.LoggingLevels.Error, "Init GetGraphFromURL Exception: " + exp.Message, Module: "GraphMLGraphFactory.GetGraphFromURL", Version: "ALPHA"); return; } } foreach (XmlNode Edge in Edges) { Graph.Node sNode = null, tNode = null; //oops edge with no node created. if (Edge.Attributes["source"] == null) { sNode = retGraph.AddNodes(Edge.Attributes["id"].Value, null); } else { sNode = retGraph.GetNode(Edge.Attributes["source"].Value); } //oops edge with no node created. if (Edge.Attributes["target"] == null) { tNode = retGraph.AddNodes(Edge.Attributes["id"].Value, null); } else { tNode = retGraph.GetNode(Edge.Attributes["target"].Value); } var nextEdge = retGraph.AddEdges(sNode, tNode); foreach (System.Xml.XmlAttribute a in Edge.Attributes) { switch (a.Name) { case "id": nextEdge.Label = a.Value; continue; case "Weight": nextEdge.Weight = Convert.ToInt32(a.Value); continue; default: nextEdge.Properties.Add(a.Name, a.Value); continue; } } } } catch (Exception exp) { myLogs.LogMessage(ARTypes.LoggingLevels.Error, "Init GetGraphFromURL Exception: " + exp.Message, Module: "GraphMLGraphFactory.GetGraphFromURL", Version: "ALPHA"); return; } return; }
public void GetGraphFromQuery(Graph.Graph retGraph, String Query, Boolean readNodeEdgeProps) { try { var neo4jGraph = CypherQueryReturnGraph(Query); foreach (var dataarray in neo4jGraph.data) { var x = dataarray[0]; //convert for String StartNodePath = x.start; String EndNodePath = x.end; var SNodeSplit = StartNodePath.Split('/'); var ENodeSplit = EndNodePath.Split('/'); Graph.Node StartNode = null; Graph.Node EndNode = null; if (retGraph.GetNode(SNodeSplit[SNodeSplit.Length - 1]) == null) { StartNode = retGraph.AddNodes(SNodeSplit[SNodeSplit.Length - 1], null); StartNode.Neo4jPath = StartNodePath; } else { StartNode = retGraph.GetNode(SNodeSplit[SNodeSplit.Length - 1]); } if (retGraph.GetNode(ENodeSplit[ENodeSplit.Length - 1]) == null) { EndNode = retGraph.AddNodes(ENodeSplit[ENodeSplit.Length - 1], null); EndNode.Neo4jPath = EndNodePath; } else { EndNode = retGraph.GetNode(ENodeSplit[ENodeSplit.Length - 1]); } var nextEdge = retGraph.AddEdges(StartNode, EndNode); //don't rip remaining properties from site if (!readNodeEdgeProps) { continue; } //get StartNode Properties String JsonOfProps = AR.Core.Communications.Neo4jConnector.getInstance().GetNodeProperties(StartNode.ID.ToString()); var Nodeprops = AR.Core.IO.SimpleJson.DeserializeObject <Dictionary <String, System.Object> >(JsonOfProps); foreach (var kv in Nodeprops) { Double dblVal = 0; Int32 intval = 0; if (StartNode.Properties.ContainsKey(kv.Key.ToString())) { continue; } if (Double.TryParse(kv.Value.ToString(), out dblVal)) { StartNode.Properties.Add(kv.Key, dblVal); continue; } if (Int32.TryParse(kv.Value.ToString(), out intval)) { StartNode.Properties.Add(kv.Key, dblVal); continue; } StartNode.Properties.Add(kv.Key, kv.Value.ToString()); } //get EndNode Properties JsonOfProps = AR.Core.Communications.Neo4jConnector.getInstance().GetNodeProperties(EndNode.ID.ToString()); Nodeprops = AR.Core.IO.SimpleJson.DeserializeObject <Dictionary <String, System.Object> >(JsonOfProps); foreach (var kv in Nodeprops) { Double dblVal = 0; Int32 intval = 0; if (EndNode.Properties.ContainsKey(kv.Key.ToString())) { continue; } if (Double.TryParse(kv.Value.ToString(), out dblVal)) { EndNode.Properties.Add(kv.Key, dblVal); continue; } if (Int32.TryParse(kv.Value.ToString(), out intval)) { EndNode.Properties.Add(kv.Key, dblVal); continue; } EndNode.Properties.Add(kv.Key, kv.Value.ToString()); } //get Edge Properties JsonOfProps = AR.Core.Communications.Neo4jConnector.getInstance().GetNodeProperties(nextEdge.ID.ToString()); var Edgeprops = AR.Core.IO.SimpleJson.DeserializeObject <Dictionary <String, System.Object> >(JsonOfProps); foreach (var kv in Edgeprops) { Double dblVal = 0; Int32 intval = 0; if (nextEdge.Properties.ContainsKey(kv.Key.ToString())) { continue; } if (Double.TryParse(kv.Value.ToString(), out dblVal)) { nextEdge.Properties.Add(kv.Key, dblVal); continue; } if (Int32.TryParse(kv.Value.ToString(), out intval)) { nextEdge.Properties.Add(kv.Key, dblVal); continue; } nextEdge.Properties.Add(kv.Key, kv.Value.ToString()); } //TODO only ripping first element off array if (x.relationships[0].Length > 0) { nextEdge.Neo4jPath = x.relationships[0]; } } } catch (Exception exp) { myLogs.LogMessage(LoggingLevels.Error, "Init Neo4jConnector Exception: " + exp.Message, Module: "Neo4jConnector.GetGraphFromQuery", Version: "ALPHA"); return; } return; }