/** * Loads up a .task file, that may or may not contain cycles. It sorts these * tasks. If there are no cycles, it will do a basic topological sort. However * if there is a cycle, it will first find the strong components, and then it * will sort those strong components topologically. * * It relies on both a Digraph object, and a TopologicalSorter non-instantiable * class. The APIs for these are given in the assignment. * * Note that because you will be probably be using recursive methods, that this * program might overrun the stack very easily. You may have to increase the * stack size in your IDE's options. * * @author Adam A. Smith * @version 2023.11.12 */ import java.io.*; import java.util.*; public class TaskSorter { public static void main(String[] args) { // make sure we have a file if (args.length == 0) { System.err.println("Please enter a file name!"); System.exit(1); } // make the graph, and print out the ordering try { Digraph digraph = makeDigraphFromTaskFile(args[0]); sortAndReport(args[0], digraph); } catch (IOException e) { System.err.println("Couldn't open file \"" +args[0]+ "\"."); System.exit(1); } } // load a rask file & make a Digraph from its lines private static Digraph makeDigraphFromTaskFile(String filename) throws IOException { Digraph digraph = new Digraph(); // read the file Scanner scanner = new Scanner(new File(filename)); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] tokens = line.split("\t"); for (int i=0; i