/* * You should not call this method directly. Instead, call HandleAdjacentNodesAfterNodeRemoval() */ private void HandleNetworkResolving(List <ElectricNetworkNode> nodesToBeResolved) { NetworkResolver networkResolver = new NetworkResolver(); networkResolver.ResolveNetworks(nodesToBeResolved); List <ElectricNetworkSeed> resolvedNetworkSeeds = networkResolver.resolvedNetworkSeeds; // If only one ElectricNetworkSeed gets returned, it means that the network doesn't break up. Just one node gets removed. if (resolvedNetworkSeeds.Count == 1) { return; } // Because all former nodes and edges now are reassigned in resolved network seeds, it is save to wipe all // content from the network before removal (unregister all nodes and edges). ElectricNetwork networkBeforeRemoval = nodesToBeResolved[0].connectedNetwork; ElectricNetworkUtil.RemoveNetworkContent(networkBeforeRemoval); // Reassign the biggest networkSeed to the network before the removal ElectricNetworkUtil.SortBySize(resolvedNetworkSeeds); ElectricNetworkSeed biggestNetworkSeed = resolvedNetworkSeeds[0]; ElectricNetworkUtil.AddNetworkContent(networkBeforeRemoval, biggestNetworkSeed); Debug.Log($"INFO: There are {biggestNetworkSeed.nodes.Count} nodes in the biggest network after removing a node. "); resolvedNetworkSeeds.Remove(biggestNetworkSeed); // For every other networkSeed: Create a new network and populate it with the contents of the networkSeed foreach (ElectricNetworkSeed networkSeed in resolvedNetworkSeeds) { ElectricNetwork electricNetwork = CreateNewElectricNetwork(); networkSeed.nodes.ForEach(node => ElectricNetworkUtil.Register(electricNetwork, node)); networkSeed.edges.ForEach(edge => ElectricNetworkUtil.Register(electricNetwork, edge)); } }
/* * There are actually two lists with traveresed nodes in this NetworkResolver: * 1) traversedNodes represents all nodes that have been passed through the traversal * 2) nodesInThisNetwork represents a subset of all traversed nodes, namely only those that are connected with each other (--> network) */ private ElectricNetworkSeed TraverseNodesInQueueAndResolveAsNetwork(Queue <ElectricNetworkNode> nodeQueue) { List <ElectricNetworkNode> nodesInThisNetwork = new List <ElectricNetworkNode>(); List <ElectricNetworkEdge> edgesInThisNetwork = new List <ElectricNetworkEdge>(); ElectricNetworkSeed resolvedNetworkSeed = new ElectricNetworkSeed(); while (nodeQueue.Count > 0) { ElectricNetworkNode nodeOnTop = nodeQueue.Peek(); // Add each connected node to the network (plus the corresponding edge) foreach (ElectricNetworkNode connectedNode in nodeOnTop.connectedNodes) { // If the resolver already visited this node, continue if (traversedNodes.Contains(connectedNode)) { continue; } // Add connected Node to queue nodeQueue.Enqueue(connectedNode); // Add node to traversed nodes (as soon as they are in queue, they count as traversed/visited) traversedNodes.Add(connectedNode); // Get edge between two nodes ElectricNetworkEdge commonEdge = ElectricNetworkUtil.GetCommonEdge(nodeOnTop, connectedNode); if (commonEdge == null) { Debug.LogError($"ERROR RESOLVING NETWORKS: " + $"There is no common edge between Node 1 {nodeOnTop} and Node 2 {connectedNode}. "); } edgesInThisNetwork.Add(commonEdge); } // Add traversed node to all 1) visited nodes and 2) nodes for network traversedNodes.Add(nodeOnTop); nodesInThisNetwork.Add(nodeOnTop); // Remove top node from queue nodeQueue.Dequeue(); } // Return resolved network resolvedNetworkSeed.nodes = nodesInThisNetwork; resolvedNetworkSeed.edges = edgesInThisNetwork; return(resolvedNetworkSeed); }
public static void AddNetworkContent(ElectricNetwork network, ElectricNetworkSeed networkSeed) { AddNetworkContent(network, networkSeed.nodes, networkSeed.edges); }