KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > parser > OrProductionRulesWithHashMap


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import com.daffodilwoods.database.utility.P;
6 import com.daffodilwoods.daffodildb.utils.DBStack;
7
8    /**
9     * FOR NONRECURSIVE RULES BEST OPTION IS TO BE USED.
10     * FOR COMPARABLERULES BINARY IS TO BE USED.
11     * FOR RECURSIVE RULES BEST OPTION IS TO BE USED.
12     */

13
14 public class OrProductionRulesWithHashMap extends ProductionRulesWithHashMap{
15    /**
16     * Array For Comparable Rules
17     */

18    Object JavaDoc []comparableRules;
19
20    /**
21     * Array For Recursive Rules
22     */

23    Object JavaDoc []recursiveObjects;
24
25    /**
26     * Array For NonRecursive Rules
27     */

28    Object JavaDoc []nonRecursiveObjects;
29
30    Object JavaDoc []nonRecWithBest;
31
32    Object JavaDoc []nonRecWithoutBest;
33
34    OrProductionRulesWithHashMap(ClassLoader JavaDoc classLoader0){
35      super(classLoader0);
36    }
37
38    /**
39     * ParsePart Method of OrProductionRules
40     * Calling parseForToken as without Best Option
41     * Calling parse of query as With Best Option
42     */

43    public Object JavaDoc parsePart(ParseElements pe)throws com.daffodilwoods.database.resource.DException{
44           if ( pe.bestOptionFlag ){
45                 if ( pe.hashMap.containsKey(nameOfRule) )
46                         return withBestOption(pe);
47                 else
48                     return withoutBestOptionForSQL(pe);
49           }
50           return withoutBestOptionForToken(pe);
51    }
52
53    /*
54     * Method Does not use Best Option.
55     * Used for making tokens of query.
56     */

57
58    private Object JavaDoc withoutBestOptionForToken(ParseElements pe)throws com.daffodilwoods.database.resource.DException{
59      Object JavaDoc value = null;
60      int ruleSize = nonRecursiveObjects == null ? -1 : nonRecursiveObjects.length;
61      for (int i = 0; i < ruleSize; i++) {
62        value = ( (ProductionRules) nonRecursiveObjects[i]).parse(pe);
63        if (! (value instanceof ParseException)) {
64          if (!recursiveflag)
65            return value;
66          pe.recursiveObject = value;
67          pe.recursionState = nameOfRule;
68          i = -1; // continuing the loop from starting
69
}
70      }
71      return value == null ? pe.tokenFlag ? applyBinary(pe) : pe.parseException : value;
72    }
73
74
75   /**
76    * This Function is For parsing withoutBest Rules first then WithBest Rules.
77    */

78
79   private Object JavaDoc withoutBestOptionForSQL(ParseElements pe)throws com.daffodilwoods.database.resource.DException{
80           Object JavaDoc object1 = pe.recursiveObject; // recursiveObject taken out to assign the value of object to next element parse
81
String JavaDoc recState = pe.recursionState; // with same reason rec state is taken out;
82
Object JavaDoc asdf = null;
83           Object JavaDoc o = comparableRules == null ? null : applyBinary(pe);
84           if ( o != null ){
85             if ( !(o instanceof ParseException) )
86               return o;
87           }
88           int ruleSize = nonRecursiveObjects == null ? -1 : nonRecursiveObjects.length - 1;
89           for(int i = ruleSize; i >= 0 ; i--){
90             ProductionRules object = (ProductionRules)nonRecursiveObjects[i];
91             Object JavaDoc value = object.parse(pe);
92             if(!(value instanceof ParseException)){
93                   asdf = value;
94                   if(!recursiveflag)
95                       return asdf;
96                   object1 = asdf;
97                   recState = nameOfRule;
98             }
99            pe.recursiveObject = object1;
100            pe.recursionState = recState;
101          }
102         if (asdf == null)
103           return pe.parseException;//withBestOption(pe);
104
else
105           return parseForRecusriveRules(asdf,pe); //asdf;
106
}
107
108   /**
109    * Method for getting RecursiveObjects for nameOfRule passed as an argument.
110    * If all of childs of thie rule is recursive then rule returns itself to it's parent.
111    * otherwise, this rule returns all of it's childs as recursive to it's parent.
112    * If any of it's childs is not Recursive then it returns null to its parent.
113    * Stack OccuredRules passed is used to avoid recursion.
114    */

115   public Object JavaDoc getRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
116       if ( rules == null || rules.length == 0 )
117          return null;
118       int size = rules.length - 1;
119       ArrayList JavaDoc list = new ArrayList JavaDoc();
120       for(int i = 0; i < rules.length; ++i){
121          ProductionRules production = (ProductionRules)rules[i];
122          if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule)
123             && ! this.ruleKey.equalsIgnoreCase(production.ruleKey)
124             && ! occuredRules.contains(production.ruleKey) ) {
125             occuredRules.push(production.ruleKey);
126             Object JavaDoc []object = (Object JavaDoc [])production.getRecursiveObject(nameOfRule,occuredRules);
127             if ( object != null ){
128                for(int k = 0; k < object.length; ++k)
129                   if ( !list.contains(object[k]) ) /* || ( ((ProductionRules)object[k]).nameOfRule.equalsIgnoreCase("SRESERVEDWORD1206543922") ||
130                      ((ProductionRules)object[k]).nameOfRule.equalsIgnoreCase("SNONRESERVEDWORD136444255") ) )*/

131                       list.add(object[k]);
132             }
133          }
134       }
135       if ( !list.isEmpty() ){
136          if ( Arrays.equals(list.toArray(),rules) )
137             return new Object JavaDoc[]{this};
138          return list.toArray();
139       }
140     return null;
141   }
142
143   /**
144    * Method for getting NonRecursiveObjects for nameOfRule passed as an argument.
145    * If Rule is recursive for itself then this rule returns itself to it's parent.
146    * otherwise, this rule returns all of it's childs as recursive to it's parent.
147    * If any of it's childs is not nonRecursive then it returns null to its parent.
148    * Stack OccuredRules passed is used to avoid recursion.
149    */

