diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/util/Utility.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/Utility.java | 248 |
1 files changed, 248 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/util/Utility.java b/src/uk/ac/ox/cs/pagoda/util/Utility.java new file mode 100644 index 0000000..120d463 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/util/Utility.java | |||
| @@ -0,0 +1,248 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.util; | ||
| 2 | |||
| 3 | import java.io.BufferedReader; | ||
| 4 | import java.io.BufferedWriter; | ||
| 5 | import java.io.File; | ||
| 6 | import java.io.FileInputStream; | ||
| 7 | import java.io.FileNotFoundException; | ||
| 8 | import java.io.FileOutputStream; | ||
| 9 | import java.io.IOException; | ||
| 10 | import java.io.InputStreamReader; | ||
| 11 | import java.io.OutputStreamWriter; | ||
| 12 | import java.io.PrintStream; | ||
| 13 | import java.text.SimpleDateFormat; | ||
| 14 | import java.util.Collection; | ||
| 15 | import java.util.Date; | ||
| 16 | import java.util.HashSet; | ||
| 17 | import java.util.LinkedList; | ||
| 18 | import java.util.Scanner; | ||
| 19 | import java.util.Set; | ||
| 20 | import java.util.Stack; | ||
| 21 | |||
| 22 | import org.apache.log4j.Logger; | ||
| 23 | import org.semanticweb.HermiT.model.Atom; | ||
| 24 | |||
| 25 | public class Utility { | ||
| 26 | |||
| 27 | private static final Logger LOGS = Logger.getLogger(""); | ||
| 28 | |||
| 29 | public static final String JAVA_FILE_SEPARATOR = "/"; | ||
| 30 | public static final String FILE_SEPARATOR = System.getProperty("file.separator"); | ||
| 31 | public static final String LINE_SEPARATOR = System.getProperty("line.separator"); | ||
| 32 | |||
| 33 | public static final String TempDirectory = (new File("tmp")).getAbsolutePath() + FILE_SEPARATOR; | ||
| 34 | |||
| 35 | public static final int TEST = -1; | ||
| 36 | public static final int FLY = 0; | ||
| 37 | public static final int UOBM = 1; | ||
| 38 | public static final int LUBM = 2; | ||
| 39 | public static final int AEO = 3; | ||
| 40 | public static final int WINE = 4; | ||
| 41 | |||
| 42 | public static Set<Atom> toSet(Atom[] data) | ||
| 43 | { | ||
| 44 | HashSet<Atom> ret = new HashSet<Atom>(); | ||
| 45 | for (Atom element: data) | ||
| 46 | ret.add(element); | ||
| 47 | return ret; | ||
| 48 | } | ||
| 49 | |||
| 50 | static Stack<PrintStream> outs = new Stack<PrintStream>(); | ||
| 51 | |||
| 52 | static { | ||
| 53 | outs.push(System.out); | ||
| 54 | } | ||
| 55 | |||
| 56 | public static boolean redirectSystemOut() | ||
| 57 | { | ||
| 58 | String stamp = new SimpleDateFormat( "HH:mm:ss").format(new Date()); | ||
| 59 | return redirectCurrentOut("./console" + stamp + ".txt"); | ||
| 60 | } | ||
| 61 | |||
| 62 | public static boolean redirectCurrentOut(String fileName) | ||
| 63 | { | ||
| 64 | File file = new File(fileName); | ||
| 65 | PrintStream out; | ||
| 66 | try { | ||
| 67 | out = new PrintStream(new FileOutputStream(file)); | ||
| 68 | } catch (FileNotFoundException e) { | ||
| 69 | e.printStackTrace(); | ||
| 70 | return false; | ||
| 71 | } | ||
| 72 | outs.push(out); | ||
| 73 | System.setOut(out); | ||
| 74 | return true; | ||
| 75 | } | ||
| 76 | |||
| 77 | public static void closeCurrentOut() { | ||
| 78 | if (!outs.isEmpty()) | ||
| 79 | outs.pop().close(); | ||
| 80 | |||
| 81 | if (!outs.isEmpty()) | ||
| 82 | System.setOut(outs.peek()); | ||
| 83 | } | ||
| 84 | |||
| 85 | public static void sparql2expression(String input, String output) throws IOException { | ||
| 86 | BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input))); | ||
| 87 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output))); | ||
| 88 | boolean first; | ||
| 89 | String line, query; | ||
| 90 | while ((line = reader.readLine()) != null) { | ||
| 91 | if (line.startsWith("^")) { | ||
| 92 | for (int i = 0; i < 4; ++i) | ||
| 93 | line = reader.readLine(); | ||
| 94 | first = true; | ||
| 95 | query = ""; | ||
| 96 | while ((line = reader.readLine()) != null && !line.startsWith("}")) | ||
| 97 | if (first) { | ||
| 98 | first = false; | ||
| 99 | query = expression(line.trim()); | ||
| 100 | } | ||
| 101 | else query += ", " + expression(line.trim()); | ||
| 102 | writer.write(query); | ||
| 103 | writer.newLine(); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | reader.close(); | ||
| 107 | writer.close(); | ||
| 108 | } | ||
| 109 | |||
| 110 | private static String expression(String line) { | ||
| 111 | String[] parts = line.split(" "); | ||
| 112 | if (parts[1].equals("rdf:type")) { | ||
| 113 | return parts[2] + "(?" + variableIndex(parts[0]) + ")"; | ||
| 114 | } | ||
| 115 | else return parts[1] + "(?" + variableIndex(parts[0]) + ",?" + variableIndex(parts[2]) + ")"; | ||
| 116 | } | ||
| 117 | |||
| 118 | private static int asciiX = (int)'X'; | ||
| 119 | |||
| 120 | private static int variableIndex(String exp) { | ||
| 121 | char var = exp.charAt(1); | ||
| 122 | return (int)var - asciiX; | ||
| 123 | } | ||
| 124 | |||
| 125 | public static String readLine(BufferedReader reader) throws IOException { | ||
| 126 | String line = reader.readLine(); | ||
| 127 | if (line == null) | ||
| 128 | return null; | ||
| 129 | return line.trim(); | ||
| 130 | } | ||
| 131 | |||
| 132 | public static String getTextfromFile(String fileName) throws FileNotFoundException { | ||
| 133 | Scanner scanner = new Scanner(new File(fileName)); | ||
| 134 | String program = scanner.useDelimiter("\\Z").next(); | ||
| 135 | scanner.close(); | ||
| 136 | return program; | ||
| 137 | } | ||
| 138 | |||
| 139 | public static String[] getPattern(BufferedReader answerReader) throws IOException { | ||
| 140 | String lastLine = readLine(answerReader), line; | ||
| 141 | while ((line = readLine(answerReader)) != null && !line.startsWith("---------")) | ||
| 142 | lastLine = line; | ||
| 143 | return lastLine.split(" "); | ||
| 144 | } | ||
| 145 | |||
| 146 | public static void removeRecursively(File file) { | ||
| 147 | if (!file.exists()) return; | ||
| 148 | |||
| 149 | if (file.isDirectory()) | ||
| 150 | for (File tFile: file.listFiles()) | ||
| 151 | removeRecursively(tFile); | ||
| 152 | file.delete(); | ||
| 153 | } | ||
| 154 | |||
| 155 | public static void removeRecursively(String fileName) { | ||
| 156 | removeRecursively(new File(fileName)); | ||
| 157 | } | ||
| 158 | |||
| 159 | public static Collection<String> getQueryTexts(String fileName) throws IOException { | ||
| 160 | BufferedReader queryReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); | ||
| 161 | String line; | ||
| 162 | Collection<String> queryTexts = new LinkedList<String>(); | ||
| 163 | while (true) { | ||
| 164 | while ((line = queryReader.readLine()) != null && ((line = line.trim()).isEmpty() || line.startsWith("#"))); | ||
| 165 | if (line == null) { | ||
| 166 | queryReader.close(); | ||
| 167 | return queryTexts; | ||
| 168 | } | ||
| 169 | |||
| 170 | StringBuffer query = new StringBuffer(); | ||
| 171 | if (!line.startsWith("^[")) | ||
| 172 | query.append(line).append(LINE_SEPARATOR); | ||
| 173 | |||
| 174 | while ((line = queryReader.readLine()) != null && !line.trim().endsWith("}")) | ||
| 175 | query.append(line).append(LINE_SEPARATOR); | ||
| 176 | query.append(line); | ||
| 177 | queryTexts.add(query.toString()); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | /** | ||
| 182 | * | ||
| 183 | * @param answerReader | ||
| 184 | * @return all lines before the next empty line | ||
| 185 | * @throws IOException | ||
| 186 | */ | ||
| 187 | public static Collection<String> getLines(BufferedReader answerReader) throws IOException { | ||
| 188 | Collection<String> answerTuples = new LinkedList<String>(); | ||
| 189 | String line; | ||
| 190 | while ((line = answerReader.readLine()) != null) { | ||
| 191 | line = line.trim(); | ||
| 192 | if (line.isEmpty()) | ||
| 193 | break; | ||
| 194 | answerTuples.add(line); | ||
| 195 | } | ||
| 196 | return answerTuples; | ||
| 197 | } | ||
| 198 | |||
| 199 | private static StringBuilder logMessage = new StringBuilder(); | ||
| 200 | |||
| 201 | private static String getLogMessage(Object[] messages) { | ||
| 202 | if (messages.length == 1) return messages[0].toString(); | ||
| 203 | else { | ||
| 204 | logMessage.setLength(0); | ||
| 205 | for (int i = 0; i < messages.length; ++i) { | ||
| 206 | if (logMessage.length() != 0) | ||
| 207 | logMessage.append(LINE_SEPARATOR); | ||
| 208 | logMessage.append(messages[i]); | ||
| 209 | } | ||
| 210 | return logMessage.toString(); | ||
| 211 | } | ||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | public static void logInfo(Object... messages) { | ||
| 216 | LOGS.info(getLogMessage(messages)); | ||
| 217 | } | ||
| 218 | |||
| 219 | public static void logTrace(Object... messages) { | ||
| 220 | LOGS.trace(getLogMessage(messages)); | ||
| 221 | } | ||
| 222 | |||
| 223 | public static void logDebug(Object... messages) { | ||
| 224 | LOGS.debug(getLogMessage(messages)); | ||
| 225 | } | ||
| 226 | |||
| 227 | public static void logError(Object... messages) { | ||
| 228 | LOGS.error(getLogMessage(messages)); | ||
| 229 | } | ||
| 230 | |||
| 231 | public static void initialise() { | ||
| 232 | File tmp = new File(TempDirectory); | ||
| 233 | if (!tmp.exists()) tmp.mkdirs(); | ||
| 234 | } | ||
| 235 | |||
| 236 | public static void cleanup() { | ||
| 237 | File tmp = new File(TempDirectory); | ||
| 238 | if (tmp.exists()) tmp.delete(); | ||
| 239 | } | ||
| 240 | |||
| 241 | public static String toFileIRI(String path) { | ||
| 242 | String iri; | ||
| 243 | if (path.startsWith(FILE_SEPARATOR)) iri = "file:" + path; | ||
| 244 | else iri = "file:\\\\\\" + path; | ||
| 245 | return iri.replace(FILE_SEPARATOR, JAVA_FILE_SEPARATOR).replace(" ", "%20"); | ||
| 246 | } | ||
| 247 | |||
| 248 | } | ||
