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

Private GIT Repository
1560d98ce27c34998c3a49f6145062565641c6e2
[blast.git] / GroupBlock.cpp
1 #include "GroupBlock.h"
2 #include "BlockParameterGeneric.h"
3 #include "AbstractInterface.h"
4 #include "ConnectedInterface.h"
5 #include "string.h"
6 #include <sstream>
7
8 int GroupBlock::counter = 1;
9
10 GroupBlock::GroupBlock(GroupBlock *_parent) throw(Exception) :  AbstractBlock() {
11
12   // force topGroup to false if this group has a parent
13   if (_parent != NULL) {    
14     topGroup = false;
15     name = QString("sub_group")+"_"+QString::number(counter++);
16   }
17   else {
18     topGroup = true;
19     name = QString("top_group");
20   }
21   parent = _parent;
22   if (parent != NULL) {
23     // adding this to the child blocks of parent
24     AB_TO_GRP(parent)->addBlock(this);
25   }
26 }
27
28 GroupBlock::~GroupBlock() {
29   foreach(AbstractBlock* block, blocks) {
30     delete block;
31   }
32 }
33
34 bool GroupBlock::isGroupBlock() {
35   return true;
36 }
37
38 bool GroupBlock::isTopGroupBlock() {
39   return topGroup;
40 }
41
42 void GroupBlock::setParent(AbstractBlock *_parent) {
43   parent = _parent;
44   if (parent != NULL) {
45     topGroup = false;
46   }
47 }
48
49 void GroupBlock::removeAllBlocks() {
50   foreach(AbstractBlock* block, blocks) {
51     if (block->isGroupBlock()) {
52       GroupBlock* group = AB_TO_GRP(block);
53       group->removeAllBlocks();
54     }
55     removeBlock(block);
56   }
57 }
58
59 void GroupBlock::removeBlock(AbstractBlock* block) {
60   /* CAUTION: no check is done if the block has connected interface
61      or not. Thus, they must be deleted elsewhere.
62   */
63   blocks.removeAll(block);
64   delete block;
65 }
66
67 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
68   foreach(AbstractBlock* block, blocks) {
69     if (block->isFunctionalBlock()) {
70       if (block->getName() == name) return block;
71     }
72   }
73   return NULL;
74 }
75
76 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
77
78   /*
79   checkedBlocks->append(this);
80
81   foreach(BlockParameter* param, params){
82     if(param->isUserParameter() && !param->isValueSet()){
83       if(!blocksToConfigure->contains(param->getOwner())){
84         blocksToConfigure->append(param->getOwner());
85       }
86     }
87   }
88   foreach(AbstractInterface *inter, outputs){
89     foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
90       if(!checkedBlocks->contains(connectedInter->getOwner())){
91         connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
92       }
93     }
94   }
95   */
96 }
97
98 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
99   BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
100   params.append(param);
101 }
102
103 void GroupBlock::removeGenericParameter(QString name) {
104   BlockParameter* p = getParameterFromName(name);
105   if (p != NULL) params.removeAll(p);
106 }
107
108 void GroupBlock::createInputPattern() {
109   foreach(AbstractInterface* iface, getControlInputs()) {
110     ConnectedInterface* connIface = AI_TO_CON(iface);
111     QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));    
112     connIface->setOutputPattern(pattern);    
113   }  
114 }
115
116 bool GroupBlock::checkInputPatternCompatibility() {
117   return true;
118 }
119
120 bool GroupBlock::computeOutputPattern(int nbExec) {
121
122   static QString fctName = "GroupBlock::computeOutputPattern()";
123 #ifdef DEBUG_FCTNAME
124   cout << "call to " << qPrintable(fctName) << endl;
125 #endif
126
127   cout << "computing output pattern of group " << qPrintable(name) << endl;
128   
129   bool canCompute = true;
130   bool compatible = true;
131   // get the input pattern on each inputs
132   createInputPattern();
133   
134   cout << "Input pattern OK" << endl;
135   // find blocks that are connected to that inputs and generators
136   QList<AbstractBlock*> fifo;
137   foreach(AbstractBlock* block, blocks) {
138         
139     bool addIt = false;
140     // if a block is a generator and has control outputs, add it
141     if (block->isGeneratorBlock()) {
142       if (block->getControlOutputs().size() > 0) addIt = true;
143     }
144     else {
145       // if the block has a control input connected from an intput of the group, add it too
146       foreach(AbstractInterface* iface, block->getControlInputs()) {
147         //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;       
148         ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
149         //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
150         
151         if (connFrom->getOwner() == this) {
152           addIt = true;
153           break;
154         }
155       }
156     }
157     if (addIt) {
158       cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
159       fifo.append(block);    
160     }
161   }
162   
163   while (!fifo.isEmpty()) {
164     AbstractBlock* block = fifo.takeFirst();
165     
166     if (block->getPatternComputed()) continue; // block has already been processed
167     
168     compatible = block->checkInputPatternCompatibility();
169     if (!compatible) {
170       cout << qPrintable(block->getName()) << " is not compatible with his input pattern" << endl;
171       break;
172     }
173     canCompute = block->computeOutputPattern();
174     if (!canCompute) {
175       cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
176       break;
177     }
178     block->setPatternComputed(true);
179     // add other blocks connected from block to the fifo
180     foreach(AbstractInterface* iface, block->getControlOutputs()) {
181       ConnectedInterface* conn = (ConnectedInterface*)iface;
182       foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
183         /* if connTo is owned by a functional block
184            or by a group block that is within this, add the block to the fifo.
185          */
186         if (connTo->getOwner()->isFunctionalBlock()) {
187           fifo.append(connTo->getOwner());
188         }
189         else if (connTo->getOwner() != this) {
190           fifo.append(connTo->getOwner());
191         }      
192       }
193     }
194   }
195   
196   if (canCompute) {
197     foreach(AbstractInterface* iface, getControlOutputs()) {
198       ConnectedInterface* connIface = AI_TO_CON(iface);
199       QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
200       connIface->setOutputPattern(pattern);    
201     }
202     setPatternComputed(true);
203   }
204   return canCompute;
205 }