150   public Object JavaDoc getNonRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
151       if ( rules == null || rules.length == 0 )
152          return null;
153       int size = rules.length;
154       ArrayList JavaDoc list = new ArrayList JavaDoc();
155       for(int i = 0; i < size ; ++i){
156          ProductionRules production = (ProductionRules)rules[i];
157          if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule)
158             && ! this.ruleKey.equalsIgnoreCase(production.ruleKey)
159             && ! occuredRules.contains(production.ruleKey) ) {
160             occuredRules.push(production.ruleKey);
161             Object JavaDoc []object = (Object JavaDoc [])production.getNonRecursiveObject(nameOfRule,occuredRules);
162             if ( object != null ){
163                for(int k = 0; k < object.length; ++k)
164                   if ( !list.contains(object[k]) )/* || ( ((ProductionRules)object[k]).nameOfRule.equalsIgnoreCase("SRESERVEDWORD1206543922") ||
165                      ((ProductionRules)object[k]).nameOfRule.equalsIgnoreCase("SNONRESERVEDWORD136444255") ) )*/

166                       list.add(object[k]);
167
168             }
169          }
170       }
171       if ( !list.isEmpty() ){
172           DBStack a = new DBStack();
173           a.push(this.ruleKey);
174           Object JavaDoc arr[] = (Object JavaDoc [])getRecursiveObject(this.ruleKey,a);
175           if ( arr != null )
176              return new Object JavaDoc[]{this};
177          return list.toArray();
178       }
179      return null;
180   }
181
182   /**
183    * Method for Applying Binary to Rules.
184    * Make a call for method specialHandling.
185    * This special provision is made for Rules other Than OrProductionRules Whose first rule
186    * is OrProductionRule and it fails.Then parse of its left and right rules are called.
187    */

188    private Object JavaDoc applyBinary(ParseElements pe)throws com.daffodilwoods.database.resource.DException{
189      if ( comparableRules == null )
190          return pe.parseException;
191           int low = 0 , high = comparableRules.length - 1;
192           while(low <= high){
193                 int mid = (low + high) / 2;
194                 ProductionRules object = (ProductionRules)comparableRules[mid];
195                 Object JavaDoc obj = object.parse(pe);
196                 if(obj instanceof ParseException){
197                    ParseException parseException = (ParseException)obj;
198                    if ( parseException.returnType == 2 ){
199                      Object JavaDoc o = specialHandling(mid-1,mid+1,pe,low,high);
200                      if ( !(o instanceof ParseException) )
201                         return o;
202                      parseException = (ParseException)o;
203                    }
204                    if(parseException.returnType < 0)
205                           high = mid - 1;
206                    else if(parseException.returnType > 0)
207                           low = mid + 1;
208                    else
209                           return obj;
210                 }
211                 else{
212                   if ( !object.recursiveflag )
213                       return obj;
214                   else{
215                       pe.recursiveObject = obj;
216                       pe.recursionState = nameOfRule;
217                       return parseForRecusriveRules(obj,pe);
218                   }
219                 }
220           }
221           return pe.parseException;
222    }
223
224    /*
225     * Method make a call to the parse of left and right rules of ComparableRule
226     * whose returnType is 2.
227     */

