X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/blast.git/blobdiff_plain/c8c1e24dac94abfacb78fb3b661c9a5dbbb0d86a..9ea814d76cc52e24b80be4a6ea78cca24a9a4915:/FunctionalBlock.cpp diff --git a/FunctionalBlock.cpp b/FunctionalBlock.cpp index 7a4d4ff..534625d 100644 --- a/FunctionalBlock.cpp +++ b/FunctionalBlock.cpp @@ -28,8 +28,12 @@ FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference lengthOP = -1; lengthPP = -1; delta = -1; + evaluator = NULL; } +FunctionalBlock::~FunctionalBlock() { + if (evaluator != NULL) delete evaluator; +} void FunctionalBlock::parametersValidation(QList* checkedBlocks, QList *blocksToConfigure) { /* @@ -115,26 +119,33 @@ QString FunctionalBlock::getReferenceHashMd5() { return ((ReferenceBlock *)reference)->getHashMd5(); } -bool FunctionalBlock::createPatterns() { +void FunctionalBlock::createPatterns() throw(Exception) { static QString fctName = "FunctionalBlock::createPatterns()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; #endif cout << "create patterns for block " << qPrintable(name) << endl; - evaluator = new ArithmeticEvaluator(); - bool ok = true; + if (evaluator == NULL) evaluator = new ArithmeticEvaluator(); if (! isGeneratorBlock()) { - ok = ok & createDelta(); - if (ok) ok = ok & createConsumptionPattern(); - if (ok) ok = ok & createProductionCounter(); + try { + createDelta(); + createConsumptionPattern(); + createProductionCounter(); + } + catch(Exception e) { + throw(e); // rethrow e + } } - if (ok) ok = ok & createProductionPattern(); - delete evaluator; - return ok; + try { + createProductionPattern(); + } + catch(Exception e) { + throw(e); + } } -bool FunctionalBlock::createDelta() { +void FunctionalBlock::createDelta() throw(Exception) { static QString fctName = "FunctionalBlock::createDelta()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; @@ -144,81 +155,91 @@ bool FunctionalBlock::createDelta() { cout << "delta for " << qPrintable(name) << " = " << qPrintable(deltaStr) << endl; if (deltaStr.isEmpty()) { delta = -1; - return true; + return; } - // look for parameter names - bool ok = true; - double result = evaluateExpression(deltaStr, &ok); - if (!ok) return false; - + // look for parameter names + double result = 0; + try { + result = evaluateExpression(deltaStr); + } + catch(Exception e) { + throw(e); + } delta = result; cout << "delta = " << delta << endl; - - return true; } -bool FunctionalBlock::createConsumptionPattern() { +void FunctionalBlock::createConsumptionPattern() throw(Exception) { static QString fctName = "FunctionalBlock::createConsumptionPattern()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; #endif - lengthCP = -1; - bool ok = true; + lengthCP = -1; QHash consPattern = implementation->getConsumptionPattern(); foreach(AbstractInterface* iface, getControlInputs()) { FunctionalInterface* connIface = AI_TO_FUN(iface); QString refName = connIface->getReference()->getName(); if (! consPattern.contains(refName)) { + throw(Exception(NO_IFACE_CP)); cerr << "no consumption pattern for reference interface " << qPrintable(refName) << endl; - return false; } - QList* pattern = expandPattern(consPattern.value(refName),&ok); - if (!ok) return false; + QList* pattern = NULL; + try { + pattern = expandPattern(consPattern.value(refName)); + } + catch(Exception e) { + throw(e); + } consumptionPattern.insert(connIface,pattern); if (lengthCP == -1) { lengthCP = pattern->size(); } else { - if (pattern->size() != lengthCP) return false; + if (pattern->size() != lengthCP) { + throw(Exception(INVALID_IFACE_CP_LENGTH)); + } } - } - return true; + } } -bool FunctionalBlock::createProductionPattern() { +void FunctionalBlock::createProductionPattern() throw(Exception){ static QString fctName = "FunctionalBlock::createProductionPattern()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; #endif - lengthPP = -1; - bool ok = true; + lengthPP = -1; QHash prodPattern = implementation->getProductionPattern(); foreach(AbstractInterface* iface, getControlOutputs()) { FunctionalInterface* connIface = AI_TO_FUN(iface); QString refName = connIface->getReference()->getName(); if (! prodPattern.contains(refName)) { - cerr << "no production pattern for reference interface " << qPrintable(refName) << endl; - return false; + throw(Exception(NO_IFACE_PP)); + } + QList* pattern = NULL; + try { + pattern = expandPattern(prodPattern.value(refName)); + } + catch(Exception e) { + throw(e); } - QList* pattern = expandPattern(prodPattern.value(refName),&ok); - if (!ok) return false; productionPattern.insert(connIface,pattern); if (lengthPP == -1) { lengthPP = pattern->size(); } else { - if (pattern->size() != lengthPP) return false; + if (pattern->size() != lengthPP) { + throw(Exception(INVALID_IFACE_PP_LENGTH)); + } } - } - return true; + } } -bool FunctionalBlock::createProductionCounter() { +void FunctionalBlock::createProductionCounter() throw(Exception) { static QString fctName = "FunctionalBlock::createProductionCounter()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; @@ -236,14 +257,20 @@ bool FunctionalBlock::createProductionCounter() { s.remove(0,1); s.chop(1); QStringList gen = s.split(":"); - if (gen.size() != 3) return false; + if (gen.size() != 3) { + throw(Exception(INVALID_IFACE_PC)); + } int start = 0; int nb = 0; int step = 0; - for(int i=0;i<3;i++) { - bool okVal; - double result = evaluateExpression(gen.at(i),&okVal); - if (!okVal) return false; + for(int i=0;i<3;i++) { + double result = 0.0; + try { + result = evaluateExpression(gen.at(i)); + } + catch(Exception e) { + throw(e); + } if (i==0) start = result; else if (i==1) nb = result; else if (i==2) step = result; @@ -252,11 +279,14 @@ bool FunctionalBlock::createProductionCounter() { productionCounter.append(start+j*step); } } - else { - bool okVal; - double result = evaluateExpression(s,&okVal); - - if (!okVal) return false; + else { + double result = 0.0; + try { + result = evaluateExpression(s); + } + catch(Exception e) { + throw(e); + } productionCounter.append(result); } } @@ -264,11 +294,9 @@ bool FunctionalBlock::createProductionCounter() { cout << val << ","; } cout << endl; - - return true; } -QList* FunctionalBlock::expandPattern(const QString& patternIn, bool* ok) { +QList* FunctionalBlock::expandPattern(const QString& patternIn) throw(Exception) { static QString fctName = "FunctionalBlock::expandPattern()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; @@ -277,22 +305,30 @@ QList* FunctionalBlock::expandPattern(const QString& patternIn, bool* ok) QList lst; QString p = patternIn; p.append(')'); - int offset = 0; + int offset = 0; QList* patternOut = new QList(); - expandPatternRecur(p,&offset,ok,patternOut); + try { + expandPatternRecur(p,&offset, patternOut); + } + catch(Exception e) { + throw(e); + } + return patternOut; } -void FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset, bool *ok, QList* patternOut) { +void FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset, QList* patternOut) throw(Exception) { while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) { QChar c = patternIn.at(*offset); if (c == '(') { *offset += 1; - expandPatternRecur(patternIn,offset, ok, patternOut); - if (!ok) { - return; + try { + expandPatternRecur(patternIn,offset, patternOut); + } + catch(Exception e) { + throw(e); } } else if (c == '0') { @@ -312,13 +348,15 @@ void FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset, *offset += 1; } if (*offset == patternIn.size()) { - *ok = false; - return; + throw(Exception(INVALID_IFACE_PATTERN)); + } + double repeat = 0; + try { + repeat = evaluateExpression(expr); + } + catch(Exception e) { + throw(e); } - double repeat = evaluateExpression(expr,ok); - if (!ok) { - return; - } // repeat just the last value in currentGroup char last = patternOut->last(); //cout << "repeat last char " << repeat << " times : " << (int)last << endl; @@ -339,12 +377,14 @@ void FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset, *offset += 1; } if (*offset == patternIn.size()) { - *ok = false; - return; + throw(Exception(INVALID_IFACE_PATTERN)); } - double repeat = evaluateExpression(expr,ok); - if (!ok) { - return; + double repeat = 0; + try { + repeat = evaluateExpression(expr); + } + catch(Exception e) { + throw(e); } /* cout << "repeat last group " << repeat << " times : "; @@ -355,18 +395,15 @@ void FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset, for(int i=1;i<(int)repeat;i++) { patternOut->append(single); } - } - //*offset += 1; - return; + } } -double FunctionalBlock::evaluateExpression(const QString& expression, bool* ok) { +double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) { static QString fctName = "FunctionalBlock::evaluateExpression()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; #endif - - *ok = true; + QHash vars; evaluator->setExpression(expression); QList varNames = evaluator->getVariableNames(); @@ -375,34 +412,29 @@ double FunctionalBlock::evaluateExpression(const QString& expression, bool* ok) paramName.remove(0,1); BlockParameter* param = reference->getParameterFromName(paramName); if (param == NULL) { - cerr << "found an unknown parameter in delta"<< endl; - *ok = false; - return 0.0; + throw(Exception(EVAL_PARAM_UNKNOWN)); } bool okVal; int val = param->getDoubleValue(&okVal); if (!okVal) { - cerr << "cannot obtain double value of paramter " << qPrintable(paramName) << endl; - *ok = false; - return 0.0; + throw(Exception(EVAL_PARAM_NOVALUE)); } vars.insert(name,(double)val); } evaluator->setVariablesValue(vars); - double result; + double result = 0.0; try { result = evaluator->evaluate(); } catch(int index) { cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl; - *ok = false; - return 0.0; + throw(Exception(EVAL_INVALID_EXPR)); } return result; } -void FunctionalBlock::createInputPattern() { +void FunctionalBlock::createInputPattern() throw(Exception) { static QString fctName = "FunctionalBlock::createInputPattern())"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; @@ -412,38 +444,36 @@ void FunctionalBlock::createInputPattern() { foreach(AbstractInterface* iface, getControlInputs()) { ConnectedInterface* connIface = AI_TO_CON(iface); QList* out = connIface->getConnectedFrom()->getOutputPattern(); + if (out->size() == 0) { + clearInputPattern(); + throw(Exception(NO_IFACE_IP)); + } if (lengthIP == -1) { lengthIP = out->size(); } else { if (out->size() < lengthIP) lengthIP = out->size(); } - if (out->size() > 0) { - QList* in = new QList(*out); - foreach(char c, *in) { - cout << (int)c; - } - cout << endl; - - inputPattern.insert(connIface,in); + + QList* in = new QList(*out); + foreach(char c, *in) { + cout << (int)c; } - else { - inputPattern.insert(connIface,NULL); - } + cout << endl; + inputPattern.insert(connIface,in); } - // remove null columns at the end of IP + // search the last valid group in IP, while(! isValidDataGroup(inputPattern,lengthIP-1)) { - removeDataGroup(inputPattern,lengthIP-1); + //removeDataGroup(inputPattern,lengthIP-1); lengthIP -= 1; } } -bool FunctionalBlock::createAdmittance(int nbExec) { +void FunctionalBlock::createAdmittance(int nbExec) throw(Exception) { static QString fctName = "FunctionalBlock::createAdmittance()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; -#endif - bool ok = true; +#endif // firstly, copy CP in AP QMapIterator* > iterC(consumptionPattern); while (iterC.hasNext()) { @@ -497,8 +527,7 @@ bool FunctionalBlock::createAdmittance(int nbExec) { j -= 1; } else { - cout << "AP and CP are not consistent" << endl; - return false; + throw(INVALID_DELTA_CP); } } } @@ -512,32 +541,31 @@ bool FunctionalBlock::createAdmittance(int nbExec) { cout << (int)(pattern->at(i)); } cout << endl; - } - return true; + } } -bool FunctionalBlock::checkInputPatternCompatibility() { +void FunctionalBlock::checkInputPatternCompatibility() throw(Exception) { static QString fctName = "FunctionalBlock::checkInputPatternCompatibility()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; #endif - - bool ok = true; + // firstly, create input pattern - createInputPattern(); - // if some patterns are not available, end now, returning false - if (lengthIP == 0) { - clearInputPattern(); - return false; + try { + createInputPattern(); + } + catch(Exception e) { + throw(e); } - int nbExec = getNumberOfExecution(); + int nbExec = createTriggers(); cout << qPrintable(name) << " will exec. " << nbExec << " times." << endl; - ok = createAdmittance(nbExec); - - if (!ok) { + try { + createAdmittance(nbExec); + } + catch(Exception e) { cout << "cannot create admittance" << endl; - return false; + throw(e); } int clock = 0; // index in IP @@ -549,7 +577,7 @@ bool FunctionalBlock::checkInputPatternCompatibility() { while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++; if (clock == lengthIP) { cerr << "Abnormal case: end of IP has been reached without finding a valid group" << endl; - return false; + throw(Exception(IP_END_NULLCOL)); } } /* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or @@ -557,19 +585,18 @@ bool FunctionalBlock::checkInputPatternCompatibility() { */ if (! samePatterns(inputPattern,clock,admittance,i)) { cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl; - return false; + throw(Exception(IP_AP_NOTCOMPAT)); // IP and AP not compatible } clock++; i++; } if (clock < lengthIP) { - cerr << "Abnormal case: AP is to short" << endl; - return false; - } - return true; + throw(Exception(AP_TOO_SHORT)); + cerr << "Abnormal case: AP is to short" << endl; + } } -bool FunctionalBlock::computeOutputPattern(int nbExec) { +void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) { static QString fctName = "FunctionalBlock::computeOutputPattern()"; #ifdef DEBUG_FCTNAME cout << "call to " << qPrintable(fctName) << endl; @@ -600,11 +627,11 @@ bool FunctionalBlock::computeOutputPattern(int nbExec) { // in case of inputPattern not created, do it if (lengthIP <= 0) { // collect the input patterns for each input - createInputPattern(); - // if some patterns are not available, end now, returning false - if (lengthIP == 0) { - clearInputPattern(); - return false; + try { + createInputPattern(); + } + catch(Exception e) { + throw(e); } cout << "input pattern array initialized with min. len " << lengthIP << endl; } @@ -692,8 +719,7 @@ bool FunctionalBlock::computeOutputPattern(int nbExec) { // clear input pattern clearInputPattern(); - } - return true; + } } bool FunctionalBlock::samePatterns(const QMap* >& patternSrc, int srcCol, const QMap* >& patternDest, int destCol) { @@ -842,24 +868,24 @@ void FunctionalBlock::clearInputPattern() { lengthIP = -1; } -int FunctionalBlock::getNumberOfExecution() { +int FunctionalBlock::createTriggers() { + triggers.clear(); /* NB: this method returns the number of executions that have been started but not necessary completed. */ - if (delta <= 0) return 0; - int nbExec = 0; + if (delta <= 0) return 0; int offset = 0; // search for the first exec. while ((offset < lengthIP) && (! isValidDataGroup(inputPattern,offset))) offset++; if (offset == lengthIP) return 0; - nbExec = 1; + triggers.append(offset); int nbGroup = 0; for(int i = offset;i