TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Similarities.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.IO;
19 using TraceLabSDK.Types;
20 
21 namespace TraceLab.Components.DevelopmentKit.IO
22 {
26  public static class Similarities
27  {
34  public static TLSimilarityMatrix Import(String filename)
35  {
36  StreamReader file = new StreamReader(filename);
37  TLSimilarityMatrix answer = new TLSimilarityMatrix();
38  String line;
39  int num = 0;
40  while ((line = file.ReadLine()) != null)
41  {
42  num++;
43  if (String.IsNullOrWhiteSpace(line))
44  continue;
45  try
46  {
47  String[] artifacts = line.Split('\t');
48  String source = artifacts[0];
49  String target = artifacts[1];
50  double score = Convert.ToDouble(artifacts[2]);
51  answer.AddLink(source, target, score);
52  }
53  catch (IndexOutOfRangeException e)
54  {
55  file.Close();
56  throw new DevelopmentKitException("Invalid data format on line " + num + " of file:" + filename, e);
57  }
58  }
59  file.Close();
60  return answer;
61  }
62 
69  public static void Export(TLSimilarityMatrix matrix, string filename)
70  {
71  TextWriter file = new StreamWriter(filename);
72  TLLinksList links = matrix.AllLinks;
73  links.Sort();
74  foreach (TLSingleLink link in links)
75  {
76  file.WriteLine("{0}\t{1}\t{2}", link.SourceArtifactId, link.TargetArtifactId, link.Score);
77  }
78  file.Flush();
79  file.Close();
80  }
81 
82  #region Export to CSV
83 
89  public static void ExportCSV(TLSimilarityMatrix similarityMatrix, string outputPath)
90  {
91  if (similarityMatrix == null)
92  {
93  throw new DevelopmentKitException("Received similarity matrix is null!");
94  }
95  if (outputPath == null)
96  {
97  throw new DevelopmentKitException("Output path cannot be null.");
98  }
99  if (!System.IO.Path.IsPathRooted(outputPath))
100  {
101  throw new DevelopmentKitException(String.Format("Absolute output path is required. Given path is '{0}'", outputPath));
102  }
103  if (outputPath.EndsWith(".csv", StringComparison.CurrentCultureIgnoreCase) == false)
104  {
105  outputPath = outputPath + ".csv";
106  }
107  using (System.IO.TextWriter writeFile = new StreamWriter(outputPath))
108  {
109  WriteMatrixCSV(similarityMatrix, writeFile);
110  writeFile.Flush();
111  writeFile.Close();
112  }
113  }
114 
120  private static void WriteMatrixCSV(TLSimilarityMatrix similarityMatrix, System.IO.TextWriter writeFile)
121  {
122  //header
123  writeFile.WriteLine("Source Artifact Id,Target Artifact Id,Probability");
124  TLLinksList traceLinks = similarityMatrix.AllLinks;
125  traceLinks.Sort();
126  foreach (TLSingleLink link in traceLinks)
127  {
128  writeFile.WriteLine("{0},{1},{2}", link.SourceArtifactId, link.TargetArtifactId, link.Score);
129  }
130  }
131 
140  public static void ExportCSVWithCorrectness(TLSimilarityMatrix similarityMatrix, TLSimilarityMatrix answerMatrix, string outputPath)
141  {
142  if (similarityMatrix == null)
143  {
144  throw new DevelopmentKitException("Received similarity matrix is null!");
145  }
146  if (answerMatrix == null)
147  {
148  throw new DevelopmentKitException("Received answer similarity matrix is null!");
149  }
150  if (outputPath == null)
151  {
152  throw new DevelopmentKitException("Output path cannot be null.");
153  }
154  if (!System.IO.Path.IsPathRooted(outputPath))
155  {
156  throw new DevelopmentKitException(String.Format("Absolute output path is required. Given path is '{0}'", outputPath));
157  }
158  if (outputPath.EndsWith(".csv", StringComparison.CurrentCultureIgnoreCase) == false)
159  {
160  outputPath = outputPath + ".csv";
161  }
162  using (System.IO.TextWriter writeFile = new StreamWriter(outputPath))
163  {
164  WriteMatrixCSVWithCorrectness(similarityMatrix, answerMatrix, writeFile);
165  writeFile.Flush();
166  writeFile.Close();
167  }
168  }
169 
178  private static void WriteMatrixCSVWithCorrectness(TLSimilarityMatrix similarityMatrix, TLSimilarityMatrix answerMatrix, System.IO.TextWriter writeFile)
179  {
180  //header
181  writeFile.WriteLine("Source Artifact Id,Target Artifact Id,Probability,Is correct");
182  TLLinksList traceLinks = similarityMatrix.AllLinks;
183  traceLinks.Sort();
184  foreach (TLSingleLink link in traceLinks)
185  {
186  writeFile.WriteLine("{0},{1},{2},{3}", link.SourceArtifactId, link.TargetArtifactId, link.Score, (answerMatrix.IsLinkAboveThreshold(link.SourceArtifactId, link.TargetArtifactId)) ? "1" : "0");
187  }
188  }
189 
190  #endregion
191 
192  #region Export to Excel spreadsheet
193 
201  public static void ExportExcelSpreadsheet(TLSimilarityMatrix similarityMatrix, TLSimilarityMatrix answerMatrix, string outputPath)
202  {
203  if (similarityMatrix == null)
204  {
205  throw new DevelopmentKitException("Received similarity matrix is null!");
206  }
207  if (answerMatrix == null)
208  {
209  throw new DevelopmentKitException("Received answer similarity matrix is null!");
210  }
211  if (outputPath == null)
212  {
213  throw new DevelopmentKitException("Output path cannot be null.");
214  }
215  if (!System.IO.Path.IsPathRooted(outputPath))
216  {
217  throw new DevelopmentKitException(String.Format("Absolute output path is required. Given path is '{0}'", outputPath));
218  }
219  if (outputPath.EndsWith(".xsl", StringComparison.CurrentCultureIgnoreCase) == false)
220  {
221  outputPath = outputPath + ".xsl";
222  }
223  Microsoft.Office.Interop.Excel.Application xlApp;
224  Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
225  Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
226  object misValue = System.Reflection.Missing.Value;
227  xlApp = new Microsoft.Office.Interop.Excel.Application();
228  xlWorkBook = xlApp.Workbooks.Add(misValue);
229  xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
230  ReadSimilarityMatrixToExcelWorksheet(similarityMatrix, answerMatrix, xlWorkSheet);
231  xlWorkBook.SaveAs(outputPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
232  xlWorkBook.Close(true, misValue, misValue);
233  xlApp.Quit();
234  ReleaseExcelObject(xlWorkSheet);
235  ReleaseExcelObject(xlWorkBook);
236  ReleaseExcelObject(xlApp);
237  }
238 
245  private static void ReadSimilarityMatrixToExcelWorksheet(TLSimilarityMatrix similarityMatrix, TLSimilarityMatrix answerMatrix, Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet)
246  {
247  //header
248  int row = 1;
249  xlWorkSheet.Cells[row, 1] = "Source Artifact Id";
250  xlWorkSheet.Cells[row, 2] = "Target Artifact Id";
251  xlWorkSheet.Cells[row, 3] = "Probability";
252  xlWorkSheet.Cells[row, 4] = "Is correct";
253  row++;
254  foreach (string sourceArtifact in similarityMatrix.SourceArtifactsIds)
255  {
256  var traceLinks = similarityMatrix.GetLinksAboveThresholdForSourceArtifact(sourceArtifact);
257  traceLinks.Sort();
258  foreach (TLSingleLink link in traceLinks)
259  {
260  xlWorkSheet.Cells[row, 1] = link.SourceArtifactId;
261  xlWorkSheet.Cells[row, 2] = link.TargetArtifactId;
262  xlWorkSheet.Cells[row, 3] = link.Score;
263  xlWorkSheet.Cells[row, 4] = (answerMatrix.IsLinkAboveThreshold(link.SourceArtifactId, link.TargetArtifactId)) ? "1" : "0";
264  row++;
265  }
266  }
267  }
268 
273  private static void ReleaseExcelObject(object obj)
274  {
275  try
276  {
277  System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
278  obj = null;
279  }
280  catch (Exception ex)
281  {
282  obj = null;
283  throw new Exception("Exception occured while releasing excel object " + ex.ToString());
284  }
285  finally
286  {
287  GC.Collect();
288  }
289  }
290 
291  #endregion
292  }
293 }