X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/blast.git/blobdiff_plain/d43174d0eb3d293b3bb5fbe76662241134e74d0d..756baf5c8eaf003e8271dab9c395de2b0e704857:/Parameters.cpp?ds=inline

diff --git a/Parameters.cpp b/Parameters.cpp
index 4fd583d..c60a927 100644
--- a/Parameters.cpp
+++ b/Parameters.cpp
@@ -9,6 +9,7 @@
 #include "GroupScene.h"
 #include "GroupItem.h"
 #include "BoxItem.h"
+#include "SourceItem.h"
 #include "InterfaceItem.h"
 #include "ConnectionItem.h"
 
@@ -23,6 +24,10 @@
 #include "Exception.h"
 #include "BlocksToConfigureWidget.h"
 
+#include "BlockParameterGeneric.h"
+
+#include "DelayInputModifier.h"
+
 Parameters::Parameters() {
   categoryTree = NULL;
   arrowWidth = 5;
@@ -43,6 +48,8 @@ Parameters::Parameters() {
   isRstClkShown = false;
 
   projectPath = QDir::currentPath();
+  
+  validityExtension = "_enb";
 }
 
 Parameters::~Parameters() {
@@ -74,21 +81,62 @@ ReferenceBlock* Parameters::getReferenceBlock(int idCategory, int idBlock) {
   BlockCategory* blockCat = categoryTree->searchCategory(idCategory);
 
   if (blockCat == NULL) return NULL;
-  ReferenceBlock* ref = blockCat->getBlock(idBlock);
+  ReferenceBlock* ref = blockCat->getBlockById(idBlock);
   return ref;
 }
 
+ReferenceBlock* Parameters::getHiddenReferenceBlock(QString blockName) {
 
-FunctionalBlock* Parameters::duplicateFunctionalBlock(FunctionalBlock *block) {
+  BlockCategory* blockCat = categoryTree->searchCategory(100);
+  if (blockCat == NULL) return NULL;
+  ReferenceBlock* ref = blockCat->getBlockByName(blockName);
+  return ref;
+}
 
-  ReferenceBlock* ref = block->getReference();
-  GroupBlock* group = AB_TO_GRP(block->getParent());
+void Parameters::createDelayBlock() {
+  delayRef = new ReferenceBlock("no.xml");
+  delayRef->addCategory(100);
+  delayRef->setName("delay");
+
+  BlockCategory* cat = categoryTree->searchCategory(100);
+  cat->blocks.append(delayRef);
+
+  AbstractInterface* interIn = new ReferenceInterface(delayRef,"data_in",AbstractInterface::Input, AbstractInterface::Data, "expression","$in_width");
+  delayRef->addInterface(interIn);
+  AbstractInterface* interInCtl = new ReferenceInterface(delayRef,"data_in_enb",AbstractInterface::Input, AbstractInterface::Control, "boolean","1");
+  delayRef->addInterface(interInCtl);
+  interInCtl->setAssociatedIface(interIn);
+  AbstractInterface* interOut = new ReferenceInterface(delayRef,"data_out",AbstractInterface::Output, AbstractInterface::Data, "expression","$in_width");
+  delayRef->addInterface(interOut);
+  AbstractInterface* interOutCtl = new ReferenceInterface(delayRef,"data_out_enb",AbstractInterface::Output, AbstractInterface::Control, "boolean","1");
+  delayRef->addInterface(interOutCtl);
+  interOutCtl->setAssociatedIface(interOut);
+  BlockParameter* param1 = new BlockParameterGeneric(delayRef,"in_width","natural","8");
+  BlockParameter* param2 = new BlockParameterGeneric(delayRef,"dly_length","natural","1");
+  delayRef->addParameter(param1);
+  delayRef->addParameter(param2);
+  delayImpl = new BlockImplementation("no.xml");
+  delayImpl->setDelta("1");
+  QHash<QString,QString> consPattern;
+  consPattern.insert("data_in_enb","1");
+  delayImpl->setConsumptionPattern(consPattern);
+  QHash<QString,QString> prodPattern;
+  prodPattern.insert("data_out_enb","O{$dly_length}1");
+  delayImpl->setProductionPattern(prodPattern);
+  delayImpl->setProductionCounter("1");
+  delayRef->addImplementation(delayImpl);
+  delayImpl->setReference(delayRef);
+  if (! delayImpl->checkPatterns()) {
+    cout << "Delay block creation: failure" << endl;
+  }
+  else {
+    cout << "Delay block creation: success" << endl;
+  }
 
-  // adding to the graph
-  FunctionalBlock* newBlock = graph->addFunctionalBlock(group,ref);
-  return newBlock;
 }
 
+
+
 void Parameters::validateXmlFile(const QString& xmlFileName, const QString& xsdFileName, XmlFileType fileType) throw(Exception) {
   // opening configFile
   QFile xmlFile(xmlFileName);
@@ -197,6 +245,16 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
   GroupBlock *groupBlock = NULL;
 
   GroupWidget* topGroup = NULL;
+
+  QString path = root.attribute("project_path","none");
+  if (path != "none") {
+    QDir dir(path);
+    if (dir.exists()) {
+      projectPath = path;
+
+    }
+    cout << "project path set to " << qPrintable(projectPath) << endl;
+  }
   /**********************************************************
    1 : getting scene and creating associated group widgets
   ***********************************************************/
@@ -239,6 +297,7 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
     if (idUpperScene != -1) {
       groupWidget->setWindowTitle(groupBlock->getName());
       groupWidget->show();
+      cout << qPrintable(groupItem->getRefBlock()->getName()) << " has upper box item in " << qPrintable(groupItem->getParentItem()->getScene()->getGroupItem()->getRefBlock()->getName()) << endl;
     }    
   }
   dispatcher->setSceneCounter(maxIdScene+1);
@@ -256,7 +315,29 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
     GroupScene *currentScene = searchSceneById(idScene,topScene);
 
     if(currentScene == NULL) throw(Exception(PROJECTFILE_CORRUPTED));
-
+    /**********************************************************
+     2.1 : getting sources if it is top scene
+    ***********************************************************/
+    if (currentScene->isTopScene()) {
+      QDomNodeList sourceNodes = currentSceneNode.elementsByTagName("source_item");
+      cout << "top scene has " << sourceNodes.length() << " sources" << endl;
+      for(int j=0; j<sourceNodes.length(); j++) {
+        QDomElement currentSBNode = sourceNodes.at(j).toElement();      
+        SourceItem* sourceItem = new SourceItem(dispatcher,this);
+        try {
+          sourceItem->load(currentSBNode);
+        }
+        catch(Exception err) {
+          throw(err);
+        }
+        cout << "source item has been read, add it to the scene" << endl;
+        // add the block to the GroupScene
+        currentScene->addSourceItem(sourceItem);
+      } 
+    }
+    /**********************************************************
+     2.2 : getting functional blocks
+    ***********************************************************/
     QDomNodeList functionalBlockNodes = currentSceneNode.elementsByTagName("bi_functional");
 
     for(int j=0; j<functionalBlockNodes.length(); j++) {
@@ -269,7 +350,7 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
         throw(err);
       }
       // add the block to the GroupScene
-      currentScene->addBlockItem(funcItem);
+      currentScene->addBoxItem(funcItem);
     }
   }
   cout << "functional blocks loaded and created succefully!" << endl;
@@ -313,9 +394,9 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
       // get the GroupItem already created and set at phase 1
       GroupItem *insideGroup = searchGroupItemById(idGroup, topScene);
       BoxItem* upperItem = NULL;
-      if(insideGroup == NULL) cout << "group null" << endl;
+      if(insideGroup == NULL) cout << "group null" << endl;      
       // now search within the scene which BoxItem has a childItem that is = to insideGroup
-      QList<BoxItem *> lst = currentScene->getBlockItems();
+      QList<BoxItem *> lst = currentScene->getBoxItems();
       foreach(BoxItem* item, lst) {
         if (item->getChildGroupItem() == insideGroup) {
           upperItem = item;
@@ -349,10 +430,10 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
         double position = currentInterfaceNode.attribute("position","none").toDouble(&ok);
         if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
 
-        ConnectedInterface *refInter = insideGroup->searchInterfaceByName(refName)->refInter;
+        ConnectedInterface *refInter = insideGroup->searchInterfaceItemByName(refName)->refInter;
         InterfaceItem *ifaceItem = new InterfaceItem(position, orientation, refInter, upperItem, this);
         ifaceItem->setId(id);
-        upperItem->addInterface(ifaceItem);
+        upperItem->addInterfaceItem(ifaceItem);
       }
     }
   }
