diff options
author | Safey A.Halim <safey.allah@gmail.com> | 2010-12-01 09:57:03 +0000 |
---|---|---|
committer | Safey A.Halim <safey.allah@gmail.com> | 2010-12-01 09:57:03 +0000 |
commit | 86a38fe85907b9f78ec56647d83e92b2e60db9e0 (patch) | |
tree | aa42f00e3fcf3d0898ccea9e42eefd3e0cb5f084 /src/monkey | |
parent | 901d3f2df5795415ae833315b9f510597992da45 (diff) | |
download | gnunet-86a38fe85907b9f78ec56647d83e92b2e60db9e0.tar.gz gnunet-86a38fe85907b9f78ec56647d83e92b2e60db9e0.zip |
Expression cleanup.
Diffstat (limited to 'src/monkey')
-rw-r--r-- | src/monkey/seaspider/org/gnunet/seaspider/ExpressionDatabaseHandler.java | 183 |
1 files changed, 152 insertions, 31 deletions
diff --git a/src/monkey/seaspider/org/gnunet/seaspider/ExpressionDatabaseHandler.java b/src/monkey/seaspider/org/gnunet/seaspider/ExpressionDatabaseHandler.java index 4cd938a71..2c1df1b54 100644 --- a/src/monkey/seaspider/org/gnunet/seaspider/ExpressionDatabaseHandler.java +++ b/src/monkey/seaspider/org/gnunet/seaspider/ExpressionDatabaseHandler.java | |||
@@ -1,29 +1,50 @@ | |||
1 | package org.gnunet.seaspider; | 1 | package org.gnunet.seaspider; |
2 | 2 | ||
3 | import java.io.File; | 3 | import java.io.File; |
4 | 4 | import java.util.HashMap; | |
5 | import java.util.Iterator; | ||
6 | import java.util.Stack; | ||
5 | import org.tmatesoft.sqljet.core.SqlJetException; | 7 | import org.tmatesoft.sqljet.core.SqlJetException; |
6 | import org.tmatesoft.sqljet.core.SqlJetTransactionMode; | 8 | import org.tmatesoft.sqljet.core.SqlJetTransactionMode; |
7 | import org.tmatesoft.sqljet.core.table.ISqlJetTable; | 9 | import org.tmatesoft.sqljet.core.table.ISqlJetTable; |
8 | import org.tmatesoft.sqljet.core.table.SqlJetDb; | 10 | import org.tmatesoft.sqljet.core.table.SqlJetDb; |
9 | 11 | ||
12 | /** | ||
13 | * ExpressionDatabaseHandler is fed by expressions from the C code parser, and | ||
14 | * inserts them into SQLite Expression database using SQLJet API. Before | ||
15 | * inserting an expression into the database, ExpressionDatabaseHandler makes | ||
16 | * sure it's not redundant. | ||
17 | * For example: | ||
18 | * int x = 0; | ||
19 | * int y = 1; | ||
20 | * int z = x + y // line 3 | ||
21 | * The parser input for line 3 is: z, x, y, x + y, and z = x + y The | ||
22 | * expressions to be committed to the database will be only: z, and z = x + y | ||
23 | */ | ||
10 | public class ExpressionDatabaseHandler { | 24 | public class ExpressionDatabaseHandler { |
11 | 25 | ||
12 | private static final boolean DEBUG = true; | 26 | private static final boolean DEBUG = false; |
13 | 27 | ||
28 | private static final boolean PRINT_STACK = false; | ||
29 | |||
14 | private static SqlJetDb db; | 30 | private static SqlJetDb db; |
15 | 31 | ||
16 | private static ISqlJetTable table; | 32 | private static ISqlJetTable table; |
17 | 33 | ||
18 | 34 | private static String currentFileName = null; | |
35 | |||
36 | private static int currentScopeEnd = 0; | ||
37 | |||
38 | private static Stack<HashMap<String, Integer>> expressionStack = new Stack<HashMap<String, Integer>>(); | ||
39 | |||
19 | public static void createExpressionDatabase(String databasePath) { | 40 | public static void createExpressionDatabase(String databasePath) { |
20 | String createTableQuery = "CREATE TABLE Expression ( expr_ID INT NOT NULL PRIMARY KEY , " + | 41 | String createTableQuery = "CREATE TABLE Expression ( expr_ID INT NOT NULL PRIMARY KEY , " |
21 | "file_name TEXT NOT NULL , expr_syntax TEXT NOT NULL ," + | 42 | + "file_name TEXT NOT NULL , expr_syntax TEXT NOT NULL ," |
22 | " start_lineno INT, end_lineno INT)"; | 43 | + " start_lineno INT, end_lineno INT)"; |
23 | 44 | ||
24 | File dbFile = new File(databasePath); | 45 | File dbFile = new File(databasePath); |
25 | dbFile.delete();/* Delete it if already existent */ | 46 | dbFile.delete();/* Delete it if already existent */ |
26 | 47 | ||
27 | /* Create Expressions database */ | 48 | /* Create Expressions database */ |
28 | try { | 49 | try { |
29 | db = SqlJetDb.open(dbFile, true); | 50 | db = SqlJetDb.open(dbFile, true); |
@@ -38,15 +59,12 @@ public class ExpressionDatabaseHandler { | |||
38 | db.createTable(createTableQuery); | 59 | db.createTable(createTableQuery); |
39 | db.beginTransaction(SqlJetTransactionMode.WRITE); | 60 | db.beginTransaction(SqlJetTransactionMode.WRITE); |
40 | table = db.getTable("Expression"); | 61 | table = db.getTable("Expression"); |
41 | } | 62 | } catch (SqlJetException e) { |
42 | catch (SqlJetException e) { | ||
43 | e.printStackTrace(); | 63 | e.printStackTrace(); |
44 | } | 64 | } |
45 | } | 65 | } |
46 | 66 | ||
47 | 67 | public static void closeDatabase() { | |
48 | public static void closeDatabase() | ||
49 | { | ||
50 | try { | 68 | try { |
51 | db.commit(); | 69 | db.commit(); |
52 | db.close(); | 70 | db.close(); |
@@ -54,27 +72,130 @@ public class ExpressionDatabaseHandler { | |||
54 | e.printStackTrace(); | 72 | e.printStackTrace(); |
55 | } | 73 | } |
56 | } | 74 | } |
57 | 75 | ||
58 | 76 | private static void doInsertExpression(String fileName, | |
59 | public static void insertIntoExpressionTable(String fileName, String expressionSyntax, | 77 | String expressionSyntax, int startLineNo, int endLineNo) { |
60 | int startLineNo, int endLineNo) | 78 | try { |
61 | { | 79 | if (DEBUG) |
80 | System.out.println(fileName + ":[" + startLineNo + "-" | ||
81 | + endLineNo + "]: " + expressionSyntax); | ||
82 | table.insert(currentFileName, expressionSyntax, startLineNo, | ||
83 | endLineNo); | ||
84 | } catch (SqlJetException e) { | ||
85 | e.printStackTrace(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | private static boolean isRedundant(String expressionSyntax) { | ||
90 | Iterator<HashMap<String, Integer>> itr = expressionStack.iterator(); | ||
91 | HashMap<String, Integer> scope; | ||
92 | |||
93 | while (itr.hasNext()) { | ||
94 | scope = itr.next(); | ||
95 | if (null != scope.get(expressionSyntax)) | ||
96 | return true; | ||
97 | } | ||
98 | |||
99 | return false; | ||
100 | } | ||
101 | |||
102 | private static int getScopeEnd(HashMap<String, Integer> scope) { | ||
103 | Iterator<Integer> itr = scope.values().iterator(); | ||
104 | return itr.next(); | ||
105 | } | ||
106 | |||
107 | private static HashMap<String, Integer> pushNewScope(int endLineNo) { | ||
108 | HashMap<String, Integer> scope = new HashMap<String, Integer>(); | ||
109 | currentScopeEnd = endLineNo; | ||
110 | expressionStack.push(scope); | ||
111 | |||
112 | return scope; | ||
113 | } | ||
114 | |||
115 | private static void printExpressionStack(String expressionSyntax, | ||
116 | int startLineNo, int endLineNo) { | ||
117 | HashMap<String, Integer> hashMap; | ||
118 | Iterator<String> itr; | ||
119 | System.out.println("Commit call for expression: " + expressionSyntax | ||
120 | + " start:" + startLineNo + " end:" + endLineNo); | ||
121 | for (int i = 0; i < expressionStack.size(); i++) { | ||
122 | hashMap = expressionStack.get(i); | ||
123 | itr = hashMap.keySet().iterator(); | ||
124 | System.out.println("Printing expressions of scope " + i + ":"); | ||
125 | while (itr.hasNext()) { | ||
126 | System.out.println(itr.next()); | ||
127 | } | ||
128 | } | ||
129 | System.out.println(""); | ||
130 | } | ||
131 | |||
132 | private static void insertExpression(String fileName, | ||
133 | String expressionSyntax, int startLineNo, int endLineNo) { | ||
134 | |||
135 | HashMap<String, Integer> currentScopeExpressions = null; | ||
136 | |||
137 | if (PRINT_STACK) | ||
138 | printExpressionStack(expressionSyntax, startLineNo, endLineNo); | ||
139 | |||
140 | if (null == currentFileName || !currentFileName.equals(fileName)) { | ||
141 | /* First time, or new file */ | ||
142 | currentFileName = fileName; | ||
143 | if (!expressionStack.empty()) | ||
144 | expressionStack.clear(); | ||
145 | currentScopeExpressions = pushNewScope(endLineNo); | ||
146 | } else { | ||
147 | if (endLineNo > currentScopeEnd) { | ||
148 | /* | ||
149 | * We are either in a new function or back to an outer scope | ||
150 | */ | ||
151 | expressionStack.pop(); | ||
152 | if (expressionStack.empty()) { | ||
153 | /* We are in a new function */ | ||
154 | currentScopeExpressions = pushNewScope(endLineNo); | ||
155 | } else { | ||
156 | /* We just left an inner scope to an outer one */ | ||
157 | currentScopeExpressions = expressionStack.lastElement(); | ||
158 | currentScopeEnd = getScopeEnd(currentScopeExpressions); | ||
159 | if (isRedundant(expressionSyntax)) | ||
160 | return; | ||
161 | } | ||
162 | } else { | ||
163 | /* Either we delved into a sub-scope or we are in the same scope */ | ||
164 | if (isRedundant(expressionSyntax)) | ||
165 | return; | ||
166 | if (endLineNo == currentScopeEnd) // same scope | ||
167 | currentScopeExpressions = expressionStack.lastElement(); | ||
168 | else | ||
169 | // new sub-scope | ||
170 | currentScopeExpressions = pushNewScope(endLineNo); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | /* Add the new expression */ | ||
175 | currentScopeExpressions.put(expressionSyntax, endLineNo); | ||
176 | doInsertExpression(fileName, expressionSyntax, startLineNo, endLineNo); | ||
177 | } | ||
178 | |||
179 | /** | ||
180 | * Inserts expression into the Expression Database | ||
181 | * | ||
182 | * @param fileName source file the expression comes from | ||
183 | * @param expressionSyntax string of the expression | ||
184 | * @param startLineNo line number of the expression | ||
185 | * @param endLineNo end line of the expression scope | ||
186 | */ | ||
187 | public static void insertIntoExpressionTable(String fileName, | ||
188 | String expressionSyntax, int startLineNo, int endLineNo) { | ||
62 | if (expressionSyntax.matches("[0-9]*")) | 189 | if (expressionSyntax.matches("[0-9]*")) |
63 | return; | 190 | return; |
64 | if (expressionSyntax.startsWith("\"")) | 191 | if (expressionSyntax.startsWith("\"")) |
65 | return; | 192 | return; |
66 | if (DEBUG) | ||
67 | System.out.println (fileName + ":[" + startLineNo + "-" + endLineNo + "]: " + expressionSyntax); | ||
68 | if (db == null) { | 193 | if (db == null) { |
69 | System.out.println("Error:Database handle is not initialized. Program will exit now!"); | 194 | System.out |
195 | .println("Error:Database handle is not initialized. Program will exit now!"); | ||
70 | System.exit(1); | 196 | System.exit(1); |
71 | } | 197 | } |
72 | 198 | ||
73 | try { | 199 | insertExpression(fileName, expressionSyntax, startLineNo, endLineNo); |
74 | table.insert(fileName, expressionSyntax, startLineNo, endLineNo); | ||
75 | } | ||
76 | catch (SqlJetException e) { | ||
77 | e.printStackTrace(); | ||
78 | } | ||
79 | } | 200 | } |
80 | } | 201 | } |