Ejemplo n.º 1
0
    /*
     * 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));
        }
    }
Ejemplo n.º 2
0
    private void HandleCreationOfASingleNewNetwork(ElectricNetworkNode addedNode, List <ElectricNetworkNode> adjacentNodes)
    {
        // Check if adjacentNodes all have no network;
        // If they do, another method about handling addon to network(s) should be called.
        foreach (ElectricNetworkNode adjacentNode in adjacentNodes)
        {
            if (adjacentNode.connectedNetwork != null)
            {
                Debug.LogError($"ERROR CREATING NODE: Node {addedNode} should be only one with network, " +
                               $"but Node {adjacentNode} already has network {adjacentNode.connectedNetwork}. ");
                return;
            }
        }
        ElectricNetwork network = CreateNewElectricNetwork();

        ElectricNetworkUtil.Register(network, addedNode);
        Debug.Log($"INFO CREATING NODE: Node {addedNode} was created with network {network}. ");

        if (adjacentNodes != null && adjacentNodes.Count > 0)
        {
            foreach (ElectricNetworkNode adjacentNode in adjacentNodes)
            {
                ElectricNetworkUtil.Register(network, adjacentNode);
                Debug.Log($"INFO REGISTERING: Node {adjacentNode} was added to network {network}. ");
            }
        }

        SortElectricNetworks();
    }
Ejemplo n.º 3
0
    private void HandleAddonToAnExistingNetwork(ElectricNetworkNode addedNode, List <ElectricNetworkNode> adjacentNodes)
    {
        ElectricNetwork network = adjacentNodes[0].connectedNetwork;

        ElectricNetworkUtil.Register(network, addedNode);
        SortElectricNetworks();
        Debug.Log($"INFO: Node {addedNode} was added to network {network}. ");
    }
Ejemplo n.º 4
0
    private void HandleAddonToMultipleExistingNetworks(ElectricNetworkNode addedNode, List <ElectricNetworkNode> adjacentNodes)
    {
        ElectricNetwork[] existingNetworks = ElectricNetworkUtil.GetDifferentNetworksOf(adjacentNodes.ToArray());
        existingNetworks = ElectricNetwork.SortBySize(existingNetworks);
        ElectricNetwork biggestNetwork = existingNetworks[0];

        // Integrate smaller networks into the biggest one
        foreach (ElectricNetwork disintegratedNetwork in existingNetworks)
        {
            if (disintegratedNetwork == biggestNetwork)
            {
                continue;
            }
            IntegrateElectricNetworkIntoAnother(biggestNetwork, disintegratedNetwork);
        }

        // Register added node into biggest network all other networks get integrated into
        ElectricNetworkUtil.Register(biggestNetwork, addedNode);

        SortElectricNetworks();
    }
Ejemplo n.º 5
0
    private void IntegrateElectricNetworkIntoAnother(ElectricNetwork targetNetwork, ElectricNetwork disintegratingNetwork)
    {
        // Create new lists. Otherwise, elements would be removed while iterating over the list.
        List <ElectricNetworkNode> movedNodes = new List <ElectricNetworkNode>(disintegratingNetwork.nodes);
        List <ElectricNetworkEdge> movedEdges = new List <ElectricNetworkEdge>(disintegratingNetwork.edges);

        // Transfer nodes
        foreach (ElectricNetworkNode node in movedNodes)
        {
            ElectricNetworkUtil.Unregister(disintegratingNetwork, node);
            ElectricNetworkUtil.Register(targetNetwork, node);
        }

        // Transfer edges
        foreach (ElectricNetworkEdge edge in movedEdges)
        {
            ElectricNetworkUtil.Unregister(disintegratingNetwork, edge);
            ElectricNetworkUtil.Register(targetNetwork, edge);
        }

        // "Destroy" (aka unlink) network
        DestroyElectricNetwork(disintegratingNetwork);
    }