1 #include "FunctionalBlock.h"
\r
2 #include "ReferenceBlock.h"
\r
3 #include "GroupBlock.h"
\r
4 #include "AbstractInterface.h"
\r
5 #include "FunctionalInterface.h"
\r
6 #include "ReferenceInterface.h"
\r
7 #include "BlockParameter.h"
\r
8 #include "ArithmeticEvaluator.h"
\r
11 FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference) throw(Exception) : AbstractBlock() {
\r
12 //if (! _reference->isReferenceBlock()) throw(Exception(BLOCK_INVALID_TYPE));
\r
13 //if (! _group->isGroupBlock()) throw(Exception(BLOCK_INVALID_TYPE));
\r
14 reference = _reference;
\r
16 name = reference->getName();
\r
18 if (reference->getImplementations().isEmpty()) {
\r
19 implementation = NULL;
\r
20 cout << "block has no implementation" << endl;
\r
23 implementation = reference->getImplementations().at(0);
\r
34 FunctionalBlock::~FunctionalBlock() {
\r
35 if (evaluator != NULL) delete evaluator;
\r
38 void FunctionalBlock::parametersValidation(QList<AbstractBlock*>* checkedBlocks, QList<AbstractBlock *> *blocksToConfigure) {
\r
40 checkedBlocks->append(this);
\r
42 foreach(BlockParameter* param, params){
\r
43 if(param->isUserParameter() && !param->isValueSet()){
\r
44 if(!blocksToConfigure->contains(param->getOwner())){
\r
45 blocksToConfigure->append(param->getOwner());
\r
49 foreach(AbstractInterface *inter, outputs){
\r
50 foreach(AbstractInterface *connectedInter, inter->getConnectedTo()){
\r
51 if(!checkedBlocks->contains(connectedInter->getOwner())){
\r
52 connectedInter->getOwner()->parametersValidation(checkedBlocks, blocksToConfigure);
\r
59 bool FunctionalBlock::isFunctionalBlock() {
\r
63 bool FunctionalBlock::isSourceBlock() {
\r
64 if (parent == NULL) return true;
\r
68 void FunctionalBlock::populate() {
\r
71 AbstractInterface* inter;
\r
73 // create parameters from reference block
\r
74 QList<BlockParameter*> lstParam = reference->getParameters();
\r
75 for(i=0;i<lstParam.size();i++) {
\r
76 p = lstParam.at(i)->clone();
\r
80 // create interfaces from reference block
\r
81 QList<AbstractInterface *> lstRef = reference->getInterfaces();
\r
82 // store relation between functional and reference
\r
83 QHash<AbstractInterface *, AbstractInterface *> hashIface;
\r
84 for(i=0;i<lstRef.size();i++) {
\r
86 inter = new FunctionalInterface(this, AI_TO_REF(lstRef.at(i)));
\r
88 catch(Exception e) {
\r
89 cerr << "Abnormal case: " << qPrintable(e.getDefaultMessage()) << endl << "Aborting execution." << endl;
\r
92 hashIface.insert(lstRef.at(i),inter);
\r
94 addInterface(inter);
\r
97 AbstractInterface* funCtlIface = NULL;
\r
98 AbstractInterface* funDataIface = NULL;
\r
100 for(i=0;i<lstRef.size();i++) {
\r
101 AbstractInterface* refIface = lstRef.at(i);
\r
102 if (refIface->getPurpose() == AbstractInterface::Control) {
\r
103 funCtlIface = hashIface.value(refIface);
\r
104 funDataIface = hashIface.value(refIface->getAssociatedIface());
\r
105 if (! funCtlIface->setAssociatedIface(funDataIface)) {
\r
106 cerr << "Abnormal case when associating a control interface to data" << endl << "Aborting execution." << endl;
\r
114 QString FunctionalBlock::getReferenceXmlFile() {
\r
115 return ((ReferenceBlock *)reference)->getXmlFile();
\r
118 QString FunctionalBlock::getReferenceHashMd5() {
\r
119 return ((ReferenceBlock *)reference)->getHashMd5();
\r
122 void FunctionalBlock::createPatterns() throw(Exception) {
\r
123 static QString fctName = "FunctionalBlock::createPatterns()";
\r
124 #ifdef DEBUG_FCTNAME
\r
125 cout << "call to " << qPrintable(fctName) << endl;
\r
128 cout << "create patterns for block " << qPrintable(name) << endl;
\r
129 if (evaluator == NULL) evaluator = new ArithmeticEvaluator();
\r
130 if (! isGeneratorBlock()) {
\r
133 createConsumptionPattern();
\r
134 createProductionCounter();
\r
136 catch(Exception e) {
\r
137 throw(e); // rethrow e
\r
141 createProductionPattern();
\r
143 catch(Exception e) {
\r
146 cout << "PP of " << qPrintable(name) << endl;
\r
147 QMapIterator<AbstractInterface*,QList<char>* > it(productionPattern);
\r
148 while (it.hasNext()) {
\r
150 QList<char>* pat = it.value();
\r
151 foreach(char c, *pat) cout << (int)c;
\r
156 void FunctionalBlock::createDelta() throw(Exception) {
\r
157 static QString fctName = "FunctionalBlock::createDelta()";
\r
158 #ifdef DEBUG_FCTNAME
\r
159 cout << "call to " << qPrintable(fctName) << endl;
\r
162 QString deltaStr = implementation->getDelta();
\r
163 cout << "delta for " << qPrintable(name) << " = " << qPrintable(deltaStr) << endl;
\r
164 if (deltaStr.isEmpty()) {
\r
169 // look for parameter names
\r
172 result = evaluateExpression(deltaStr);
\r
174 catch(Exception e) {
\r
178 cout << "delta = " << delta << endl;
\r
181 void FunctionalBlock::createConsumptionPattern() throw(Exception) {
\r
182 static QString fctName = "FunctionalBlock::createConsumptionPattern()";
\r
183 #ifdef DEBUG_FCTNAME
\r
184 cout << "call to " << qPrintable(fctName) << endl;
\r
187 // first clear if already exists
\r
188 clearConsumptionPattern();
\r
191 QHash<QString,QString> consPattern = implementation->getConsumptionPattern();
\r
193 foreach(AbstractInterface* iface, getControlInputs()) {
\r
194 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
195 QString refName = connIface->getReference()->getName();
\r
196 if (! consPattern.contains(refName)) {
\r
197 throw(Exception(NO_IFACE_CP,this));
\r
198 cerr << "no consumption pattern for reference interface " << qPrintable(refName) << endl;
\r
200 QList<char>* pattern = NULL;
\r
202 pattern = expandPattern(consPattern.value(refName));
\r
204 catch(Exception e) {
\r
207 consumptionPattern.insert(connIface,pattern);
\r
208 if (lengthCP == -1) {
\r
209 lengthCP = pattern->size();
\r
212 if (pattern->size() != lengthCP) {
\r
213 throw(Exception(INVALID_IFACE_CP_LENGTH,this));
\r
219 void FunctionalBlock::createProductionPattern() throw(Exception){
\r
220 static QString fctName = "FunctionalBlock::createProductionPattern()";
\r
221 #ifdef DEBUG_FCTNAME
\r
222 cout << "call to " << qPrintable(fctName) << endl;
\r
225 // first clear if already exists
\r
226 clearProductionPattern();
\r
229 QHash<QString,QString> prodPattern = implementation->getProductionPattern();
\r
231 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
232 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
233 QString refName = connIface->getReference()->getName();
\r
234 if (! prodPattern.contains(refName)) {
\r
235 throw(Exception(NO_IFACE_PP,this));
\r
237 QList<char>* pattern = NULL;
\r
239 pattern = expandPattern(prodPattern.value(refName));
\r
241 catch(Exception e) {
\r
244 productionPattern.insert(connIface,pattern);
\r
245 if (lengthPP == -1) {
\r
246 lengthPP = pattern->size();
\r
249 if (pattern->size() != lengthPP) {
\r
250 throw(Exception(INVALID_IFACE_PP_LENGTH,this));
\r
256 void FunctionalBlock::createProductionCounter() throw(Exception) {
\r
257 static QString fctName = "FunctionalBlock::createProductionCounter()";
\r
258 #ifdef DEBUG_FCTNAME
\r
259 cout << "call to " << qPrintable(fctName) << endl;
\r
262 // first clear if already exists
\r
263 productionCounter.clear();
\r
266 QStringList counterParts = implementation->getProductionCounter().split(",");
\r
267 foreach(QString s, counterParts) {
\r
268 cout << "cont part = " << qPrintable(s) << endl;
\r
270 double val = s.toDouble(&ok);
\r
272 productionCounter.append(val);
\r
274 else if (s.at(0) == '{') {
\r
277 QStringList gen = s.split(":");
\r
278 if (gen.size() != 3) {
\r
279 throw(Exception(INVALID_IFACE_PC,this));
\r
284 for(int i=0;i<3;i++) {
\r
285 double result = 0.0;
\r
287 result = evaluateExpression(gen.at(i));
\r
289 catch(Exception e) {
\r
292 if (i==0) start = result;
\r
293 else if (i==1) nb = result;
\r
294 else if (i==2) step = result;
\r
296 for(int j=0;j<nb;j++) {
\r
297 productionCounter.append(start+j*step);
\r
301 double result = 0.0;
\r
303 result = evaluateExpression(s);
\r
305 catch(Exception e) {
\r
308 productionCounter.append(result);
\r
311 foreach(int val, productionCounter) {
\r
312 cout << val << ",";
\r
317 QList<char>* FunctionalBlock::expandPattern(const QString& patternIn) throw(Exception) {
\r
318 static QString fctName = "FunctionalBlock::expandPattern()";
\r
319 #ifdef DEBUG_FCTNAME
\r
320 cout << "call to " << qPrintable(fctName) << endl;
\r
322 /* expanding a pattern is done in two steps :
\r
323 - 1 : finding all variables that correspond to an expression
\r
324 and copy them in the pattern
\r
325 - 2 : parsing the result
\r
327 Note that the result MUST contain only variables that have a
\r
328 integer/double value. Otherwise, expanding will fail.
\r
334 QString p = replaceExpressions(patternIn);
\r
339 QList<char>* patternOut = new QList<char>();
\r
341 patternOut->append(expandPatternRecur(p,&offset));
\r
343 catch(Exception e) {
\r
350 QString FunctionalBlock::replaceExpressions(const QString& patternIn) throw(Exception) {
\r
352 QString res = patternIn;
\r
354 QRegularExpression re("[$][a-zA-Z0-9_]+");
\r
358 QRegularExpressionMatchIterator matcher = re.globalMatch(res);
\r
359 while(matcher.hasNext()) {
\r
360 QRegularExpressionMatch m = matcher.next();
\r
361 QString param = m.captured(0);
\r
362 QString paramName = param;
\r
363 paramName.remove(0,1);
\r
364 BlockParameter* p = getParameterFromName(paramName);
\r
365 if ((p != NULL) && (p->getType() == BlockParameter::Expression)) {
\r
366 res.replace(param,p->getStringValue());
\r
368 cout << "found an expr: " << qPrintable(paramName) << ", patern => " << qPrintable(res) << endl;
\r
375 QList<char> FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset) throw(Exception) {
\r
377 QList<char> patternOut;
\r
379 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) {
\r
381 QChar c = patternIn.at(*offset);
\r
385 patternOut.append(expandPatternRecur(patternIn,offset));
\r
387 catch(Exception e) {
\r
391 else if (c == '0') {
\r
392 patternOut.append(0);
\r
394 else if (c == '1') {
\r
395 patternOut.append(1);
\r
397 else if (c == 'X') {
\r
398 patternOut.append(-1);
\r
400 else if (c == '{') {
\r
402 QString expr = "";
\r
403 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
\r
404 expr += patternIn.at(*offset);
\r
407 if (*offset == patternIn.size()) {
\r
408 throw(Exception(INVALID_IFACE_PATTERN,this));
\r
412 repeat = evaluateExpression(expr);
\r
414 catch(Exception e) {
\r
417 // repeat just the last value in currentGroup
\r
418 char last = patternOut.last();
\r
419 //cout << "repeat last char " << repeat << " times : " << (int)last << endl;
\r
421 for(int i=1;i<(int)repeat;i++) {
\r
422 patternOut.append(last);
\r
428 // must check if after ), there is a {
\r
429 if ((*offset < patternIn.size()-1) && (patternIn.at(*offset+1) == '{')) {
\r
431 QString expr = "";
\r
432 while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) {
\r
433 expr += patternIn.at(*offset);
\r
436 if (*offset == patternIn.size()) {
\r
437 throw(Exception(INVALID_IFACE_PATTERN,this));
\r
441 repeat = evaluateExpression(expr);
\r
443 catch(Exception e) {
\r
447 cout << "repeat last group " << repeat << " times : ";
\r
448 foreach (char c, currentGroup) cout <<(int)c;
\r
451 QList<char> single = patternOut;
\r
452 for(int i=1;i<(int)repeat;i++) {
\r
453 patternOut.append(single);
\r
459 double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) {
\r
460 static QString fctName = "FunctionalBlock::evaluateExpression()";
\r
461 #ifdef DEBUG_FCTNAME
\r
462 cout << "call to " << qPrintable(fctName) << endl;
\r
465 QHash<QString,double> vars;
\r
466 evaluator->setExpression(expression);
\r
467 QList<QString> varNames = evaluator->getVariableNames();
\r
468 foreach (QString name, varNames) {
\r
469 QString paramName = name;
\r
470 paramName.remove(0,1);
\r
471 BlockParameter* param = getParameterFromName(paramName);
\r
472 if (param == NULL) {
\r
473 throw(Exception(EVAL_PARAM_UNKNOWN,this));
\r
476 int val = param->getDoubleValue(&okVal);
\r
478 throw(Exception(EVAL_PARAM_NOVALUE,this));
\r
480 vars.insert(name,(double)val);
\r
483 evaluator->setVariablesValue(vars);
\r
484 double result = 0.0;
\r
486 result = evaluator->evaluate();
\r
489 cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl;
\r
490 throw(Exception(EVAL_INVALID_EXPR,this));
\r
495 void FunctionalBlock::computeAdmittanceDelays() throw(Exception) {
\r
496 static QString fctName = "FunctionalBlock::computeAdmittanceDelays()";
\r
497 #ifdef DEBUG_FCTNAME
\r
498 cout << "call to " << qPrintable(fctName) << endl;
\r
500 QList<int> inClock;
\r
503 clearAdmittanceDelays();
\r
505 // trying to synchronize the first one in AP
\r
506 QMapIterator<AbstractInterface*,QList<char>* > iterAP(admittance);
\r
507 QMapIterator<AbstractInterface*,QList<char>* > iterIP(inputPattern);
\r
509 while (iterAP.hasNext()) {
\r
512 QList<char>* ap = iterAP.value();
\r
513 QList<char>* ip = iterIP.value();
\r
515 while ((first < lengthIP) && (ip->at(first) == 0)) first++;
\r
516 while ((first < lengthAP) && (ap->at(first) == 0)) first--;
\r
517 delays.append(first);
\r
519 QList<int>* delays = new QList<int>();
\r
520 admittanceDelays.insert(iterAP.key(), delays);
\r
523 QMapIterator<AbstractInterface*,QList<int>* > iterDelays(admittanceDelays);
\r
525 // get the delay to apply
\r
527 for(int i=0;i<delays.size();i++) {
\r
528 if (delays[i] > maxDelay) maxDelay = delays[i];
\r
530 // adding the delays to IP
\r
533 while (iterIP.hasNext()) {
\r
536 QList<char>* ip = iterIP.value();
\r
537 QList<int>* d = iterDelays.value();
\r
538 d->append(maxDelay-delays[i]);
\r
539 cout << "prependind " << qPrintable(iterIP.key()->getName()) << " with " << (maxDelay-delays[i]) << " 0" << endl;
\r
540 for(int j=0;j<maxDelay-delays[i];j++) {
\r
543 for(int j=0;j<delays[i];j++) {
\r
548 lengthIP += maxDelay;
\r
550 cout << "IP length = " << lengthIP << ", AP length = " << lengthAP << endl;
\r
556 // if AP is a valid group, search for the next valid group in IP
\r
557 if (isValidDataGroup(admittance,apIndex)) {
\r
559 while ((ipIndex < lengthIP) && (! isValidDataGroup(inputPattern,ipIndex))) ipIndex++;
\r
560 if (ipIndex == lengthIP) {
\r
568 iterDelays.toFront();
\r
570 if (samePatterns(inputPattern,ipIndex,admittance,apIndex)) {
\r
571 while (iterAP.hasNext()) {
\r
574 QList<char>* ap = iterAP.value();
\r
575 if (ap->at(apIndex) == 1) {
\r
576 QList<int>* d = iterDelays.value();
\r
577 d->append(0); // the 1 is at its good place, so no delay
\r
582 cout << "diff between IP and AP at " << apIndex << endl;
\r
583 // search for the next 1 in IP for every input that has a 1 in AP
\r
585 while (iterAP.hasNext()) {
\r
589 QList<char>* ap = iterAP.value();
\r
590 QList<char>* ip = iterIP.value();
\r
591 QList<int>* d = iterDelays.value();
\r
592 // case 1: 1 in IP is too late
\r
593 if ((ap->at(apIndex) == 1) && (ip->at(ipIndex) == 0)) {
\r
595 while ( ((ipIndex+delay) < lengthIP) && (ip->at(ipIndex+delay) == 0) ) delay++;
\r
596 cout << "found a delay of " << (-delay) << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
\r
597 // moving the 1 to its normal pos.
\r
598 ip->replace(ipIndex,1);
\r
599 ip->replace(ipIndex+delay,0);
\r
602 // case 2: 1 in IP is too soon
\r
603 else if ((ap->at(apIndex) == 0) && (ip->at(ipIndex) == 1)) {
\r
605 while ( ((apIndex+delay) < lengthAP) && (ap->at(apIndex+delay) == 0) ) delay++;
\r
606 cout << "found a delay of " << delay << " for iface " << qPrintable(iterAP.key()->getName()) << endl;
\r
607 // search for next 0 in IP to put the 1
\r
608 int k = ipIndex+delay;
\r
609 while ((k < lengthIP) && (ip->at(k) == 1)) k++;
\r
610 ip->replace(ipIndex,0);
\r
615 if (! samePatterns(inputPattern,inClock,admittance,apIndex)) {
\r
616 cout << "Abnormal case while searching for delays" << endl;
\r
622 if ((apIndex >= lengthAP) || (ipIndex >= lengthIP)) stop = true;
\r
624 iterDelays.toFront();
\r
625 while (iterDelays.hasNext()) {
\r
627 QList<int>* d = iterDelays.value();
\r
628 foreach(int v, *d) {
\r
636 void FunctionalBlock::createInputPattern() throw(Exception) {
\r
637 static QString fctName = "FunctionalBlock::createInputPattern())";
\r
638 #ifdef DEBUG_FCTNAME
\r
639 cout << "call to " << qPrintable(fctName) << endl;
\r
643 foreach(AbstractInterface* iface, getControlInputs()) {
\r
645 ConnectedInterface* connIface = AI_TO_CON(iface);
\r
646 // check if it is connected
\r
647 if (connIface->getConnectedFrom() == NULL) {
\r
648 throw(Exception(IFACE_NOT_CONNECTED,this));
\r
650 // get the precursor output pattern
\r
651 QList<char>* out = connIface->getConnectedFrom()->getOutputPattern();
\r
652 AbstractInputModifier* modifier = connIface->getInputModifier();
\r
653 // check if the input is modified
\r
654 if (modifier != NULL) {
\r
656 out = modifier->getModifiedInput(out);
\r
659 if (out->size() == 0) {
\r
660 clearInputPattern();
\r
661 throw(Exception(NO_IFACE_IP,this));
\r
663 if (lengthIP == -1) {
\r
664 lengthIP = out->size();
\r
667 if (out->size() < lengthIP) lengthIP = out->size();
\r
670 QList<char>* in = new QList<char>(*out);
\r
671 foreach(char c, *in) {
\r
675 inputPattern.insert(connIface,in);
\r
677 // search the last valid group in IP,
\r
678 while(! isValidDataGroup(inputPattern,lengthIP-1)) {
\r
679 //removeDataGroup(inputPattern,lengthIP-1);
\r
684 void FunctionalBlock::createAdmittance(int nbExec) throw(Exception) {
\r
685 static QString fctName = "FunctionalBlock::createAdmittance()";
\r
686 #ifdef DEBUG_FCTNAME
\r
687 cout << "call to " << qPrintable(fctName) << endl;
\r
689 // firstly, copy CP in AP
\r
690 QMapIterator<AbstractInterface*,QList<char>* > iterC(consumptionPattern);
\r
691 while (iterC.hasNext()) {
\r
693 QList<char>* pattern = new QList<char>(*(iterC.value()));
\r
694 admittance.insert(iterC.key(), pattern);
\r
696 lengthAP = lengthCP;
\r
698 cout << "trigger 1 at c.c. 0" << endl;
\r
699 for(int i=1;i<nbExec;i++) {
\r
700 // searching for the clock cycle for which a new exec starts
\r
702 while ((clock < lengthAP) && (nbGroup < delta)) {
\r
703 if (isValidDataGroup(admittance,clock)) nbGroup+=1;
\r
706 while ((clock < lengthAP) && (! isValidDataGroup(admittance,clock))) clock+=1;
\r
707 cout << "trigger " << (i+1) << " at c.c. " << clock << endl;
\r
709 // combine CP with AP at sc
\r
710 for(int j=0;j<lengthCP;j++) {
\r
711 // first case : column of CP must be placed beyond AP's end.
\r
712 if (sc == lengthAP) {
\r
713 cout << i << "," << j << " append in AP at " << sc << endl;
\r
714 appendToPattern(consumptionPattern,j,admittance,1);
\r
718 // second case : CP and AP can be combined directly (i.e. no X | 1 to do)
\r
719 else if (canCombinePatterns(consumptionPattern,j,admittance,sc)) {
\r
720 cout << i << "," << j << " combine at " << sc << endl;
\r
721 combinePatterns(consumptionPattern,j,admittance,sc);
\r
724 // third case : CP has an X column
\r
725 else if (isOnlyXDataGroup(consumptionPattern,j)) {
\r
726 cout << i << "," << j << " shift rigth AP to combine at " << sc << endl;
\r
727 shiftRightPattern(admittance,sc);
\r
729 if (! canCombinePatterns(consumptionPattern,j,admittance,sc)) {
\r
730 cerr << "Abnormal case when combining AP and CP" << endl;
\r
732 combinePatterns(consumptionPattern,j,admittance,sc);
\r
735 // fourth case : AP has an X column
\r
736 else if (isOnlyXDataGroup(admittance,sc)) {
\r
737 cout << i << "," << j << " jump c.c. for CP at " << sc << endl;
\r
742 throw(INVALID_DELTA_CP);
\r
746 // turn all X into 0
\r
747 QMapIterator<AbstractInterface*,QList<char>* > iterA(admittance);
\r
748 while (iterA.hasNext()) {
\r
750 QList<char>* pattern = iterA.value();
\r
751 for(int i=0;i<pattern->size();i++) {
\r
752 if (pattern->at(i) == -1) pattern->replace(i,0);
\r
753 cout << (int)(pattern->at(i));
\r
759 void FunctionalBlock::checkInputPatternCompatibility() throw(Exception) {
\r
760 static QString fctName = "FunctionalBlock::checkInputPatternCompatibility()";
\r
761 #ifdef DEBUG_FCTNAME
\r
762 cout << "call to " << qPrintable(fctName) << endl;
\r
765 // firstly, create input pattern
\r
767 createInputPattern();
\r
769 catch(Exception e) {
\r
772 int nbExec = createTriggers();
\r
773 cout << qPrintable(name) << " will exec. " << nbExec << " times." << endl;
\r
776 createAdmittance(nbExec);
\r
778 catch(Exception e) {
\r
779 cout << "cannot create admittance" << endl;
\r
783 int clock = 0; // index in IP
\r
784 int i = 0; // index in AP
\r
785 while ((clock < lengthIP) && (i < lengthAP)) {
\r
787 // if AP is a valid group, search for the next valid group in IP
\r
788 if (isValidDataGroup(admittance,i)) {
\r
789 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
790 if (clock == lengthIP) {
\r
791 cerr << "Abnormal case: end of IP has been reached without finding a valid group" << endl;
\r
792 throw(Exception(IP_END_NULLCOL,this));
\r
795 /* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or
\r
796 are both null columns
\r
798 if (! samePatterns(inputPattern,clock,admittance,i)) {
\r
799 cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl;
\r
800 throw(Exception(IP_AP_NOTCOMPAT,this)); // IP and AP not compatible
\r
805 if (clock < lengthIP) {
\r
806 throw(Exception(AP_TOO_SHORT,this));
\r
807 cerr << "Abnormal case: AP is to short" << endl;
\r
811 void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
\r
812 static QString fctName = "FunctionalBlock::computeOutputPattern()";
\r
813 #ifdef DEBUG_FCTNAME
\r
814 cout << "call to " << qPrintable(fctName) << endl;
\r
817 clearOutputPattern();
\r
819 /* case 1: the block is a generator for which output pattern
\r
820 must be computed for a nbExec following executions
\r
824 cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
\r
825 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
826 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
827 // create output pattern
\r
828 QList<char>* pp = productionPattern.value(connIface);
\r
829 QList<char>* pattern = new QList<char>(*pp);
\r
830 for(int i=1;i<nbExec;i++) pattern->append(*pp);
\r
831 // assign pattern to interface
\r
832 connIface->setOutputPattern(pattern);
\r
833 // store it in QMap
\r
834 outputPattern.insert(connIface,pattern);
\r
838 cout << "computing output pattern of " << qPrintable(name) << endl;
\r
840 // in case of inputPattern not created, do it
\r
841 if (lengthIP <= 0) {
\r
843 cout << "Strange case: input pattern is not created while it is time to compute output pattern !" << endl;
\r
844 // collect the input patterns for each input
\r
846 createInputPattern();
\r
848 catch(Exception e) {
\r
851 cout << "input pattern array initialized with min. len " << lengthIP << endl;
\r
854 // initialize the output pattern
\r
856 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
857 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
858 lengthOP = lengthIP+productionPattern.value(connIface)->size();
\r
859 QList<char>* pattern = new QList<char>();
\r
860 for(int i=0;i<lengthOP;i++) pattern->append(0);
\r
861 connIface->setOutputPattern(pattern);
\r
862 outputPattern.insert(connIface,pattern);
\r
864 cout << "output pattern array initialized" << endl;
\r
868 // search for the beginning of the first execution.
\r
869 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
870 cout << "found 1st exec clock: " << clock << endl;
\r
872 while (clock < lengthIP) {
\r
873 // initialize counters for current execution.
\r
874 int p = 0; // index in production pattern
\r
875 int o = 0; // clock+o will give the clock cycle of each output group
\r
876 int cip = 0; // clock+cip give the clock cycle of an input group
\r
877 int ccp = 0; // ccp give a column in the consumptio pattern
\r
878 int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
\r
879 int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
\r
880 bool cannotCompleteExec = false;
\r
881 for(int m=0;m<productionCounter.size();m++) {
\r
882 // search for the first production in PP
\r
883 while (!isValidDataGroup(productionPattern,p)) {
\r
887 int gap = 0; // count the number of extra null columns
\r
888 // search for PC(m) valid input group in IP
\r
889 while (nip < productionCounter.at(m)) {
\r
890 if (clock+cip < lengthIP) {
\r
891 if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
\r
896 cannotCompleteExec = true;
\r
901 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
903 // search for PC(m) valid input group in IP
\r
904 while (ncp < productionCounter.at(m)) {
\r
905 if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
\r
909 o += gap; // to take into acocunt of extra null columns
\r
910 combinePatterns(productionPattern,p,outputPattern,clock+o);
\r
915 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
917 // current exec. taken into accunt
\r
920 // search for the next exec.
\r
923 while ((clock < lengthIP) && (nip < delta)) {
\r
924 if (isValidDataGroup(inputPattern,clock)) nip += 1;
\r
925 if (nip < delta) clock += 1;
\r
927 cout << "found exec " << nbExec << " at clock: " << clock << endl;
\r
929 // find the last valid output data group
\r
930 while(! isValidDataGroup(outputPattern,lengthOP-1)) {
\r
931 removeDataGroup(outputPattern,lengthOP-1);
\r
935 // clear input pattern
\r
936 clearInputPattern();
\r
942 void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) {
\r
943 static QString fctName = "FunctionalBlock::computeOutputPattern()";
\r
944 #ifdef DEBUG_FCTNAME
\r
945 cout << "call to " << qPrintable(fctName) << endl;
\r
948 // case 1: the block is a generator for which output pattern
\r
949 // must be computed for a nbExec following executions
\r
952 cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl;
\r
953 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
954 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
955 // create output pattern
\r
956 QList<char>* pp = productionPattern.value(connIface);
\r
957 QList<char>* pattern = new QList<char>(*pp);
\r
958 for(int i=1;i<nbExec;i++) pattern->append(*pp);
\r
959 // assign pattern to interface
\r
960 connIface->setOutputPattern(pattern);
\r
961 // store it in QMap
\r
962 outputPattern.insert(connIface,pattern);
\r
966 cout << "computing output pattern of " << qPrintable(name) << endl;
\r
968 // in case of inputPattern not created, do it
\r
969 if (lengthIP <= 0) {
\r
970 // collect the input patterns for each input
\r
972 createInputPattern();
\r
974 catch(Exception e) {
\r
977 cout << "input pattern array initialized with min. len " << lengthIP << endl;
\r
980 // initialize the output pattern
\r
982 foreach(AbstractInterface* iface, getControlOutputs()) {
\r
983 FunctionalInterface* connIface = AI_TO_FUN(iface);
\r
984 lengthOP = lengthIP+productionPattern.value(connIface)->size();
\r
985 QList<char>* pattern = new QList<char>();
\r
986 for(int i=0;i<lengthOP;i++) pattern->append(0);
\r
987 connIface->setOutputPattern(pattern);
\r
988 outputPattern.insert(connIface,pattern);
\r
990 cout << "output pattern array initialized" << endl;
\r
994 // search for the beginning of the first execution.
\r
995 while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++;
\r
996 cout << "found 1st exec clock: " << clock << endl;
\r
998 while (clock < lengthIP) {
\r
999 // initialize counters for current execution.
\r
1000 int p = 0; // index in production pattern
\r
1001 int o = 0; // clock+o will give the clock cycle of each output group
\r
1002 int cip = 0; // clock+cip give the clock cycle of an input group
\r
1003 int ccp = 0; // ccp give a column in the consumptio pattern
\r
1004 int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP
\r
1005 int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP
\r
1006 bool cannotCompleteExec = false;
\r
1007 for(int m=0;m<productionCounter.size();m++) {
\r
1008 // search for the first production in PP
\r
1009 while (!isValidDataGroup(productionPattern,p)) {
\r
1013 int gap = 0; // count the number of extra null columns
\r
1014 // search for PC(m) valid input group in IP
\r
1015 while (nip < productionCounter.at(m)) {
\r
1016 if (clock+cip < lengthIP) {
\r
1017 if (isValidDataGroup(inputPattern,clock+cip)) nip += 1;
\r
1022 cannotCompleteExec = true;
\r
1027 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
1029 // search for PC(m) valid input group in IP
\r
1030 while (ncp < productionCounter.at(m)) {
\r
1031 if (isValidDataGroup(consumptionPattern,ccp)) ncp += 1;
\r
1035 o += gap; // to take into acocunt of extra null columns
\r
1036 combinePatterns(productionPattern,p,outputPattern,clock+o);
\r
1041 if (cannotCompleteExec) break; // no need to go further since the next search of input data group will lead to go outside inputPattern
\r
1043 // current exec. taken into accunt
\r
1046 // search for the next exec.
\r
1049 while ((clock < lengthIP) && (nip < delta)) {
\r
1050 if (isValidDataGroup(inputPattern,clock)) nip += 1;
\r
1051 if (nip < delta) clock += 1;
\r
1053 cout << "found exec " << nbExec << " at clock: " << clock << endl;
\r
1055 // find the last valid output data group
\r
1056 while(! isValidDataGroup(outputPattern,lengthOP-1)) {
\r
1057 removeDataGroup(outputPattern,lengthOP-1);
\r
1061 // clear input pattern
\r
1062 clearInputPattern();
\r
1066 bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
\r
1068 if (patternSrc.size() != patternDest.size()) return false;
\r
1069 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1070 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1071 while (iterSrc.hasNext()) {
\r
1074 QList<char>* srcPat = iterSrc.value();
\r
1075 QList<char>* destPat = iterDest.value();
\r
1076 if (srcCol >= srcPat->size()) return false;
\r
1077 if (destCol >= destPat->size()) return false;
\r
1078 if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
\r
1083 bool FunctionalBlock::samePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, const QList<int> &srcCols, const QMap<AbstractInterface*, QList<char>* >& patternDest, int destCol) {
\r
1084 if (patternSrc.size() != srcCols.size()) return false;
\r
1085 if (patternSrc.size() != patternDest.size()) return false;
\r
1087 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1088 QListIterator<int> iterSrcCol(srcCols);
\r
1089 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1090 while (iterSrc.hasNext()) {
\r
1092 int srcCol = iterSrcCol.next();
\r
1094 QList<char>* srcPat = iterSrc.value();
\r
1095 QList<char>* destPat = iterDest.value();
\r
1096 if (srcCol >= srcPat->size()) return false;
\r
1097 if (destCol >= destPat->size()) return false;
\r
1098 if (srcPat->at(srcCol) != destPat->at(destCol)) return false;
\r
1103 bool FunctionalBlock::canCombinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
\r
1104 if (patternSrc.size() != patternDest.size()) return false;
\r
1105 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1106 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1107 while (iterSrc.hasNext()) {
\r
1110 QList<char>* srcPat = iterSrc.value();
\r
1111 QList<char>* destPat = iterDest.value();
\r
1112 if (srcCol >= srcPat->size()) return false;
\r
1113 if (destCol >= destPat->size()) return false;
\r
1114 if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return false;
\r
1115 if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return false;
\r
1120 void FunctionalBlock::combinePatterns(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int destCol) {
\r
1121 if (patternSrc.size() != patternDest.size()) return;
\r
1122 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1123 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1124 while (iterSrc.hasNext()) {
\r
1127 QList<char>* srcPat = iterSrc.value();
\r
1128 QList<char>* destPat = iterDest.value();
\r
1129 if (srcCol >= srcPat->size()) return;
\r
1130 if (destCol >= destPat->size()) return;
\r
1131 if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return;
\r
1132 if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return;
\r
1133 destPat->replace(destCol,destPat->at(destCol) | srcPat->at(srcCol));
\r
1137 void FunctionalBlock::appendToPattern(const QMap<AbstractInterface*, QList<char>* >& patternSrc, int srcCol, QMap<AbstractInterface*, QList<char>* > patternDest, int nbCols) {
\r
1138 if (patternSrc.size() != patternDest.size()) return;
\r
1139 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(patternSrc);
\r
1140 QMapIterator<AbstractInterface*, QList<char>* > iterDest(patternDest);
\r
1141 while (iterSrc.hasNext()) {
\r
1144 QList<char>* srcPat = iterSrc.value();
\r
1145 QList<char>* destPat = iterDest.value();
\r
1147 while ((srcCol+i < srcPat->size()) && (i<nbCols)) {
\r
1148 destPat->append(srcPat->at(srcCol+i));
\r
1154 void FunctionalBlock::removeDataGroup(QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1155 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1156 while (iterSrc.hasNext()) {
\r
1158 QList<char>* srcPat = iterSrc.value();
\r
1159 if (offset < srcPat->size()) {
\r
1160 srcPat->removeAt(offset);
\r
1165 void FunctionalBlock::shiftRightPattern(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1166 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1167 while (iterSrc.hasNext()) {
\r
1169 QList<char>* srcPat = iterSrc.value();
\r
1170 if (offset < srcPat->size()) {
\r
1171 srcPat->insert(offset,0);
\r
1176 bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1177 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1178 while (iterSrc.hasNext()) {
\r
1180 QList<char>* srcPat = iterSrc.value();
\r
1181 if (offset >= srcPat->size()) return false;
\r
1182 if (srcPat->at(offset) == 1) return true;
\r
1187 bool FunctionalBlock::isValidDataGroup(const QMap<AbstractInterface*, QList<char>* >& pattern, const QList<int> offsets) {
\r
1188 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1189 QListIterator<int> iterOffsets(offsets);
\r
1190 while (iterSrc.hasNext()) {
\r
1192 int offset = iterOffsets.next();
\r
1193 QList<char>* srcPat = iterSrc.value();
\r
1194 if (offset >= srcPat->size()) return false;
\r
1195 if (srcPat->at(offset) == 1) return true;
\r
1200 bool FunctionalBlock::isOnlyXDataGroup(const QMap<AbstractInterface *, QList<char> *> &pattern, int offset) {
\r
1201 QMapIterator<AbstractInterface*, QList<char>* > iterSrc(pattern);
\r
1202 while (iterSrc.hasNext()) {
\r
1204 QList<char>* srcPat = iterSrc.value();
\r
1205 if (offset >= srcPat->size()) return false;
\r
1206 if (srcPat->at(offset) != -1) return false;
\r
1211 void FunctionalBlock::clearConsumptionPattern() {
\r
1212 QMapIterator<AbstractInterface*, QList<char>* > iterP(consumptionPattern);
\r
1213 while (iterP.hasNext()) {
\r
1215 QList<char>* pattern = iterP.value();
\r
1216 if (pattern != NULL) delete pattern;
\r
1218 consumptionPattern.clear();
\r
1222 void FunctionalBlock::clearProductionPattern() {
\r
1223 QMapIterator<AbstractInterface*, QList<char>* > iterP(productionPattern);
\r
1224 while (iterP.hasNext()) {
\r
1226 QList<char>* pattern = iterP.value();
\r
1227 if (pattern != NULL) delete pattern;
\r
1229 productionPattern.clear();
\r
1233 void FunctionalBlock::clearInputPattern() {
\r
1235 QMapIterator<AbstractInterface*,QList<char>* > iterI(inputPattern);
\r
1236 while (iterI.hasNext()) {
\r
1238 QList<char>* pattern = iterI.value();
\r
1239 if (pattern != NULL) delete pattern;
\r
1241 inputPattern.clear();
\r
1245 void FunctionalBlock::clearOutputPattern() {
\r
1247 QMapIterator<AbstractInterface*,QList<char>* > iterO(outputPattern);
\r
1248 while (iterO.hasNext()) {
\r
1250 ConnectedInterface* connIface = AI_TO_CON(iterO.key());
\r
1251 connIface->resetOutputPattern();
\r
1252 QList<char>* pattern = iterO.value();
\r
1253 if (pattern != NULL) delete pattern;
\r
1255 outputPattern.clear();
\r
1259 void FunctionalBlock::clearAdmittanceDelays() {
\r
1260 QMapIterator<AbstractInterface*, QList<int>* > iterA(admittanceDelays);
\r
1261 while (iterA.hasNext()) {
\r
1263 QList<int>* d = iterA.value();
\r
1264 if (d != NULL) delete d;
\r
1266 admittanceDelays.clear();
\r
1269 int FunctionalBlock::createTriggers() {
\r
1271 /* NB: this method returns the number of executions that have been started
\r
1272 but not necessary completed.
\r
1274 if (delta <= 0) return 0;
\r
1276 // search for the first exec.
\r
1277 while ((offset < lengthIP) && (! isValidDataGroup(inputPattern,offset))) offset++;
\r
1278 if (offset == lengthIP) return 0;
\r
1279 triggers.append(offset);
\r
1281 for(int i = offset;i<lengthIP;i++) {
\r
1282 if (isValidDataGroup(inputPattern,i)) nbGroup++;
\r
1283 if (nbGroup == delta+1) {
\r
1284 triggers.append(i);
\r
1288 return triggers.size();
\r