1 package net.obsearch.example.ted;
2
3 import java.util.BitSet;
4 import java.util.HashSet;
5 import java.util.LinkedList;
6 import java.util.List;
7
8
9
10 import antlr.BaseAST;
11 import antlr.CommonAST;
12 import antlr.Token;
13 import antlr.collections.AST;
14
15
16
17
18
19 public class SliceASTForStandardTed extends SliceASTIds {
20
21
22 private int ttype = Token.INVALID_TYPE;
23
24
25 private SliceASTForStandardTed heavy = null;
26
27 public int updateDecendantInformation(){
28 decendants = 0;
29 SliceASTForStandardTed n = (SliceASTForStandardTed)this.getLeftmostChild();
30 while(n != null){
31 decendants += n.updateDecendantInformation();
32 n = (SliceASTForStandardTed)n.getNextSibling();
33 }
34 return decendants + 1;
35 }
36
37
38
39
40
41
42
43
44
45
46
47
48 public int getDescendants(){
49 if(decendants == -1){
50 this.updateDecendantInformation();
51 }
52 return decendants;
53 }
54
55 public int getSize(){
56 return getDescendants() + 1;
57 }
58
59
60
61
62 public LinkedList<SliceASTForStandardTed> topLight(){
63 if(heavy == null){
64 updateHeavyPathInformation();
65 }
66 LinkedList<SliceASTForStandardTed> res = new LinkedList<SliceASTForStandardTed>();
67 topLightAux(res);
68 return res;
69 }
70
71 protected void topLightAux(LinkedList<SliceASTForStandardTed> m){
72 SliceASTForStandardTed n = this.getLeftmostChild();
73 while(n != null){
74 if(n != this.heavy){
75 m.add(n);
76 }else{
77 n.topLightAux(m);
78 }
79 n = (SliceASTForStandardTed)n.getNextSibling();
80 }
81 }
82
83 public SliceASTForStandardTed getHeavy(){
84 return this.heavy;
85 }
86
87 public void updateHeavyPathInformation(){
88
89 if(getLeftmostChild() != null){
90 SliceASTForStandardTed biggest = this.getLeftmostChild();
91 biggest.updateHeavyPathInformation();
92 SliceASTForStandardTed n = (SliceASTForStandardTed) this.getLeftmostChild().getNextSibling();
93 while(n != null){
94 if(biggest.getDescendants() < n.getDescendants()){
95 biggest = n;
96 }
97 n.updateHeavyPathInformation();
98 n = (SliceASTForStandardTed)n.getNextSibling();
99 }
100 this.heavy = biggest;
101 }
102 }
103
104
105
106 public SliceASTForStandardTed findFirstNodeThatMatches(String label){
107 SliceASTForStandardTed result = null;
108 if(this.text.equals(label)){
109 result = this;
110 }else{
111 SliceASTForStandardTed n = this.getLeftmostChild();
112 while(n != null && result == null){
113 result = n.findFirstNodeThatMatches(label);
114 n = (SliceASTForStandardTed)n.getNextSibling();
115 }
116 }
117 return result;
118 }
119
120
121 public String getText() {
122 return text;
123 }
124
125
126 public int getType() {
127 return ttype;
128 }
129
130 public void initialize(int t, String txt) {
131 setType(t);
132 setText(txt);
133 }
134
135 public void initialize(AST t) {
136 setText(t.getText());
137 setType(t.getType());
138 }
139
140 public SliceASTForStandardTed() {
141 }
142 public SliceASTForStandardTed(int t, String txt) {
143 initialize(t, txt);
144 }
145 public SliceASTForStandardTed(Token tok) {
146 initialize(tok);
147 }
148 public SliceASTForStandardTed(SliceASTForStandardTed t) {
149 initialize(t.ttype, t.text);
150 }
151
152
153 public void initialize(Token tok) {
154 setText(tok.getText());
155 setType(tok.getType());
156 }
157
158
159 public void setText(String text_) {
160 text = text_;
161 }
162
163
164 public void setType(int ttype_) {
165 ttype = ttype_;
166 }
167
168
169
170
171 public SliceASTForStandardTed getLeftmostChild(){
172 return (SliceASTForStandardTed)super.getFirstChild();
173 }
174
175
176
177 public String prettyPrint() {
178 SliceASTForStandardTed t = this;
179 String ts = "";
180 if (t.getFirstChild() != null) ts += " (";
181 ts += " " + this.toString();
182 if (t.getFirstChild() != null) {
183 ts += ((SliceASTForStandardTed)t.getFirstChild()).prettyPrint();
184 }
185 if (t.getFirstChild() != null) ts += " )";
186 if (t.getNextSibling() != null) {
187 ts += ((SliceASTForStandardTed)t.getNextSibling()).prettyPrint();
188 }
189 return ts;
190 }
191
192
193
194
195
196
197 public boolean equalsTree(AST t) {
198 SliceASTForStandardTed j = (SliceASTForStandardTed)t;
199 if(j.getSize() != this.getSize()){
200 return false;
201 }else{
202 return super.equalsTree(t);
203 }
204 }
205
206
207
208
209
210
211
212
213 public int detailedTreeComparison(AST t) {
214 SliceASTForStandardTed j = (SliceASTForStandardTed)t;
215 if(j.getSize() != this.getSize()){
216 if(this.equals(t)){
217 return 3;
218 }else{
219 return 0;
220 }
221 }else{
222 return detailedTreeAux(t);
223 }
224 }
225
226
227
228
229
230 protected int detailedTreeAux(AST t) {
231 int res = -1;
232
233
234
235 if (this.getFirstChild() != null) {
236 if (this.getFirstChild().equalsList(t.getFirstChild())){ res = 2;}else{res = 0;}
237 }
238
239 else if (t.getFirstChild() != null) {
240 res = 0;
241 }
242
243 if (this.equals(t) && ( res == 2 || res == -1)) {
244 res = 1;
245 }
246 if(res == -1){
247 res = 0;
248 }
249 if(this.equals(t) && res == 0){
250 res = 3;
251 }
252
253 return res;
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 }