public void Initialise() { if (!IsInitialised) { foreach (var node in Nodes) { if (Relationships.Any(q => q.From == node.Id)) { NodesNeedToBePlaced.Add(node); } else { ChildNodes.Add(node); } } //If no parent can be found, set the top left node to parent if (ChildNodes.Count == 0) { AddTopLeftNodeToParentNode(); } PlaceNodesToMap(); ReCheckForIncorrectDepth(); IsInitialised = true; } }
private void AddTopLeftNodeToParentNode() { //if no parent is found, then select the left most and top most node for the parent. var orderedNodesOrderByLeft = NodesNeedToBePlaced.OrderBy(q => q.XPosition); var orderedNodesOrderByLeftAndTop = orderedNodesOrderByLeft.ThenBy(q => q.YPosition); //get the first node var node = orderedNodesOrderByLeftAndTop.FirstOrDefault(); if (node != null) { ChildNodes.Add(node); NodesNeedToBePlaced.Remove(node); } }
private void PlaceNodesToMap() { var isUpdated = true; while (NodesNeedToBePlaced.Count > 0 && isUpdated) { isUpdated = false; var allChildNodes = GetAllChildNodes().ToList(); foreach (var node in allChildNodes) { var currentNode = node; //getting all relationships that to this node var relationships = Relationships.Where(q => q.To == currentNode.Id).ToList(); //From the relationships found from last step, find the nodes. foreach (var relationship in relationships) { var findedChild = NodesNeedToBePlaced.FirstOrDefault(q => q.Id == relationship.From); if (findedChild != null) { if (!node.ChildNodes.Contains(findedChild)) { findedChild.Depth = node.Depth + 1; node.ChildNodes.Add(findedChild); NodesNeedToBePlaced.Remove(findedChild); isUpdated = true; } Relationships.Remove(relationship); } else { //Add another child } } } } if (_nodesNeedToBePlaced.Count > 0) { //If there are nodes that have not been placed, select the left most and top most node as a parent. AddTopLeftNodeToParentNode(); PlaceNodesToMap(); } }