TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
PDG.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.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using TraceLabSDK;
22 
23 namespace TraceLab.Components.Types.Preprocessors.ExecutionTraces
24 {
28  [Serializable]
29  [WorkspaceType]
30  public class PDG
31  {
32  #region Static utilities
33 
39  public static PDG Convert(BiGramCollection bigrams)
40  {
41  PDG pdg = new PDG();
42 
43  foreach (BiGram bigram in bigrams)
44  {
45  if (!pdg._nodes.ContainsKey(bigram.Caller))
46  {
47  pdg.Add(new PDGNode(bigram.Caller));
48  }
49  pdg._nodes[bigram.Caller].AddChild(bigram.Callee);
50  if (!pdg._nodes.ContainsKey(bigram.Callee))
51  pdg.Add(new PDGNode(bigram.Callee));
52  }
53 
54  return pdg;
55  }
56 
62  public static PDG DeepCopy(PDG pdg)
63  {
64  PDG pdgCopy = new PDG();
65  pdgCopy._nodes = new SerializableDictionary<string,PDGNode>();
66  foreach (KeyValuePair<string, PDGNode> kvpNode in pdg._nodes)
67  {
68  PDGNode nodeCopy = new PDGNode(kvpNode.Value.MethodName);
69  foreach (PDGEdge edge in kvpNode.Value.OutgoingEdges)
70  {
71  nodeCopy.SetEdgeWeight(edge.OutgoingNodeID, edge.Weight);
72  }
73  pdgCopy.Add(nodeCopy);
74  }
75  return pdgCopy;
76  }
77 
78  #endregion
79 
80  private SerializableDictionary<string, PDGNode> _nodes;
81  private List<string> _mapping;
82  private SerializableDictionary<string, int> _indexes;
83 
87  public IEnumerable<PDGNode> Nodes
88  {
89  get
90  {
91  return _nodes.Values;
92  }
93  }
94 
98  public PDG()
99  {
100  _nodes = new SerializableDictionary<string, PDGNode>();
101  _mapping = new List<string>();
102  _indexes = new SerializableDictionary<string, int>();
103  }
104 
109  public void Add(PDGNode pdgNode)
110  {
111  _nodes.Add(pdgNode.MethodName, pdgNode);
112  _mapping.Add(pdgNode.MethodName);
113  _indexes.Add(pdgNode.MethodName, _mapping.Count - 1);
114  }
115 
121  public PDGNode GetNode(string node)
122  {
123  PDGNode pdgNode;
124  _nodes.TryGetValue(node, out pdgNode);
125  return pdgNode;
126  }
127 
133  public PDGNode GetNode(int index)
134  {
135  return _nodes[_mapping[index]];
136  }
137 
143  public int IndexOf(string nodeID)
144  {
145  if (!_indexes.ContainsKey(nodeID))
146  {
147  return -2;
148  }
149  return _indexes[nodeID];
150  }
151 
156  public string Print()
157  {
158  StringBuilder sb = new StringBuilder("===PDG BEGIN===");
159  sb.AppendLine();
160  foreach (PDGNode node in Nodes)
161  {
162  sb.AppendLine(node.ToString());
163  }
164  sb.AppendLine("===PDG END===");
165  return sb.ToString();
166  }
167  }
168 }