19 using TraceLabSDK.Types;
20 using System.Collections.Generic;
22 using System.Xml.XPath;
24 namespace TraceLab.Components.DevelopmentKit.IO
37 public static TLSimilarityMatrix
Import(String filename)
39 StreamReader file =
new StreamReader(filename);
40 TLSimilarityMatrix answer =
new TLSimilarityMatrix();
42 while ((line = file.ReadLine()) != null)
44 String[] artifacts = line.Split();
45 String source = artifacts[0];
46 for (
int i = 1; i < artifacts.Length; i++)
48 String target = artifacts[i].Trim();
51 answer.AddLink(source, target, 1);
63 public static void Export(TLSimilarityMatrix answerMatrix,
string filename)
68 tw =
new StreamWriter(filename);
69 foreach (
string sourceID
in answerMatrix.SourceArtifactsIds)
72 foreach (
string targetID
in answerMatrix.GetSetOfTargetArtifactIdsAboveThresholdForSourceArtifact(sourceID))
74 tw.Write(
" " + targetID);
99 TLSimilarityMatrix oracle =
new TLSimilarityMatrix();
100 foreach (
string file
in Directory.GetFiles(directory))
102 string id = Path.GetFileName(file);
103 TextReader fReader =
new StreamReader(file);
105 while ((line = fReader.ReadLine()) != null)
107 if (String.IsNullOrWhiteSpace(line))
109 oracle.AddLink(
id, line, 1);
124 public static void ExportXML(TLSimilarityMatrix answerSet,
string sourceId,
string targetId,
string outputPath)
126 if (answerSet == null)
128 throw new TraceLabSDK.ComponentException(
"Received null answer similarity matrix");
130 System.Xml.XmlWriterSettings settings =
new System.Xml.XmlWriterSettings();
131 settings.Indent =
true;
132 settings.CloseOutput =
true;
133 settings.CheckCharacters =
true;
135 using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(outputPath, settings))
137 writer.WriteStartDocument();
138 writer.WriteStartElement(
"answer_set");
139 WriteAnswerSetXMLInfo(writer, sourceId, targetId);
140 WriteXMLLinks(answerSet, writer);
141 writer.WriteEndElement();
142 writer.WriteEndDocument();
154 private static void WriteAnswerSetXMLInfo(System.Xml.XmlWriter writer,
string sourceId,
string targetId)
156 writer.WriteStartElement(
"answer_info");
157 writer.WriteElementString(
"source_artifacts_collection", sourceId.Trim());
158 writer.WriteElementString(
"target_artifacts_collection", targetId.Trim());
159 writer.WriteEndElement();
167 private static void WriteXMLLinks(TLSimilarityMatrix answerSet, System.Xml.XmlWriter writer)
169 writer.WriteStartElement(
"links");
170 foreach (TLSingleLink link
in answerSet.AllLinks)
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();
178 writer.WriteEndElement();
187 public static TLSimilarityMatrix
ImportXML(
string filepath,
bool trimValues)
189 string friendlyAnswerSetFilename = System.IO.Path.GetFileName(filepath);
191 TLSimilarityMatrix answerSet =
new TLSimilarityMatrix();
193 XPathDocument doc =
new XPathDocument(filepath);
194 XPathNavigator nav = doc.CreateNavigator();
197 XPathNavigator iter = nav.SelectSingleNode(
"/answer_set/answer_info/source_artifacts_collection");
198 string source_artifacts_collection_id = iter.Value;
200 iter = nav.SelectSingleNode(
"/answer_set/answer_info/target_artifacts_collection");
201 string target_artifacts_collection_id = iter.Value;
203 XPathNodeIterator linksIterator = nav.Select(
"/answer_set/links/link");
205 string source_artifact_id;
206 string target_artifact_id;
207 double confidence_score;
208 while (linksIterator.MoveNext())
211 iter = linksIterator.Current.SelectSingleNode(
"source_artifact_id");
214 throw new XmlException(String.Format(
"The source_artifact_id has not been provided for the link. File location: {0}", filepath));
217 source_artifact_id = iter.Value;
220 source_artifact_id = source_artifact_id.Trim();
224 iter = linksIterator.Current.SelectSingleNode(
"target_artifact_id");
227 throw new XmlException(String.Format(
"The target_artifact_id has not been provided for the link. File location: {0}", filepath));
230 target_artifact_id = iter.Value;
233 target_artifact_id = target_artifact_id.Trim();
237 iter = linksIterator.Current.SelectSingleNode(
"confidence_score");
241 confidence_score = 1.0;
245 string tmpValue = iter.Value;
246 if (trimValues) tmpValue = tmpValue.Trim();
248 if (
double.TryParse(tmpValue, out confidence_score) ==
false)
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));
254 answerSet.AddLink(source_artifact_id, target_artifact_id, confidence_score);