/
SymbolModifier.cpp
736 lines (632 loc) · 21.9 KB
/
SymbolModifier.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "stdafx.h"
using namespace CodeAtlas;
void CodeAtlas::WordCollector::modifyTree( )
{
collectWords();
//m_tree->print(SymbolInfo::Project | SymbolInfo::Class | SymbolInfo::Function, SymbolNodeAttr::ATTR_SYMWORD);
}
void CodeAtlas::WordCollector::updateDirtyData()
{
}
void CodeAtlas::WordCollector::collectWords()
{
SmartDepthIterator it(m_tree->getRoot(), SmartDepthIterator::POSTORDER, SymbolInfo::All);
for (SymbolNode::Ptr pNode; pNode = *it; ++it)
{
const UnderstandHandle& hdl = pNode->getDBHandle();
SymbolWordAttr::Ptr pAttr = pNode->getOrAddAttr<SymbolWordAttr>();
pAttr->clear();
pAttr->setWords(pNode);
// merge child words
for (SymbolNode::ChildIterator pChild = pNode->childBegin();
pChild != pNode->childEnd(); ++pChild)
{
SymbolWordAttr::Ptr pChildAttr = pChild.value()->getAttr<SymbolWordAttr>();
if (!pChildAttr)
continue;
pAttr->unite(*pChildAttr);
/*
if (pNode->getSymInfo().isClass() && pChild.value()->getSymInfo().isFunction())
{
pAttr->mergeNameWordWeightMap(pChildAttr->getNameWordWeightMap());
}
*/
}
pAttr->computeStatistics();
}
//m_tree->print(SymbolInfo::Folder, SymbolNodeAttr::ATTR_SYMWORD);
/*
const QSet<SymbolNode::Ptr>& dirtyProj = m_tree->getDirtyProject();
for (QSet<SymbolNode::Ptr>::ConstIterator pProj = dirtyProj.constBegin();
pProj != dirtyProj.constEnd(); ++pProj)
{
// find child nodes that is not a project
const SymbolNode::Ptr proj = *pProj;
QList<SymbolNode::Ptr> childList;
proj->findChild(
SymbolInfo::All &
~(SymbolInfo::Project) &
~(SymbolInfo::Unknown),
childList);
// no such nodes, so all the children are project nodes, ignore them
if (childList.size() == 0)
continue;
// add word attr to subtree of the dirty project
SymbolNode::SmartDepthIterator it(proj, SmartDepthIterator::POSTORDER);
for (SymbolNode::Ptr pNode; pNode = *it; ++it)
{
SymbolWordAttr::Ptr pAttr = pNode->getOrAddAttr<SymbolWordAttr>();
// project nodes has been processed in SymbolTreeBuilder
if (pNode->getSymInfo().elementType() == SymbolInfo::Project &&
pAttr->nWords() != 0)
continue;
pAttr->clear();
pAttr->setWords(pNode);
// merge child words
for (SymbolNode::ChildIterator pChild = pNode->childBegin();
pChild != pNode->childEnd(); ++pChild)
{
SymbolWordAttr::Ptr pChildAttr = pChild.value()->getAttr<SymbolWordAttr>();
if (!pChildAttr)
continue;
pAttr->unite(*pChildAttr);
}
pAttr->computeStatistics();
}
}*/
}
void CodeAtlas::WordCollector::buildDocTermMat( QList<SymbolNode::Ptr>& nodeList, SparseMatrix& docTermMat, Eigen::VectorXf& radiusVec )
{
int nSymbols = nodeList.size();
int nWords = SymbolWordAttr::totalWords();
docTermMat = SparseMatrix(nSymbols, nWords);
radiusVec.resize(nSymbols);
for (int ithNode = 0; ithNode < nodeList.size(); ++ithNode)
{
SymbolNode::Ptr& pNode = nodeList[ithNode];
SymbolWordAttr::Ptr pAttr = pNode->getAttr<SymbolWordAttr>();
if (!pAttr)
continue;
for (QMap<int, float>::ConstIterator pWord = pAttr->getWordWeightMap().constBegin();
pWord != pAttr->getWordWeightMap().constEnd(); ++pWord)
{
int wordID = pWord.key();
float wordCount = pWord.value();
docTermMat.insert(ithNode, wordID) = wordCount;
}
float r = qSqrt(1 + pAttr->getTotalWordWeight() * .1f);
radiusVec(ithNode) = r;
}
docTermMat.makeCompressed();
}
void CodeAtlas::FuzzyDependBuilder::modifyTree( )
{
const QSet<SymbolNode::Ptr>& dirtyProj = m_tree->getDirtyProject();
for (QSet<SymbolNode::Ptr>::ConstIterator pProj = dirtyProj.constBegin();
pProj != dirtyProj.constEnd(); ++pProj)
{
buildProjDepend(*pProj);
}
// m_tree->print(SymbolInfo::All & ~SymbolInfo::Block,
// SymNodeAttr::ATTR_FUZZYDEPEND,
// SymbolEdge::EDGE_FUZZY_DEPEND);
}
QString CodeAtlas::FuzzyDependBuilder::collectFuzzyDepend( SymbolNode::Ptr& node )
{
// ignore dependency under a function
SymbolInfo nodeInfo = node->getSymInfo();
if (nodeInfo.elementType() & SymbolInfo::Variable)
return nodeInfo.type() + " " + nodeInfo.name();
QString nodeCode;
CodeAttr::Ptr pCode = node->getAttr<CodeAttr>();
if (pCode)
nodeCode = pCode->getCode();
if (nodeInfo.elementType() & SymbolInfo::Function)
return nodeCode;
QMultiHash<QString, ChildPack> childList;
int ithChild = 0;
bool* isNonVar = new bool[node->childCount()];
for (SymbolNode::ChildIterator pChild = node->childBegin();
pChild != node->childEnd(); ++pChild, ++ithChild)
{
ChildPack p;
p.m_info = pChild.key();
p.m_code = collectFuzzyDepend(pChild.value());
p.m_pNode = pChild.value();
p.m_index = ithChild;
childList.insertMulti(p.m_info.name(), p);
isNonVar[ithChild] = (p.m_info.elementType() & SymbolInfo::Variable) == 0;
nodeCode += "\n" + p.m_code;
}
FuzzyDependAttr::Ptr pAttr = node->getOrAddAttr<FuzzyDependAttr>();
buildChildDepend(childList, pAttr->vtxEdgeMatrix(), pAttr->edgeWeightVector(), pAttr->dependPairList());
buildSubGraphLevel(pAttr->vtxEdgeMatrix(), isNonVar, ithChild, pAttr->levelVector());
delete[] isNonVar;
return nodeCode;
}
void CodeAtlas::FuzzyDependBuilder::buildSubGraphLevel(const SparseMatrix& veMat, const bool* const validMask, int nNodes, VectorXi& level)
{
SparseMatrix subMat; VectorXi subLevel;
if(!GraphUtility::getSubGraph(veMat, validMask, subMat))
{
level.setConstant(nNodes, GraphUtility::s_defaultBaseLevel);
return;
}
GraphUtility::computeVtxLevel(subMat, subLevel, GraphUtility::s_defaultBaseLevel);
level.resize(nNodes);
for (int i = 0, ithNon = 0; i < nNodes; ++i)
{
if (!validMask[i])
level[i] = GraphUtility::s_defaultBaseLevel;
else
level[i] = subLevel[ithNon++];
}
}
void CodeAtlas::FuzzyDependBuilder::buildChildDepend( QMultiHash<QString, ChildPack>& childList ,
Eigen::SparseMatrix<double>& vtxEdgeMat,
Eigen::VectorXd& edgeWeightVec,
QList<FuzzyDependAttr::DependPair>& dependPair)
{
QStringList codeList;
QVector<ChildPack*> childPackPtr;
for (QMultiHash<QString, ChildPack>::Iterator pChild = childList.begin();
pChild != childList.end(); ++pChild)
{
codeList.push_back(pChild.value().m_code);
childPackPtr.push_back(&pChild.value());
}
std::vector<Triplet> tripletArray;
QVector<double> edgeWeightArray;
// add dependency edges between child nodes
int ithSrc = 0;
for (QMultiHash<QString, ChildPack>::Iterator pChild = childList.begin();
pChild != childList.end(); ++pChild, ++ithSrc)
{
// for each child, find number of occurrences of this child's name
ChildPack& srcChild = pChild.value();
const QString& srcName = pChild.key();
QVector<int> occurence;
WordExtractor::findOccurrence(srcName, codeList, occurence);
for (int ithTar = 0; ithTar < childPackPtr.size(); ++ithTar)
{
int nOccur = occurence[ithTar];
if (nOccur == 0 || ithTar == ithSrc)
continue;
ChildPack& tarChild = *childPackPtr[ithTar];
SymbolEdge::Ptr pEdge = SymbolTree::getOrAddEdge(srcChild.m_pNode, tarChild.m_pNode, SymbolEdge::EDGE_FUZZY_DEPEND);
pEdge->clear();
pEdge->strength() = nOccur;
int nEdge = tripletArray.size()/2;
tripletArray.push_back(Triplet(srcChild.m_index, nEdge ,1.0));
tripletArray.push_back(Triplet(tarChild.m_index, nEdge ,-1.0));
edgeWeightArray.push_back(nOccur);
dependPair.push_back(FuzzyDependAttr::DependPair(srcChild.m_pNode, tarChild.m_pNode, nOccur));
}
}
if (int nEdges = tripletArray.size()/2)
{
vtxEdgeMat.resize(childList.size(),nEdges);
vtxEdgeMat.setFromTriplets(tripletArray.begin(), tripletArray.end());
edgeWeightVec.resize(nEdges);
memcpy(edgeWeightVec.data(), edgeWeightArray.data(), sizeof(double)* nEdges);
}
}
void CodeAtlas::FuzzyDependBuilder::buildProjDepend( const SymbolNode::Ptr& proj )
{
QList<SymbolNode::Ptr> globalSymList;
SymbolTree::getGlobalSymbols(proj, globalSymList);
ProjectAttr::Ptr projAttr = proj->getOrAddAttr<ProjectAttr>();
projAttr->setGlobalSymList(globalSymList);
QMultiHash<QString, ChildPack> childList;
bool* isNonVar = new bool[globalSymList.size()];
for (int ithGloSym = 0; ithGloSym < globalSymList.size(); ++ithGloSym)
{
ChildPack p;
SymbolNode::Ptr pSym = globalSymList[ithGloSym];
pSym->getOrAddAttr<GlobalSymAttr>();
p.m_info = pSym->getSymInfo();
p.m_code = collectFuzzyDepend(pSym);
p.m_pNode = pSym;
p.m_index = ithGloSym;
childList.insertMulti(p.m_info.name(), p);
isNonVar[ithGloSym] = (p.m_info.elementType() & SymbolInfo::Variable) == 0;
}
FuzzyDependAttr::Ptr pAttr = proj->getOrAddAttr<FuzzyDependAttr>();
buildChildDepend(childList,pAttr->vtxEdgeMatrix(), pAttr->edgeWeightVector(), pAttr->dependPairList());
buildSubGraphLevel(pAttr->vtxEdgeMatrix(), isNonVar, globalSymList.size(), pAttr->levelVector());
delete[] isNonVar;
}
void CodeAtlas::SymbolModifier::setBuilder( SymbolTreeBuilder* builder )
{
m_builder = builder;
}
void CodeAtlas::TopicLODMaker::modifyTree()
{
typedef SymbolWordAttr::WordMap WordMap;
QList<WordMap> inputWordList;
QRectF sceneRect = UIManager::instance()->getScene().sceneRect();
float maxRange = max(sceneRect.width(), sceneRect.height());
int standardViewRange = 300; // 300 * 300 screen
float minLod = standardViewRange / maxRange;
float avgSize = 0;
// use tfidf to compute priority
LodMerger lodMerger;
int ithNode = 0;
SymbolTree* tree = m_tree;
LodAttr::Ptr lodAttr = tree->getRoot()->getOrAddAttr<LodAttr>();
QList<SymbolNode::Ptr> nodeList;
SmartDepthIterator it(tree->getRoot(), SmartDepthIterator::PREORDER, SymbolInfo::ClassStruct|SymbolInfo::FunctionSignalSlot);
for (SymbolNode::Ptr node; node = *it; ++it, ++ithNode)
{
SymbolInfo info = node->getSymInfo();
if (info.elementType() & SymbolInfo::Folder)
continue;
SymbolWordAttr::Ptr wordAttr = node->getAttr<SymbolWordAttr>();
if (wordAttr.isNull())continue;
const WordMap& wordWeightMap = wordAttr->getNameWordWeightMap();
if (!wordWeightMap.size())continue;
UIElementAttr::Ptr uiAttr = node->getAttr<UIElementAttr>();
if (uiAttr.isNull())continue;
NodeUIItem::Ptr uiItem = uiAttr->getUIItem();
if (uiItem.isNull())continue;
QRectF rectInScene = uiItem->mapToScene(uiItem->entityRect()).boundingRect();
lodMerger.addItem(rectInScene.center(), rectInScene.width()* 0.5f);
inputWordList.push_back(wordWeightMap);
WordMap::iterator beg = inputWordList.back().begin();
WordMap::iterator end = inputWordList.back().end();
for (WordMap::iterator pW = beg; pW != end; ++pW)
{
*pW *= (rectInScene.width() * rectInScene.height());
}
nodeList.push_back(node);
avgSize += rectInScene.width();
if (info.isTopLevel())
{
it.skipSubTree();
}
}
if (!inputWordList.size())
return;
avgSize /= inputWordList.size();
float maxLod = max(40 / avgSize, minLod);
printf("%d symbols to perform LOD filter\n", inputWordList.size());
float viewSize[2] = {55,30};
for (float lod = maxLod; lod >= minLod; lod /= 2.5f)
{
// cluster nodes
lodMerger.setMinMergeSize(QSizeF(viewSize[0] / lod, viewSize[1] / lod));
lodMerger.compute();
// collect words
int nCluster = lodMerger.getClusterNum();
if (!nCluster)
return;
typedef SymbolWordAttr::WordMap WordMap;
QVector<WordMap> clusterWordList(nCluster);
for (int i = 0; i < lodMerger.getInputItemNum(); ++i)
{
int clusterID = lodMerger.getClusterID(i);
const WordMap& wordWeightMap = inputWordList[i];
SymbolWordAttr::insertWords(clusterWordList[clusterID], wordWeightMap);
}
// compute tfidf
TFIDFMaker tfidfMaker;
for (int i = 0; i < clusterWordList.size(); ++i)
{
WordMap::Iterator pW;
for (pW = clusterWordList[i].begin(); pW != clusterWordList[i].end(); ++pW)
{
tfidfMaker.addWord(i, pW.key(), pW.value());
}
}
tfidfMaker.computeTFIDF();
// extract words from each cluster
typedef LodAttr::WordCloudItem WordCloudItem;
WordCloudItem wcItem;
QList<WordCloudItem> txtItemList;
float minP = FLT_MAX, maxP = 0;
for (int ithCluster = 0; ithCluster < nCluster; ++ithCluster)
{
const QRectF& rect = lodMerger.getClusterRect(ithCluster);
float topLeft[2] = {rect.center().x(), rect.center().y()};
float dim[2] = {rect.width()*0.5f, rect.height()*0.5f};
WordMap& wordMap = clusterWordList[ithCluster];
// find best word for each cluster
WordMap::Iterator pBestWord;
float maxPriority = -FLT_MAX;
for (WordMap::Iterator pW = wordMap.begin(); pW!= wordMap.end(); ++pW)
{
int wordID = pW.key();
float priority = tfidfMaker.getTFIDF(ithCluster, wordID);
if (priority > maxPriority)
{
maxPriority = priority;
pBestWord = pW;
}
}
if (maxPriority == -FLT_MAX)
continue;
// record that word
wcItem.m_clusterID = ithCluster;
int wordID = pBestWord.key();
float visualPriority = sqrt(rect.width() * rect.width() + rect.height() * rect.height());
wcItem.m_txt = SymbolWordAttr::getWord(wordID);
wcItem.m_priority = tfidfMaker.getTFIDF(ithCluster, wordID) * (visualPriority) ;
minP = min(minP, wcItem.m_priority);
maxP = max(maxP, wcItem.m_priority);
float frag[2] = {0.5,0.5};//{rand()/float(RAND_MAX)*2.f-1.f, rand()/float(RAND_MAX)*2.f-1.f};
wcItem.m_pos.rx() = topLeft[0] + frag[0] * dim[0] * 0.5f;
wcItem.m_pos.ry() = topLeft[1] + frag[1] * dim[1] * 0.5f;
txtItemList.push_back(wcItem);
}
int nTxtItem = txtItemList.size();
// compute view size
float minFontSize = 12, maxFontSize = 25, interval = 4;
QFont font("Tahoma", 7, QFont::Light);
for (int i = 0; i < nTxtItem; ++i)
{
WordCloudItem &item = txtItemList[i];
float fOffset = (maxFontSize-minFontSize)*sqrt((item.m_priority-minP)/(maxP-minP+1e-3));
int iOffset = int(fOffset / interval) * interval;
int fs = minFontSize + iOffset;
item.m_fontSize = fs;
//printf("pointsize: %d maxSize = %f min %f p%f\n", fs, maxP, minP, item.m_priority);
font.setPointSize(item.m_fontSize);
QFontMetrics fm = QFontMetrics(font);
item.m_viewSize = fm.size(0, item.m_txt);
}
lodAttr->addLodWordCloud(lod, txtItemList);
printf("lod = %f words = %d\n", lod, txtItemList.size());
// prepare next input
QList<QRectF> clusterRectList;
for (int i = 0; i < nCluster; ++i)
{
clusterRectList.push_back(lodMerger.getClusterRect(i));
}
inputWordList = clusterWordList.toList();
lodMerger.clearAll();
lodMerger.addItem(clusterRectList);
}
}
void CodeAtlas::GeneralDependBuilder::buildDependPath()
{
QList<DBDependData> dpData;
buildDependData(dpData);
QList<DependPath>& dependPath = getDependPath();
dependPath.clear();
for (int i = 0; i < dpData.size(); ++i)
{
DBDependData& dp = dpData[i];
SymbolPath srcPath, tarPath;
if(!m_builder->getSymbolPath(dp.getSrc(), srcPath) ||
!m_builder->getSymbolPath(dp.getTar(), tarPath))
continue;
dependPath.push_back(DependPath(srcPath, tarPath, dp.getType()));
}
}
bool CodeAtlas::GeneralDependBuilder::buildDependData(QList<DBDependData>& dependData)
{
dependData.clear();
SmartDepthIterator it(m_tree->getRoot(), SmartDepthIterator::PREORDER, SymbolInfo::All);
for (SymbolNode::Ptr node; node = *it; ++it)
{
QList<UnderstandHandle> dependList;
QList<unsigned> refTypeList;
const UnderstandHandle& hdl = node->getDBHandle();
m_builder->getDatabase()->findDepends(hdl, dependList, refTypeList);
for (int i = 0; i < dependList.size(); ++i)
{
dependData.push_back(DBDependData(hdl, dependList[i], refTypeList[i]));
}
}
return true;
}
void CodeAtlas::GeneralDependBuilder::modifyTree()
{
buildDependPath();
QList<DependPath>& dependPath = getDependPath();
SymbolPath path;
QList<int> dependList;
for (int i = 0; i < dependPath.size(); ++i)
{
dependList.append(i);
}
buildNodeDepend(m_tree->getRoot(), path, dependList);
}
void CodeAtlas::GeneralDependBuilder::clearCache()
{
getDependPath().clear();
}
void CodeAtlas::GeneralDependBuilder::buildNodeDepend( SymbolNode::Ptr node, SymbolPath& nodePath, QList<int>& dependIDList )
{
int depth = nodePath.getSymbolCount();
QList<DependPath>& dependPathList = getDependPath();
QHash<SymbolInfo, int> childIDMap;
QList<SymbolNode::Ptr> childList;
bool* isNonVar = new bool[node->childCount()+1];
for (SymbolNode::ChildIterator pChild = node->childBegin();
pChild != node->childEnd(); ++pChild)
{
int ithChild = childIDMap.size();
childIDMap[pChild.key()] = ithChild;
childList.push_back(pChild.value());
isNonVar[ithChild] = (pChild.key().elementType() & SymbolInfo::Variable) == 0;
}
// collect depends
struct EdgeAttribute
{
int m_nEdge;
unsigned m_flag;
};
QHash<Pair, EdgeAttribute> edgeMap;
for (int i = 0; i < dependIDList.size(); ++i)
{
int id = dependIDList[i];
DependPath& dependPath = dependPathList[id];
SymbolPath& srcPath = dependPath.m_src;
SymbolPath& tarPath = dependPath.m_tar;
const SymbolInfo* srcInfo = srcPath.getSymbol(depth);
const SymbolInfo* tarInfo = tarPath.getSymbol(depth);
if (!srcInfo || !tarInfo)
continue;
QHash<SymbolInfo, int>::iterator pSrc = childIDMap.find(*srcInfo);
QHash<SymbolInfo, int>::iterator pTar = childIDMap.find(*tarInfo);
if (pSrc == childIDMap.end() || pTar == childIDMap.end() || pSrc.value() == pTar.value())
continue;
Pair idPair(pSrc.value(), pTar.value());
QHash<Pair, EdgeAttribute>::iterator pEdge = edgeMap.find(idPair);
if (pEdge == edgeMap.end())
{
EdgeAttribute ea;
ea.m_nEdge = 1;
ea.m_flag = dependPath.m_type;
edgeMap[idPair] = ea;
}
else
{
EdgeAttribute& ea = pEdge.value();
ea.m_nEdge++;
ea.m_flag |= dependPath.m_type;
}
}
std::vector<Triplet<double>> tripletArray;
vector<double> edgeWeightArray;
QList<FuzzyDependAttr::DependPair> dependPairList;
for (QHash<Pair, EdgeAttribute>::iterator pPair = edgeMap.begin();
pPair != edgeMap.end(); ++pPair)
{
int src = pPair.key().m_src;
int tar = pPair.key().m_tar;
int val = pPair.value().m_nEdge;
unsigned flag = pPair.value().m_flag;
int nEdge= tripletArray.size()/2;
tripletArray.push_back(Eigen::Triplet<double>(src, nEdge ,1.0));
tripletArray.push_back(Eigen::Triplet<double>(tar, nEdge ,-1.0));
edgeWeightArray.push_back(val);
SymbolNode::Ptr srcNode = childList[src];
SymbolNode::Ptr tarNode = childList[tar];
SymbolEdge::Ptr pEdge = SymbolTree::getOrAddEdge(srcNode, tarNode, SymbolEdge::EDGE_FUZZY_DEPEND);
pEdge->clear();
pEdge->strength() = val;
pEdge->flag() = flag;
dependPairList.push_back(FuzzyDependAttr::DependPair(srcNode, tarNode, val));
}
// add attr
FuzzyDependAttr::Ptr pAttr = node->getOrAddAttr<FuzzyDependAttr>();
Eigen::SparseMatrix<double>& vtxEdgeMat = pAttr->vtxEdgeMatrix();
Eigen::VectorXd& edgeWeightVec = pAttr->edgeWeightVector();
int nEdges = tripletArray.size()/2;
if (nEdges)
{
vtxEdgeMat.resize(childIDMap.size(), nEdges);
vtxEdgeMat.setFromTriplets(tripletArray.begin(), tripletArray.end());
edgeWeightVec.resize(nEdges);
memcpy(edgeWeightVec.data(), &edgeWeightArray[0], sizeof(double)* nEdges);
GraphUtility::computeSubGraphVtxLevel(vtxEdgeMat, isNonVar, node->childCount(), pAttr->levelVector());
pAttr->dependPairList() = dependPairList;
}
delete[] isNonVar;
// analysis module
CutModularizer cm(0.8);
cm.addNodes(childList);
cm.setDependency(vtxEdgeMat, edgeWeightVec);
cm.compute();
for (int i = 0; i < childList.size(); ++i)
{
UIElementAttr::Ptr uiAttr = childList[i]->getOrAddAttr<UIElementAttr>();
uiAttr->clusterID() = cm.getComponent(i);
}
// get child
for (SymbolNode::ChildIterator pChild = node->childBegin();
pChild != node->childEnd(); ++pChild)
{
const SymbolInfo& childInfo = pChild.key();
nodePath.addChildSymbol(childInfo);
QList<int> childDependIDList;
for (int ithDepend = 0; ithDepend < dependIDList.size(); ++ithDepend)
{
int id = dependIDList[ithDepend];
DependPath& dependPath = dependPathList[id];
const SymbolInfo* srcInfo = dependPath.m_src.getSymbol(depth);
const SymbolInfo* tarInfo = dependPath.m_tar.getSymbol(depth);
if (!srcInfo || !tarInfo)
{
continue;
}
if (*srcInfo != childInfo || *tarInfo != childInfo)
{
continue;
}
childDependIDList.push_back(id);
}
buildNodeDepend(pChild.value(), nodePath, childDependIDList);
nodePath.removeChildSymbol();
}
}
QList<GeneralDependBuilder::DependPath>& CodeAtlas::GeneralDependBuilder::getDependPath()
{
DependRawDataAttr::Ptr depRaw = m_tree->getRoot()->getOrAddAttr<DependRawDataAttr>();
return depRaw->getDependPath();
}
void CodeAtlas::Modulizer::randomModularize()
{
// collect global symbols
typedef QHash<SymbolPath, int> PathMap;
PathMap pathMap;
SmartDepthIterator it(m_tree->getRoot(), SmartDepthIterator::PREORDER, SymbolInfo::All, SymbolInfo::All&~SymbolInfo::Block);
for (SymbolNode::Ptr pNode; pNode = *it; ++it)
{
SymbolInfo info = pNode->getSymInfo();
if (!info.isTopLevel())
continue;
int pathCount = pathMap.size();
pathMap[pNode->getSymPath()] = pathCount;
}
RandomWalkClusterer randClusterer(1000,1000000);
randClusterer.setVtxCount(pathMap.size());
DependRawDataAttr::Ptr depAttr = m_tree->getRoot()->getAttr<DependRawDataAttr>();
typedef DependRawDataAttr::DependPath DP;
QList<DP>& dpList = depAttr->getDependPath();
for (int i = 0; i < dpList.size(); ++i)
{
DP& dp = dpList[i];
PathMap::Iterator pSrc = pathMap.find(dp.m_src.getTopLevelItemPath());
PathMap::Iterator pTar = pathMap.find(dp.m_tar.getTopLevelItemPath());
if (pSrc == pathMap.end() || pTar == pathMap.end())
{
continue;
}
int srcID = pSrc.value();
int tarID = pTar.value();
if (srcID == tarID)
{
continue;
}
float w=0.5;
// if (dp.m_type & Ref_Call)
// {
// w = 1;
// }
// else if (dp.m_type & Ref_Base)
// {
// w = 5;
// }
if (dp.m_type & (Ref_Use|Ref_Modify))
{
w = 0.5;
}
else
{
continue;
}
randClusterer.addUndirectedEdge(srcID, tarID, w);
}
randClusterer.compute();
for (PathMap::Iterator pP = pathMap.begin(); pP != pathMap.end(); ++pP)
{
SymbolNode::Ptr node = m_tree->findItem(pP.key());
int clusterID = randClusterer.getClusterID(pP.value());
qDebug() << node->getSymInfo().name() << " \t cID:" << clusterID << endl;
UIElementAttr::Ptr uiAttr = node->getOrAddAttr<UIElementAttr>();
uiAttr->clusterID() = clusterID;
}
}