degrees.put( node, Integer.valueOf( degree ) );
}
for( int i = 0, size = nodesLeft.size(); i < size; i++ ) {
if( nodesPending.isEmpty() )
throw new InternalReasonerException( "Cycle detected in the taxonomy!" );
TaxonomyNode<T> node = nodesPending.values().iterator().next();
int deg = degrees.get( node );
if( deg != 0 )
throw new InternalReasonerException( "Cycle detected in the taxonomy " + node + " "
+ deg + " " + nodesSorted.size() + " " + nodes.size() );
nodesPending.remove( node.getName() );
nodesLeft.remove( node );
if( includeEquivalents )
nodesSorted.addAll( node.getEquivalents() );
else
nodesSorted.add( node.getName() );
for( TaxonomyNode<T> sub : node.getSubs() ) {
int degree = degrees.get( sub );
if( degree == 1 ) {
nodesPending.put( sub.getName(), sub );
degrees.put( sub, 0 );
}
else
degrees.put( sub, degree - 1 );
}
}
if( !nodesLeft.isEmpty() )
throw new InternalReasonerException( "Failed to sort elements: " + nodesLeft );
log.fine( "done" );
return nodesSorted;
}