+QList<char> FunctionalBlock::expandPattern(const QString& pattern, bool* ok) {\r
+ static QString fctName = "FunctionalBlock::expandPattern()";\r
+#ifdef DEBUG_FCTNAME\r
+ cout << "call to " << qPrintable(fctName) << endl;\r
+#endif\r
+ \r
+ QList<char> lst;\r
+ QString p = pattern;\r
+ p.append(')');\r
+ int offset = 0;\r
+ lst = expandPatternRecur(p,&offset,ok); \r
+ return lst;\r
+}\r
+\r
+QList<char> FunctionalBlock::expandPatternRecur(const QString& pattern, int *offset, bool *ok) {\r
+ \r
+ QList<char> currentGroup; \r
+ \r
+ while ((*offset < pattern.size()) && (pattern.at(*offset) != ')')) {\r
+ \r
+ QChar c = pattern.at(*offset);\r
+ if (c == '(') {\r
+ *offset += 1;\r
+ currentGroup += expandPatternRecur(pattern,offset, ok);\r
+ if (!ok) {\r
+ return currentGroup;\r
+ }\r
+ }\r
+ else if (c == '0') {\r
+ currentGroup.append(0);\r
+ }\r
+ else if (c == '1') {\r
+ currentGroup.append(1);\r
+ }\r
+ else if (c == 'X') {\r
+ currentGroup.append(-1);\r
+ }\r
+ else if (c == '{') {\r
+ *offset += 1;\r
+ QString expr = ""; \r
+ while ((*offset < pattern.size()) && (pattern.at(*offset) != '}')) {\r
+ expr += pattern.at(*offset); \r
+ *offset += 1;\r
+ }\r
+ if (*offset == pattern.size()) {\r
+ *ok = false;\r
+ return currentGroup;\r
+ }\r
+ double repeat = evaluateExpression(expr,ok);\r
+ if (!ok) {\r
+ return currentGroup; \r
+ } \r
+ // repeat just the last value in currentGroup\r
+ char last = currentGroup.last(); \r
+ //cout << "repeat last char " << repeat << " times : " << (int)last << endl;\r
+ \r
+ for(int i=1;i<(int)repeat;i++) {\r
+ currentGroup += last;\r
+ }\r
+ } \r
+ *offset += 1;\r
+ }\r
+ \r
+ // must check if after ), there is a {\r
+ if ((*offset < pattern.size()-1) && (pattern.at(*offset+1) == '{')) {\r
+ *offset += 2;\r
+ QString expr = ""; \r
+ while ((*offset < pattern.size()) && (pattern.at(*offset) != '}')) {\r
+ expr += pattern.at(*offset); \r
+ *offset += 1;\r
+ }\r
+ if (*offset == pattern.size()) {\r
+ *ok = false;\r
+ return currentGroup;\r
+ }\r
+ double repeat = evaluateExpression(expr,ok);\r
+ if (!ok) {\r
+ return currentGroup; \r
+ }\r
+ /*\r
+ cout << "repeat last group " << repeat << " times : ";\r
+ foreach (char c, currentGroup) cout <<(int)c;\r
+ cout << endl; \r
+ */\r
+ QList<char> single = currentGroup;\r
+ for(int i=1;i<(int)repeat;i++) {\r
+ currentGroup += single;\r
+ } \r
+ }\r
+ //*offset += 1;\r
+ return currentGroup;\r
+}\r
+\r
+double FunctionalBlock::evaluateExpression(const QString& expression, bool* ok) {\r
+ static QString fctName = "FunctionalBlock::evaluateExpression()";\r
+#ifdef DEBUG_FCTNAME\r
+ cout << "call to " << qPrintable(fctName) << endl;\r
+#endif\r
+ \r
+ *ok = true;\r
+ QHash<QString,double> vars;\r
+ evaluator->setExpression(expression);\r
+ QList<QString> varNames = evaluator->getVariableNames();\r
+ foreach (QString name, varNames) {\r
+ QString paramName = name;\r
+ paramName.remove(0,1);\r
+ BlockParameter* param = reference->getParameterFromName(paramName); \r
+ if (param == NULL) {\r
+ cerr << "found an unknown parameter in delta"<< endl;\r
+ *ok = false;\r
+ return 0.0;\r
+ }\r
+ bool okVal;\r
+ int val = param->getDoubleValue(&okVal);\r
+ if (!okVal) {\r
+ cerr << "cannot obtain double value of paramter " << qPrintable(paramName) << endl;\r
+ *ok = false;\r
+ return 0.0;\r
+ }\r
+ vars.insert(name,(double)val); \r
+ } \r
+ evaluator->setVariablesValue(vars);\r
+ double result;\r
+ try {\r
+ result = evaluator->evaluate();\r
+ }\r
+ catch(int index) {\r
+ cerr << "Error at index " << index << ": " << qPrintable(evaluator->getError()) << endl;\r
+ *ok = false;\r
+ return 0.0;\r
+ }\r
+ return result;\r
+}\r
+\r