diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/query/QueryManager.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-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.java | 123 |
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 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.FileNotFoundException; | ||
| 5 | import java.util.Collection; | ||
| 6 | import java.util.HashMap; | ||
| 7 | import java.util.LinkedList; | ||
| 8 | import java.util.Map; | ||
| 9 | import java.util.Scanner; | ||
| 10 | |||
| 11 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 12 | |||
| 13 | public 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 | } | ||
