Example #1
0
        public GraphComponents GetClusteredGraph()
        {
            IEnumerable <INodeShape> nodes = _sourceGraph.GetNodeViewModels();

            _partitionedGraph = new GraphComponents(_sourceGraph.Scope);

            // Loop over all the nodes in the list
            foreach (NodeViewModelBase nodeVM in nodes)
            {
                // Create a partition node for clusters
                PartitionNode pn = GetClusterAsPartitionNode(nodeVM);

                // Add the partition node to the graph
                _partitionedGraph.AddNodeViewModel(pn);
            }

            // Loop over all of the partition nodes in our partition graph
            foreach (PartitionNode pn in _partitionNodes)
            {
                // Loop over all the external connections
                foreach (Edge edge in pn.ExternalConnections)
                {
                    INodeShape targetNodeVM = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).GetNodeViewModel(edge.Target);

                    // Check if the edge's target node is in our partition collection
                    if (_nodeToPartition.ContainsKey(targetNodeVM))
                    {
                        IEdge newEdge = edge.Copy(pn, _nodeToPartition[targetNodeVM]);
                        _partitionedGraph.Data.AddEdge(newEdge);
                    }
                }
            }

            return(_partitionedGraph);
        }
Example #2
0
        private int NodeSizeComparer(INodeShape first, INodeShape second)
        {
            double areaFirst  = first.Width * first.Height;
            double areaSecond = second.Width * second.Height;

            return(-areaFirst.CompareTo(areaSecond));
        }
Example #3
0
        /// <summary>
        /// Removes the provided node view model (and its parent node) from the graph
        /// </summary>
        /// <param name="nodeVM">The node view model to be removed from the graph</param>
        public void RemoveNodeViewModel(INodeShape nodeVM)
        {
            // Validate the provided nodeVM
            if (nodeVM == null)
            {
                throw new System.ArgumentNullException("NodeVM", "Invalid node view model provided");
            }

            // Make sure the node view model even exists before going
            // through the motions to remove it
            if (this.nodeViewModels.Contains(nodeVM))
            {
                // Ensure we are not dealing with a partition nodeVM
                if (!(nodeVM is PartitionNode))
                {
                    NodeViewModelBase targetNodeVM = nodeVM as NodeViewModelBase;

                    // Remove the parent node from the graph
                    this.graphData.RemoveNode(targetNodeVM.ParentNode);

                    // Remove event handlers for this node VM
                    targetNodeVM.NodeMoved  -= new System.EventHandler <System.EventArgs>(nodeViewModel_NodeMoved);
                    targetNodeVM.NodeLoaded -= new System.EventHandler <System.EventArgs>(nodeViewModel_NodeLoaded);

                    // Remove the view model from the internal collections
                    nodeToNodeViewModel.Remove(targetNodeVM.ParentNode);
                }
                else
                {
                    this.graphData.RemoveNode(nodeVM as PartitionNode);
                }

                nodeViewModels.Remove(nodeVM);
            }
        }
Example #4
0
        /// <summary>
        /// Positions the nodes
        /// </summary>
        /// <param name="isAnimated">Indicates whether or not the layout should be animated</param>
        /// <param name="graphComponents">The object containing the graph data</param>
        /// <param name="rootNode">Root node</param>
        public void ComputeLayout(bool isAnimated, GraphComponents graphComponents, INode rootNode)
        {
            nodeVMs       = graphComponents.GetNodeViewModels().Where(node => !node.IsHidden).ToArray();
            positions     = new Point[nodeVMs.Length];
            edgeCounts    = new int[nodeVMs.Length];
            nodeIDToIndex = new Dictionary <string, int>();

            for (int i = 0; i < nodeVMs.Length; i++)
            {
                // Save this nodes position
                positions[i] = nodeVMs[i].Position;

                if (nodeVMs[i] is PartitionNode)
                {
                    nodeIDToIndex[(nodeVMs[i] as PartitionNode).ID] = i;
                }
                else
                {
                    nodeIDToIndex[(nodeVMs[i] as NodeViewModelBase).ParentNode.ID] = i;
                }

                // Get a count of all the edges for this node

                //testcount += GraphComponents.GetNumberOfEdges(nodeVMs[i].ParentNode);

                List <INodeShape> visitedNeighbors = new List <INodeShape>();
                Model.INode       currentNode      = null;

                if (nodeVMs[i] is PartitionNode)
                {
                    currentNode = nodeVMs[i] as Model.INode;
                }
                else
                {
                    currentNode = (nodeVMs[i] as NodeViewModelBase).ParentNode;
                }

                // Loop over all the edges for this node
                foreach (Model.IEdge edge in graphComponents.GetEdges(currentNode))
                {
                    // Get the node at the opposite end of this edge
                    INodeShape oppositeNode = graphComponents.GetOppositeNode(edge, currentNode);

                    // Ensure that this edge is not a similarity edge and that
                    // the opposite node has not been visited already
                    if (!(edge is Model.SimilarityDataEdge) && !visitedNeighbors.Contains(oppositeNode))
                    {
                        edgeCounts[i]++;
                        visitedNeighbors.Add(oppositeNode);
                    }
                }
            }

            // Old version is doing this next call asynchronously
            if (nodeVMs.Length > 1)
            {
                CalculatePositions(graphComponents);
            }
        }