228    private Object JavaDoc specialHandling(int below,int above,ParseElements pe,int low,int high)throws com.daffodilwoods.database.resource.DException{
229      ProductionRules object = null;
230      if ( below >= 0 ){
231               object = (ProductionRules)comparableRules[below];
232                 Object JavaDoc obj = object.parse(pe);
233          if ( !(obj instanceof ParseException) )
234             return obj;
235      }
236      if ( above < comparableRules.length ){
237               object = (ProductionRules)comparableRules[above];
238                 Object JavaDoc obj = object.parse(pe);
239          if ( !(obj instanceof ParseException) )
240             return obj;
241      }
242      return parseForRestOfComparableRules(pe,low,high);
243    }
244
245
246    private Object JavaDoc parseForRestOfComparableRules(ParseElements pe,int low,int high)throws com.daffodilwoods.database.resource.DException{
247      for(int i = low; i < high; ++i){
248         ProductionRules object = (ProductionRules)comparableRules[i];
249         Object JavaDoc obj = object.parse(pe);
250         if ( !(obj instanceof ParseException) )
251           if ( !object.recursiveflag )
252               return obj;
253           else{
254               pe.recursiveObject = obj;
255               pe.recursionState = nameOfRule;
256               return parseForRecusriveRules(obj,pe);
257           }
258      }
259      return pe.parseException;
260    }
261
262   /**
263    * ApplyBinary Method is used to have Best Rule among all the orRules.
264    */

265    private Object JavaDoc applyBinary(ParseElements pe,Best best)throws com.daffodilwoods.database.resource.DException{
266       if ( comparableRules == null ) return pe.parseException;
267           int low = 0 , high = comparableRules.length - 1;
268           int position = pe.position;
269           while(low <= high){
270                 int mid = (low + high) / 2;
271                 ProductionRules object = (ProductionRules)comparableRules[mid];
272                 Object JavaDoc obj = object.parse(pe);
273               if(obj instanceof ParseException){
274                    ParseException parseException = (ParseException)obj;
275              if ( !pe.tokenFlag && parseException.returnType == 2 ){
276                  specialHandling(mid-1,mid+1,pe,best,position); // change i have done
277
return best.sobject;
278               }
279                 if(parseException.returnType < 0)
280                     high = mid - 1;
281                 else if(parseException.returnType > 0)
282                    low = mid + 1;
283                 else
284                    return obj;
285               }
286               else{
287                 if ( position != pe.position)// && checkLookAheadStack(pe) )
288
best.update(obj,pe.position);
289                 if ( !object.recursiveflag ){
290                    specialHandling(mid - 1,mid + 1,pe,best,position);
291                    return best.sobject;
292                 }
293                 pe.recursiveObject = best.sobject;
294                 pe.recursionState = nameOfRule;
295                 return parseForRecusriveRules(best,pe);
296               }
297           }
298           return pe.parseException;
299    }
300
301    /*
302     * Method make a call to the parse of left and right rules of ComparableRule
303     * in case we require best among the rules.
304     */

305    private void specialHandling(int below,int above,ParseElements pe,Best best,int position)throws com.daffodilwoods.database.resource.DException{
306      ProductionRules object = null;
307      pe.position = position;
308      if ( below >= 0 ){
309               object = (ProductionRules)comparableRules[below];
310                 Object JavaDoc obj = object.parse(pe);
311          if ( !(obj instanceof ParseException) ){
312            if ( position != pe.position)// && checkLookAheadStack(pe) )
313
best.update(obj,pe.position);
314          }
315      }
316      if ( above < comparableRules.length ){
317          object = (ProductionRules)comparableRules[above];
318          pe.position = position;
319          Object JavaDoc obj = object.parse(pe);
320          if ( !(obj instanceof ParseException) ){
321            if ( position != pe.position)// && checkLookAheadStack(pe) )
322
best.update(obj,pe.position);
323          }
324      }
325    }
326
327
328
329    /**
330     * Method for calling parse of NonRecursive Rules
331     * NonRecursive Rules are categorised in to two categories.
332     * a) Rules which are Comparable.
333     * b) Rules which are Non Comparable.
334     * At First, parse of Non Comparable Rule is called with Best Option.
335     * Then Parse of Comparable Rule is called with Binary.
336     * Thereafter parse Of Recusrive Rules are called wityh Best Option.
337     */

