18 using RPlugin.Exceptions;
20 using System.Collections.Generic;
22 using System.Reflection;
24 using TraceLab.Components.RPlugin.Properties;
25 using TraceLabSDK.Types;
27 namespace TraceLab.Components.DevelopmentKit.Metrics.Traceability
34 private readonly
string _baseScript = Settings.Default.Resources +
"PCA.R";
35 private readonly
string[] _requiredPackages =
new string[] {
"psych",
"GPArotation" };
37 private TLSimilarityMatrix[] _matrices;
38 private string _outputFile;
43 public override string BaseScript
54 public override string[] RequiredPackages
58 return _requiredPackages;
66 public PCAScript(params TLSimilarityMatrix[] matrices) : base()
77 string tableFile = CreateTable(_matrices);
79 _arguments =
new List<object>();
80 _arguments.Add(tableFile);
81 _arguments.Add(_outputFile);
91 List<double> pca =
new List<double>();
92 TextReader file =
new StreamReader(_outputFile);
94 string line = String.Empty;
95 while ((line = file.ReadLine()) != null)
97 string[] entries = line.Split(
',');
98 if (entries.Length != 3)
102 pca.Add(Convert.ToDouble(entries[2]));
104 if (pca.Count != _matrices.Length)
106 throw new RDataException(
"PCA returned the incorrect number of entries.");
108 return pca.ToArray();
116 private string CreateTable(params TLSimilarityMatrix[] matrices)
118 if (matrices.Length < 2)
123 TextWriter tableWriter =
new StreamWriter(tableFile);
124 tableWriter.Write(
"M1");
125 for (
int i = 1; i < matrices.Length; i++)
127 if (matrices[i].Count != matrices[0].Count)
129 throw new RDataException(
"Matrices have different count of links.");
131 tableWriter.Write(String.Format(
"\tM{0}", i + 1));
133 tableWriter.WriteLine();
134 foreach (TLSingleLink link
in matrices[0].AllLinks)
136 tableWriter.Write(String.Format(
"{0}_{1}\t{2}",
137 link.SourceArtifactId,
138 link.TargetArtifactId,
141 for (
int i = 1; i < matrices.Length; i++)
143 tableWriter.Write(String.Format(
"\t{0}", matrices[i].GetScoreForLink(link.SourceArtifactId, link.TargetArtifactId)));
145 tableWriter.WriteLine();
149 return tableFile.Name;