-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoostHCFDetector.cpp
212 lines (168 loc) · 5.33 KB
/
BoostHCFDetector.cpp
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
/***************************************************************************
* 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 BoostHCFDetector.cpp
* Performing HCF detection using BOOST Graph Lib
*
* @author Mushthofa
*/
#include "BoostHCFDetector.h"
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/graphviz.hpp>
BoostHCFDetector::BoostHCFDetector()
:HCFDetector()
{}
bool BoostHCFDetector::isHCF(Component* comp)
{
std::vector<AtomNodePtr> nodes = comp->getNodes();
HCFDetector::Edges edges;
std::vector<std::vector<AtomNodePtr> > cycles;
findCycles(nodes, cycles);
//std::cout<<"Number of positive cycles: "<<cycles.size()<<std::endl;
std::vector<std::vector<AtomNodePtr> >::iterator c_it;
for(c_it = cycles.begin(); c_it!= cycles.end(); ++c_it)
{
if(findHeadCycle(*c_it))
{
return false;
}
}
return true;
}
void BoostHCFDetector::makeEdges(const std::vector<AtomNodePtr>& nodes, Edges& edges) const
{
for (std::vector<AtomNodePtr>::const_iterator node = nodes.begin();
node != nodes.end();
++node)
{
HCFDetector::Vertex v1 = (*node)->getId();
//
// considering only positive dependencies
//
for (std::set<Dependency>::const_iterator d = (*node)->getPositivePreceding().begin();
d != (*node)->getPositivePreceding().end();
++d)
{
HCFDetector::Vertex v2 = (*d).getAtomNode()->getId();
edges.push_back(HCFDetector::Edge(v1, v2));
}
}
}
void BoostHCFDetector::selectNodes(const Vertices& vertices,
const std::vector<AtomNodePtr>& nodes,
std::vector<AtomNodePtr>& newnodes) const
{
newnodes.clear();
for (HCFDetector::Vertices::const_iterator vi = vertices.begin();
vi != vertices.end();
++vi)
{
std::vector<AtomNodePtr>::const_iterator an;
for (an = nodes.begin(); an != nodes.end(); ++an)
{
if ((*an)->getId() == *vi)
break;
}
if (an != nodes.end())
{
newnodes.push_back(*an);
}
}
}
void BoostHCFDetector::findCycles(const std::vector<AtomNodePtr>& nodes,
std::vector<std::vector<AtomNodePtr> >& sccs)
{
HCFDetector::Edges edges;
makeEdges(nodes, edges);
//
// create a label table for the graphviz output
//
std::string nms[nodes.size()];
std::ostringstream oss;
PrintVisitor rpv(oss);
for (unsigned y = 0; y < nodes.size(); ++y)
{
oss.str("");
nodes[y]->getAtom()->accept(rpv);
std::string at(oss.str());
nms[y] = at.c_str();
}
using namespace boost;
{
typedef adjacency_list <vecS, vecS, directedS> Graph;
Graph G(0);
for (Edges::const_iterator ei = edges.begin();
ei != edges.end();
++ei)
{
add_edge((*ei).first, (*ei).second, G);
}
// boost::write_graphviz(std::cout, G);
std::vector<int> component(num_vertices(G));
int num = strong_components(G, &component[0]);
std::vector<AtomNodePtr> scc;
for (int cn = 0; cn < num; ++cn)
{
Vertices thiscomponent;
for (std::vector<int>::size_type i = 0; i != component.size(); ++i)
{
if (component[i] == cn)
thiscomponent.push_back(Vertex(i));
}
//
// only add components with more than one vertex:
//
if (thiscomponent.size() > 1)
{
//components.push_back(thiscomponent);
scc.clear();
selectNodes(thiscomponent, nodes, scc);
sccs.push_back(scc);
}
}
}
}
bool BoostHCFDetector::findHeadCycle(std::vector<AtomNodePtr>& cycle) const
{
std::vector<AtomNodePtr>::iterator n_it;
for(n_it = cycle.begin(); n_it!=cycle.end(); ++n_it)
{
for (std::set<Dependency>::const_iterator di = (*n_it)->getDisjunctivePreceding().begin();
di != (*n_it)->getDisjunctivePreceding().end();
++di)
{
if (find(cycle.begin(), cycle.end(), (*di).getAtomNode()) != cycle.end())
{
/*
AtomNodePtr first = *n_it;
AtomNodePtr second = (*di).getAtomNode();
std::cout<<"Found head cycle between "<<*(first->getAtom())<<
" and "<<*(second->getAtom())<<std::endl;
*/
return true;
}
}
}
return false;
}
//End