KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hansel > stack > OrOrOp


1 package org.hansel.stack;
2
3
4 public class OrOrOp extends BinaryOperatorEntry {
5     private static final Compressor[] COMPRESSORS = new Compressor[] {
6         new OrOrCompressor(),
7         new AndCompressor(),
8         new NotCompressor()
9     };
10
11     public OrOrOp(HanselValue op1,
12                   HanselValue op2) {
13         super("||", 11, op1, op2);
14     }
15
16     public HanselValue compress() {
17         HanselValue result = this;
18
19         int i=0;
20         while ((i < COMPRESSORS.length) && (result instanceof OrOrOp)) {
21             HanselValue op1 = ((OrOrOp) result).getOperator1().compress();
22             HanselValue op2 = ((OrOrOp) result).getOperator2().compress();
23             HanselValue newResult = null;
24
25             if (COMPRESSORS[i].canApply(op1, op2)) {
26                 newResult = COMPRESSORS[i].compress(op1, op2);
27
28                 if (result.toString().length() < newResult.toString().length()) {
29                     result = newResult;
30                 }
31             } else if (COMPRESSORS[i].canApply(op2, op1)) {
32                 newResult = COMPRESSORS[i].compress(op2, op1);
33
34                 if (result.toString().length() < newResult.toString().length()) {
35                     result = newResult;
36                 }
37             }
38
39             if ((newResult != null) && (result.toString().length() > newResult.toString().length())) {
40                 result = newResult;
41                 i = 0;
42             } else {
43                 i++;
44             }
45         }
46      
47         return result;
48     }
49
50     private interface Compressor {
51         boolean canApply(HanselValue op1, HanselValue op2);
52         HanselValue compress(HanselValue op1, HanselValue op2);
53     }
54
55     /**
56      * Compresses "a || (!a && b [ && c] )" to "a || b [ && c]".
57      */

58     public static class OrOrCompressor implements Compressor {
59         public boolean canApply(HanselValue op1, HanselValue op2) {
60             if (!(op1 instanceof AndAndOp)) {
61                 return false;
62             }
63             
64             HanselValue op11 = ((AndAndOp) op1).getOperator1();
65             String JavaDoc searchFor = op2.invert().toString();
66
67             while (op11 != null) {
68                 if (op11.toString().equals(searchFor)) {
69                     return true;
70                 }
71
72                 // If op11 is an instance of AndAndOp, we still might
73
// find an adequate operator.
74
if (op11 instanceof AndAndOp) {
75                     op11 = ((AndAndOp) op11).getOperator1();
76                 } else {
77                     op11 = null;
78                 }
79             }
80
81             return false;
82         }
83
84         public HanselValue compress(HanselValue op1, HanselValue op2) {
85             String JavaDoc searchFor = op2.invert().toString();
86             HanselValue remainder = ((AndAndOp) op1).getOperator2();
87             HanselValue extract = ((AndAndOp) op1).getOperator1();
88            
89             while (!(extract.toString().equals(searchFor))) {
90                 remainder = new AndAndOp(((AndAndOp) extract).getOperator2(),
91                                          remainder);
92                 extract = ((AndAndOp) extract).getOperator1();
93             }
94
95             return new OrOrOp(op2, remainder);
96         }
97     }
98
99     /**
100      * Compresses "a && (a || b)" to "a && b".
101      */

102     public static class AndCompressor implements Compressor {
103         public boolean canApply(HanselValue op1, HanselValue op2) {
104             return (op1 instanceof AndAndOp) &&
105                    (op2 instanceof AndAndOp);
106         }
107
108         public HanselValue compress(HanselValue op1, HanselValue op2) {
109             AndAndOp and1 = (AndAndOp) op1;
110             AndAndOp and2 = (AndAndOp) op2;
111
112             String JavaDoc term = and2.getOperator1().toString();
113             HanselValue eliminated = eliminate(and1, term);
114
115             if (eliminated != null) {
116                 return new AndAndOp(and2.getOperator1(),
117                                      new OrOrOp(eliminated,
118                                                 and2.getOperator2()).compress());
119             }
120
121             return new OrOrOp(op1, op2);
122         }
123
124         private HanselValue eliminate(AndAndOp and, String JavaDoc term) {
125             HanselValue op1 = and.getOperator1();
126             
127             // Does this and contain the term?
128
if (op1.toString().equals(term)) {
129                 return and.getOperator2();
130             }
131         
132             // if not: Try to eliminate recursivly
133
if (op1 instanceof AndAndOp) {
134                 HanselValue eliminated = eliminate((AndAndOp) op1, term);
135                 if (eliminated != null) {
136                     return new AndAndOp(eliminated, and.getOperator2());
137                 }
138             }
139
140             // eliminating failed.
141
return null;
142         }
143     }
144
145     /**
146      * Compresses "!a || !b" to "!(a && b)".
147      */

148     public static class NotCompressor implements Compressor {
149         public boolean canApply(HanselValue op1, HanselValue op2) {
150             return (op1 instanceof NotOp) &&
151                    (op2 instanceof NotOp);
152         }
153
154         public HanselValue compress(HanselValue op1, HanselValue op2) {
155             return new NotOp(new AndAndOp(op1.invert(), op2.invert()));
156         }
157     }
158     
159 }
160
Popular Tags