1 #include "GroupBlock.h"
2 #include "BlockParameterGeneric.h"
3 #include "AbstractInterface.h"
4 #include "ConnectedInterface.h"
8 int GroupBlock::counter = 1;
10 GroupBlock::GroupBlock(GroupBlock *_parent) throw(Exception) : AbstractBlock() {
12 // force topGroup to false if this group has a parent
13 if (_parent != NULL) {
15 name = QString("sub_group")+"_"+QString::number(counter++);
19 name = QString("top_group");
23 // adding this to the child blocks of parent
24 AB_TO_GRP(parent)->addBlock(this);
28 GroupBlock::~GroupBlock() {
29 foreach(AbstractBlock* block, blocks) {
34 bool GroupBlock::isGroupBlock() {
38 bool GroupBlock::isTopGroupBlock() {
42 void GroupBlock::setParent(AbstractBlock *_parent) {
49 void GroupBlock::removeAllBlocks() {
50 foreach(AbstractBlock* block, blocks) {
51 if (block->isGroupBlock()) {
52 GroupBlock* group = AB_TO_GRP(block);
53 group->removeAllBlocks();
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.
63 blocks.removeAll(block);
67 AbstractBlock *GroupBlock::getFunctionalBlockByName(QString name) {
68 foreach(AbstractBlock* block, blocks) {
69 if (block->isFunctionalBlock()) {
70 if (block->getName() == name) return block;
76 void GroupBlock::parametersValidation(QList<AbstractBlock *> *checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
79 checkedBlocks->append(this);
81 foreach(BlockParameter* param, params){
82 if(param->isUserParameter() && !param->isValueSet()){
83 if(!blocksToConfigure->contains(param->getOwner())){
84 blocksToConfigure->append(param->getOwner());
88 foreach(AbstractInterface *inter, outputs){
89 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
90 if(!checkedBlocks->contains(connectedInter->getOwner())){
91 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
98 void GroupBlock::addGenericParameter(QString name, QString type, QString value) {
99 BlockParameter* param = new BlockParameterGeneric(this, name, type, value);
100 params.append(param);
103 void GroupBlock::removeGenericParameter(QString name) {
104 BlockParameter* p = getParameterFromName(name);
105 if (p != NULL) params.removeAll(p);
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);
116 void GroupBlock::computeAdmittanceDelays() throw(Exception) {
117 throw(Exception(INVALID_GROUPBLOCK_USE));
120 void GroupBlock::checkInputPatternCompatibility() throw(Exception){
121 throw(Exception(INVALID_GROUPBLOCK_USE));
125 void GroupBlock::computeOutputPattern(int nbExec) throw(Exception) {
127 static QString fctName = "GroupBlock::computeOutputPattern()";
129 cout << "call to " << qPrintable(fctName) << endl;
132 cout << "computing output pattern of group " << qPrintable(name) << endl;
134 bool canCompute = false;
135 // get the input pattern on each inputs
136 createInputPattern();
138 cout << "Input pattern OK" << endl;
139 // find blocks that are connected to that inputs and generators
140 QList<AbstractBlock*> fifo;
141 foreach(AbstractBlock* block, blocks) {
144 // if a block is a generator and has control outputs, add it
145 if (block->isGeneratorBlock()) {
146 if (block->getControlOutputs().size() > 0) addIt = true;
149 // if the block has all its connected inputs that are connected to an intput of the group, add it too
151 foreach(AbstractInterface* iface, block->getControlInputs()) {
152 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
153 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
154 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
156 if (connFrom == NULL) {
160 else if (connFrom->getOwner() != this) {
167 cout << "adding " << qPrintable(block->getName()) << " to initialize the FIFO" << endl;
168 block->setTraversalLevel(0); // level 0 = first blocks to be evaluated
173 while (!fifo.isEmpty()) {
174 AbstractBlock* block = fifo.takeFirst();
176 if (block->getPatternComputed()) continue; // block has already been processed
178 cout << "computing compat and output for " << qPrintable(block->getName()) << endl;
182 block->checkInputPatternCompatibility();
185 cout << qPrintable(block->getName()) << " is not compatible with its input pattern" << endl;
190 block->computeOutputPattern();
193 cout << "cannot finalize output pattern computation of " << qPrintable(block->getName()) << endl;
197 block->setPatternComputed(true);
198 /* add other blocks connected from block to the fifo but only if
199 all their connected inputs are connected to blocks that have
202 foreach(AbstractInterface* iface, block->getControlOutputs()) {
203 ConnectedInterface* conn = (ConnectedInterface*)iface;
204 foreach(ConnectedInterface* connTo, conn->getConnectedTo()) {
206 AbstractBlock* block1 = connTo->getOwner();
207 cout << "testing if " << qPrintable(block1->getName()) << " has all connected inputs connected to already processed blocks" << endl;
211 foreach(AbstractInterface* iface, block1->getControlInputs()) {
212 //cout << qPrintable(iface->getName()) << " of " << qPrintable(iface->getOwner()->getName()) << " connected to " << endl;
213 ConnectedInterface* connFrom = ((ConnectedInterface*)iface)->getConnectedFrom();
214 //cout << qPrintable(connFrom->getName()) << " of " << qPrintable(connFrom->getOwner()->getName()) << endl;
216 if ((connFrom != NULL) && (connFrom->getOwner()->getPatternComputed() == false)) {
221 if (connFrom->getOwner()->getTraversalLevel() > maxLevel) maxLevel = connFrom->getOwner()->getTraversalLevel();
226 cout << "adding " << qPrintable(block1->getName()) << " to the FIFO" << endl;
227 block1->setTraversalLevel(maxLevel+1); // level 0 = first blocks to be evaluated
235 foreach(AbstractInterface* iface, getControlOutputs()) {
236 ConnectedInterface* connIface = AI_TO_CON(iface);
237 QList<char>* pattern = new QList<char>(*(connIface->getConnectedFrom()->getOutputPattern()));
238 connIface->setOutputPattern(pattern);
240 setPatternComputed(true);