18 using System.Collections.Generic;
21 using System.Reflection;
23 using RPlugin.Exceptions;
24 using TraceLab.Components.DevelopmentKit.IO;
25 using TraceLab.Components.RPlugin.Properties;
26 using TraceLab.Components.Types.Preprocessors.ExecutionTraces;
27 using TraceLab.Components.Types.Tracers.WebMining;
28 using TraceLabSDK.Types;
30 namespace TraceLab.Components.DevelopmentKit.Tracers.WebMining
37 private readonly
string _baseScript = Settings.Default.Resources +
"PageRank.R";
38 private readonly
string[] _requiredPackages =
new string[] {
"base" };
41 private string _mappingFile;
42 private string _outputFile;
44 private string _traceID;
49 public override string BaseScript
60 public override string[] RequiredPackages
64 return _requiredPackages;
88 switch (_config.Weight)
91 NormalizeEdgeBinary();
94 NormalizeEdgeFrequencies();
100 string matrixFile = GenerateTransitionProbabilityMatrix();
102 _arguments =
new List<object>();
103 _arguments.Add(matrixFile);
104 _arguments.Add(_outputFile);
105 _arguments.Add(_config.Beta);
106 _arguments.Add(_config.Epsilon);
118 if (ranks.Count() != map.Count())
120 throw new RDataException(
"Results file in incorrect format: incorrect number of entries");
122 TLSimilarityMatrix rankList =
new TLSimilarityMatrix();
123 for (
int i = 0; i < map.Count(); i++)
125 rankList.AddLink(_traceID, map.ElementAt(i), ranks.ElementAt(i));
130 #region Private methods
132 private void NormalizeEdgeFrequencies()
135 foreach (
PDGNode node
in _pdg.Nodes)
137 double sumNodeFrequencies = 0.0;
141 sumNodeFrequencies += edge.
Weight;
150 private void NormalizeEdgeBinary()
153 foreach (
PDGNode node
in _pdg.Nodes)
156 if (!binaryWeight.Equals(0.0))
158 binaryWeight = 1.0 / binaryWeight;
162 edge.
Weight = binaryWeight;
167 private string GenerateTransitionProbabilityMatrix()
169 int n = _pdg.Nodes.Count();
170 double defaultValue = 1.0 / n;
171 double[] rowValues =
new double[n];
174 TextWriter matrixWriter =
new StreamWriter(matrixFS);
177 TextWriter edgeWriter =
new StreamWriter(edgeFS);
180 _mappingFile = mapFS.Name;
181 TextWriter mapWriter =
new StreamWriter(mapFS);
183 for (
int nodeIndex = 0; nodeIndex < _pdg.Nodes.Count(); nodeIndex++)
185 PDGNode pdgNode = _pdg.GetNode(nodeIndex);
189 for (
int i = 0; i < n; i++)
191 rowValues[i] = defaultValue;
196 for (
int i = 0; i < n; i++)
204 for (
int indexOutgoingEdge = 0; indexOutgoingEdge < pdgNode.
OutgoingEdges.Count(); indexOutgoingEdge++)
207 int columnFrequencies = _pdg.IndexOf(pdgOutgoingEdge.
OutgoingNodeID);
209 if (columnFrequencies == -1)
214 rowValues[columnFrequencies] = pdgOutgoingEdge.
Weight;
221 matrixWriter.WriteLine(String.Join(
" ", rowValues));
225 matrixWriter.Flush();
226 matrixWriter.Close();
231 return matrixFS.Name;