/** Private method. Prompts for a solver until valid value is specified by the user. */ private static Solver promptSolver(Scanner scanner) { System.out.println("Select algorithm:"); String line = scanner.nextLine(); Solver solver = Solver.fromIdentifier(line.trim()); if (solver != null) { return solver; } else { System.out.println("Invalid input."); return promptSolver(scanner); } }
/** * Main entry point. * * <p>Can be used in interactive and non-interactive mode. */ public static void main(String[] args) { int argc = args.length; Solver solver = null; SATFormula formula = null; switch (argc) { case 0: { Scanner scanner = new Scanner(System.in); solver = promptSolver(scanner); System.out.println("Enter the clause definition (end with Ctrl+D):"); List<String> lines = new ArrayList<>(); while (scanner.hasNextLine()) { lines.add(scanner.nextLine().trim()); } scanner.close(); formula = parseFormula(lines); break; } case 2: Path path = Paths.get(args[1]); Reader reader = null; try { reader = new Reader(path); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } solver = Solver.fromIdentifier(args[0]); if (solver == null) { System.err.println("Error: unsupported algorithm."); System.exit(-1); } formula = parseFormula(reader); break; default: System.out.println("Error: wrong usage."); System.exit(-1); } System.out.println("Solution: " + solver.solve(formula)); }