Example #5
0
        //TODO:  WE SHOULD HAVE EVENTS FOR WHEN NODE VIEWMODELS ARE REMOVED OR ADDED

        #region Edge View Models

        /// <summary>
        /// Creates a new EdgeViewModel
        /// </summary>
        /// <param name="edge">The IEdge instance that is responsible
        /// for the edge view model being created</param>
        private void CreateEdgeViewModel(IEdge edge)
        {
            IEdgeViewModel edgeViewModel;
            bool           isNew = true;

            // Check if the edge view model already exists
            if (edgeToEdgeViewModel.ContainsKey(edge))
            {
                isNew = false;
            }

            // Make sure that the edge exists in the graph data structure
            if (!graphData.ContainsEdge(edge))
            {
                // Since the edge doesn't exist in the graph
                // data structure, we need to add it
                graphData.AddEdge(edge);
            }

            // Create a new edge viewmodel
            if (isNew)
            {
                edgeViewModel = EdgeViewModelBase.GetEdgeViewModel(edge, graphData.Scope);
            }
            else
            {
                edgeViewModel = edgeToEdgeViewModel[edge];
            }

            // Get the source node view model
            INodeShape sourceNodeViewModel = null;
            INodeShape targetNodeViewModel = null;

            nodeToNodeViewModel.TryGetValue(edgeViewModel.ParentEdge.Source, out sourceNodeViewModel);
            nodeToNodeViewModel.TryGetValue(edgeViewModel.ParentEdge.Target, out targetNodeViewModel);

            if (sourceNodeViewModel != null)
            {
                edgeViewModel.X1 = sourceNodeViewModel.CenterPoint.X;
                edgeViewModel.Y2 = sourceNodeViewModel.CenterPoint.Y;
            }

            if (targetNodeViewModel != null)
            {
                edgeViewModel.X1 = targetNodeViewModel.CenterPoint.X;
                edgeViewModel.Y2 = targetNodeViewModel.CenterPoint.Y;
            }

            // If this is a new node, add it
            if (isNew)
            {
                // Add the edge to the edge/edgeviewmodel collection
                edgeToEdgeViewModel.Add(edge, edgeViewModel);

                // Fire the EdgeViewModelRemoved event
                OnEdgeViewModelAdded(new EdgeViewModelEventArgs(edgeViewModel, this.scope));
            }
        }
Example #6
0
        public static Boolean HasUri(this INodeShape myINodeShape)
        {
            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            return(myINodeShape.Uri != null);
        }
Example #7
0
        public static NodeShapes GetShape(this INodeShape myINodeShape)
        {
            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            return(myINodeShape.Shape);
        }
Example #8
0
        public static Boolean HasShape(this INodeShape myINodeShape)
        {
            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            return(myINodeShape.Shape != NodeShapes.NOTSET);
        }
Example #9
0
        public void RemoveNode(string nodeId)
        {
            GraphComponents graphComponents = GraphManager.Instance.DefaultGraphComponentsInstance;
            GraphData       graphData       = graphComponents.Data;

            INode      node      = graphData.GetNode(nodeId);
            INodeShape nodeShape = graphComponents.GetNodeViewModel(node);

            graphComponents.RemoveNodeViewModel(nodeShape);
        }
