* @throws CycleInDagException
*/
public static Table[] sortTables() throws CycleInDagException
{
CatalogContext cc = new CatalogContext(s_cat.getCatalog());
DependencyGraph dgraph = DependencyGraphGenerator.generate(cc);
int size = dgraph.getVertexCount();
Table[] ret = new Table[size];
// zero_list: vertices whose in-degree is zero
List<DesignerVertex> zero_list = new LinkedList<DesignerVertex>();
List<DesignerVertex> non_zero_list = new LinkedList<DesignerVertex>();
// initialize two lists
for (DesignerVertex v : dgraph.getVertices())
{
if (dgraph.getPredecessorCount(v) == 0)
{
zero_list.add(v);
// System.out.println("To zero_list: " + v);
}
else
{
non_zero_list.add(v);
// System.out.println("To non_zero_list: " + v);
}
}
int cnt = 0;
while (true)
{
if (zero_list.isEmpty() && non_zero_list.isEmpty())
break;
if (zero_list.isEmpty())
{
throw new CycleInDagException();
}
DesignerVertex cur = zero_list.remove(0);
ret[cnt++] = (Table) (cur.getCatalogItem());
// System.out.println("Next ver: " + cur);
Collection<DesignerVertex> successors = dgraph.getSuccessors(cur);
dgraph.removeVertex(cur);
for (DesignerVertex successor : successors)
{
if (dgraph.getPredecessorCount(successor) == 0)
{
non_zero_list.remove(successor);
zero_list.add(successor);
// System.out.println("Move " + successor +
// " from non_zero to zero");