import java.io.*; import java.util.*; /** * FileManager is a simple class designed to read * files formatted for the SAT1 program. The format * of the file must comply to these rules:

* 1) comments start at the beginning of the file only * and are denoted by a 'c'.

* 2) there may be zero or more lines of comments followed * immediately by the line p cnf num_vars num_clauses * where num_vars and num_clauses are integers and * match the correct number of variables and clauses * appearing in the formula file.

* 3) the remainder of the file is comprised of num_clauses * number of lines containing only the numbers from 1-num_vars, * that correspond to their correct boolean variable, and their * negatives (to negate the boolean variables). Every line ends * with the number 0 so the FileManager will know that the clause * has been completely read.

* * To use this class, prepFile() must be called first so the FileManager * will be able to access the appropriate file. The parse() method will * return the formula contained in the file as a byte matrix. * * @author Andrew J. Fabian * @version 01/31/07 */ public class FileManager { private Scanner reader; /** * Initialize the Scanner to read from the appropriate file. * * @param filename The name of the file to be accessed. * @return true if the load was successful. */ public boolean prepFile(String filename) { boolean success = true; try { reader = new Scanner(new File(filename)); } catch(FileNotFoundException fnfe) { success = false; } return success; } /** * This method must be called after prepFile() * has been successful. This method will parse * the file as described in the FileManager class * javadoc. It will then return a byte matrix * representation of the SAT formula contained * in the file. * * @return a byte matrix representation of the formula * @throws IOException if the file is improperly formatted */ public byte[][] parse() throws IOException { int x, y; //x=num of variables, y=num of clauses byte[][] formula = null; try { while(reader.next().equals("c")) //skip comments reader.nextLine(); reader.next(); //skip to parameters x = reader.nextInt(); //read number of variables y = reader.nextInt(); //read number of clauses //initialize formula byte matrix formula = new byte[x][y]; for(int i=0; i 0) { if(formula[element-1][i] == -1) formula[Math.abs(element)-1][i] = 0; else formula[element-1][i] = 1; } element = reader.nextInt(); } } } catch(InputMismatchException ime) { throw new IOException("Bad input file format!"); } catch(NoSuchElementException nsee) { throw new IOException("Bad input file format!"); } return formula; } }