338
339    private Object JavaDoc withBestOption(ParseElements pe)throws com.daffodilwoods.database.resource.DException{
340      Object JavaDoc object1 = pe.recursiveObject; // recursiveObject taken out to assign the value of object to next element parse
341
String JavaDoc recState = pe.recursionState; // with same reason rec state is taken out;
342
Best best = new Best();
343      int position = pe.position;
344      int ruleSize = nonRecursiveObjects == null ? - 1 : nonRecursiveObjects.length - 1;
345        for(int i = ruleSize ; i >= 0; i--){
346           ProductionRules object = (ProductionRules)nonRecursiveObjects[i];
347           Object JavaDoc value = object.parse(pe);
348           if(position != pe.position && !(value instanceof ParseException)){
349                   best.update(value,pe.position);
350                   pe.position = position;
351               }
352           pe.recursiveObject = object1;
353           pe.recursionState = recState;
354        }
355        applyBinary(pe,best);
356        if(best.sposition > position){
357                          pe.position = best.sposition;
358                          if(!recursiveflag)
359                             return best.sobject ;
360                          position = best.sposition;
361                          pe.recursiveObject = best.sobject;
362                          pe.recursionState = nameOfRule;
363           return parseForRecusriveRules(best,pe);
364             }
365      return pe.parseException;
366     }
367
368
369    /**
370     * Method For Calling Parse Of Recursive Rules if any.
371     */

372    private Object JavaDoc parseForRecusriveRules(Object JavaDoc asdf,ParseElements pe)throws com.daffodilwoods.database.resource.DException{
373           Best best = new Best();
374           best.update(asdf,pe.position);
375           while(true){
376                  int position = pe.position;
377                  int prvPosition = best.sposition;
378                  String JavaDoc st = pe.recursionState;
379                  Object JavaDoc asq = pe.recursiveObject;
380                  int ruleSize = recursiveObjects == null ? -1 : recursiveObjects.length - 1;
381                  for(int i = ruleSize ; i >= 0 ; i--){
382                         ProductionRules object = (ProductionRules)recursiveObjects[i];
383                         Object JavaDoc value = object.parse(pe);
384                         if(position != pe.position && !(value instanceof ParseException)){
385                           best.update(value,pe.position);
386                            pe.position = position;
387                         }
388                        pe.recursiveObject = asq;
389                        pe.recursionState = st;
390                  }
391                  if(best.sposition > prvPosition){
392                         pe.position = best.sposition;
393                         asdf = best.sobject ;
394                         if (!recursiveflag){
395                           pe.recursiveObject = null;
396                           pe.recursionState = null;
397                            return asdf;
398                         }
399                         pe.recursiveObject = asdf;
400                         pe.recursionState = nameOfRule;
401                  }
402                  else{
403                   if (asdf == null){
404                       pe.recursiveObject = null;
405                       pe.recursionState = null;
406                       return pe.parseException;
407                   }
408                   else{
409                      pe.recursiveObject = null;
410                      pe.recursionState = null;
411                      return asdf;
412                  }
413                }
414             }
415    }
416
417
418    /**
419     * Method For Calling Parse Of Recursive Rules if any.
420     */

421    private Object JavaDoc parseForRecusriveRules(Best best,ParseElements pe)throws com.daffodilwoods.database.resource.DException{
422           Object JavaDoc asdf = best.sobject;
423           while(true){
424                  int position = pe.position;
425                  int prvPosition = best.sposition;
426        String JavaDoc st = pe.recursionState;
427        Object JavaDoc asq = pe.recursiveObject;
428        int ruleSize = recursiveObjects == null ? -1 : recursiveObjects.length - 1;
429                  for(int i = ruleSize ; i >= 0 ; i--){
430                         ProductionRules object = (ProductionRules)recursiveObjects[i];
431                         Object JavaDoc value = object.parse(pe);
432                         if(position != pe.position && !(value instanceof ParseException)){
433                                   best.update(value,pe.position);
434                            pe.position = position;
435                         }
436          pe.recursiveObject = asq;
437          pe.recursionState = st;
438                  }
439                  if(best.sposition > prvPosition){
440                         pe.position = best.sposition;
441                         asdf = best.sobject ;
442                         if (!recursiveflag){
443             pe.recursiveObject = null;
444             pe.recursionState = null;
445                            return asdf;
446          }
447                         pe.recursiveObject = asdf;
448                         pe.recursionState = nameOfRule;
449                  }
450                  else{
451                   if (asdf == null){
452                          pe.recursiveObject = null;
453           pe.recursionState = null;
454           return pe.parseException;
455                   }
456                   else{
457          pe.recursiveObject = null;
458          pe.recursionState = null;
459                         return asdf;
460         }
461                  }
462           }
463    }
464
465    public String JavaDoc toString(){
466       return nameOfRule;
467    }
468
469    /**
470     * Best Class is used to implementing Best Option.
471     * It Holds the object whose parsePosition is best among all or Rules.
472     */

473    public static class Best{
474           Object JavaDoc sobject ;
475           int sposition = -1;
476           void update (Object JavaDoc object,int position){
477                  if(position >= sposition){
478                         sobject = object;
479                         sposition = position;
480                  }
481           }
482
483           public String JavaDoc toString() {
484              StringBuffer JavaDoc str = new StringBuffer JavaDoc("[Best ");
485              str.append(sposition).append("--").append(sobject).append("]");
486              return str.toString();
487           }
488    }
489 }
490
Popular Tags