コード例 #1
0
ファイル: GraphNode.cs プロジェクト: vfauth/PPcurry
 /// <summary>
 /// Add a link to the node
 /// </summary>
 public void AddLink(GraphLink link)
 {
     // Add the link
     if (!ConnectedLinks.Contains(link))
     {
         ConnectedLinks.Add(link);
     }
     // Add the linked node
     if (link.ConnectedNodes.Item1 != this)
     {
         if (!LinkedNodes.Contains(link.ConnectedNodes.Item1))
         {
             LinkedNodes.Add(link.ConnectedNodes.Item1);
         }
     }
     else
     {
         if (!LinkedNodes.Contains(link.ConnectedNodes.Item2))
         {
             LinkedNodes.Add(link.ConnectedNodes.Item2);
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Generate the Graph by parsing the nodes of the boardgrid
        /// </summary>
        private void GenerateGraph()
        {
            Dictionary <Wire, List <GraphNode> >      wiresNotAdded      = new Dictionary <Wire, List <GraphNode> >();      // List the wires that have not been processed yet and the nodes to which they are connected
            Dictionary <Component, List <GraphNode> > componentsNotAdded = new Dictionary <Component, List <GraphNode> >(); // List the components that have not been processed yet and the nodes to which they are connected

            // Parse all grid nodes and create corresponding graph nodes
            for (int i = 0; i < BoardGrid.Nodes.Count; i++)
            {
                for (int j = 0; j < BoardGrid.Nodes[i].Count; j++)
                {
                    Node gridNode = BoardGrid.Nodes[i][j];
                    if (!gridNode.IsIsolated()) // Isolated nodes are discarded
                    {
                        GraphNode node = new GraphNode(gridNode);
                        Nodes.Add(node);                                             // Create the GraphNode
                        foreach (object gridLink in gridNode.ConnectedElements.Keys) // Save all connected wires and components
                        {
                            if (gridLink is Wire wire)
                            {
                                if (wiresNotAdded.ContainsKey(wire))
                                {
                                    wiresNotAdded[wire].Add(node);
                                }
                                else
                                {
                                    wiresNotAdded[wire] = new List <GraphNode> {
                                        node
                                    }
                                };
                            }
                            else if (gridLink is Component component)
                            {
                                if (componentsNotAdded.ContainsKey(component))
                                {
                                    componentsNotAdded[component].Add(node);
                                }
                                else
                                {
                                    componentsNotAdded[component] = new List <GraphNode> {
                                        node
                                    }
                                };
                            }
                        }
                    }
                }
            }

            Dictionary <GraphNode, GraphNode> mergedNodes = new Dictionary <GraphNode, GraphNode>(); // List all the nodes (keys) merged into another node (values)

            // Parse all wires and merge the connected nodes
            foreach (Wire wire in wiresNotAdded.Keys)
            {
                // Ensure that the nodes to merge have not already been merged into another node and discarded
                while (mergedNodes.ContainsKey(wiresNotAdded[wire][0]))
                {
                    wiresNotAdded[wire][0] = mergedNodes[wiresNotAdded[wire][0]];
                }
                while (mergedNodes.ContainsKey(wiresNotAdded[wire][1]))
                {
                    wiresNotAdded[wire][1] = mergedNodes[wiresNotAdded[wire][1]];
                }
                if (wiresNotAdded[wire][0] != wiresNotAdded[wire][1])             // A node can't be merged with itself
                {
                    MergeNodes(wiresNotAdded[wire][0], wiresNotAdded[wire][1]);   // Merge the nodes
                    mergedNodes[wiresNotAdded[wire][1]] = wiresNotAdded[wire][0]; // Save the operation
                }
            }

            // Create all links
            foreach (Component component in componentsNotAdded.Keys)
            {
                // Ensure that the nodes to use have not been merged into another node and discarded
                while (mergedNodes.ContainsKey(componentsNotAdded[component][0]))
                {
                    componentsNotAdded[component][0] = mergedNodes[componentsNotAdded[component][0]];
                }
                while (mergedNodes.ContainsKey(componentsNotAdded[component][1]))
                {
                    componentsNotAdded[component][1] = mergedNodes[componentsNotAdded[component][1]];
                }

                GraphLink link = new GraphLink(componentsNotAdded[component][0], componentsNotAdded[component][1], component); // Create the link
                componentsNotAdded[component][0].AddLink(link);                                                                // Add the link to both nodes
                componentsNotAdded[component][1].AddLink(link);
                Links.Add(link);
            }
        }