private string GenerateAdjacencyMatrix() { int n = _pdg.Nodes.Count(); double defaultValue = 1.0 / n; double[] rowValues = new double[n]; FileStream matrixFS = RUtil.CreateCacheFile("HITS." + _traceID + ".TPM.matrix"); TextWriter matrixWriter = new StreamWriter(matrixFS); //FileStream edgeFS = RUtil.CreateCacheFile("HITS." + _traceID + ".TPM.edges"); //TextWriter edgeWriter = new StreamWriter(edgeFS); FileStream mapFS = RUtil.CreateCacheFile("HITS." + _traceID + ".TPM.map"); _mappingFile = mapFS.Name; TextWriter mapWriter = new StreamWriter(mapFS); for (int nodeIndex = 0; nodeIndex < _pdg.Nodes.Count(); nodeIndex++) { PDGNode pdgNode = _pdg.GetNode(nodeIndex); for (int i = 0; i < n; i++) { rowValues[i] = 0; } //edgeWriter.WriteLine(pdgNode.OutgoingEdges.Count()); // write number of outgoing edges for Topical HITS algorithm for (int indexOutgoingEdge = 0; indexOutgoingEdge < pdgNode.OutgoingEdges.Count(); indexOutgoingEdge++) { PDGEdge pdgOutgoingEdge = pdgNode.OutgoingEdges.ElementAt(indexOutgoingEdge); int columnFrequencies = _pdg.IndexOf(pdgOutgoingEdge.OutgoingNodeID); // for positive values only if ((columnFrequencies < 0)) { throw new RDataException(); // continue; } if (_config.Weight == WebMiningWeightEnum.Binary) { rowValues[columnFrequencies] = 1; } else if (_config.Weight == WebMiningWeightEnum.Frequency) { rowValues[columnFrequencies] = pdgOutgoingEdge.Weight; } else { throw new RDataException("Unknown weighting scheme: " + _config.Weight); } } //for (int i=1;i<=n;i++) //{ // matrixWriter.Write(rowValuesFrequencies[i]+" "); // binaryWriter.Write(rowValuesBinary[i]+" "); //} matrixWriter.WriteLine(String.Join(" ", rowValues)); mapWriter.WriteLine(pdgNode.MethodName); } matrixWriter.Flush(); matrixWriter.Close(); //edgeWriter.Flush(); //edgeWriter.Close(); mapWriter.Flush(); mapWriter.Close(); return(matrixFS.Name); }
private string GenerateTransitionProbabilityMatrix() { int n = _pdg.Nodes.Count(); double defaultValue = 1.0 / n; double[] rowValues = new double[n]; FileStream matrixFS = RUtil.CreateCacheFile("PageRank." + _traceID + ".TPM.matrix"); TextWriter matrixWriter = new StreamWriter(matrixFS); FileStream edgeFS = RUtil.CreateCacheFile("PageRank." + _traceID + ".TPM.edges"); TextWriter edgeWriter = new StreamWriter(edgeFS); FileStream mapFS = RUtil.CreateCacheFile("PageRank." + _traceID + ".TPM.map"); _mappingFile = mapFS.Name; TextWriter mapWriter = new StreamWriter(mapFS); for (int nodeIndex = 0; nodeIndex < _pdg.Nodes.Count(); nodeIndex++) { PDGNode pdgNode = _pdg.GetNode(nodeIndex); if (pdgNode.OutgoingEdges.Count() == 0) { for (int i = 0; i < n; i++) { rowValues[i] = defaultValue; } } else { for (int i = 0; i < n; i++) { rowValues[i] = 0.0; } } edgeWriter.WriteLine(pdgNode.OutgoingEdges.Count()); // write number of outgoing edges for most of the advanced PageRank algorithms for (int indexOutgoingEdge = 0; indexOutgoingEdge < pdgNode.OutgoingEdges.Count(); indexOutgoingEdge++) { PDGEdge pdgOutgoingEdge = pdgNode.OutgoingEdges.ElementAt(indexOutgoingEdge); int columnFrequencies = _pdg.IndexOf(pdgOutgoingEdge.OutgoingNodeID); // for positive values only if (columnFrequencies == -1) { throw new RDataException("Invalid column index."); // continue; } rowValues[columnFrequencies] = pdgOutgoingEdge.Weight; } //for (int i=1;i<=n;i++) //{ // matrixWriter.Write(rowValues[i]+" "); //} matrixWriter.WriteLine(String.Join(" ", rowValues)); mapWriter.WriteLine(pdgNode.MethodName); } matrixWriter.Flush(); matrixWriter.Close(); edgeWriter.Flush(); edgeWriter.Close(); mapWriter.Flush(); mapWriter.Close(); return(matrixFS.Name); }