@@ -374,13 +455,47 @@ GroupWidget *Parameters::loadProject(QDomElement root) throw(Exception) {
     InterfaceItem *iface2 = searchInterfaceItemById(to,topScene);
 
     if(iface1 != NULL && iface2 != NULL){
-      dispatcher->connect(iface1,iface2);
+      dispatcher->createConnection(iface1,iface2);
     } else {
       cout << "interfaces not found, connect canceled!" << endl;
     }
   }
   cout << "connections loaded and created succefully!" << endl;
 
+  QDomNodeList modifierNodes = root.elementsByTagName("modifier");
+
+  for(int i=0; i<modifierNodes.length(); i++){
+    QDomElement currentModifierNode = modifierNodes.at(i).toElement();
+
+    int id = currentModifierNode.attribute("id","none").toInt(&ok);
+    if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+
+    QString typeStr = currentModifierNode.attribute("type","none");
+    if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+    QString paramsStr = currentModifierNode.attribute("params","none");
+    if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+
+    /* NB: just adding delays for now. To be cont'd */
+    InterfaceItem *iface = searchInterfaceItemById(id,topScene);
+
+    if ((iface == NULL ) || (iface->refInter == NULL) || (iface->refInter->getAssociatedIface() == NULL)) {
+      cout << "modified interface not found, modifiers setup canceled!" << endl;
+    }
+    else {
+      ConnectedInterface* connIface = AI_TO_CON(iface->refInter->getAssociatedIface());
+
+      AbstractInputModifier* mod = NULL;
+      if (typeStr == "delay") {
+        int delay = paramsStr.toInt(&ok);
+        if(!ok) throw(Exception(PROJECTFILE_CORRUPTED));
+        mod = new DelayInputModifier(connIface, delay);
+        connIface->setInputModifier(mod);
+      }
+    }
+  }
+
+  cout << "modifiers loaded and created succefully!" << endl;
+
   return topGroup;
 }
 
