/** * Reads query file and parses the queries. * * @requires querystream, outstream, are successfully initialized * @requires g to be already generated * @param queryStream file input stream of query file * @param outStream file output stream of output file * @param g * @effects writes results to output file */ private static void parseQuery(FileInputStream queryStream, FileOutputStream outStream, Graph g) { try { BufferedWriter output = new BufferedWriter(new OutputStreamWriter(outStream)); BufferedReader queryReader = new BufferedReader(new InputStreamReader(queryStream)); final int COMMAND_INDEX = 0; final int U1_INDEX = 1; final int U2_INDEX = 2; final String QUERY_ENDING = " ?"; Set<Set<String>> queries = new LinkedHashSet<Set<String>>(); String line; while ((line = queryReader.readLine()) != null) { // each query set contains two user id strings and a command // this also handles duplicate user ids with different commands Set<String> query = new LinkedHashSet<String>(); // a bit overkill, but eliminate any unnecessary whitespace if needed and replaces with // single space String[] columns = line.trim().replaceAll("\\s+", " ").split(" "); // first column is query // second column is user 1 // third column is user 2 String command = columns[COMMAND_INDEX]; String id1 = columns[U1_INDEX]; String id2 = columns[U2_INDEX]; query.add(id1); query.add(id2); query.add(command); // check if query ends with question mark (with leading space) and query is unique if (line.endsWith(QUERY_ENDING) && !queries.contains(query)) { queries.add(query); Vertex u1 = new Vertex(id1); Vertex u2 = new Vertex(id2); printResults(output, g, u1, u2, command); } } queryReader.close(); output.close(); // return new LinkedHashMap<List<Vertex>, String>(queries); } catch (Exception e) { // if something goes wrong throw new RuntimeException(e); } }