X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/blast.git/blobdiff_plain/dd3fe103df79a5a4c2962e2f05fec9a9ed58580d..df84660fd98b39737692d57b68c8b2c2d34af0f1:/FunctionalBlock.cpp?ds=sidebyside diff --git a/FunctionalBlock.cpp b/FunctionalBlock.cpp index cb8ceba..7be000a 100644 --- a/FunctionalBlock.cpp +++ b/FunctionalBlock.cpp @@ -5,6 +5,7 @@ #include "FunctionalInterface.h" #include "ReferenceInterface.h" #include "BlockParameter.h" +#include "ArithmeticEvaluator.h" FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference) throw(Exception) : AbstractBlock() { @@ -13,8 +14,26 @@ FunctionalBlock::FunctionalBlock(GroupBlock *_parent, ReferenceBlock *_reference reference = _reference; parent = _parent; name = reference->getName(); + + if (reference->getImplementations().isEmpty()) { + implementation = NULL; + cout << "block has no implementation" << endl; + } + else { + implementation = reference->getImplementations().at(0); + } + lengthAP = -1; + lengthCP = -1; + lengthIP = -1; + lengthOP = -1; + lengthPP = -1; + delta = -1; + evaluator = NULL; } +FunctionalBlock::~FunctionalBlock() { + if (evaluator != NULL) delete evaluator; +} void FunctionalBlock::parametersValidation(QList* checkedBlocks, QList *blocksToConfigure) { /* @@ -41,6 +60,11 @@ bool FunctionalBlock::isFunctionalBlock() { return true; } +bool FunctionalBlock::isSourceBlock() { + if (parent == NULL) return true; + return false; +} + void FunctionalBlock::populate() { int i; BlockParameter* p; @@ -91,7 +115,914 @@ QString FunctionalBlock::getReferenceXmlFile() { return ((ReferenceBlock *)reference)->getXmlFile(); } -QString FunctionalBlock::getReferenceHashMd5() -{ +QString FunctionalBlock::getReferenceHashMd5() { return ((ReferenceBlock *)reference)->getHashMd5(); } + +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; + if (evaluator == NULL) evaluator = new ArithmeticEvaluator(); + if (! isGeneratorBlock()) { + try { + createDelta(); + createConsumptionPattern(); + createProductionCounter(); + } + catch(Exception e) { + throw(e); // rethrow e + } + } + try { + createProductionPattern(); + } + catch(Exception e) { + throw(e); + } + cout << "PP of " << qPrintable(name) << endl; + QMapIterator* > it(productionPattern); + while (it.hasNext()) { + it.next(); + QList* pat = it.value(); + foreach(char c, *pat) cout << (int)c; + cout << endl; + } +} + +void FunctionalBlock::createDelta() throw(Exception) { + static QString fctName = "FunctionalBlock::createDelta()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + QString deltaStr = implementation->getDelta(); + cout << "delta for " << qPrintable(name) << " = " << qPrintable(deltaStr) << endl; + if (deltaStr.isEmpty()) { + delta = -1; + return; + } + + // look for parameter names + double result = 0; + try { + result = evaluateExpression(deltaStr); + } + catch(Exception e) { + throw(e); + } + delta = result; + cout << "delta = " << delta << endl; +} + +void FunctionalBlock::createConsumptionPattern() throw(Exception) { + static QString fctName = "FunctionalBlock::createConsumptionPattern()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + 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; + } + 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) { + throw(Exception(INVALID_IFACE_CP_LENGTH)); + } + } + } +} + +void FunctionalBlock::createProductionPattern() throw(Exception){ + static QString fctName = "FunctionalBlock::createProductionPattern()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + 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)) { + throw(Exception(NO_IFACE_PP)); + } + QList* pattern = NULL; + try { + pattern = expandPattern(prodPattern.value(refName)); + } + catch(Exception e) { + throw(e); + } + productionPattern.insert(connIface,pattern); + if (lengthPP == -1) { + lengthPP = pattern->size(); + } + else { + if (pattern->size() != lengthPP) { + throw(Exception(INVALID_IFACE_PP_LENGTH)); + } + } + } +} + +void FunctionalBlock::createProductionCounter() throw(Exception) { + static QString fctName = "FunctionalBlock::createProductionCounter()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + QStringList counterParts = implementation->getProductionCounter().split(","); + foreach(QString s, counterParts) { + cout << "cont part = " << qPrintable(s) << endl; + bool ok; + double val = s.toDouble(&ok); + if (ok) { + productionCounter.append(val); + } + else if (s.at(0) == '{') { + s.remove(0,1); + s.chop(1); + QStringList gen = s.split(":"); + 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++) { + 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; + } + for(int j=0;j* FunctionalBlock::expandPattern(const QString& patternIn) throw(Exception) { + static QString fctName = "FunctionalBlock::expandPattern()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + QList lst; + QString p = patternIn; + p.append(')'); + int offset = 0; + QList* patternOut = new QList(); + try { + patternOut->append(expandPatternRecur(p,&offset)); + } + catch(Exception e) { + throw(e); + } + + return patternOut; +} + +QList FunctionalBlock::expandPatternRecur(const QString& patternIn, int *offset) throw(Exception) { + + QList patternOut; + + while ((*offset < patternIn.size()) && (patternIn.at(*offset) != ')')) { + + QChar c = patternIn.at(*offset); + if (c == '(') { + *offset += 1; + try { + patternOut.append(expandPatternRecur(patternIn,offset)); + } + catch(Exception e) { + throw(e); + } + } + else if (c == '0') { + patternOut.append(0); + } + else if (c == '1') { + patternOut.append(1); + } + else if (c == 'X') { + patternOut.append(-1); + } + else if (c == '{') { + *offset += 1; + QString expr = ""; + while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) { + expr += patternIn.at(*offset); + *offset += 1; + } + if (*offset == patternIn.size()) { + throw(Exception(INVALID_IFACE_PATTERN)); + } + double repeat = 0; + try { + repeat = evaluateExpression(expr); + } + catch(Exception e) { + throw(e); + } + // repeat just the last value in currentGroup + char last = patternOut.last(); + //cout << "repeat last char " << repeat << " times : " << (int)last << endl; + + for(int i=1;i<(int)repeat;i++) { + patternOut.append(last); + } + } + *offset += 1; + } + + // must check if after ), there is a { + if ((*offset < patternIn.size()-1) && (patternIn.at(*offset+1) == '{')) { + *offset += 2; + QString expr = ""; + while ((*offset < patternIn.size()) && (patternIn.at(*offset) != '}')) { + expr += patternIn.at(*offset); + *offset += 1; + } + if (*offset == patternIn.size()) { + throw(Exception(INVALID_IFACE_PATTERN)); + } + double repeat = 0; + try { + repeat = evaluateExpression(expr); + } + catch(Exception e) { + throw(e); + } + /* + cout << "repeat last group " << repeat << " times : "; + foreach (char c, currentGroup) cout <<(int)c; + cout << endl; + */ + QList single = patternOut; + for(int i=1;i<(int)repeat;i++) { + patternOut.append(single); + } + } + return patternOut; +} + +double FunctionalBlock::evaluateExpression(const QString& expression) throw(Exception) { + static QString fctName = "FunctionalBlock::evaluateExpression()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + QHash vars; + evaluator->setExpression(expression); + QList varNames = evaluator->getVariableNames(); + foreach (QString name, varNames) { + QString paramName = name; + paramName.remove(0,1); + BlockParameter* param = reference->getParameterFromName(paramName); + if (param == NULL) { + throw(Exception(EVAL_PARAM_UNKNOWN)); + } + bool okVal; + int val = param->getDoubleValue(&okVal); + if (!okVal) { + throw(Exception(EVAL_PARAM_NOVALUE)); + } + vars.insert(name,(double)val); + } + + evaluator->setVariablesValue(vars); + double result = 0.0; + try { + result = evaluator->evaluate(); + } + catch(int index) { + cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl; + throw(Exception(EVAL_INVALID_EXPR)); + } + return result; +} + +void FunctionalBlock::createInputPattern() throw(Exception) { + static QString fctName = "FunctionalBlock::createInputPattern())"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + lengthIP = -1; + 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(); + } + + QList* in = new QList(*out); + foreach(char c, *in) { + cout << (int)c; + } + cout << endl; + inputPattern.insert(connIface,in); + } + // search the last valid group in IP, + while(! isValidDataGroup(inputPattern,lengthIP-1)) { + //removeDataGroup(inputPattern,lengthIP-1); + lengthIP -= 1; + } +} + +void FunctionalBlock::createAdmittance(int nbExec) throw(Exception) { + static QString fctName = "FunctionalBlock::createAdmittance()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + // firstly, copy CP in AP + QMapIterator* > iterC(consumptionPattern); + while (iterC.hasNext()) { + iterC.next(); + QList* pattern = new QList(*(iterC.value())); + admittance.insert(iterC.key(), pattern); + } + lengthAP = lengthCP; + int clock = 0; + cout << "trigger 1 at c.c. 0" << endl; + for(int i=1;i* > iterA(admittance); + while (iterA.hasNext()) { + iterA.next(); + QList* pattern = iterA.value(); + for(int i=0;isize();i++) { + if (pattern->at(i) == -1) pattern->replace(i,0); + cout << (int)(pattern->at(i)); + } + cout << endl; + } +} + +void FunctionalBlock::checkInputPatternCompatibility() throw(Exception) { + static QString fctName = "FunctionalBlock::checkInputPatternCompatibility()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + // firstly, create input pattern + try { + createInputPattern(); + } + catch(Exception e) { + throw(e); + } + int nbExec = createTriggers(); + cout << qPrintable(name) << " will exec. " << nbExec << " times." << endl; + + try { + createAdmittance(nbExec); + } + catch(Exception e) { + cout << "cannot create admittance" << endl; + throw(e); + } + + int clock = 0; // index in IP + int i = 0; // index in AP + while ((clock < lengthIP) && (i < lengthAP)) { + + // if AP is a valid group, search for the next valid group in IP + if (isValidDataGroup(admittance,i)) { + 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; + throw(Exception(IP_END_NULLCOL)); + } + } + /* at that point 2 cases of compat : IP(clock) and AP(i) are equal valid group, or + are both null columns + */ + if (! samePatterns(inputPattern,clock,admittance,i)) { + cout << "AP(" << i << ") and IP(" << clock << ") are not equal" << endl; + throw(Exception(IP_AP_NOTCOMPAT)); // IP and AP not compatible + } + clock++; + i++; + } + if (clock < lengthIP) { + throw(Exception(AP_TOO_SHORT)); + cerr << "Abnormal case: AP is to short" << endl; + } +} + +void FunctionalBlock::computeOutputPattern(int nbExec) throw(Exception) { + static QString fctName = "FunctionalBlock::computeOutputPattern()"; +#ifdef DEBUG_FCTNAME + cout << "call to " << qPrintable(fctName) << endl; +#endif + + /* case 1: the block is a generator for which output pattern + must be computed for a nbExec following executions + */ + + + if (nbExec > 0) { + cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl; + foreach(AbstractInterface* iface, getControlOutputs()) { + FunctionalInterface* connIface = AI_TO_FUN(iface); + // create output pattern + QList* pp = productionPattern.value(connIface); + QList* pattern = new QList(*pp); + for(int i=1;iappend(*pp); + // assign pattern to interface + connIface->setOutputPattern(pattern); + // store it in QMap + outputPattern.insert(connIface,pattern); + } + } + else { + cout << "computing output pattern of " << qPrintable(name) << endl; + + // in case of inputPattern not created, do it + if (lengthIP <= 0) { + // collect the input patterns for each input + try { + createInputPattern(); + } + catch(Exception e) { + throw(e); + } + cout << "input pattern array initialized with min. len " << lengthIP << endl; + } + + // initialize the output pattern + lengthOP = 0; + foreach(AbstractInterface* iface, getControlOutputs()) { + FunctionalInterface* connIface = AI_TO_FUN(iface); + lengthOP = lengthIP+productionPattern.value(connIface)->size(); + QList* pattern = new QList(); + for(int i=0;iappend(0); + connIface->setOutputPattern(pattern); + outputPattern.insert(connIface,pattern); + } + cout << "output pattern array initialized" << endl; + + int clock = 0; + nbExec = 0; + // search for the beginning of the first execution. + while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++; + cout << "found 1st exec clock: " << clock << endl; + + while (clock < lengthIP) { + // initialize counters for current execution. + int p = 0; // index in production pattern + int o = 0; // clock+o will give the clock cycle of each output group + int cip = 0; // clock+cip give the clock cycle of an input group + int ccp = 0; // ccp give a column in the consumptio pattern + int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP + int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP + bool cannotCompleteExec = false; + for(int m=0;m 0) { + cout << "computing output pattern of " << qPrintable(name) << " for " << nbExec << " executions" << endl; + foreach(AbstractInterface* iface, getControlOutputs()) { + FunctionalInterface* connIface = AI_TO_FUN(iface); + // create output pattern + QList* pp = productionPattern.value(connIface); + QList* pattern = new QList(*pp); + for(int i=1;iappend(*pp); + // assign pattern to interface + connIface->setOutputPattern(pattern); + // store it in QMap + outputPattern.insert(connIface,pattern); + } + } + else { + cout << "computing output pattern of " << qPrintable(name) << endl; + + // in case of inputPattern not created, do it + if (lengthIP <= 0) { + // collect the input patterns for each input + try { + createInputPattern(); + } + catch(Exception e) { + throw(e); + } + cout << "input pattern array initialized with min. len " << lengthIP << endl; + } + + // initialize the output pattern + lengthOP = 0; + foreach(AbstractInterface* iface, getControlOutputs()) { + FunctionalInterface* connIface = AI_TO_FUN(iface); + lengthOP = lengthIP+productionPattern.value(connIface)->size(); + QList* pattern = new QList(); + for(int i=0;iappend(0); + connIface->setOutputPattern(pattern); + outputPattern.insert(connIface,pattern); + } + cout << "output pattern array initialized" << endl; + + int clock = 0; + nbExec = 0; + // search for the beginning of the first execution. + while ((clock < lengthIP) && (! isValidDataGroup(inputPattern,clock))) clock++; + cout << "found 1st exec clock: " << clock << endl; + + while (clock < lengthIP) { + // initialize counters for current execution. + int p = 0; // index in production pattern + int o = 0; // clock+o will give the clock cycle of each output group + int cip = 0; // clock+cip give the clock cycle of an input group + int ccp = 0; // ccp give a column in the consumptio pattern + int nip = 0; // number of input data groups already consumed during the current execution, used while exploring IP + int ncp = 0; // number of input data groups already consumed during the current execution, used while exploring CP + bool cannotCompleteExec = false; + for(int m=0;m* >& patternSrc, int srcCol, const QMap* >& patternDest, int destCol) { + + if (patternSrc.size() != patternDest.size()) return false; + QMapIterator* > iterSrc(patternSrc); + QMapIterator* > iterDest(patternDest); + while (iterSrc.hasNext()) { + iterSrc.next(); + iterDest.next(); + QList* srcPat = iterSrc.value(); + QList* destPat = iterDest.value(); + if (srcCol >= srcPat->size()) return false; + if (destCol >= destPat->size()) return false; + if (srcPat->at(srcCol) != destPat->at(destCol)) return false; + } + return true; +} + +bool FunctionalBlock::canCombinePatterns(const QMap* >& patternSrc, int srcCol, QMap* > patternDest, int destCol) { + if (patternSrc.size() != patternDest.size()) return false; + QMapIterator* > iterSrc(patternSrc); + QMapIterator* > iterDest(patternDest); + while (iterSrc.hasNext()) { + iterSrc.next(); + iterDest.next(); + QList* srcPat = iterSrc.value(); + QList* destPat = iterDest.value(); + if (srcCol >= srcPat->size()) return false; + if (destCol >= destPat->size()) return false; + if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return false; + if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return false; + } + return true; +} + +void FunctionalBlock::combinePatterns(const QMap* >& patternSrc, int srcCol, QMap* > patternDest, int destCol) { + if (patternSrc.size() != patternDest.size()) return; + QMapIterator* > iterSrc(patternSrc); + QMapIterator* > iterDest(patternDest); + while (iterSrc.hasNext()) { + iterSrc.next(); + iterDest.next(); + QList* srcPat = iterSrc.value(); + QList* destPat = iterDest.value(); + if (srcCol >= srcPat->size()) return; + if (destCol >= destPat->size()) return; + if ((srcPat->at(srcCol) == -1) && (destPat->at(destCol) == 1)) return; + if ((srcPat->at(srcCol) == 1) && (destPat->at(destCol) == -1)) return; + destPat->replace(destCol,destPat->at(destCol) | srcPat->at(srcCol)); + } +} + +void FunctionalBlock::appendToPattern(const QMap* >& patternSrc, int srcCol, QMap* > patternDest, int nbCols) { + if (patternSrc.size() != patternDest.size()) return; + QMapIterator* > iterSrc(patternSrc); + QMapIterator* > iterDest(patternDest); + while (iterSrc.hasNext()) { + iterSrc.next(); + iterDest.next(); + QList* srcPat = iterSrc.value(); + QList* destPat = iterDest.value(); + int i=0; + while ((srcCol+i < srcPat->size()) && (iappend(srcPat->at(srcCol+i)); + i++; + } + } +} + +void FunctionalBlock::removeDataGroup(QMap *> &pattern, int offset) { + QMapIterator* > iterSrc(pattern); + while (iterSrc.hasNext()) { + iterSrc.next(); + QList* srcPat = iterSrc.value(); + if (offset < srcPat->size()) { + srcPat->removeAt(offset); + } + } +} + +void FunctionalBlock::shiftRightPattern(const QMap *> &pattern, int offset) { + QMapIterator* > iterSrc(pattern); + while (iterSrc.hasNext()) { + iterSrc.next(); + QList* srcPat = iterSrc.value(); + if (offset < srcPat->size()) { + srcPat->insert(offset,0); + } + } +} + +bool FunctionalBlock::isValidDataGroup(const QMap *> &pattern, int offset) { + QMapIterator* > iterSrc(pattern); + while (iterSrc.hasNext()) { + iterSrc.next(); + QList* srcPat = iterSrc.value(); + if (offset >= srcPat->size()) return false; + if (srcPat->at(offset) == 1) return true; + } + return false; +} + +bool FunctionalBlock::isOnlyXDataGroup(const QMap *> &pattern, int offset) { + QMapIterator* > iterSrc(pattern); + while (iterSrc.hasNext()) { + iterSrc.next(); + QList* srcPat = iterSrc.value(); + if (offset >= srcPat->size()) return false; + if (srcPat->at(offset) != -1) return false; + } + return true; +} + +void FunctionalBlock::clearConsumptionPattern() { + QMapIterator* > iterP(consumptionPattern); + while (iterP.hasNext()) { + iterP.next(); + QList* pattern = iterP.value(); + if (pattern != NULL) delete pattern; + } + consumptionPattern.clear(); + lengthCP = -1; +} + +void FunctionalBlock::clearProductionPattern() { + QMapIterator* > iterP(productionPattern); + while (iterP.hasNext()) { + iterP.next(); + QList* pattern = iterP.value(); + if (pattern != NULL) delete pattern; + } + productionPattern.clear(); + lengthPP = -1; +} + +void FunctionalBlock::clearInputPattern() { + + QMapIterator* > iterI(inputPattern); + while (iterI.hasNext()) { + iterI.next(); + QList* pattern = iterI.value(); + if (pattern != NULL) delete pattern; + } + inputPattern.clear(); + lengthIP = -1; +} + +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 offset = 0; + // search for the first exec. + while ((offset < lengthIP) && (! isValidDataGroup(inputPattern,offset))) offset++; + if (offset == lengthIP) return 0; + triggers.append(offset); + int nbGroup = 0; + for(int i = offset;i