1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package uk.ac.ox.cs.pagoda.query;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import uk.ac.ox.cs.pagoda.util.Utility;
public class QueryManager {
public Collection<QueryRecord> collectQueryRecords(String queryfile) {
Collection<QueryRecord> ret = new LinkedList<QueryRecord>();
for (String queryText: collectQueryTexts(queryfile))
ret.add(create(queryText));
return ret;
}
public static Collection<String> collectQueryTexts(String queryfile) {
Scanner scanner = null;
try {
scanner = new Scanner(new File(queryfile));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
Collection<String> ret = new LinkedList<String>();
StringBuilder sb = new StringBuilder();
int leftToMatch;
String text;
while (scanner.hasNextLine()) {
leftToMatch = -1;
for (String line; scanner.hasNextLine(); ) {
line = scanner.nextLine();
if (line.length() > 6 && line.substring(0, 6).equalsIgnoreCase("SELECT")) {
String next = line.split(" ")[1];
if (!next.equalsIgnoreCase("distinct"))
line = line.substring(0, 6) + " distinct" + line.substring(6);
}
for (int i = 0; i < line.length(); ++i)
if (line.charAt(i) == '{')
if (leftToMatch == -1) leftToMatch = 1;
else ++leftToMatch;
else if (line.charAt(i) == '}') --leftToMatch;
// if (line.isEmpty()) break;
if (!line.isEmpty())
sb.append(line).append(Utility.LINE_SEPARATOR);
if (leftToMatch == 0) break;
}
text = preprocess(sb.toString());
if (!text.isEmpty())
ret.add(text);
sb.setLength(0);
}
scanner.close();
return ret;
}
private static String preprocess(String text) {
int index;
text = text.trim();
while (text.startsWith("^") || text.startsWith("#") || text.startsWith("//") || text.startsWith("@"))
if ((index = text.indexOf("\n")) != -1)
text = text.substring(index + 1);
else {
text = "";
break;
}
return text; // text.replace(" a ", " <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ");
}
private Map<String, QueryRecord> allRecords = new HashMap<String, QueryRecord>();
private int queryCounter = 0;
public QueryRecord create(String text, int i, int j) {
// StringBuilder queryText = new StringBuilder();
// for (String seq : text.split("\s")) {
// if (seq.length() == 0) continue;
// if (queryText.length() != 0) queryText.append(" ");
// queryText.append(seq);
// }
// text = queryText.toString();
text = text.replaceAll("\\s+", " ").trim();
QueryRecord ret = allRecords.get(text);
if (ret != null) return ret;
else {
if (i == -1) {
i = ++queryCounter;
}
ret = new QueryRecord(this, text, i, j);
allRecords.put(text, ret);
return ret;
}
}
public QueryRecord create(String text, int i) {
return create(text, i, 0);
}
public void remove(String queryText) {
allRecords.remove(queryText);
}
public void put(String text, QueryRecord queryRecord) {
allRecords.put(text, queryRecord);
}
public QueryRecord create(String queryText) {
return create(queryText, -1, 0);
}
}
|