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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 | #include "meshkit/MKGraph.hpp"
#include "meshkit/MeshOp.hpp"
#include "lemon/core.h"
#include "lemon/adaptors.h"
#include "lemon/connectivity.h"
#include "lemon/math.h"
#include "lemon/graph_to_eps.h"
namespace MeshKit
{
MKGraph::MKGraph()
: mkGraph(), rootNode(NULL), leafNode(NULL), nodeMap(mkGraph, NULL)
{
}
MKGraph::~MKGraph()
{
}
void MKGraph::clear_graph()
{
// get all the non-root, non-leaf nodes
std::vector<lemon::ListDigraph::Node> nodes;
for (lemon::ListDigraph::NodeIt nit(mkGraph); nit != lemon::INVALID; ++nit)
if (nit != rootNode->get_node() && nit != leafNode->get_node())
nodes.push_back(nit);
// now delete all those nodes
// lemon will automatically delete all the edges connected to these nodes
for (std::vector<lemon::ListDigraph::Node>::iterator vit = nodes.begin(); vit != nodes.end(); vit++) {<--- Prefer prefix ++/-- operators for non-primitive types.
if (nodeMap[*vit]) delete nodeMap[*vit];
else mkGraph.erase(*vit);
}
// restore an edge between the root and leaf !! comment this out
// we are not creating it anymore in the constructor of MKCore
/*if (!mkGraph.valid(lemon::ArcLookUp<lemon::ListDigraph>(mkGraph)(rootNode->get_node(), leafNode->get_node())))
mkGraph.addArc(rootNode->get_node(), leafNode->get_node());*/
}
void MKGraph::print_graph(const char * filename)
{
for (lemon::ListDigraph::NodeIt nit(mkGraph); nit != lemon::INVALID; ++nit) {
std::cout << "Node: " << mkGraph.id(nit) << " ";
if (nit == rootNode->get_node()) std::cout << "root node" << std::endl;
else if (nit == leafNode->get_node()) std::cout << "leaf node" << std::endl;
else if (nodeMap[nit]) std::cout << nodeMap[nit]->get_name() << std::endl;
else std::cout << "(no MeshOp)" << std::endl;
}
for (lemon::ListDigraph::ArcIt ait(mkGraph); ait != lemon::INVALID; ++ait) {
//lemon::ListDigraph::A
lemon::ListDigraph::Node s=mkGraph.source(ait);
lemon::ListDigraph::Node t=mkGraph.target(ait);
std::cout << "Arc: "<< mkGraph.id(ait)<< " : "<< nodeMap[s]->get_name() << "(" << mkGraph.id(s)<< ") - " <<
nodeMap[t]->get_name() << "(" << mkGraph.id(t)<< ")\n";
}
typedef lemon::dim2::Point<int> Point;
lemon::ListDigraph::NodeMap< Point > coords(mkGraph);
lemon::Bfs<lemon::ListDigraph> bfs(mkGraph);
bfs.init();
bfs.addSource(rootNode->get_node());
lemon::ListDigraph::NodeMap<std::string> label(mkGraph);
while (!bfs.emptyQueue()) {
lemon::ListDigraph::Node nd = bfs.processNextNode();
//assert(nd != lemon::INVALID && (nodeMap[nd] || (nd == leafNode->get_node() || nd == rootNode->get_node())));
std::cout << "BFS_Node, distance: " << bfs.dist(nd) << "\n ";
coords[nd]=Point(10*bfs.dist(nd), 10*mkGraph.id(nd));
if (nd == rootNode->get_node())
label[nd]="Root";
else
{
if (nd == leafNode->get_node())
label[nd] = "Leaf";
else
label[nd] = nodeMap[nd]->get_name();
}
}
std::string filen;
if (filename)
filen=std::string(filename);
else
filen = "graph.eps";
graphToEps(mkGraph, filen).coords(coords).nodeTexts(label).
nodeTextSize(3).drawArrows().run();
}
void MKGraph::print_bfs_graph() <--- The function 'print_bfs_graph' is never used.
{
lemon::Bfs<lemon::ListDigraph> bfs(mkGraph);
bfs.init();
bfs.addSource(rootNode->get_node());
while (!bfs.emptyQueue()) {
lemon::ListDigraph::Node nd = bfs.processNextNode();
assert(nd != lemon::INVALID && (nodeMap[nd] || (nd == leafNode->get_node() || nd == rootNode->get_node())));
std::cout << "BFS_Node, distance: " << bfs.dist(nd) << ", ";
if (nd == rootNode->get_node()) std::cout << "root node" << std::endl;
else if (nd == leafNode->get_node()) std::cout << "leaf node" << std::endl;
else if (nodeMap[nd]) std::cout << nodeMap[nd]->get_name() << std::endl;
else std::cout << "(no MeshOp)" << std::endl;
}
}
//! Get the MeshOp corresponding to a graph node
MeshOp *MKGraph::get_meshop(lemon::ListDigraph::Node node) const
{
return dynamic_cast<MeshOp*>(nodeMap[node]);
}
GraphNode *MKGraph::other_node(lemon::ListDigraph::Arc arc, GraphNode *node) const
{
lemon::ListDigraph::Node src = mkGraph.source(arc);
if (src != node->get_node()) return get_node(src);
else return get_node(mkGraph.target(arc));
}
MeshOp *MKGraph::find_meshop(std::string op_name) const<--- The function 'find_meshop' is never used.
{
GraphNode *node = find_node(op_name);
return node ? dynamic_cast<MeshOp*>(node) : 0;
}
GraphNode *MKGraph::find_node(std::string op_name) const
{
// run BFS on forward graph
lemon::Bfs<lemon::ListDigraph> bfs(mkGraph);
bfs.init();
bfs.addSource(rootNode->get_node());
while (!bfs.emptyQueue()) {
lemon::ListDigraph::Node nd = bfs.processNextNode();
assert(nd != lemon::INVALID && (nodeMap[nd] || (nd == leafNode->get_node() || nd == rootNode->get_node())));
if (nodeMap[nd] && nodeMap[nd]->get_name() == op_name) return nodeMap[nd];
}
return NULL;
}
void MKGraph::insert_node(GraphNode *inserted, GraphNode *before, GraphNode *after)
{
// if inserted is a leaf node (i.e. is connected to leafNode), disconnect from that
if (mkGraph.target(inserted->out_arcs()) == leafNode->get_node())
mkGraph.erase(inserted->out_arcs());
// if inserted is a root node (i.e. is connected to rootNode), also disconnect that
if (mkGraph.source(inserted->in_arcs()) == rootNode->get_node())
mkGraph.erase(inserted->in_arcs());
lemon::ListDigraph::InArcIt iter, jter;
if (after != NULL) { // if after is specified
lemon::ListDigraph::Node after_node = after->get_node();
// check if it is already connected
bool b_connected = false;
for (iter = inserted->in_arcs(); iter != lemon::INVALID; ++iter) {
if (mkGraph.source(iter) == after_node) {
b_connected = true;
break;
}
}
if (!b_connected) mkGraph.addArc(after_node, inserted->get_node()); // add a new arc
// remove the arc from after node
for (iter = before->in_arcs(); iter != lemon::INVALID; ++iter) {
if (mkGraph.source(iter) == after_node) {
mkGraph.erase(iter);
break;
}
}
}
else { // check all predecessors
for (iter = before->in_arcs(); iter != lemon::INVALID;) {
lemon::ListDigraph::Node after_node = mkGraph.source(iter);
// check if it is already connected
bool b_connected = false;
for (jter = inserted->in_arcs(); jter != lemon::INVALID; ++jter) {
if (mkGraph.source(jter) == after_node) {
b_connected = true;
break;
}
}
if (!b_connected) mkGraph.addArc(after_node, inserted->get_node()); // add a new arc
jter = iter;
++iter;
mkGraph.erase(jter); // remove the arc from after node
}
}
// now link inserted to before
mkGraph.addArc(inserted->get_node(), before->get_node());
// if before is a root node (i.e. is connected to rootNode), also disconnect that
if (mkGraph.source(before->in_arcs()) == rootNode->get_node())
mkGraph.erase(before->in_arcs());
}
void MKGraph::add_arc(GraphNode *source, GraphNode *target)
{
// add an arc from one node to another, e.g. to add a dependency between them
// get the corresponding Lemon nodes
lemon::ListDigraph::Node lsource = source->get_node(),
ltarget = target->get_node();
// if inserted is a leaf node (i.e. is connected to leafNode), disconnect from that
if (mkGraph.target(source->out_arcs()) == leafNode->get_node())
mkGraph.erase(source->out_arcs());
// if before is a root node (i.e. is connected to rootNode), also disconnect that
if (mkGraph.source(target->in_arcs()) == rootNode->get_node())
mkGraph.erase(target->in_arcs());
// now link them
mkGraph.addArc(lsource, ltarget);
}
//! Run setup on the graph
void MKGraph::setup(bool reset)
{
// run BFS on reversed graph
lemon::ReverseDigraph<lemon::ListDigraph> rg(mkGraph);
if (reset)
{
lemon::Bfs<lemon::ReverseDigraph<lemon::ListDigraph> > rbfs1(rg);
rbfs1.init();
rbfs1.addSource(leafNode->get_node());
while (!rbfs1.emptyQueue()) {
lemon::ListDigraph::Node nd = rbfs1.processNextNode();
assert(nd != lemon::INVALID && (nodeMap[nd] || (nd == leafNode->get_node() || nd == rootNode->get_node())));
if (nodeMap[nd]) nodeMap[nd]->setup_called(false);
}
}
bool called_one;
do {
called_one = false;
lemon::Bfs<lemon::ReverseDigraph<lemon::ListDigraph> > rbfs2(rg);
rbfs2.init();
rbfs2.addSource(leafNode->get_node());
while (!rbfs2.emptyQueue()) {
lemon::ListDigraph::Node nd = rbfs2.processNextNode();
assert(nd != lemon::INVALID && (nodeMap[nd] || (nd == leafNode->get_node() || nd == rootNode->get_node())));
if (nodeMap[nd] && !nodeMap[nd]->setup_called()) {
nodeMap[nd]->setup_this();
nodeMap[nd]->setup_called(true);
called_one = true;
}
}
}
while (called_one);
}
//! Run execute on the graph
void MKGraph::execute()
{
typedef lemon::IterableIntMap<lemon::ListDigraph, lemon::ListDigraph::Node> topomap;
// Run execute_this on all nodes in topological order
topomap topo_levels( mkGraph );
lemon::topologicalSort( mkGraph, topo_levels );
for( int i = 0; i<topo_levels.size(); ++i){
for( topomap::ItemIt j(topo_levels, i); j != lemon::INVALID; ++j ){
GraphNode* gn = nodeMap[ j ];
assert( gn );
gn->execute_called(false);
}
}
for( int i = 0; i<topo_levels.size(); ++i){
for( topomap::ItemIt j(topo_levels, i); j != lemon::INVALID; ++j ){
GraphNode* gn = nodeMap[ j ];
assert( gn );
if (!gn->execute_called()) {
gn->execute_this();
gn->execute_called(true);
}
}
}
}
// run execute on all nodes before this node (it may be a second run)
void MKGraph::execute_before(GraphNode * upto)
{
typedef lemon::IterableIntMap<lemon::ListDigraph, lemon::ListDigraph::Node> topomap;
// Run execute_this on all nodes in topological order
topomap topo_levels( mkGraph );
int upToId = mkGraph.id(upto->get_node());
lemon::topologicalSort( mkGraph, topo_levels );
for( int i = 0; i<topo_levels.size(); ++i){
for( topomap::ItemIt j(topo_levels, i); j != lemon::INVALID; ++j ){
GraphNode* gn = nodeMap[ j ];
assert( gn );
if (mkGraph.id(gn->get_node())==upToId)
return; // stop when we reached our node, do not execute again
if (!gn->execute_called()) {
gn->execute_this();
gn->execute_called(true);
}
}
}
return;
}
} // namespace MeshKit
|