Example #10
0
        public static INodeShape ClearShape(this INodeShape myINodeShape)
        {
            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            myINodeShape.Shape = NodeShapes.NOTSET;

            return(myINodeShape);
        }
Example #11
0
        public static INodeShape ClearUri(this INodeShape myINodeShape)
        {
            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            myINodeShape.Uri = null;

            return(myINodeShape);
        }
Example #12
0
        /// <summary>
        /// Returns the view model for the provided Node
        /// </summary>
        /// <param name="node">The Node whose view model is being
        /// requested</param>
        /// <returns>the view model for the provided node</returns>
        public INodeShape GetNodeViewModel(INode node)
        {
            INodeShape nodeVM = null;

            if (this.nodeToNodeViewModel.TryGetValue(node, out nodeVM))
            {
                return(nodeVM);
            }

            return(null);
        }
Example #13
0
 public NodeConfig(INodeShape shape, Converter c, Pen nodepen, Pen selectednodepen, Color fillNodeColor,
                   Color fillSelectedNodeColor, Font font, Color fontColor)
 {
     Shape                 = shape;
     Converter             = c;
     NodePen               = nodepen;
     SelectedNodePen       = selectednodepen;
     FillNodeColor         = fillNodeColor;
     FillSelectedNodeColor = fillSelectedNodeColor;
     Font      = font;
     FontColor = fontColor;
 }
Example #14
0
        public static INodeShape SetUri(this INodeShape myINodeShape, Uri myUri)
        {
            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            if (myUri == null)
            {
                throw new ArgumentNullException("myUri must not be null!");
            }

            myINodeShape.Uri = myUri;

            return(myINodeShape);
        }
Example #15
0
        public static INode SetShape(this INode myINode, INodeShape myINodeShape)
        {
            if (myINode == null)
            {
                throw new ArgumentNullException("myINode must not be null!");
            }

            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            myINode.Shape = myINodeShape;

            return(myINode);
        }
Example #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="nodeVM"></param>
        /// <returns></returns>
        private IList <INodeShape> GetNeighbors(INodeShape nodeVM)
        {
            List <INodeShape> goodNeighbors = new List <INodeShape>();

            // The edges are stored in the primary graph scope, they were not copied
            // to the partition graph

            IEnumerable <IEdge> edges = null;

            // Get the collection of edges for this node.  How we get the edges
            // depends on what type of node we are dealing with.
            if (nodeVM is PartitionNode)
            {
                edges = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetEdges(nodeVM as PartitionNode);
            }
            else
            {
                edges = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetEdges((nodeVM as NodeViewModelBase).ParentNode);
            }

            // Loop over the edges
            foreach (IEdge edge in edges)
            {
                // Determine if an edge predicate was provided and if it is true or not
                if (EdgePredicate == null || EdgePredicate(edge))
                {
                    INodeShape oppositeNode = null;

                    // Get the node at the opposite end of this edge.  How we get
                    // the node depends on what type of node we are dealing with.
                    if (nodeVM is PartitionNode)
                    {
                        oppositeNode = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetOppositeNode(edge, nodeVM as PartitionNode);
                    }
                    else
                    {
                        oppositeNode = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetOppositeNode(edge, (nodeVM as NodeViewModelBase).ParentNode);
                    }

                    goodNeighbors.Add(oppositeNode);
                }
            }

            return(goodNeighbors);
        }
Example #17
0
        /// <summary>
        /// Adds the provided node view model (and its ParentNode) to the graph
        /// </summary>
        /// <param name="nodeVM">The node view model that should be added to the graph</param>
        public void AddNodeViewModel(INodeShape nodeVM)
        {
            // Validate the provided nodeVM
            if (nodeVM == null)
            {
                throw new ArgumentNullException("NodeVM", "Invalid node view model provided");
            }

            // Skip the current view model if it already exists
            if (this.nodeViewModels.Contains(nodeVM))
            {
                return;
            }

            // Determine if this is a PartitionNode
            if (!(nodeVM is PartitionNode))
            {
                NodeViewModelBase targetNode = nodeVM as NodeViewModelBase;
                INode             parentNode = targetNode.ParentNode;

                if (nodeToNodeViewModel.ContainsKey(targetNode.ParentNode))
                {
                    return;
                }
                else
                {
                    this.graphData.AddNode(parentNode);

                    // Process the orphan edges
                    ProcessOrphanEdges(parentNode);

                    // Setup event handlers for this node VM
                    targetNode.NodeMoved  += new System.EventHandler <System.EventArgs>(nodeViewModel_NodeMoved);
                    targetNode.NodeLoaded += new System.EventHandler <System.EventArgs>(nodeViewModel_NodeLoaded);

                    nodeToNodeViewModel.Add(targetNode.ParentNode, nodeVM);
                }
            }
            else
            {
                this.graphData.AddNode(nodeVM as PartitionNode);
            }

            nodeViewModels.Add(nodeVM);
        }