@@ -522,14 +637,14 @@ void Parameters::loadReferencesFromXml() throw(Exception) {
       QString line = in.readLine();
       line = in.readLine();
 
-      if (!line.contains("<block>")) {
+      if (!line.contains("<block")) {
         blockXML.close();
         continue;
       }
 
       blockXML.close();
       try {
-        validateXmlFile(fileName,"block.xsd",Reference);
+        validateXmlFile(fileName,"reference.xsd",Reference);
       }
       catch(Exception err) {
         throw(err);
@@ -665,14 +780,14 @@ void Parameters::loadImplementationsFromXml() throw(Exception) {
       implXML.close();
       cout << "OK" << endl;
       cout << "reading " << qPrintable(fileName) << " content ...";
-      /*
+
       try {
-        validateXmlFile(fileName,"block.xsd",Implementation);
+        validateXmlFile(fileName,"implementation.xsd",Implementation);
       }
       catch(Exception e) {
         throw(e);
       }
-      */
+
       // reading in into QDomDocument
       QDomDocument document ("FileXML");
       if (!implXML.open(QIODevice::ReadOnly)) {
@@ -690,6 +805,12 @@ void Parameters::loadImplementationsFromXml() throw(Exception) {
       QString refXml = implRoot.attribute("ref_name","none");
       QString refMd5 = implRoot.attribute("ref_md5","none");
       BlockImplementation* impl = new BlockImplementation(fileName,refXml,refMd5);
+      try {
+        impl->loadPatterns(implRoot);
+      }
+      catch(int err) {
+        throw(err);
+      }
       availableImplementations.append(impl);
 
       ReferenceBlock* ref = NULL;
@@ -701,9 +822,12 @@ void Parameters::loadImplementationsFromXml() throw(Exception) {
       }
       if (ref == NULL) {
         cout << "Cannot find a reference block for impl :" << qPrintable(fileName) << endl;
-      }
-      ref->addImplementation(impl);
-      impl->setReference(ref);
+      }      
+      else {          
+        ref->addImplementation(impl);
+        impl->setReference(ref);
+        if (! impl->checkPatterns()) throw(Exception(IMPLFILE_CORRUPTED));
+      }      
       cout << "OK" << endl;
     }
   }
@@ -748,8 +872,11 @@ void Parameters::loadImplementationsFromLib() throw(Exception) {
     if (ref == NULL) {
       cout << "Cannot find a reference block for impl :" << qPrintable(impl->getXmlFile()) << endl;
     }
-    ref->addImplementation(impl);
-    impl->setReference(ref);
+    else {          
+      ref->addImplementation(impl);
+      impl->setReference(ref);
+      if (! impl->checkPatterns()) throw(Exception(IMPLFILE_CORRUPTED));
+    }
   }
   libFile.close();
 }
@@ -844,9 +971,9 @@ QList<AbstractBlock *> Parameters::getBlocksToConfigure() {
 
 
 void Parameters::updateToolbar() {
-  int nb = currentScene->getBlockItems().length();
+  int nb = currentScene->getBoxItems().length();
   for(int i = 0; i<nb; i++){
-    if(currentScene->getBlockItems().at(i)->isSelected()){
+    if(currentScene->getBoxItems().at(i)->isSelected()){
       currentScene->getGroupWidget()->enableGroupButton(true);
       return;
     }
@@ -940,6 +1067,35 @@ void Parameters::save(QString confFile) {
     }
 
     writer.writeEndElement();    //</connections>
+
+    QList<InterfaceItem *> lstIfaceItem;
+    // search for modifiers
+    foreach(ConnectionItem* item, allConnections) {
+      InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
+      AbstractInputModifier* mod = fromIfaceItem->refInter->getInputModifier();
+      if (mod != NULL) {
+        if (!lstIfaceItem.contains(fromIfaceItem)) lstIfaceItem.append(fromIfaceItem);
+      }
+      InterfaceItem* toIfaceItem = item->getToInterfaceItem();
+      mod = toIfaceItem->refInter->getInputModifier();
+      if (mod != NULL) {
+        if (!lstIfaceItem.contains(toIfaceItem)) lstIfaceItem.append(toIfaceItem);
+      }
+    }
+    // write input modifiers
+    writer.writeStartElement("modifiers");
+    foreach(InterfaceItem* item, lstIfaceItem) {
+      AbstractInputModifier* mod = item->refInter->getInputModifier();
+      if (mod != NULL) {
+        writer.writeStartElement("modifier");
+        writer.writeAttribute("id", QString::number(item->getId()));
+        writer.writeAttribute("type",mod->getTypeStr());
+        writer.writeAttribute("params", mod->getParametersStr());
+        writer.writeEndElement();
+      }
+    }
+
+    writer.writeEndElement();    //</modifiers>
     writer.writeEndElement();      //</blast_project
 
     writer.writeEndDocument();
@@ -956,8 +1112,13 @@ void Parameters::setArrowPathes() {
   _inArrow.lineTo(arrowLineLength+arrowWidth,-arrowHeight/2);
   _inArrow.lineTo(arrowLineLength+arrowWidth,arrowHeight/2);
   _inArrow.lineTo(arrowLineLength,0);
-  _inArrow.closeSubpath();
-  inArrow = _inArrow;
+  //_inArrow.closeSubpath();
+  dataArrowIn = _inArrow;
+
+  QPainterPath _inArrowC;
+  _inArrowC.lineTo(arrowLineLength,0);
+  _inArrowC.addEllipse(arrowLineLength,-arrowHeight/2,arrowHeight-1,arrowHeight-1);
+  clkrstArrow = _inArrowC;
 
   QPainterPath _outArrow;
   _outArrow.lineTo(arrowLineLength,0);
@@ -965,8 +1126,8 @@ void Parameters::setArrowPathes() {
   _outArrow.lineTo(arrowLineLength+arrowWidth,0);
   _outArrow.lineTo(arrowLineLength,arrowHeight/2);
   _outArrow.lineTo(arrowLineLength,0);
-  _outArrow.closeSubpath();
-  outArrow = _outArrow;
+  //_outArrow.closeSubpath();
+  dataArrowOut = _outArrow;
 
 }
 
@@ -996,7 +1157,7 @@ GroupItem* Parameters::searchGroupItemById(int id, GroupScene *scene) {
 
 BoxItem* Parameters::searchBlockItemById(int id, GroupScene *scene) {
 
-  foreach(BoxItem *item, scene->getBlockItems()){
+  foreach(BoxItem *item, scene->getBoxItems()){
     if(item->getId() == id){
       return item;
     }
@@ -1010,6 +1171,27 @@ BoxItem* Parameters::searchBlockItemById(int id, GroupScene *scene) {
   return NULL;
 }
 
+BoxItem* Parameters::searchFunctionalBlock(AbstractBlock* block) {
+
+  return searchFunctionalBlockRecur(block,topScene);
+}
+
+BoxItem* Parameters::searchFunctionalBlockRecur(AbstractBlock* block, GroupScene* scene) {
+
+  foreach(BoxItem *item, scene->getBoxItems()){
+    if(item->getRefBlock() == block) {
+      return item;
+    }
+  }
+
+  BoxItem* item = NULL;
+  foreach(GroupScene *s, scene->getChildrenScene()) {
+    item = searchFunctionalBlockRecur(block,s);
+    if (item != NULL) return item;
+  }
+  return NULL;
+}
+
 InterfaceItem* Parameters::searchInterfaceItemById(int id, GroupScene* scene) {
 
   foreach(InterfaceItem *item, scene->getGroupItem()->getInterfaces()){
@@ -1017,7 +1199,16 @@ InterfaceItem* Parameters::searchInterfaceItemById(int id, GroupScene* scene) {
       return item;
     }
   }
-  foreach(BoxItem *block, scene->getBlockItems()){
+  if (scene->isTopScene()) {
+    foreach(SourceItem *block, scene->getSourceItems()){
+      foreach(InterfaceItem *item, block->getInterfaces()){
+        if(item->getId() == id){
+          return item;
+        }
+      }
+    } 
+  }
+  foreach(BoxItem *block, scene->getBoxItems()){
     foreach(InterfaceItem *item, block->getInterfaces()){
       if(item->getId() == id){
         return item;