View Javadoc

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   * This class provides extra functionality required by tree edit distance algorithms and the like
16   * @author amuller
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      /*public int getDescendants(){
38      	int decendants = 0;
39      	SliceAST n = this.getLeftmostChild();
40      	while(n != null){
41      		decendants += n.getDescendants();
42      		decendants++;
43      		n = (SliceAST)n.getNextSibling();
44      	}
45      	return decendants;
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       * toplight never 
60       * @return
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     /** Get the token text for this node */
121     public String getText() {
122         return text;
123     }
124 
125     /** Get the token type for this node */
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     /** Set the token text for this node */
159     public void setText(String text_) {
160         text = text_;
161     }
162 
163     /** Set the token type for this node */
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 	/** Print out a child-sibling tree in LISP notation */
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      * little speed up to the normal equalsTree method
195      * @see antlr.BaseAST#equalsTree(antlr.collections.AST)
196      */
197     public boolean equalsTree(AST t) {
198     	SliceASTForStandardTed j = (SliceASTForStandardTed)t;
199     	if(j.getSize() != this.getSize()){ // little speed up! ;)
200     		return false;
201     	}else{
202     		return super.equalsTree(t);
203     	}
204     }
205     
206     /*
207      * 1 = equal
208      * 0 = not equal
209      * 2 = equal if we rename the root
210      * 3 = children not equal but the same root
211      * @see antlr.BaseAST#equalsTree(antlr.collections.AST)
212      */
213     public int detailedTreeComparison(AST t) {
214     	SliceASTForStandardTed j = (SliceASTForStandardTed)t;
215     	if(j.getSize() != this.getSize()){ // little speed up! ;)
216     		if(this.equals(t)){
217     			return 3;
218     		}else{
219     			return 0;
220     		}
221     	}else{
222     		return detailedTreeAux(t);
223     	}
224     }
225     
226     /** Is tree rooted at 'this' equal to 't'?  The siblings
227      *  of 'this' are ignored.
228      *  
229      */
230     protected int detailedTreeAux(AST t) {
231     	int res = -1;
232         // check roots first.
233         
234         // if roots match, do full list match test on children.
235         if (this.getFirstChild() != null) {
236             if (this.getFirstChild().equalsList(t.getFirstChild())){ res = 2;}else{res = 0;}
237         }
238         // sibling has no kids, make sure t doesn't either
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     // TODO refactor all these things. hack to keep the unit tests working
258     // dummy
259     /**public int updateDecendantInformation(){
260     	assert false;
261     	return -1;
262     }*/
263     // dummy
264     /*public void updateIdInfo(){
265     	assert false;
266     }
267     // dummy
268     public void updateContains(){
269     	assert false;
270     }
271     // dummy
272     public int getId(){
273     	assert false;
274     	return -1;
275     }
276     // dummy
277     public boolean containsNode(int i){
278     	assert false;
279     	return false;
280     }*/
281     
282     
283 }