TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
TPTP.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.IO;
20 using System.Linq;
21 using System.Text;
22 using TraceLab.Components.Types.Preprocessors.ExecutionTraces;
23 
24 namespace TraceLab.Components.DevelopmentKit.Preprocessors.ExecutionTraces
25 {
29  public static class TPTP
30  {
36  public static BiGramCollection GenerateBiGrams(string filename)
37  {
38  TextReader brTrace = new StreamReader(filename);
39  BiGramCollection bigrams = new BiGramCollection();
40 
41  int lineNumber = 0;
42  Dictionary<string, string> idToClass = new Dictionary<string,string>();
43  Dictionary<string, string> idToMethod = new Dictionary<string,string>();
44  // Stack<methodID>
45  Dictionary<string, Stack<string>> threadToMethodsStackTranslator = new Dictionary<string, Stack<string>>();
46  string currentLine;
47 
48  while ((currentLine = brTrace.ReadLine()) != null)
49  {
50  lineNumber++;
51  string[] traceLineSplit;
52  // classDef
53  if (currentLine.StartsWith("<classDef "))
54  {
55  traceLineSplit = currentLine.Split(' ');
56  if (traceLineSplit[1].StartsWith("name=") == false)
57  throw new Exception();
58  if (traceLineSplit[2].StartsWith("sourceName=") == false)
59  throw new Exception();
60  if (traceLineSplit[3].StartsWith("classId=") == false)
61  throw new Exception();
62  string name = ExtractValueFromQuotes(traceLineSplit[1]);
63  string classID = ExtractValueFromQuotes(traceLineSplit[3]);
64  //System.out.println(classID+"\t"+name);
65  idToClass.Add(classID, name);
66  continue;
67  }
68  // methodDef
69  if (currentLine.StartsWith("<methodDef "))
70  {
71  traceLineSplit = currentLine.Split(' ');
72  if (traceLineSplit[1].StartsWith("name=") == false)
73  throw new Exception();
74  if (traceLineSplit[2].StartsWith("signature=") == false)
75  throw new Exception();
76  if (traceLineSplit[3].StartsWith("startLineNumber=") == false)
77  throw new Exception();
78  if (traceLineSplit[4].StartsWith("endLineNumber=") == false)
79  throw new Exception();
80  if (traceLineSplit[5].StartsWith("methodId=") == false)
81  throw new Exception();
82  if (traceLineSplit[6].StartsWith("classIdRef=") == false)
83  throw new Exception();
84  string name = ExtractValueFromQuotes(traceLineSplit[1]);
85  string signature = ExtractValueFromQuotes(traceLineSplit[2]);
86  string methodID = ExtractValueFromQuotes(traceLineSplit[5]);
87  string classID = ExtractValueFromQuotes(traceLineSplit[6]);
88  //System.out.println("#"+name+"\t"+signature+"\t"+methodID+"\t"+classID);
89  //leave all the names in; the methods that are not in the corpus will be eliminated because they will not have an ID
90  if (name.Equals("-init-"))
91  {
92  string fullClassName = idToClass[classID];
93  //System.out.println("\tfullClassName="+fullClassName);
94  string className = fullClassName.Substring(fullClassName.LastIndexOf("/") + 1);
95  // remove "outer" classes from inner classes
96  if (className.IndexOf("$") > 0)
97  {
98  //System.out.println(currentLine);
99  className = className.Substring(className.LastIndexOf("$") + 1);
100  }
101  name = className;
102  //System.out.println("\tclassName="+className);
103  //System.out.println("\tname="+name);
104  }
105  if (name.EndsWith("$"))
106  {
107  name = name.Substring(0, name.LastIndexOf("$"));
108  }
109  string fullMethodName = idToClass[classID] + "#" + name;
110  fullMethodName = fullMethodName.Replace('/', '.');
111  fullMethodName = fullMethodName.Replace('#','.');
112  fullMethodName = fullMethodName.Replace('$','.');
113  //System.out.println("MethodName="+fullMethodName);
114  idToMethod.Add(methodID, fullMethodName);
115  continue;
116  }
117  // methodEntry
118  if (currentLine.StartsWith("<methodEntry "))
119  {
120  traceLineSplit = currentLine.Split(' ');
121  if (traceLineSplit[1].StartsWith("threadIdRef=") == false)
122  throw new Exception();
123  if (traceLineSplit[2].StartsWith("time=") == false)
124  throw new Exception();
125  if (traceLineSplit[3].StartsWith("methodIdRef=") == false)
126  throw new Exception();
127  if (traceLineSplit[4].StartsWith("classIdRef=") == false)
128  throw new Exception();
129  if (traceLineSplit[5].StartsWith("ticket=") == false)
130  throw new Exception();
131  if (traceLineSplit[6].StartsWith("stackDepth=") == false)
132  throw new Exception();
133  string threadID = ExtractValueFromQuotes(traceLineSplit[1]);
134  string methodIDTrace = ExtractValueFromQuotes(traceLineSplit[3]);
135  //Stack<PairMethodIDTraceMethodIDCorpus> currentThreadMethodsStack=threadToMethodsStackTranslator.get(threadID);
136  if (!threadToMethodsStackTranslator.ContainsKey(threadID))
137  {
138  threadToMethodsStackTranslator.Add(threadID, new Stack<string>());
139  }
140 
141  //string fullMethodName = idToMethod[methodIDTrace];
142  //string IDCorpusForFullMethodName = inputOutput.getPositionOfMethodMappingInCorpus(fullMethodName);
143 
144  //PairMethodIDTraceMethodIDCorpus parentMethod=null;
145  string parentMethodID = null;
146  try
147  {
148  parentMethodID = threadToMethodsStackTranslator[threadID].Peek();
149  }
150  catch (InvalidOperationException)
151  {
152  // stack is empty
153  // do nothing, we will be adding childMethodID
154  }
155 
156  //PairMethodIDTraceMethodIDCorpus childMethod=new PairMethodIDTraceMethodIDCorpus(methodIDTrace,fullMethodName,IDCorpusForFullMethodName);
157  //string childMethod = fullMethodName;
158  string childMethodID = methodIDTrace;
159  threadToMethodsStackTranslator[threadID].Push(childMethodID);
160 
161  if (threadToMethodsStackTranslator[threadID].Count == 1)
162  {
163  //if its only 1 element, it couldn't have been called by anything
164  continue;
165  }
166 
167  //if (parentMethod.methodIDCorpus.equals("-1")||childMethod.methodIDCorpus.equals("-1"))
168  if (String.IsNullOrWhiteSpace(parentMethodID) || String.IsNullOrWhiteSpace(childMethodID))
169  continue;
170 
171  if (idToMethod[parentMethodID].EndsWith(".class")
172  || idToMethod[parentMethodID].EndsWith(".-clinit-")
173  || idToMethod[parentMethodID].EndsWith(".1")
174  || idToMethod[parentMethodID].Contains(".1.")
175  )
176  continue;
177 
178  if (idToMethod[childMethodID].EndsWith(".class")
179  || idToMethod[childMethodID].EndsWith(".-clinit-")
180  || idToMethod[childMethodID].EndsWith(".1")
181  || idToMethod[childMethodID].Contains(".1.")
182  )
183  continue;
184 
185  //do not add "recursive calls". This is due to the fact that these are calls between overwritten methods
186  if (parentMethodID.Equals(childMethodID) || idToMethod[parentMethodID].Equals(idToMethod[childMethodID]))
187  continue;
188 
189  bigrams.Add(new BiGram(idToMethod[parentMethodID], idToMethod[childMethodID]));
190  continue;
191  }
192  // methodExit
193  if (currentLine.StartsWith("<methodExit "))
194  {
195  traceLineSplit = currentLine.Split(' ');
196  if (traceLineSplit[1].StartsWith("threadIdRef=") == false)
197  throw new Exception();
198  if (traceLineSplit[2].StartsWith("methodIdRef=") == false)
199  throw new Exception();
200  if (traceLineSplit[3].StartsWith("classIdRef=") == false)
201  throw new Exception();
202  if (traceLineSplit[4].StartsWith("ticket=") == false)
203  throw new Exception();
204  if (traceLineSplit[5].StartsWith("time=") == false)
205  throw new Exception();
206 
207  string threadID = ExtractValueFromQuotes(traceLineSplit[1]);
208  string methodIDTrace = ExtractValueFromQuotes(traceLineSplit[2]);
209 
210  //Stack<PairMethodIDTraceMethodIDCorpus> currentThreadMethodsStack=threadToMethodsStackTranslator.get(threadID);
211  string topOfStack = threadToMethodsStackTranslator[threadID].Peek();
212  if (topOfStack.Equals(methodIDTrace) == false)
213  throw new Exception(currentLine + "\r\n" + threadToMethodsStackTranslator[threadID]);
214  threadToMethodsStackTranslator[threadID].Pop();
215 
216  continue;
217  }
218  }
219  brTrace.Close();
220  return bigrams;
221  }
222 
228  public static ISet<string> GenerateUniqueMethods(string filename)
229  {
230  TextReader brTrace = new StreamReader(filename);
231  //System.out.println("Processing File: "+inputOutput.getFileNameTrace(issueID));
232 
233  HashSet<string> setOfUniqueMethodsTrace = new HashSet<string>();
234  int lineNumber = 0;
235  Dictionary<string, string> idToClass = new Dictionary<string,string>();
236  Dictionary<string, string> idToMethod = new Dictionary<string, string>();
237 
238  string currentLine;
239  while ((currentLine = brTrace.ReadLine()) !=null)
240  {
241  lineNumber++;
242  string[] traceLineSplit;
243  // classDef
244  if (currentLine.StartsWith("<classDef "))
245  {
246  traceLineSplit = currentLine.Split(' ');
247  if (traceLineSplit[1].StartsWith("name=") == false)
248  throw new Exception();
249  if (traceLineSplit[2].StartsWith("sourceName=") == false)
250  throw new Exception();
251  if (traceLineSplit[3].StartsWith("classId=") == false)
252  throw new Exception();
253 
254  string name = ExtractValueFromQuotes(traceLineSplit[1]);
255  string classID = ExtractValueFromQuotes(traceLineSplit[3]);
256  //System.out.println(classID+"\t"+name);
257  idToClass.Add(classID, name);
258  continue;
259  }
260  // methodDef
261  if (currentLine.StartsWith("<methodDef "))
262  {
263  traceLineSplit = currentLine.Split(' ');
264  if (traceLineSplit[1].StartsWith("name=") == false)
265  throw new Exception();
266  if (traceLineSplit[2].StartsWith("signature=") == false)
267  throw new Exception();
268  if (traceLineSplit[3].StartsWith("startLineNumber=") == false)
269  throw new Exception();
270  if (traceLineSplit[4].StartsWith("endLineNumber=") == false)
271  throw new Exception();
272  if (traceLineSplit[5].StartsWith("methodId=") == false)
273  throw new Exception();
274  if (traceLineSplit[6].StartsWith("classIdRef=") == false)
275  throw new Exception();
276 
277  string name = ExtractValueFromQuotes(traceLineSplit[1]);
278  string signature = ExtractValueFromQuotes(traceLineSplit[2]);
279  string methodID = ExtractValueFromQuotes(traceLineSplit[5]);
280  string classID = ExtractValueFromQuotes(traceLineSplit[6]);
281  //System.out.println("#"+name+"\t"+signature+"\t"+methodID+"\t"+classID);
282 
283  //eliminate names such as "access$123"
284  if (name.IndexOf("$") > 0)
285  name = name.Substring(0, name.IndexOf("$"));
286 
287  // clinit are for static blocks
288  if (name.Equals("-clinit-"))
289  {
290  continue;
291  }
292 
293  if (traceLineSplit[1].Equals("name=\"class$\""))
294  {
295  continue;
296  }
297 
298  if (name.Equals("-init-"))
299  {
300  string fullClassName = idToClass[classID];
301  //System.out.println("\tfullClassName="+fullClassName);
302  string className = fullClassName.Substring(fullClassName.LastIndexOf("/") + 1);
303 
304  // remove "outer" classes from inner classes
305  if (className.IndexOf("$") > 0)
306  {
307  //System.out.println(currentLine);
308  className = className.Substring(className.LastIndexOf("$") + 1);
309  }
310 
311  name = className;
312 
313  //System.out.println("\tclassName="+className);
314  //System.out.println("\tname="+name);
315  }
316  string fullMethodName = idToClass[classID] + "." + name;
317  fullMethodName = fullMethodName.Replace('/','.');
318  fullMethodName = fullMethodName.Replace('#','.');
319  fullMethodName = fullMethodName.Replace('$','.');
320  //System.out.println("MethodName="+fullMethodName);
321  idToMethod.Add(methodID, fullMethodName);
322  setOfUniqueMethodsTrace.Add(fullMethodName);
323  continue;
324  }
325  }
326  brTrace.Close();
327  return setOfUniqueMethodsTrace;
328  }
329 
330  // string: identifier="value"
331  private static string ExtractValueFromQuotes(string entry)
332  {
333  int startPos = entry.IndexOf("\"") + 1;
334  int length = entry.LastIndexOf("\"") - startPos;
335  return entry.Substring(startPos, length);
336  }
337  }
338 }