Map<Integer, DNVNode> nodeToParent = new HashMap<Integer, DNVNode>();
Map<Integer, List<DNVNode>> parentToNodes = new HashMap<Integer, List<DNVNode>>();
Map<Integer, DNVNode> allNodes = new HashMap<Integer, DNVNode>( graph.getNodesMap( currentLevel ) );
DNVNode tempNode;
DNVNode parentNode;
while( allNodes.size() > 0 )
{
tempNode = allNodes.values().iterator().next();
allNodes.remove( tempNode.getId() );
parentNode = createParentNode( tempNode, newLevel, graph, nodeToParent, parentToNodes );
handleNeighbors( tempNode.getNeighbors(), parentNode, allNodes, nodeToParent );
}
// Set connections from parent to sub nodes
Iterator<Integer> ids = nodeToParent.keySet().iterator();
Integer tempId;
List<DNVNode> tempList;
DNVNode tempNode2;
while( ids.hasNext() )
{
tempId = ids.next();
tempNode = nodeToParent.get( tempId );
tempList = parentToNodes.get( tempNode.getId() );
tempNode2 = (DNVNode)graph.getNodeById( tempId );
if( tempNode2 != null && !tempList.contains( tempNode2 ) )
{
tempList.add( tempNode2 );
parentToNodes.put( tempNode.getId(), tempList );
tempNode.setLabel( "" + tempList.size() );
}
if( tempNode2 == null )
{
System.out.println( "Trying to add a link from a non-existing node" );
}
}
// Create the edges and set the connections from parent to subnodes
List<DNVEdge> edgeList;
ids = parentToNodes.keySet().iterator();
DNVNode tempParent;
DNVNode tempParent2;
DNVEdge tempEdge;
while( ids.hasNext() )
{
tempId = ids.next();
tempParent2 = (DNVNode)graph.getNodeById( tempId );
tempList = parentToNodes.get( tempId );
tempParent2.setSubNodes( tempList );
for( int i = 0; i < tempList.size(); i++ )
{
tempNode = tempList.get( i );
edgeList = tempNode.getFromEdges();
for( int j = 0; j < edgeList.size(); j++ )
{
tempEdge = edgeList.get( j );
tempNode2 = tempEdge.getTo();
tempParent = nodeToParent.get( tempNode2.getId() );
if( tempParent != null && !tempParent.getId().equals( tempId ) && tempParent != tempParent2 )
{
if( !tempParent.getNeighbors().contains( tempParent2 ) )
{
tempEdge = new DNVEdge( newLevel.intValue(), DNVEdge.DEFAULT_RESTING_DISTANCE, false, tempParent2, tempParent, graph );
tempParent2.addFromEdge( tempEdge );
tempParent.addToEdge( tempEdge );
graph.addNode( newLevel, tempEdge );
}
}
}
edgeList = tempNode.getToEdges();
for( int j = 0; j < edgeList.size(); j++ )
{
tempEdge = edgeList.get( j );
tempNode2 = tempEdge.getFrom();
tempParent = nodeToParent.get( tempNode2.getId() );
if( tempParent != null && !tempParent.getId().equals( tempId ) && tempParent != tempParent2 )
{
tempParent2 = (DNVNode)graph.getNodeById( tempId );
if( !tempParent.getNeighbors().contains( tempParent2 ) )
{
tempEdge = new DNVEdge( newLevel.intValue(), DNVEdge.DEFAULT_RESTING_DISTANCE, false, tempParent, tempParent2, graph );
tempParent2.addToEdge( tempEdge );
tempParent.addFromEdge( tempEdge );
graph.addNode( newLevel, tempEdge );
}
}
}