TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
Oracle.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 using System.Collections.Generic;
21 using System.Xml;
22 using System.Xml.XPath;
23 
24 namespace TraceLab.Components.DevelopmentKit.IO
25 {
29  public static class Oracle
30  {
37  public static TLSimilarityMatrix Import(String filename)
38  {
39  StreamReader file = new StreamReader(filename);
40  TLSimilarityMatrix answer = new TLSimilarityMatrix();
41  String line;
42  while ((line = file.ReadLine()) != null)
43  {
44  String[] artifacts = line.Split();
45  String source = artifacts[0];
46  for (int i = 1; i < artifacts.Length; i++)
47  {
48  String target = artifacts[i].Trim();
49  if (target != "")
50  {
51  answer.AddLink(source, target, 1);
52  }
53  }
54  }
55  return answer;
56  }
57 
63  public static void Export(TLSimilarityMatrix answerMatrix, string filename)
64  {
65  TextWriter tw = null;
66  try
67  {
68  tw = new StreamWriter(filename);
69  foreach (string sourceID in answerMatrix.SourceArtifactsIds)
70  {
71  tw.Write(sourceID);
72  foreach (string targetID in answerMatrix.GetSetOfTargetArtifactIdsAboveThresholdForSourceArtifact(sourceID))
73  {
74  tw.Write(" " + targetID);
75  }
76  tw.WriteLine();
77  }
78  tw.Flush();
79  tw.Close();
80  }
81  catch (Exception e)
82  {
83  if (tw != null)
84  {
85  tw.Close();
86  }
87  throw new DevelopmentKitException("There was an exception writing to file (" + filename + ")", e);
88  }
89  }
90 
97  public static TLSimilarityMatrix ImportDirectory(string directory)
98  {
99  TLSimilarityMatrix oracle = new TLSimilarityMatrix();
100  foreach (string file in Directory.GetFiles(directory))
101  {
102  string id = Path.GetFileName(file);
103  TextReader fReader = new StreamReader(file);
104  string line;
105  while ((line = fReader.ReadLine()) != null)
106  {
107  if (String.IsNullOrWhiteSpace(line))
108  continue;
109  oracle.AddLink(id, line, 1);
110  }
111  }
112  return oracle;
113  }
114 
115  #region XML I/O
116 
124  public static void ExportXML(TLSimilarityMatrix answerSet, string sourceId, string targetId, string outputPath)
125  {
126  if (answerSet == null)
127  {
128  throw new TraceLabSDK.ComponentException("Received null answer similarity matrix");
129  }
130  System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
131  settings.Indent = true;
132  settings.CloseOutput = true;
133  settings.CheckCharacters = true;
134  //create file
135  using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(outputPath, settings))
136  {
137  writer.WriteStartDocument();
138  writer.WriteStartElement("answer_set");
139  WriteAnswerSetXMLInfo(writer, sourceId, targetId);
140  WriteXMLLinks(answerSet, writer);
141  writer.WriteEndElement(); //answer_set
142  writer.WriteEndDocument();
143  writer.Close();
144  }
145  //System.Diagnostics.Trace.WriteLine("File created , you can find the file " + outputPath);
146  }
147 
154  private static void WriteAnswerSetXMLInfo(System.Xml.XmlWriter writer, string sourceId, string targetId)
155  {
156  writer.WriteStartElement("answer_info");
157  writer.WriteElementString("source_artifacts_collection", sourceId.Trim());
158  writer.WriteElementString("target_artifacts_collection", targetId.Trim());
159  writer.WriteEndElement();
160  }
161 
167  private static void WriteXMLLinks(TLSimilarityMatrix answerSet, System.Xml.XmlWriter writer)
168  {
169  writer.WriteStartElement("links");
170  foreach (TLSingleLink link in answerSet.AllLinks)
171  {
172  writer.WriteStartElement("link");
173  writer.WriteElementString("source_artifact_id", link.SourceArtifactId.Trim());
174  writer.WriteElementString("target_artifact_id", link.TargetArtifactId.Trim());
175  writer.WriteElementString("confidence_score", link.Score.ToString().Trim());
176  writer.WriteEndElement();
177  }
178  writer.WriteEndElement(); // artifacts
179  }
180 
187  public static TLSimilarityMatrix ImportXML(string filepath, bool trimValues)
188  {
189  string friendlyAnswerSetFilename = System.IO.Path.GetFileName(filepath);
190 
191  TLSimilarityMatrix answerSet = new TLSimilarityMatrix();
192 
193  XPathDocument doc = new XPathDocument(filepath);
194  XPathNavigator nav = doc.CreateNavigator();
195 
196  //read collection info
197  XPathNavigator iter = nav.SelectSingleNode("/answer_set/answer_info/source_artifacts_collection");
198  string source_artifacts_collection_id = iter.Value;
199 
200  iter = nav.SelectSingleNode("/answer_set/answer_info/target_artifacts_collection");
201  string target_artifacts_collection_id = iter.Value;
202 
203  XPathNodeIterator linksIterator = nav.Select("/answer_set/links/link");
204 
205  string source_artifact_id;
206  string target_artifact_id;
207  double confidence_score;
208  while (linksIterator.MoveNext())
209  {
210  // Parse Source Artifact Id
211  iter = linksIterator.Current.SelectSingleNode("source_artifact_id");
212  if (iter == null)
213  {
214  throw new XmlException(String.Format("The source_artifact_id has not been provided for the link. File location: {0}", filepath));
215  }
216 
217  source_artifact_id = iter.Value;
218  if (trimValues)
219  {
220  source_artifact_id = source_artifact_id.Trim();
221  }
222 
223  // Parse Target Artifact Id
224  iter = linksIterator.Current.SelectSingleNode("target_artifact_id");
225  if (iter == null)
226  {
227  throw new XmlException(String.Format("The target_artifact_id has not been provided for the link. File location: {0}", filepath));
228  }
229 
230  target_artifact_id = iter.Value;
231  if (trimValues)
232  {
233  target_artifact_id = target_artifact_id.Trim();
234  }
235 
236  //Parse confidence score
237  iter = linksIterator.Current.SelectSingleNode("confidence_score");
238  if (iter == null)
239  {
240  //if confidence score is not provided set it to default value 1
241  confidence_score = 1.0;
242  }
243  else
244  {
245  string tmpValue = iter.Value;
246  if (trimValues) tmpValue = tmpValue.Trim();
247 
248  if (double.TryParse(tmpValue, out confidence_score) == false)
249  {
250  throw new XmlException(String.Format("The confidence score provided for link from source artifact {0} to target artifact is in incorrect format {1}. File location: {2}", source_artifact_id, target_artifact_id, filepath));
251  }
252  }
253 
254  answerSet.AddLink(source_artifact_id, target_artifact_id, confidence_score);
255  }
256 
257  return answerSet;
258  }
259 
260  #endregion
261  }
262 }