-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyGraph.cpp
More file actions
260 lines (206 loc) · 6.91 KB
/
DependencyGraph.cpp
File metadata and controls
260 lines (206 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/***************************************************************************
* Copyright (C) 2009 by Mushthofa *
* unintendedchoice@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**
* @brief The actual definition of the dependency graph
* Also construct components and components dependency graph
* @author Roman Schindlauer, Mushthofa
*/
#include <sstream>
#include <vector>
#include <list>
#include "DependencyGraph.h"
#include <boost/utility.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/topological_sort.hpp>
#include <boost/graph/depth_first_search.hpp>
DependencyGraph::DependencyGraph(const NodeGraph& ng, ComponentFinder* cf)
: nodegraph(ng), componentFinder(cf)
{
const std::vector<AtomNodePtr> allnodes = nodegraph.getNodes();
std::vector<std::vector<AtomNodePtr> > strongComponents;
//
// keep track of the nodes that belong to a SCC
//
std::vector<AtomNodePtr> visited;
//
// find all strong components
//
getStrongComponents(allnodes, strongComponents);
//
// go through strong components
//
for (std::vector<std::vector<AtomNodePtr> >::const_iterator scc = strongComponents.begin();
scc != strongComponents.end();
++scc)
{
Component* comp = new ProgramComponent(*scc);
/*
std::cout<<"SCC = ";
std::vector<AtomNodePtr>::const_iterator itsc;
for(itsc=scc->begin(); itsc!=scc->end(); ++itsc)
{
std::cout<<*(*itsc)<<" ";
}
std::cout<<std::endl;
*/
/* delete empty components */
/*
if(comp->getBottom().empty())
delete comp;
else*/
components.push_back(comp);
//
// mark these scc nodes as visited
// TODO: this is not so nice here
//
for (std::vector<AtomNodePtr>::const_iterator ni = (*scc).begin();
ni != (*scc).end();
++ni)
{
visited.push_back(*ni);
}
}
/* Set up dependencies between components */
std::vector<Component*>::iterator ci;
for(ci=components.begin(); ci!=components.end(); ++ci)
{
std::vector<AtomNodePtr> currentNodes = (*ci)->getNodes();
/*
std::cout<<"current component's nodes = ";
for(std::vector<AtomNodePtr>::iterator ai=currentNodes.begin();
ai!=currentNodes.end(); ++ai)
{
std::cout<< *((*ai)->getAtom()) <<", ";
}
std::cout<<std::endl;
*/
std::list<AtomNodePtr> previousNodes = (*ci)->getIncomingNodes();
std::list<AtomNodePtr>::const_iterator pi;
for(pi=previousNodes.begin(); pi!=previousNodes.end(); ++pi)
{
std::vector<Component*>::iterator ci2;
for(ci2=components.begin(); ci2!=components.end(); ++ci2)
{
if(ci!=ci2) /* Don't add the current component itself */
{
AtomPtr checkAtom = (*pi)->getAtom();
if((*ci2)->isInComponent(checkAtom))
{
/* We want to store the dependency on both sides */
/* to make it easier to compute labeling and so on.. */
(*ci)->addPreviousComponent(*ci2);
(*ci2)->addNextComponent(*ci);
}
}
}
}
}
/* Make the DAG labeling for each component */
labelComponents();
}
//void DependencyGraph::
DependencyGraph::~DependencyGraph()
{
for (std::vector<Component*>::const_iterator ci = components.begin();
ci != components.end();
++ci)
{
delete *ci;
}
}
void DependencyGraph::labelComponents()
{
/* Still empty for now, but later might be used for components simplification and merging */
}
bool DependencyGraph::hasNegEdge(const std::vector<AtomNodePtr>& nodes)
{
for (std::vector<AtomNodePtr>::const_iterator ni = nodes.begin();
ni != nodes.end();
++ni)
{
//
// since an SCC is always cyclic, we only have to consider preceding,
// not preceding AND succeeding!
//
for (std::set<Dependency>::const_iterator di = (*ni)->getPreceding().begin();
di != (*ni)->getPreceding().end();
++di)
{
if (((*di).getType() == Dependency::NEG_PRECEDING))
//|| ((*di).getType() == Dependency::DISJUNCTIVE))
//
// a scc has a negated edge only if the "target" of the edge is also in the cycle!
//
if (find(nodes.begin(), nodes.end(), (*di).getAtomNode()) != nodes.end())
return true;
}
}
return false;
}
void DependencyGraph::getStrongComponents(const std::vector<AtomNodePtr>& nodes,
std::vector<std::vector<AtomNodePtr> >& sccs)
{
componentFinder->findStrongComponents(nodes, sccs);
}
std::vector<Component*> DependencyGraph::getComponents() const
{
/* If we have only one component, return immediately */
if(components.size() <= 1)
return components;
/* Otherwise, return the components in a topological sort */
std::vector<Component*> sortedComponents;
using namespace boost;
{
typedef adjacency_list <vecS, vecS, directedS> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
Graph G(0);
//typedef std::pair<Component*, Component*> Edge;
std::vector<Component*>::const_iterator cit;
for(cit = components.begin(); cit != components.end(); ++cit)
{
std::set<Component*> previousComp = (*cit)->getPreviousComponents();
std::set<Component*>::const_iterator pi;
for(pi=previousComp.begin(); pi!=previousComp.end(); ++pi)
{
add_edge((*pi)->getLabel(), (*cit)->getLabel(), G);
}
}
typedef std::list<Vertex> Ordering;
Ordering sortedComp;
/* Do topological sort */
topological_sort(G, std::front_inserter(sortedComp));
/* Put the result in sortedComponents*/
Ordering::const_iterator oit;
for(oit = sortedComp.begin(); oit!=sortedComp.end(); ++oit)
{
/* Look for the component with the correct label */
std::vector<Component*>::const_iterator cit;
for(cit = components.begin(); cit != components.end(); ++cit)
{
if((*cit)->getLabel() == *oit)
{
sortedComponents.push_back(*cit);
}
}
}
}
return sortedComponents;
}
// End