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