diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/util/ConjunctiveQueryHelper.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/util/ConjunctiveQueryHelper.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/util/ConjunctiveQueryHelper.java b/src/uk/ac/ox/cs/pagoda/util/ConjunctiveQueryHelper.java new file mode 100644 index 0000000..937c2c4 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/util/ConjunctiveQueryHelper.java | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.util; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | |||
| 7 | public class ConjunctiveQueryHelper { | ||
| 8 | |||
| 9 | public static String[][] getAnswerVariables(String queryText) { | ||
| 10 | Collection<String> disVars = new LinkedList<String>(), undisVars = new LinkedList<String>(); | ||
| 11 | for (String var: getAllVariables(queryText)) | ||
| 12 | if (var.startsWith("?")) disVars.add(var.substring(1)); | ||
| 13 | else undisVars.add(var.substring(2)); | ||
| 14 | |||
| 15 | String[] distinguishedVariables = disVars.toArray(new String[0]); | ||
| 16 | String[] undistinguishedVariables = undisVars.toArray(new String[0]); | ||
| 17 | String[] answerVariables = null; | ||
| 18 | |||
| 19 | String uppercase = queryText.toUpperCase(); | ||
| 20 | int selectIndex = uppercase.indexOf("SELECT"); | ||
| 21 | int whereIndex = uppercase.indexOf("WHERE"); | ||
| 22 | String selectClause = queryText.substring(selectIndex + 6, whereIndex); | ||
| 23 | if (selectClause.contains("*")) answerVariables = distinguishedVariables; | ||
| 24 | else { | ||
| 25 | String[] terms = selectClause.split(" "); | ||
| 26 | int num = 0; | ||
| 27 | for (int i = 0; i < terms.length; ++i) | ||
| 28 | if (terms[i].startsWith("?")) ++num; | ||
| 29 | answerVariables = new String[num]; | ||
| 30 | for (int i = 0, j = 0; i < terms.length; ++i) | ||
| 31 | if (terms[i].startsWith("?")) | ||
| 32 | answerVariables[j++] = terms[i].substring(1); | ||
| 33 | } | ||
| 34 | |||
| 35 | if (answerVariables != distinguishedVariables) { | ||
| 36 | int index = 0; | ||
| 37 | for (; index < answerVariables.length; ++index) { | ||
| 38 | distinguishedVariables[index] = answerVariables[index]; | ||
| 39 | disVars.remove(answerVariables[index]); | ||
| 40 | } | ||
| 41 | for (String var: disVars) | ||
| 42 | distinguishedVariables[index++] = var; | ||
| 43 | } | ||
| 44 | |||
| 45 | return new String[][] { answerVariables, distinguishedVariables, undistinguishedVariables }; | ||
| 46 | } | ||
| 47 | |||
| 48 | private static Collection<String> getAllVariables(String queryText) { | ||
| 49 | Collection<String> vars = new HashSet<String>(); | ||
| 50 | int start, end = 0; | ||
| 51 | char ch; | ||
| 52 | while ((start = queryText.indexOf("?", end)) != -1) { | ||
| 53 | end = start + 1; | ||
| 54 | while (end + 1 < queryText.length() && (ch = queryText.charAt(end + 1)) != '\n' && ch != ' ') | ||
| 55 | ++end; | ||
| 56 | vars.add(queryText.substring(start, end + 1)); | ||
| 57 | } | ||
| 58 | |||
| 59 | end = 0; | ||
| 60 | while ((start = queryText.indexOf("_:", end)) != -1) { | ||
| 61 | end = start + 1; | ||
| 62 | while (end + 1 < queryText.length() && (ch = queryText.charAt(end + 1)) != '\n' && ch != ' ') | ||
| 63 | ++end; | ||
| 64 | vars.add(queryText.substring(start, end + 1)); | ||
| 65 | } | ||
| 66 | |||
| 67 | return vars; | ||
| 68 | } | ||
| 69 | |||
| 70 | } | ||
