]> AND Private Git Repository - blast.git/blob - Graph.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
added generator-cst
[blast.git] / Graph.cpp
1 #include "Graph.h"
2 #include "GroupBlock.h"
3 #include "ReferenceBlock.h"
4 #include "FunctionalBlock.h"
5
6 Graph::Graph() {
7   topGroup = new GroupBlock(NULL);
8   topGroup->setName("top group");
9   groups.append(topGroup);
10 }
11
12 Graph::~Graph() {
13   
14   delete topGroup;
15 }
16
17 QList<AbstractInterface *> Graph::getOutsideInterfaces() {
18   return topGroup->getInterfaces();
19 }
20
21 GroupBlock* Graph::createChildGroupBlock(GroupBlock* parent) {
22   GroupBlock* b = new GroupBlock(parent);
23   groups.append(b);
24   return b;
25 }
26
27 bool Graph::removeGroupBlock(GroupBlock *group) {
28   group->removeAllBlocks();
29   GroupBlock* parent = AB_TO_GRP(group->getParent());
30   parent->removeBlock(group);
31   groups.removeAll(group);
32 }
33
34 GroupBlock* Graph::getGroupBlockByName(QString name) {
35   foreach(GroupBlock* group, groups) {
36     if (group->getName() == name) return group;
37   }
38   return NULL;
39 }
40
41 FunctionalBlock* Graph::createFunctionalBlock(GroupBlock* group, ReferenceBlock* ref) {
42
43   FunctionalBlock* newBlock = new FunctionalBlock(group,ref);
44   newBlock->populate();
45   group->addBlock(newBlock);
46
47   return newBlock;
48 }
49
50 FunctionalBlock* Graph::duplicateFunctionalBlock(FunctionalBlock *block) {
51
52   ReferenceBlock* ref = block->getReference();
53   GroupBlock* group = AB_TO_GRP(block->getParent());
54
55   // adding to the graph
56   FunctionalBlock* newBlock = createFunctionalBlock(group,ref);
57   return newBlock;
58 }
59
60
61 bool Graph::removeFunctionalBlock(FunctionalBlock* block) {
62   GroupBlock* group = AB_TO_GRP(block->getParent());
63   group->removeBlock(block);
64   return true;
65 }
66
67 FunctionalBlock* Graph::getFunctionalBlockByName(QString name, GroupBlock* parent) {
68   FunctionalBlock* block = NULL;
69   if (parent != NULL) {
70     block = AB_TO_FUN(parent->getFunctionalBlockByName(name));
71   }
72   else {
73     foreach(GroupBlock* group, groups) {
74       block = AB_TO_FUN(group->getFunctionalBlockByName(name));
75       if (block != NULL) return block;
76     }
77   }
78   return block;
79 }
80
81 FunctionalBlock* Graph::createSourceBlock(ReferenceBlock* ref) {
82
83   FunctionalBlock* newBlock = new FunctionalBlock(NULL,ref);
84   newBlock->populate();
85   sources.append(newBlock);
86   return newBlock;
87 }
88
89 FunctionalBlock* Graph::duplicateSourceBlock(FunctionalBlock *block) {
90
91   ReferenceBlock* ref = block->getReference();
92   GroupBlock* group = AB_TO_GRP(block->getParent());
93
94   // adding to the graph
95   FunctionalBlock* newBlock = createSourceBlock(ref);
96   return newBlock;
97 }
98
99 FunctionalBlock* Graph::getSourceBlockByName(QString name) {
100   foreach(FunctionalBlock* block, sources) {
101     if (block->getName() == name) return block;
102   }
103   return NULL;
104 }
105
106 bool Graph::removeSourceBlock(FunctionalBlock *block) {
107   sources.removeAll(block);
108   return true;
109 }
110
111 bool Graph::createPatterns() {
112   bool ok = true;
113   foreach(AbstractBlock* block, sources) {
114     FunctionalBlock* funBlock = AB_TO_FUN(block);
115     ok = funBlock->createPatterns();
116     if (!ok) return false;
117   }
118   
119   foreach(AbstractBlock* block, groups) {
120     GroupBlock* group = AB_TO_GRP(block);    
121     foreach(AbstractBlock* inBlock, group->getBlocks()) {
122       if (inBlock->isFunctionalBlock()) {
123         FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
124         ok = funBlock->createPatterns();
125         if (!ok) return false;
126       }
127     }
128   }
129   return true;
130 }
131
132 void Graph::resetPatternComputed() {
133   foreach(AbstractBlock* block, sources) {
134     block->setPatternComputed(false);
135   }
136   foreach(AbstractBlock* block, groups) {
137     GroupBlock* group = AB_TO_GRP(block);
138     group->setPatternComputed(false);
139     foreach(AbstractBlock* inBlock, group->getBlocks()) {
140       inBlock->setPatternComputed(false);
141     }
142   }
143 }