Example #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="initialNode"></param>
        /// <returns></returns>
        private PartitionNode GetClusterAsPartitionNode(INodeShape initialNode)
        {
            // Determine if the provided node is already
            // stored in our internal collection.  If it
            // is, return it.
            if (_nodeToPartition.ContainsKey(initialNode))
            {
                return(_nodeToPartition[initialNode]);
            }

            PartitionNode pn = new PartitionNode(_sourceGraph.Scope, "Partition" + (_currentId++).ToString());

            _partitionNodes.Add(pn);

            Dictionary <INodeShape, bool> nodesInCluster     = new Dictionary <INodeShape, bool>();
            Queue <INodeShape>            nodesToTriangulate = new Queue <INodeShape>();

            nodesInCluster[initialNode] = true;
            nodesToTriangulate.Enqueue(initialNode);

            while (nodesToTriangulate.Count > 0)
            {
                List <INodeShape> tempList = FindTrianglesToAdd(nodesToTriangulate.Dequeue(), nodesInCluster);

                foreach (INodeShape nodeVM in tempList)
                {
                    // Add the node view model to our queue
                    nodesToTriangulate.Enqueue(nodeVM);
                }
            }

            // Loop over all the node view models in the dictionary
            foreach (NodeViewModelBase nodeVM in nodesInCluster.Keys)
            {
                // Add the node to the partition
                pn.AddNode(nodeVM);

                // Save the updated partition node
                _nodeToPartition[nodeVM] = pn;
            }

            return(pn);
        }
Example #19
0
        /// <summary>
        /// Adds the provided edgeviewmodel (and it's parent edge)
        /// to the graph
        /// </summary>
        /// <param name="edgeVMs">The edge view model to be added to the graph</param>
        public void AddEdgeViewModel(IEdgeViewModel edgeVM)
        {
            // Add the ParentEdge for this view model.  The AddEdge method
            // will check if the edge already exists.
            this.graphData.AddEdge(edgeVM.ParentEdge);

            // Check if the edge view model already exists
            if (!this.edgeToEdgeViewModel.ContainsKey(edgeVM.ParentEdge))
            {
                if (graphData.OrphanEdges.Contains(edgeVM.ParentEdge))
                {
                    _orphanEdgeViewModels.Add(edgeVM);
                }
                else
                {
                    // Get the viewmodels for the source and target nodes
                    INodeShape sourceNodeVM = GetNodeViewModel(edgeVM.ParentEdge.Source);
                    INodeShape targetNodeVM = GetNodeViewModel(edgeVM.ParentEdge.Target);

                    // Ensure the edge is attached to its source
                    if (sourceNodeVM != null)
                    {
                        edgeVM.X1 = sourceNodeVM.CenterPoint.X;
                        edgeVM.Y1 = sourceNodeVM.CenterPoint.Y;
                    }

                    // Ensure the edge is attached to its target
                    if (targetNodeVM != null)
                    {
                        edgeVM.X2 = targetNodeVM.CenterPoint.X;
                        edgeVM.Y2 = targetNodeVM.CenterPoint.Y;
                    }

                    // Now we need to update the edge to edgeviewmodels collection
                    edgeToEdgeViewModel.Add(edgeVM.ParentEdge, edgeVM);

                    // Fire the EdgeViewModelAdded event
                    OnEdgeViewModelAdded(new EdgeViewModelEventArgs(edgeVM, this.scope));
                }
            }
        }
