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