KickJava   Java API By Example, From Geeks To Geeks.

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


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 OrProductionRules extends ProductionRules{
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
35    OrProductionRules(ClassLoader JavaDoc classLoader0){
36       super(classLoader0);
37    }
38
39    /**
40     * ParsePart Method of OrProductionRules
41     * Calling parseForToken as without Best Option
42     * Calling parse of query as With Best Option
43     */

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

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

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

117    public Object JavaDoc getRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
118       if ( rules == null || rules.length == 0 )
119          return null;
120       int size = rules.length - 1;
121       ArrayList JavaDoc list = new ArrayList JavaDoc();
122       for(int i = 0; i < rules.length; ++i){
123          ProductionRules production = (ProductionRules)rules[i];
124          if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule)
125               && ! this.nameOfRule.equalsIgnoreCase(production.ruleKey)
126               && ! occuredRules.contains(production.ruleKey) ) {
127             occuredRules.push(production.ruleKey);
128             Object JavaDoc []object = (Object JavaDoc [])production.getRecursiveObject(nameOfRule,occuredRules);
129             if ( object != null ){
130                for(int k = 0; k < object.length; ++k)
131                   if ( !list.contains(object[k]) )
132                      list.add(object[k]);
133             }
134          }
135       }
136       if ( !list.isEmpty() ){
137          if ( Arrays.equals(list.toArray(),rules) )
138             return new Object JavaDoc[]{this};
139          return list.toArray();
140       }
141       return null;
142    }
143
144    /**
145     * Method for getting NonRecursiveObjects for nameOfRule passed as an argument.
146     * If Rule is recursive for itself then this rule returns itself to it's parent.
147     * otherwise, this rule returns all of it's childs as recursive to it's parent.
148     * If any of it's childs is not nonRecursive then it returns null to its parent.
149     * Stack OccuredRules passed is used to avoid recursion.
150     */

151    public Object JavaDoc getNonRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
152       if ( rules == null || rules.length == 0 )
153          return null;
154       int size = rules.length;
155       ArrayList JavaDoc list = new ArrayList JavaDoc();
156       for(int i = 0; i < size ; ++i){
157          ProductionRules production = (ProductionRules)rules[i];
158          if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule)
159               && ! this.nameOfRule.equalsIgnoreCase(production.ruleKey)
160               && ! occuredRules.contains(production.ruleKey) ) {
161             occuredRules.push(production.ruleKey);
162             Object JavaDoc []object = (Object JavaDoc [])production.getNonRecursiveObject(nameOfRule,occuredRules);
163             if ( object != null ){
164                for(int k = 0; k < object.length; ++k)
165                   if ( !list.contains(object[k]) )
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     * Method for calling parse of NonRecursive Rules
330     * NonRecursive Rules are categorised in to two categories.
331     * a) Rules which are Comparable.
332     * b) Rules which are Non Comparable.
333     * At First, parse of Non Comparable Rule is called with Best Option.
334     * Then Parse of Comparable Rule is called with Binary.
335     * Thereafter parse Of Recusrive Rules are called wityh Best Option.
336     */

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

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

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

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