1 #include "BlockImplementation.h"
\r
3 #include "FunctionalBlock.h"
\r
4 #include "ReferenceBlock.h"
\r
5 #include "ReferenceInterface.h"
\r
6 #include "FunctionalInterface.h"
\r
7 #include "BlockParameter.h"
\r
8 #include <QHashIterator>
\r
11 BlockImplementation::BlockImplementation(const QString& _xmlFile) {
\r
13 productionCounter = "";
\r
16 evaluator = new ArithmeticEvaluator;
\r
17 evaluator->setVariableMarkers("@$");
\r
20 BlockImplementation::BlockImplementation(const QString& _xmlFile, const QString &_referenceXml, const QString &_referenceMd5) {
\r
21 xmlFile = _xmlFile;
\r
22 productionCounter = "";
\r
24 referenceXml = _referenceXml;
\r
25 referenceMd5 = _referenceMd5;
\r
28 void BlockImplementation::loadPatterns(QDomElement& root) throw(Exception) {
\r
30 QDomNodeList patternNode = root.elementsByTagName("patterns");
\r
32 if (patternNode.isEmpty()) {
\r
33 cout << "impl has no patterns" << endl;
\r
37 QDomElement patternElt = patternNode.at(0).toElement();
\r
39 QDomElement eltDelta = patternElt.firstChildElement("delta");
\r
40 delta = eltDelta.attribute("value","none");
\r
42 QDomElement eltCons = eltDelta.nextSiblingElement("consumption");
\r
44 QDomNodeList listNodeInput = eltCons.elementsByTagName("input");
\r
45 for(int i=0; i<listNodeInput.size(); i++) {
\r
46 QDomNode node = listNodeInput.at(i);
\r
47 QDomElement elt = node.toElement();
\r
48 QString nameStr = elt.attribute("name","none");
\r
49 if (nameStr == "none") throw(Exception(IMPLFILE_CORRUPTED));
\r
50 QString patternStr = elt.attribute("pattern","none");
\r
51 consumptionPattern.insert(nameStr,patternStr);
\r
54 QDomElement eltProd = eltCons.nextSiblingElement("production");
\r
56 productionCounter = eltProd.attribute("counter","none");
\r
57 QDomNodeList listNodeOutput = eltProd.elementsByTagName("output");
\r
58 for(int i=0; i<listNodeOutput.size(); i++) {
\r
59 QDomNode node = listNodeOutput.at(i);
\r
60 QDomElement elt = node.toElement();
\r
61 QString nameStr = elt.attribute("name","none");
\r
62 if (nameStr == "none") throw(Exception(IMPLFILE_CORRUPTED));
\r
63 QString patternStr = elt.attribute("pattern","none");
\r
64 productionPattern.insert(nameStr,patternStr);
\r
66 cout << "patterns summary:" << endl;
\r
67 QHashIterator<QString,QString> iterP(productionPattern);
\r
68 while (iterP.hasNext()) {
\r
70 cout << qPrintable(iterP.key()) << " -> " << qPrintable(iterP.value()) << endl;
\r
72 cout << "impls patterns read correctly" << endl;
\r
75 bool BlockImplementation::checkPatterns() {
\r
78 if (reference == NULL) {
\r
79 cout << "no ref. while checking patterns of implementation " << endl;
\r
83 AbstractInterface* iface;
\r
84 QHashIterator<QString,QString> iterI(consumptionPattern);
\r
85 while (iterI.hasNext()) {
\r
87 iface = reference->getIfaceFromName(iterI.key());
\r
88 if (iface == NULL) {
\r
89 cout << "cannot found an input ref. iface for impl. iface " << qPrintable(iterI.key()) << endl;
\r
93 QHashIterator<QString,QString> iterO(productionPattern);
\r
94 while (iterO.hasNext()) {
\r
96 iface = reference->getIfaceFromName(iterO.key());
\r
97 if (iface == NULL) {
\r
98 cout << "cannot found an output ref. iface for impl. iface " << qPrintable(iterI.key()) << endl;
\r
105 QString BlockImplementation::eval(QString line, QTextStream& out) {
\r
106 QString res, s, begLine, endLine, expr;
\r
107 evaluator->setExpression(line);
\r
108 QRegExp *rxString = new QRegExp("(.*)@{(.*)}(.*)");
\r
109 QRegExp *rxValue = new QRegExp("(.*)@val{(.*)}(.*)");
\r
110 QRegExp *rxExpr = new QRegExp(".*@eval{(.*)}.*");
\r
112 int nbAt = line.count('@');
\r
114 for(int i = 0; i < line.size(); i++) {
\r
115 if(rxString->indexIn(line)) {
\r
116 begLine = rxString->cap(1);
\r
117 s = rxString->cap(2);
\r
118 endLine = rxString->cap(3);
\r
119 res = begLine + evalString(s) + endLine + '\n';
\r
123 for(int i = 0; i < line.size(); i++) {
\r
124 if(rxValue->indexIn(line)) {
\r
125 begLine = rxValue->cap(1);
\r
126 s = rxValue->cap(2);
\r
127 endLine = rxValue->cap(3);
\r
128 res = begLine + evalValue(s) + endLine + '\n';
\r
132 for(int i = 0; i < line.size(); i++) {
\r
133 if(rxExpr->indexIn(line)) {
\r
134 expr = rxExpr->cap(1);
\r
135 if(expr.count('@') == 0) {
\r
136 evaluator->setExpression(expr);
\r
137 s = QString::number(evaluator->evaluate());
\r
139 res = begLine + s + endLine + '\n';
\r
147 QString BlockImplementation::evalComplex(QString line, int id) {
\r
148 QString res, s, begLine, endLine, expr;
\r
149 QRegExp *rxString = new QRegExp("(.*)@{(.*)}(.*)");
\r
150 QRegExp *rxValue = new QRegExp("(.*)@val{(.*)}(.*)");
\r
151 QRegExp *rxExpr = new QRegExp(".*@eval{(.*)}.*");
\r
152 QRegExp *rxFor = new QRegExp("@foreach{.*}(@{.*})(.*)@endforeach");
\r
153 QRegExp *rxCase = new QRegExp("@caseeach{.*,(.*),(.*)}(@{.*})(.*)@endcaseeach");
\r
154 QRegExp *rxCaseDown = new QRegExp("@#-:(.*)");
\r
155 QRegExp *rxCaseUp = new QRegExp("@#:(.*)");
\r
156 evaluator->setExpression(line);
\r
158 int nbAt = line.count('@') - 2;
\r
160 for(int i = 0; i < line.size(); i++) {
\r
161 if(rxString->indexIn(line)) {
\r
162 begLine = rxString->cap(1);
\r
163 s = rxString->cap(2);
\r
164 endLine = rxString->cap(3);
\r
165 if(evalStringComplex(s)->size() == 0)
\r
166 line = begLine + evalString(s) + endLine;
\r
170 for(int i = 0; i < line.size(); i++) {
\r
171 if(rxValue->indexIn(line)) {
\r
172 begLine = rxValue->cap(1);
\r
173 s = rxValue->cap(2);
\r
174 endLine = rxValue->cap(3);
\r
175 line = begLine + evalValue(s) + endLine;
\r
179 for(int i = 0; i < line.size(); i++) {
\r
180 if(rxExpr->indexIn(line)) {
\r
181 expr = rxExpr->cap(1);
\r
182 if(expr.count('@') == 0) {
\r
183 evaluator->setExpression(expr);
\r
184 s = QString::number(evaluator->evaluate());
\r
186 res = begLine + s + endLine + '\n';
\r
193 if(rxFor->indexIn(line)) {
\r
194 QString intName, instruc;
\r
195 intName = rxFor->cap(1);
\r
196 instruc = rxFor->cap(2);
\r
197 QList<AbstractInterface*> *intList = evalStringComplex(intName);
\r
198 if(intList->size() != 0) {
\r
199 for(int i = 0; i < intList->size(); i++) {
\r
200 res = intList->at(i)->getName() + instruc + '\n';
\r
207 if(rxCase->indexIn(line)) {
\r
208 QString intName, sigName, cases, instruc;
\r
210 sigName = rxCase->cap(1);
\r
211 cases = rxCase->cap(2);
\r
212 intName = rxCase->cap(3);
\r
213 instruc = rxCase->cap(4);
\r
214 QList<AbstractInterface*> *intList = evalStringComplex(intName);
\r
215 int listSize = intList->count();
\r
216 res = "case " + sigName + " is\n";
\r
217 if(rxCaseUp->indexIn(cases)) {
\r
218 number = rxCaseUp->cap(1).toInt();
\r
219 for(int j = number; j < listSize; j++) {
\r
221 for(int i = 0; i < listSize; i++) {
\r
222 res += "\twhen " + QString::number(number) + " " + intList->at(i)->getName() + instruc + "\n";
\r
226 res += "\twhen " + number + ' ' + intName + instruc + "\n";
\r
230 if(rxCaseDown->indexIn(cases)) {
\r
231 number = rxCaseDown->cap(1).toInt();
\r
232 for(int j = number; j < listSize; j++) {
\r
234 for(int i = 0; i < listSize; i++) {
\r
235 res += "\twhen " + QString::number(number) + " " + intList->at(i)->getName() + instruc + "\n";
\r
239 res += "\twhen " + number + ' ' + intName + instruc + "\n";
\r
242 res += "end case ;\n";
\r
249 QString BlockImplementation::evalString(QString s) {
\r
251 QString name = getIfaceUserName(block->AbstractBlock::getIfaceFromName(s));
\r
255 QList<AbstractInterface*>* BlockImplementation::evalStringComplex(QString s) {
\r
258 QList<AbstractInterface*> *listInterfaces = new QList<AbstractInterface*>();
\r
259 AbstractInterface *inter = block->AbstractBlock::getIfaceFromName(s);
\r
260 QList<AbstractInterface*> listIntBlock = block->getInterfaces();
\r
261 for(int i = 0; i < listIntBlock.size(); i++) {
\r
262 if(inter->getName().compare(listIntBlock.at(i)->getName()) < -1) {
\r
263 listInterfaces->insert(j, inter);
\r
267 return listInterfaces;
\r
270 QString BlockImplementation::evalValue(QString s) {
\r
273 if(paramMap.contains(s))
\r
274 val = paramMap.value(s);
\r
278 QString BlockImplementation::getIfaceUserName(AbstractInterface* refIface) {
\r
280 if (! refIface->isReferenceInterface()) return "";
\r
282 AbstractInterface* funcIface = NULL;
\r
284 if (refIface->getDirection() == AbstractInterface::Input) {
\r
285 foreach(AbstractInterface* iface, block->getInputs()) {
\r
286 FunctionalInterface* fi = (FunctionalInterface*)iface;
\r
287 if (fi->getReference() == refIface) {
\r
293 else if (refIface->getDirection() == AbstractInterface::Output) {
\r
294 foreach(AbstractInterface* iface, block->getOutputs()) {
\r
295 FunctionalInterface* fi = (FunctionalInterface*)iface;
\r
296 if (fi->getReference() == refIface) {
\r
302 else if (refIface->getDirection() == AbstractInterface::InOut) {
\r
303 foreach(AbstractInterface* iface, block->getBidirs()) {
\r
304 FunctionalInterface* fi = (FunctionalInterface*)iface;
\r
305 if (fi->getReference() == refIface) {
\r
311 if (funcIface == NULL) return "";
\r
313 return funcIface->getName();
\r
316 QDataStream& operator<<(QDataStream &out, const BlockImplementation &impl) {
\r
318 out.setVersion(QDataStream::Qt_5_0);
\r
320 QByteArray blockData;
\r
321 QDataStream toWrite(&blockData, QIODevice::WriteOnly);
\r
323 toWrite << impl.xmlFile;
\r
324 toWrite << impl.referenceXml;
\r
325 toWrite << impl.referenceMd5;
\r
327 toWrite << impl.delta;
\r
328 toWrite << impl.consumptionPattern;
\r
329 toWrite << impl.productionPattern;
\r
330 toWrite << impl.productionCounter;
\r
337 QDataStream& operator>>(QDataStream &in, BlockImplementation &impl) {
\r
341 in.setVersion(QDataStream::Qt_5_0);
\r
345 in >> impl.xmlFile;
\r
346 in >> impl.referenceXml;
\r
347 in >> impl.referenceMd5;
\r
348 // loading patterns
\r
350 in >> impl.consumptionPattern;
\r
351 in >> impl.productionPattern;
\r
352 in >> impl.productionCounter;
\r
357 QString BlockImplementation::calculateWidth(QString s){
\r
358 QRegExp *rxWidth = new QRegExp("$*([a-zA-Z0-9_-]*)");
\r
359 QStringList matchList = s.split(" ");
\r
362 QList<BlockParameter*> listParams = reference->getParameters();
\r
364 while ((pos = rxWidth->indexIn(s, pos)) != -1) {
\r
365 matchList << rxWidth->cap(1);
\r
366 pos += rxWidth->matchedLength();
\r
369 for (int i = 0; i < matchList.size(); i++) {
\r
370 QString match = matchList.at(i);
\r
371 if(rxWidth->indexIn(match)) {
\r
372 for(int j = 0; j < listParams.size(); j++) {
\r
373 if(match.compare(listParams.at(j)->getName())) {
\r
374 BlockParameter *param = listParams.at(i);
\r
375 if(param->getContext() == "generic") {
\r
376 match = match.remove('$');
\r
379 match = param->getValue().toString();
\r
385 line = matchList.join(' ');
\r
386 evaluator->setExpression(line);
\r
387 res = evaluator->evaluate();
\r