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

Private GIT Repository
changed name of the class that converts VHDL -> XML
[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 void 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
93   // adding to the graph
94   FunctionalBlock* newBlock = createSourceBlock(ref);
95   return newBlock;
96 }
97
98 FunctionalBlock* Graph::getSourceBlockByName(QString name) {
99   foreach(FunctionalBlock* block, sources) {
100     if (block->getName() == name) return block;
101   }
102   return NULL;
103 }
104
105 bool Graph::removeSourceBlock(FunctionalBlock *block) {
106   sources.removeAll(block);
107   return true;
108 }
109
110 void Graph::createPatterns() throw(Exception) {
111   
112   foreach(AbstractBlock* block, sources) {
113     FunctionalBlock* funBlock = AB_TO_FUN(block);
114     try {
115       funBlock->createPatterns();
116     }
117     catch(Exception e) {
118       throw(e);      
119     }
120   }
121   
122   foreach(AbstractBlock* block, groups) {
123     GroupBlock* group = AB_TO_GRP(block);    
124     foreach(AbstractBlock* inBlock, group->getBlocks()) {
125       if (inBlock->isFunctionalBlock()) {
126         FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
127         try {
128           funBlock->createPatterns();
129         }
130         catch(Exception e) {
131           throw(e);
132         }
133       }
134     }
135   }  
136 }
137
138 void Graph::resetPatternComputed() {
139   foreach(AbstractBlock* block, sources) {
140     block->setPatternComputed(false);
141   }
142   foreach(AbstractBlock* block, groups) {
143     GroupBlock* group = AB_TO_GRP(block);
144     group->setPatternComputed(false);
145     foreach(AbstractBlock* inBlock, group->getBlocks()) {
146       inBlock->setPatternComputed(false);
147     }
148   }
149 }
150
151 void Graph::computeOutputPatterns(int nbExec) throw(Exception) {
152   
153   try {
154     createPatterns();
155   }
156   catch(Exception e) {
157     throw(e);    
158   }
159
160   resetPatternComputed();  
161   // search for all block that are generators.
162   QList<FunctionalBlock*> generators;
163   generators.append(sources);
164   foreach(AbstractBlock* block, groups) {    
165     GroupBlock* group = AB_TO_GRP(block);    
166     foreach(AbstractBlock* inBlock, group->getBlocks()) {
167       FunctionalBlock* funBlock = AB_TO_FUN(inBlock);
168       if ((inBlock->isFunctionalBlock()) && (inBlock->isGeneratorBlock())) {
169         generators.append(funBlock);
170       }
171     }    
172   }
173   // search for maximum PP length
174   int maxPP = 0;
175   foreach(FunctionalBlock* block, generators) {    
176     if (block->getProductionPatternLength() > maxPP) maxPP = block->getProductionPatternLength();
177   }
178   // compute output for generators
179   int maxExecLen = maxPP*nbExec;
180   foreach(FunctionalBlock* block, generators) {    
181     int d = block->getProductionPatternLength();
182     block->computeOutputPattern((maxExecLen+d-1)/d);
183   }
184   // compute output for top group
185   try {
186     topGroup->computeOutputPattern();
187   }
188   catch(Exception e) {
189     throw(e);
190   }
191 }