Example #20
0
        public static XElement ToXML(this INodeShape myINodeShape, XElement myGEXFXElement)
        {
            #region Initial checks

            if (myINodeShape == null)
            {
                throw new ArgumentNullException("myINodeShape must not be null!");
            }

            if (myGEXFXElement == null)
            {
                throw new ArgumentNullException("myGEXFXElement must not be null!");
            }

            #endregion

            if (myGEXFXElement.Attribute(XNamespace.Xmlns + "viz") == null)
            {
                myGEXFXElement.Add(new XAttribute(XNamespace.Xmlns + "viz", viz.NamespaceName));
            }

            return(new XElement(viz + "shape", new XAttribute("value", myINodeShape.Shape)));
        }
Example #21
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="initialNode"></param>
        /// <returns></returns>
        private PartitionNode GetClusterAsPartitionNode(INodeShape initialNode)
        {
            // Determine if the provided node is already
            // stored in our internal collection.  If it
            // is, return it.
            if (_nodeToPartition.ContainsKey(initialNode))
                return _nodeToPartition[initialNode];

            PartitionNode pn = new PartitionNode(_sourceGraph.Scope, "Partition" + (_currentId++).ToString());
            _partitionNodes.Add(pn);

            Dictionary<INodeShape, bool> nodesInCluster = new Dictionary<INodeShape, bool>();
            Queue<INodeShape> nodesToTriangulate = new Queue<INodeShape>();

            nodesInCluster[initialNode] = true;
            nodesToTriangulate.Enqueue(initialNode);

            while (nodesToTriangulate.Count > 0)
            {
                List<INodeShape> tempList = FindTrianglesToAdd(nodesToTriangulate.Dequeue(), nodesInCluster);

                foreach (INodeShape nodeVM in tempList)
                {
                    // Add the node view model to our queue
                    nodesToTriangulate.Enqueue(nodeVM);
                }
            }

            // Loop over all the node view models in the dictionary
            foreach (NodeViewModelBase nodeVM in nodesInCluster.Keys)
            {
                // Add the node to the partition
                pn.AddNode(nodeVM);

                // Save the updated partition node
                _nodeToPartition[nodeVM] = pn;
            }

            return pn;
        }
Example #22
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nodeVM"></param>
        /// <returns></returns>
        private IList<INodeShape> GetNeighbors(INodeShape nodeVM)
        {
            List<INodeShape> goodNeighbors = new List<INodeShape>();

            // The edges are stored in the primary graph scope, they were not copied
            // to the partition graph

            IEnumerable<IEdge> edges = null;

            // Get the collection of edges for this node.  How we get the edges
            // depends on what type of node we are dealing with.
            if (nodeVM is PartitionNode)
                edges = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetEdges(nodeVM as PartitionNode);
            else
                edges = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetEdges((nodeVM as NodeViewModelBase).ParentNode);

            // Loop over the edges
            foreach (IEdge edge in edges)
            {
                // Determine if an edge predicate was provided and if it is true or not
                if (EdgePredicate == null || EdgePredicate(edge))
                {
                    INodeShape oppositeNode = null;

                    // Get the node at the opposite end of this edge.  How we get
                    // the node depends on what type of node we are dealing with.
                    if (nodeVM is PartitionNode)
                        oppositeNode = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetOppositeNode(edge, nodeVM as PartitionNode);
                    else
                        oppositeNode = GraphManager.Instance.GetGraphComponents(nodeVM.Scope).GetOppositeNode(edge, (nodeVM as NodeViewModelBase).ParentNode);

                    goodNeighbors.Add(oppositeNode);
                }
            }

            return goodNeighbors;
        }
Example #23
0
        private int NodeSizeComparer(INodeShape first, INodeShape second)
        {
            double areaFirst = first.Width * first.Height;
            double areaSecond = second.Width * second.Height;

            return -areaFirst.CompareTo(areaSecond);
        }
