Salome HOME
Manage initnode and finalizenode in the foreachloop progress bar.
[modules/yacs.git] / src / engine / Proc.cxx
index 990b80f8c87c8d9d15649b28c6ef05e329b73cc3..2eaaddaebe1799ec09b25fb53ee6be51f3f25c23 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2006-2013  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2015  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -75,10 +75,7 @@ Proc::~Proc()
   for(pt=typeMap.begin();pt!=typeMap.end();pt++)
     ((*pt).second)->decrRef();
 
-  //get rid of containers in container map
-  std::map<std::string, Container*>::const_iterator it;
-  for(it=containerMap.begin();it!=containerMap.end();it++)
-    ((*it).second)->decrRef();
+  removeContainers();
 
   //get rid of loggers in logger map
   std::map<std::string, Logger*>::const_iterator lt;
@@ -221,6 +218,34 @@ YACS::StatesForNode Proc::getNodeState(int numId)
   return state;
 }
 
+std::string Proc::getNodeProgress(int numId)
+{
+  std::string progress = "0";
+  if(YACS::ENGINE::Node::idMap.count(numId) == 0)
+    {
+      cerr << "Unknown node id " << numId << endl;
+    }
+  else if (YACS::ENGINE::ComposedNode* node = dynamic_cast<YACS::ENGINE::ComposedNode*>(YACS::ENGINE::Node::idMap[numId]))
+    progress = node->getProgress();
+  return progress;
+}
+
+int Proc::getGlobalProgressPercent()
+{
+  list<ProgressWeight> weightList = getProgressWeight();
+  int weightDone = 0;
+  int weightTotal = 0;
+  int progressPercent = 0;
+  for(list<ProgressWeight>::const_iterator iter=weightList.begin();iter!=weightList.end();iter++)
+    {
+      weightDone += (*iter).weightDone;
+      weightTotal += (*iter).weightTotal;
+    }
+  if (weightTotal > 0)
+    progressPercent = int(float(weightDone) / float(weightTotal) * 100);
+  return progressPercent;
+}
+
 std::string Proc::getXMLState(int numId)
 {
   if(YACS::ENGINE::Node::idMap.count(numId) == 0)
@@ -431,7 +456,7 @@ void Proc::modified()
 /*!
  * \param xmlSchemaFile: the file name
  */
-void Proc::saveSchema(std::string xmlSchemaFile)
+void Proc::saveSchema(const std::string& xmlSchemaFile)
 {
   VisitorSaveSchema vss(this);
   vss.openFileSchema(xmlSchemaFile);
@@ -443,7 +468,7 @@ void Proc::saveSchema(std::string xmlSchemaFile)
 /*!
  * \param xmlStateFile: the file name
  */
-void Proc::saveState(std::string xmlStateFile)
+void Proc::saveState(const std::string& xmlStateFile)
 {
   VisitorSaveState vst(this);
   vst.openFileDump(xmlStateFile);
@@ -451,15 +476,24 @@ void Proc::saveState(std::string xmlStateFile)
   vst.closeFileDump();
 }
 
+void Proc::removeContainers()
+{
+  //get rid of containers in container map
+  std::map<std::string, Container*>::const_iterator it;
+  for(it=containerMap.begin();it!=containerMap.end();it++)
+    ((*it).second)->decrRef();
+  containerMap.clear();
+}
+
 //! Create a new Container and store it in containerMap
 /*!
  * \param name: the container name and key in containerMap
  * \param kind: the container kind (depends on runtime)
  * \return the created Container
  */
-Container* Proc::createContainer(const std::string& name,const std::string& kind)
+Container *Proc::createContainer(const std::string& name, const std::string& kind)
 {
-  Container* co=  getRuntime()->createContainer(kind);
+  Container *co(getRuntime()->createContainer(kind));
   co->setName(name);
   if(containerMap.count(name)!=0)
     containerMap[name]->decrRef();
@@ -475,7 +509,7 @@ Container* Proc::createContainer(const std::string& name,const std::string& kind
  *
  * \param inst: the component instance
  * \param name: the component instance name
- * \param restCtr: try to reuse instance number previously released, false by default
+ * \param resetCtr: try to reuse instance number previously released, false by default
  */
 void Proc::addComponentInstance(ComponentInstance* inst, const std::string& name, bool resetCtr)
 {
@@ -568,3 +602,47 @@ const Proc * Proc::getProc() const
 {
   return this;
 }
+
+/*!
+ * This method is useful if this has been modified recursively and an update is needed between all the
+ * containers and components refered by children and little children and maps.
+ */
+void Proc::updateContainersAndComponents()
+{
+  std::map<std::string, Container*> myContainerMap;
+  std::map<std::string, ComponentInstance*> myComponentInstanceMap;
+  DeploymentTree treeToDup(getDeploymentTree());
+  vector<Container *> conts(treeToDup.getAllContainers());
+  for(vector<Container *>::const_iterator iterCt=conts.begin();iterCt!=conts.end();iterCt++)
+    {
+      Container *tmp(*iterCt);
+      if(tmp)
+        {
+          if(myContainerMap.find(tmp->getName())!=myContainerMap.end())
+            {
+              std::ostringstream oss; oss << "Proc::updateContainersAndComponents : more than one container instance with name \"" << tmp->getName() << "\" !";
+              throw YACS::Exception(oss.str());
+            }
+          myContainerMap[tmp->getName()]=tmp;
+          tmp->incrRef();
+        }
+      vector<ComponentInstance *> comps=treeToDup.getComponentsLinkedToContainer(*iterCt);
+      for(vector<ComponentInstance *>::iterator iterCp=comps.begin();iterCp!=comps.end();iterCp++)
+        {
+          ComponentInstance *tmp2(*iterCp);
+          if(tmp2)
+            {
+              if(myComponentInstanceMap.find(tmp2->getCompoName())!=myComponentInstanceMap.end())
+                {
+                  std::ostringstream oss; oss << "Proc::updateContainersAndComponents : more than one component instance with name \"" << tmp2->getCompoName() << "\" !";
+                  throw YACS::Exception(oss.str());
+                }
+            }
+          myComponentInstanceMap[tmp2->getCompoName()]=tmp2;
+          tmp2->incrRef();
+        }
+    }
+  removeContainers();
+  containerMap=myContainerMap;
+  componentInstanceMap=myComponentInstanceMap;
+}