From 048641f29cabc2ba81a01bae7b698be97e9245eb Mon Sep 17 00:00:00 2001 From: vogler Date: Mon, 30 Apr 2012 00:25:47 +0200 Subject: [PATCH] Exercise 2: parsing, regex --- .gitignore | 28 +----------- 01/tinydb/samples/Exercise2.java | 78 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 01/tinydb/samples/Exercise2.java diff --git a/.gitignore b/.gitignore index 5c0760e..fe12691 100644 --- a/.gitignore +++ b/.gitignore @@ -16,32 +16,8 @@ *.log *.aux *.synctex.gz +*.class #ignore thumbnails created by windows Thumbs.db -#Ignore files build by Visual Studio -*.obj -*.exe -*.pdb -*.user -*.aps -*.pch -*.vspscc -*_i.c -*_p.c -*.ncb -*.suo -*.tlb -*.tlh -*.bak -*.cache -*.ilk -*.log -[Bb]in -[Dd]ebug*/ -*.lib -*.sbr -obj/ -[Rr]elease*/ -_ReSharper*/ -[Tt]est[Rr]esult* +[Bb]in \ No newline at end of file diff --git a/01/tinydb/samples/Exercise2.java b/01/tinydb/samples/Exercise2.java new file mode 100644 index 0000000..b4f795a --- /dev/null +++ b/01/tinydb/samples/Exercise2.java @@ -0,0 +1,78 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import tinydb.Database; +import tinydb.Table; + +public class Exercise2 { + + // select (*|attribute(,attribute)*) + // from relation binding(,relation binding)* + // where binding.attribute=(binding.attribute|constant) + // (and binding.attribute=(binding.attribute|constant))* + public static void main(String[] args) throws java.io.IOException { + Database db = Database.open("../data/uni"); + System.out.println("Enter your query below. Type 'quit' to exit."); + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + loop: + while(true){ + String q = br.readLine(); + if(q.equals("quit")) break; + Pattern p = Pattern.compile("select (.+) from (.+) where (.+)"); + Matcher m = p.matcher(q); + if(!m.matches()){ + System.err.println("Query doesn't match the pattern: "+p.toString()); + continue; + } + String selections = m.group(1); + String relations = m.group(2); + String joincond = m.group(3); + // (*|attribute(,attribute)*) + p = Pattern.compile("\\*|\\w+(,\\w+)*"); + if(!p.matcher(selections).matches()){ + System.err.println("Selections don't match the pattern: "+p.toString()); + System.err.println("Your input: "+selections); + continue; + } + // relation binding(,relation binding)* + p = Pattern.compile("\\w+ \\w+(,\\w+ \\w+)*"); + if(!p.matcher(relations).matches()){ + System.err.println("Relations don't match the pattern: "+p.toString()); + System.err.println("Your input: "+relations); + continue; + } + // binding.attribute=(binding.attribute|constant) + // (and binding.attribute=(binding.attribute|constant))* + p = Pattern.compile("\\w+\\.\\w+=\\w+(and \\w+\\.\\w+=\\w+)*"); + if(!p.matcher(joincond).matches()){ + System.err.println("Join conditions don't match the pattern: "+p.toString()); + System.err.println("Your input: "+joincond); + continue; + } + + // TODO save values in file + + // handle relations + Map h_rel = new HashMap(); + String[] a_rel = relations.split(","); + for(String s : a_rel){ + String[] binding = s.split(" "); + System.out.println("trying to open "+binding[0]); + Table table = db.getTable(binding[0]); + if(table == null){ // TODO java.io.IOException: Stream closed + System.err.println("Table "+binding[0]+" doesn't exist"); + continue loop; + } + h_rel.put(binding[1], table); + } + + + br.close(); + } + } +}