diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-28 17:11:35 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-28 17:11:35 +0100 |
| commit | de3749532d060f26c966a81c03f9a5d846c33d06 (patch) | |
| tree | 6cc5ba2837dc3c167b100f5e8be4e8c16da7be98 /src/uk/ac/ox/cs/pagoda/util/Utility.java | |
| parent | 4298cdef8e551325cd16b4e24ae6699c44b60751 (diff) | |
| download | ACQuA-de3749532d060f26c966a81c03f9a5d846c33d06.tar.gz ACQuA-de3749532d060f26c966a81c03f9a5d846c33d06.zip | |
Merged updates from upstream.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/util/Utility.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/Utility.java | 104 |
1 files changed, 42 insertions, 62 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/util/Utility.java b/src/uk/ac/ox/cs/pagoda/util/Utility.java index 7b23e0d..e98cc81 100644 --- a/src/uk/ac/ox/cs/pagoda/util/Utility.java +++ b/src/uk/ac/ox/cs/pagoda/util/Utility.java | |||
| @@ -12,25 +12,30 @@ import java.util.*; | |||
| 12 | 12 | ||
| 13 | public class Utility { | 13 | public class Utility { |
| 14 | 14 | ||
| 15 | private static Logger LOGS; | ||
| 16 | static { | ||
| 17 | LOGS = Logger.getLogger("PAGOdA"); | ||
| 18 | LOGS.setLevel(Level.INFO); | ||
| 19 | } | ||
| 20 | |||
| 21 | public static final String JAVA_FILE_SEPARATOR = "/"; | 15 | public static final String JAVA_FILE_SEPARATOR = "/"; |
| 22 | public static final String FILE_SEPARATOR = System.getProperty("file.separator"); | 16 | public static final String FILE_SEPARATOR = System.getProperty("file.separator"); |
| 23 | public static final String LINE_SEPARATOR = System.getProperty("line.separator"); | 17 | public static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
| 24 | 18 | public static final int TEST = -1; | |
| 25 | private static final String TEMP_DIR_PATH= "pagoda_tmp"; | ||
| 26 | private static String tempDir; | ||
| 27 | |||
| 28 | public static final int TEST = -1; | ||
| 29 | public static final int FLY = 0; | 19 | public static final int FLY = 0; |
| 30 | public static final int UOBM = 1; | 20 | public static final int UOBM = 1; |
| 31 | public static final int LUBM = 2; | 21 | public static final int LUBM = 2; |
| 32 | public static final int AEO = 3; | 22 | public static final int AEO = 3; |
| 33 | public static final int WINE = 4; | 23 | public static final int WINE = 4; |
| 24 | private static final String TEMP_DIR_PATH = "pagoda_tmp"; | ||
| 25 | static Stack<PrintStream> outs = new Stack<PrintStream>(); | ||
| 26 | private static Logger LOGS; | ||
| 27 | private static String tempDir; | ||
| 28 | private static int asciiX = (int) 'X'; | ||
| 29 | private static StringBuilder logMessage = new StringBuilder(); | ||
| 30 | |||
| 31 | static { | ||
| 32 | LOGS = Logger.getLogger("PAGOdA"); | ||
| 33 | LOGS.setLevel(Level.DEBUG); | ||
| 34 | } | ||
| 35 | |||
| 36 | static { | ||
| 37 | outs.push(System.out); | ||
| 38 | } | ||
| 34 | 39 | ||
| 35 | public static String getGlobalTempDirAbsolutePath() { | 40 | public static String getGlobalTempDirAbsolutePath() { |
| 36 | if(tempDir == null) { | 41 | if(tempDir == null) { |
| @@ -38,7 +43,7 @@ public class Utility { | |||
| 38 | Path path = Files.createTempDirectory(TEMP_DIR_PATH); | 43 | Path path = Files.createTempDirectory(TEMP_DIR_PATH); |
| 39 | tempDir = path.toString(); | 44 | tempDir = path.toString(); |
| 40 | new File(tempDir).deleteOnExit(); | 45 | new File(tempDir).deleteOnExit(); |
| 41 | } catch (IOException e) { | 46 | } catch(IOException e) { |
| 42 | e.printStackTrace(); | 47 | e.printStackTrace(); |
| 43 | System.exit(1); | 48 | System.exit(1); |
| 44 | } | 49 | } |
| @@ -46,46 +51,39 @@ public class Utility { | |||
| 46 | return tempDir; | 51 | return tempDir; |
| 47 | } | 52 | } |
| 48 | 53 | ||
| 49 | public static Set<Atom> toSet(Atom[] data) | 54 | public static Set<Atom> toSet(Atom[] data) { |
| 50 | { | ||
| 51 | HashSet<Atom> ret = new HashSet<Atom>(); | 55 | HashSet<Atom> ret = new HashSet<Atom>(); |
| 52 | for (Atom element: data) | 56 | for(Atom element : data) |
| 53 | ret.add(element); | 57 | ret.add(element); |
| 54 | return ret; | 58 | return ret; |
| 55 | } | 59 | } |
| 56 | 60 | ||
| 57 | static Stack<PrintStream> outs = new Stack<PrintStream>(); | ||
| 58 | |||
| 59 | static { | ||
| 60 | outs.push(System.out); | ||
| 61 | } | ||
| 62 | |||
| 63 | public static boolean redirectSystemOut() | 61 | public static boolean redirectSystemOut() |
| 64 | { | 62 | { |
| 65 | String stamp = new SimpleDateFormat( "HH:mm:ss").format(new Date()); | 63 | String stamp = new SimpleDateFormat( "HH:mm:ss").format(new Date()); |
| 66 | return redirectCurrentOut("./console" + stamp + ".txt"); | 64 | return redirectCurrentOut("./console" + stamp + ".txt"); |
| 67 | } | 65 | } |
| 68 | 66 | ||
| 69 | public static boolean redirectCurrentOut(String fileName) | 67 | public static boolean redirectCurrentOut(String fileName) |
| 70 | { | 68 | { |
| 71 | File file = new File(fileName); | 69 | File file = new File(fileName); |
| 72 | PrintStream out; | 70 | PrintStream out; |
| 73 | try { | 71 | try { |
| 74 | out = new PrintStream(new FileOutputStream(file)); | 72 | out = new PrintStream(new FileOutputStream(file)); |
| 75 | } catch (FileNotFoundException e) { | 73 | } catch (FileNotFoundException e) { |
| 76 | e.printStackTrace(); | 74 | e.printStackTrace(); |
| 77 | return false; | 75 | return false; |
| 78 | } | 76 | } |
| 79 | outs.push(out); | 77 | outs.push(out); |
| 80 | System.setOut(out); | 78 | System.setOut(out); |
| 81 | return true; | 79 | return true; |
| 82 | } | 80 | } |
| 83 | 81 | ||
| 84 | public static void closeCurrentOut() { | 82 | public static void closeCurrentOut() { |
| 85 | if (!outs.isEmpty()) | 83 | if (!outs.isEmpty()) |
| 86 | outs.pop().close(); | 84 | outs.pop().close(); |
| 87 | 85 | ||
| 88 | if (!outs.isEmpty()) | 86 | if(!outs.isEmpty()) |
| 89 | System.setOut(outs.peek()); | 87 | System.setOut(outs.peek()); |
| 90 | } | 88 | } |
| 91 | 89 | ||
| @@ -103,7 +101,7 @@ public class Utility { | |||
| 103 | while ((line = reader.readLine()) != null && !line.startsWith("}")) | 101 | while ((line = reader.readLine()) != null && !line.startsWith("}")) |
| 104 | if (first) { | 102 | if (first) { |
| 105 | first = false; | 103 | first = false; |
| 106 | query = expression(line.trim()); | 104 | query = expression(line.trim()); |
| 107 | } | 105 | } |
| 108 | else query += ", " + expression(line.trim()); | 106 | else query += ", " + expression(line.trim()); |
| 109 | writer.write(query); | 107 | writer.write(query); |
| @@ -122,8 +120,6 @@ public class Utility { | |||
| 122 | else return parts[1] + "(?" + variableIndex(parts[0]) + ",?" + variableIndex(parts[2]) + ")"; | 120 | else return parts[1] + "(?" + variableIndex(parts[0]) + ",?" + variableIndex(parts[2]) + ")"; |
| 123 | } | 121 | } |
| 124 | 122 | ||
| 125 | private static int asciiX = (int)'X'; | ||
| 126 | |||
| 127 | private static int variableIndex(String exp) { | 123 | private static int variableIndex(String exp) { |
| 128 | char var = exp.charAt(1); | 124 | char var = exp.charAt(1); |
| 129 | return (int)var - asciiX; | 125 | return (int)var - asciiX; |
| @@ -135,12 +131,12 @@ public class Utility { | |||
| 135 | return null; | 131 | return null; |
| 136 | return line.trim(); | 132 | return line.trim(); |
| 137 | } | 133 | } |
| 138 | 134 | ||
| 139 | public static String getTextfromFile(String fileName) throws FileNotFoundException { | 135 | public static String getTextfromFile(String fileName) throws FileNotFoundException { |
| 140 | Scanner scanner = new Scanner(new File(fileName)); | 136 | Scanner scanner = new Scanner(new File(fileName)); |
| 141 | String program = scanner.useDelimiter("\\Z").next(); | 137 | String program = scanner.useDelimiter("\\Z").next(); |
| 142 | scanner.close(); | 138 | scanner.close(); |
| 143 | return program; | 139 | return program; |
| 144 | } | 140 | } |
| 145 | 141 | ||
| 146 | public static String[] getPattern(BufferedReader answerReader) throws IOException { | 142 | public static String[] getPattern(BufferedReader answerReader) throws IOException { |
| @@ -152,59 +148,57 @@ public class Utility { | |||
| 152 | 148 | ||
| 153 | public static void removeRecursively(File file) { | 149 | public static void removeRecursively(File file) { |
| 154 | if (!file.exists()) return; | 150 | if (!file.exists()) return; |
| 155 | 151 | ||
| 156 | if (file.isDirectory()) | 152 | if (file.isDirectory()) |
| 157 | for (File tFile: file.listFiles()) | 153 | for (File tFile: file.listFiles()) |
| 158 | removeRecursively(tFile); | 154 | removeRecursively(tFile); |
| 159 | file.delete(); | 155 | file.delete(); |
| 160 | } | 156 | } |
| 161 | 157 | ||
| 162 | public static void removeRecursively(String fileName) { | 158 | public static void removeRecursively(String fileName) { |
| 163 | removeRecursively(new File(fileName)); | 159 | removeRecursively(new File(fileName)); |
| 164 | } | 160 | } |
| 165 | 161 | ||
| 166 | public static Collection<String> getQueryTexts(String fileName) throws IOException { | 162 | public static Collection<String> getQueryTexts(String fileName) throws IOException { |
| 167 | BufferedReader queryReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); | 163 | BufferedReader queryReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); |
| 168 | String line; | 164 | String line; |
| 169 | Collection<String> queryTexts = new LinkedList<String>(); | 165 | Collection<String> queryTexts = new LinkedList<String>(); |
| 170 | while (true) { | 166 | while (true) { |
| 171 | while ((line = queryReader.readLine()) != null && ((line = line.trim()).isEmpty() || line.startsWith("#"))); | 167 | while((line = queryReader.readLine()) != null && ((line = line.trim()).isEmpty() || line.startsWith("#"))) ; |
| 172 | if (line == null) { | 168 | if (line == null) { |
| 173 | queryReader.close(); | 169 | queryReader.close(); |
| 174 | return queryTexts; | 170 | return queryTexts; |
| 175 | } | 171 | } |
| 176 | 172 | ||
| 177 | StringBuffer query = new StringBuffer(); | 173 | StringBuffer query = new StringBuffer(); |
| 178 | if (!line.startsWith("^[")) | 174 | if (!line.startsWith("^[")) |
| 179 | query.append(line).append(LINE_SEPARATOR); | 175 | query.append(line).append(LINE_SEPARATOR); |
| 180 | 176 | ||
| 181 | while ((line = queryReader.readLine()) != null && !line.trim().endsWith("}")) | 177 | while((line = queryReader.readLine()) != null && !line.trim().endsWith("}")) |
| 182 | query.append(line).append(LINE_SEPARATOR); | 178 | query.append(line).append(LINE_SEPARATOR); |
| 183 | query.append(line); | 179 | query.append(line); |
| 184 | queryTexts.add(query.toString()); | 180 | queryTexts.add(query.toString()); |
| 185 | } | 181 | } |
| 186 | } | 182 | } |
| 187 | 183 | ||
| 188 | /** | 184 | /** |
| 189 | * | 185 | * |
| 190 | * @param answerReader | 186 | * @param answerReader |
| 191 | * @return all lines before the next empty line | 187 | * @return all lines before the next empty line |
| 192 | * @throws IOException | 188 | * @throws IOException |
| 193 | */ | 189 | */ |
| 194 | public static Collection<String> getLines(BufferedReader answerReader) throws IOException { | 190 | public static Collection<String> getLines(BufferedReader answerReader) throws IOException { |
| 195 | Collection<String> answerTuples = new LinkedList<String>(); | 191 | Collection<String> answerTuples = new LinkedList<String>(); |
| 196 | String line; | 192 | String line; |
| 197 | while ((line = answerReader.readLine()) != null) { | 193 | while ((line = answerReader.readLine()) != null) { |
| 198 | line = line.trim(); | 194 | line = line.trim(); |
| 199 | if (line.isEmpty()) | 195 | if (line.isEmpty()) |
| 200 | break; | 196 | break; |
| 201 | answerTuples.add(line); | 197 | answerTuples.add(line); |
| 202 | } | 198 | } |
| 203 | return answerTuples; | 199 | return answerTuples; |
| 204 | } | 200 | } |
| 205 | 201 | ||
| 206 | private static StringBuilder logMessage = new StringBuilder(); | ||
| 207 | |||
| 208 | private static String getLogMessage(Object[] messages) { | 202 | private static String getLogMessage(Object[] messages) { |
| 209 | if (messages.length == 1) return messages[0].toString(); | 203 | if (messages.length == 1) return messages[0].toString(); |
| 210 | else { | 204 | else { |
| @@ -242,20 +236,6 @@ public class Utility { | |||
| 242 | if (LOGS != null) | 236 | if (LOGS != null) |
| 243 | LOGS.error(getLogMessage(messages)); | 237 | LOGS.error(getLogMessage(messages)); |
| 244 | } | 238 | } |
| 245 | |||
| 246 | // public static void initialise() { | ||
| 247 | // File tmp = new File(TempDirectory); | ||
| 248 | // if (!tmp.exists()) tmp.mkdirs(); | ||
| 249 | // } | ||
| 250 | // | ||
| 251 | // public static void cleanup() { | ||
| 252 | // File tmp = new File(TempDirectory); | ||
| 253 | // if (tmp.exists()) { | ||
| 254 | // for (File file: tmp.listFiles()) | ||
| 255 | // file.delete(); | ||
| 256 | // tmp.delete(); | ||
| 257 | // } | ||
| 258 | // } | ||
| 259 | 239 | ||
| 260 | public static String toFileIRI(String path) { | 240 | public static String toFileIRI(String path) { |
| 261 | String iri; | 241 | String iri; |