Example #24
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="currentNode"></param>
        /// <param name="nodesInCluster"></param>
        /// <returns></returns>
        private List <INodeShape> FindTrianglesToAdd(INodeShape currentNode, Dictionary <INodeShape, bool> nodesInCluster)
        {
            //this dictionary is actually a meaningless matching - the only purpose
            //is to ensure constant time for the contains() method
            Dictionary <INodeShape, bool> nodeToInTriangle     = new Dictionary <INodeShape, bool>();
            List <INodeShape>             nodesFoundForCluster = new List <INodeShape>();
            bool neighborInGraph = false;

            // Loop over all the neighbors for the current node
            foreach (INodeShape neighbor in GetNeighbors(currentNode))
            {
                // Determing if the neighbor we are dealing with is a partition node
                if (neighbor is PartitionNode)
                {
                    neighborInGraph = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.ContainsNode(neighbor as PartitionNode);
                }
                else
                {
                    neighborInGraph = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.ContainsNode((neighbor as NodeViewModelBase).ParentNode);
                }

                if (!nodeToInTriangle.ContainsKey(neighbor) && neighborInGraph)
                {
                    int neighborCount = 0;

                    // Get the count of neighbors for this node.  We determine
                    // this differently depending on the type of node.
                    if (neighbor is PartitionNode)
                    {
                        neighborCount = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.Neighbors(neighbor as PartitionNode).Count();
                    }
                    else
                    {
                        neighborCount = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.Neighbors((neighbor as NodeViewModelBase).ParentNode).Count();
                    }

                    if (neighborCount == 1)
                    {
                        nodeToInTriangle[neighbor] = true;
                        if (!nodesInCluster.ContainsKey(neighbor))
                        {
                            nodesInCluster[neighbor] = true;
                            nodesFoundForCluster.Add(neighbor);

                            //if neighbor was already put in its own cluster, remove that cluster from the partitioning
                            if (_nodeToPartition.ContainsKey(neighbor))
                            {
                                _partitionNodes.Remove(_nodeToPartition[neighbor]);
                                _partitionedGraph.RemoveNodeViewModel(_nodeToPartition[neighbor]);
                                _nodeToPartition.Remove(neighbor);
                            }
                        }
                    }

                    foreach (NodeViewModelBase neighbor2 in GetNeighbors(neighbor))
                    {
                        if (GetNeighbors(neighbor2).Contains(currentNode))
                        {
                            if (!nodeToInTriangle.ContainsKey(neighbor))
                            {
                                nodeToInTriangle[neighbor] = true;
                                if (!nodesInCluster.ContainsKey(neighbor))
                                {
                                    nodesInCluster[neighbor] = true;
                                    nodesFoundForCluster.Add(neighbor);
                                }
                            }

                            if (!nodeToInTriangle.ContainsKey(neighbor2))
                            {
                                nodeToInTriangle[neighbor2] = true;
                                if (!nodesInCluster.ContainsKey(neighbor2))
                                {
                                    nodesInCluster[neighbor2] = true;
                                    nodesFoundForCluster.Add(neighbor2);
                                }
                            }
                        }
                    }
                }
            }

            return(nodesFoundForCluster);
        }
Example #25
0
        protected void CalculatePositions(GraphComponents graphComponents)
        {
            double   deltaNorm;
            double   otherNorm;
            bool     converged           = false;
            double   T                   = K;
            double   tolerance           = 0.02;
            double   coolingConstant     = 0.99;
            Point    tempDelta           = new Point();
            Point    displacement        = new Point();
            TimeSpan globalForceTimespan = new TimeSpan();
            TimeSpan localForceTimespan  = new TimeSpan();
            TimeSpan indexOfTime         = new TimeSpan();
            DateTime start;

            Model.INode currentNode = null;

            List <int> listOfAllNodeIndices = new List <int>();

            // If we aren't partitioning into grids then use all the nodes every time
            if (!useGraphPartitioning)
            {
                for (int i = 0; i < nodeVMs.Length; i++)
                {
                    listOfAllNodeIndices.Add(i);
                }
            }

            for (int loop = 0; loop < maxIterations && !converged; loop++)
            {
                double max = 200;
                double min = 65;
                double R   = max - ((loop / maxIterations) * (max - min) + min);

                if (useGraphPartitioning)
                {
                    // Put all vertices into appropraite cells
                    CalculateNodeCells(R);
                }

                converged = true;

                // Loop over nodes
                for (int currentNodeIndex = 0; currentNodeIndex < nodeVMs.Length; currentNodeIndex++)
                {
                    if (nodeVMs[currentNodeIndex] is PartitionNode)
                    {
                        currentNode = nodeVMs[currentNodeIndex] as Model.INode;
                    }
                    else
                    {
                        currentNode = (nodeVMs[currentNodeIndex] as NodeViewModelBase).ParentNode;
                    }

                    displacement = new Point(0, 0);

                    // global repulsive force (huh??)
                    start = DateTime.Now;

                    IList <int> repulsionNodes = null;
                    if (useGraphPartitioning)
                    {
                        // Find all nodes, within a certain distance, to perform repulstion on.
                        // Get nodes within maxDistance from this node.
                        double maxDistance = 50; //TODO:  MAKE THIS CONFIGURABLE
                        repulsionNodes = FindNodesForRepulsion(currentNodeIndex, R, maxDistance);
                    }
                    else
                    {
                        // Just repulse all nodes
                        repulsionNodes = listOfAllNodeIndices;
                    }

                    // Loop over all nodes in repulsion list
                    foreach (int i in repulsionNodes)
                    {
                        // We skip this calculation for the current node
                        if (i != currentNodeIndex)
                        {
                            tempDelta.X = positions[i].X - positions[currentNodeIndex].X;
                            tempDelta.Y = positions[i].Y - positions[currentNodeIndex].Y;

                            if (tempDelta.X == 0)
                            {
                                tempDelta.X = random.NextDouble() * .001 * K;
                            }
                            if (tempDelta.Y == 0)
                            {
                                tempDelta.Y = random.NextDouble() * .001 * K;
                            }

                            deltaNorm = Math.Max(1, Math.Abs(tempDelta.X) + Math.Abs(tempDelta.Y));
                            otherNorm = Math.Abs(positions[i].X + Math.Abs(positions[i].Y));

                            double globalForce = GlobalForce(deltaNorm, otherNorm);

                            displacement.X += (tempDelta.X / deltaNorm) * globalForce;
                            displacement.Y += (tempDelta.Y / deltaNorm) * globalForce;
                        }
                    }

                    globalForceTimespan += (DateTime.Now - start);

                    // Local forces
                    start = DateTime.Now;

                    // Loop over all the edges for this node
                    foreach (Model.IEdge edge in graphComponents.GetEdges(currentNode))
                    {
                        DateTime startIndex = DateTime.Now;

                        int    index  = -1;
                        string nodeID = string.Empty;

                        INodeShape oppositeNode = graphComponents.GetOppositeNode(edge, currentNode);
                        //NodeViewModelBase oppositeNodeVM = GraphComponents.GetOppositeNode(edgeVM.ParentEdge, nodeVMs[currentNode].ParentNode);

                        // Get the ID for the opposite node.  How we do this depends
                        // on the type of node that we are dealing with.
                        if (oppositeNode is PartitionNode)
                        {
                            nodeID = (oppositeNode as PartitionNode).ID;
                        }
                        else
                        {
                            nodeID = (oppositeNode as NodeViewModelBase).ParentNode.ID;
                        }

                        if (!nodeIDToIndex.TryGetValue(nodeID, out index))
                        {
                            continue;
                        }
                        indexOfTime += DateTime.Now - startIndex;

                        if (index != -1)
                        {
                            tempDelta = new Point(positions[index].X - positions[currentNodeIndex].X, positions[index].Y - positions[currentNodeIndex].Y);

                            if (tempDelta.X == 0)
                            {
                                tempDelta.X = random.NextDouble() * .001 * K;
                            }
                            if (tempDelta.Y == 0)
                            {
                                tempDelta.Y = random.NextDouble() * .001 * K;
                            }

                            deltaNorm = Math.Max(Math.Sqrt((tempDelta.X * tempDelta.X) + (tempDelta.Y * tempDelta.Y)), 1);
                            otherNorm = Math.Max(Math.Sqrt((oppositeNode.Position.X * oppositeNode.Position.X) + (oppositeNode.Position.Y * oppositeNode.Position.Y)), 1);

                            double localForce = LocalForce(deltaNorm, edgeCounts[currentNodeIndex], edge, graphComponents);

                            displacement.X += (tempDelta.X / deltaNorm) * localForce;
                            displacement.Y += (tempDelta.Y / deltaNorm) * localForce;
                        }
                    }

                    localForceTimespan += (DateTime.Now - start);

                    // Reposition node (huh??)
                    if (displacement.X == 0)
                    {
                        displacement.X = 1;
                    }

                    double displacementNorm = Math.Sqrt((displacement.X * displacement.X) + (displacement.Y * displacement.Y));
                    double newX             = positions[currentNodeIndex].X + (displacement.X / displacementNorm) * Math.Min(T, displacementNorm);
                    double newY             = positions[currentNodeIndex].Y + (displacement.Y / displacementNorm) * Math.Min(T, displacementNorm);

                    tempDelta = new Point(newX - positions[currentNodeIndex].X, newY - positions[currentNodeIndex].Y);

                    positions[currentNodeIndex].X = newX;
                    positions[currentNodeIndex].Y = newY;

                    // no clue what this is doing
                    if (Math.Sqrt((tempDelta.X * tempDelta.X) + (tempDelta.Y * tempDelta.Y)) > K * tolerance)
                    {
                        converged = false;
                    }
                }

                // cool (huh??)
                T *= coolingConstant;
            }

            //System.Diagnostics.Debug.WriteLine(globalForceTimespan);
            //System.Diagnostics.Debug.WriteLine(localForceTimespan);
            //System.Diagnostics.Debug.WriteLine(indexOfTime);
        }
Example #26
0
        public static INode SetShape(this INode myINode, INodeShape myINodeShape)
        {
            if (myINode == null)
                throw new ArgumentNullException("myINode must not be null!");

            if (myINodeShape == null)
                throw new ArgumentNullException("myINodeShape must not be null!");

            myINode.Shape = myINodeShape;

            return myINode;
        }
Example #27
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentNode"></param>
        /// <param name="nodesInCluster"></param>
        /// <returns></returns>
        private List<INodeShape> FindTrianglesToAdd(INodeShape currentNode, Dictionary<INodeShape, bool> nodesInCluster)
        {
            //this dictionary is actually a meaningless matching - the only purpose
            //is to ensure constant time for the contains() method
            Dictionary<INodeShape, bool> nodeToInTriangle = new Dictionary<INodeShape, bool>();
            List<INodeShape> nodesFoundForCluster = new List<INodeShape>();
            bool neighborInGraph = false;

            // Loop over all the neighbors for the current node
            foreach (INodeShape neighbor in GetNeighbors(currentNode))
            {
                // Determing if the neighbor we are dealing with is a partition node
                if (neighbor is PartitionNode)
                    neighborInGraph = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.ContainsNode(neighbor as PartitionNode);
                else
                    neighborInGraph = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.ContainsNode((neighbor as NodeViewModelBase).ParentNode);

                if (!nodeToInTriangle.ContainsKey(neighbor) && neighborInGraph)
                {
                    int neighborCount = 0;

                    // Get the count of neighbors for this node.  We determine
                    // this differently depending on the type of node.
                    if (neighbor is PartitionNode)
                        neighborCount = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.Neighbors(neighbor as PartitionNode).Count();
                    else
                        neighborCount = GraphManager.Instance.GetGraphComponents(_sourceGraph.Scope).Data.Neighbors((neighbor as NodeViewModelBase).ParentNode).Count();

                    if (neighborCount == 1)
                    {
                        nodeToInTriangle[neighbor] = true;
                        if (!nodesInCluster.ContainsKey(neighbor))
                        {
                            nodesInCluster[neighbor] = true;
                            nodesFoundForCluster.Add(neighbor);

                            //if neighbor was already put in its own cluster, remove that cluster from the partitioning
                            if (_nodeToPartition.ContainsKey(neighbor))
                            {
                                _partitionNodes.Remove(_nodeToPartition[neighbor]);
                                _partitionedGraph.RemoveNodeViewModel(_nodeToPartition[neighbor]);
                                _nodeToPartition.Remove(neighbor);
                            }
                        }
                    }

                    foreach (NodeViewModelBase neighbor2 in GetNeighbors(neighbor))
                    {
                        if (GetNeighbors(neighbor2).Contains(currentNode))
                        {
                            if (!nodeToInTriangle.ContainsKey(neighbor))
                            {
                                nodeToInTriangle[neighbor] = true;
                                if (!nodesInCluster.ContainsKey(neighbor))
                                {
                                    nodesInCluster[neighbor] = true;
                                    nodesFoundForCluster.Add(neighbor);
                                }
                            }

                            if (!nodeToInTriangle.ContainsKey(neighbor2))
                            {
                                nodeToInTriangle[neighbor2] = true;
                                if (!nodesInCluster.ContainsKey(neighbor2))
                                {
                                    nodesInCluster[neighbor2] = true;
                                    nodesFoundForCluster.Add(neighbor2);
                                }
                            }
                        }
                    }
                }
            }

            return nodesFoundForCluster;
        }