Salome HOME
Fix the python API of the savestate feature.
[modules/yacs.git] / src / runtime / VisitorSalomeSaveState.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "VisitorSalomeSaveState.hxx"
21 #include "TypeConversions.hxx"
22 #include "ForEachLoop.hxx"
23 #include "Proc.hxx"
24 #include "Executor.hxx"
25 #include "AutoLocker.hxx"
26
27 #include "YacsTrace.hxx"
28
29 using namespace YACS::ENGINE;
30
31 VisitorSalomeSaveState::VisitorSalomeSaveState(ComposedNode *root)
32 :VisitorSaveState(root)
33 {
34 }
35
36 VisitorSalomeSaveState::~VisitorSalomeSaveState()
37 {
38 }
39
40 void VisitorSalomeSaveState::visitForEachLoop(ForEachLoop *node)
41 {
42   node->ComposedNode::accept(this);
43   if (!_out) throw Exception("No file open for dump state");
44   std::string name = _root->getName();
45   if (static_cast<ComposedNode*>(node) != _root) name = _root->getChildName(node);
46   DEBTRACE("VisitorSaveState::visitForEachLoop ------ " << name);
47   _out << "  <node type='forEachLoop'>" << std::endl;
48   _out << "    <name>" << name << "</name>" << std::endl;
49   _out << "    <state>" << _nodeStateName[node->getState()] << "</state>" << std::endl;
50 //  VisitorSaveState::visitForEachLoop(node);
51   std::list<InputPort *> setOfInputPort = node->getLocalInputPorts();
52   std::list<InputPort *>::iterator iter;
53   for(iter = setOfInputPort.begin(); iter != setOfInputPort.end(); iter++)
54   {
55     _out << "    <inputPort>" << std::endl;
56     _out << "      <name>" << (*iter)->getName() << "</name>" << std::endl;
57     try
58       {
59         _out << "      ";
60         _out << (*iter)->dump();
61       }
62     catch (YACS::Exception &e)
63       {
64         DEBTRACE("caught YACS:Exception: " << e.what());
65         _out << "<value><error><![CDATA[" << e.what() << "]]></error></value>" << std::endl;
66       }
67     _out << "    </inputPort>" << std::endl;
68   }
69   
70   StatesForNode state = node->getState();
71   if(YACS::LOADED == state or
72     YACS::ACTIVATED == state or
73     YACS::SUSPENDED == state or
74     YACS::EXECFAILED == state or
75     YACS::PAUSE == state or
76     YACS::TORECONNECT == state or
77     YACS::INTERNALERR == state or
78     YACS::FAILED == state or
79     YACS::ERROR == state)
80   {
81     ForEachLoopPassedData* processedData = node->getProcessedData();
82     if(processedData)
83     {
84       const std::vector<unsigned int>& processedIndexes = processedData->getIds();
85       std::vector<SequenceAny *>::const_iterator it_outputs;
86       std::vector<std::string>::const_iterator it_names;
87       
88       for(it_outputs = processedData->getOutputs().begin(), it_names = processedData->getOutputNames().begin();
89           it_names != processedData->getOutputNames().end();
90           it_outputs++, it_names++)
91       {
92         _out << "    <loopOutputPort>" << std::endl;
93         _out << "      <name>" << (*it_names) << "</name>" << std::endl;
94         for(unsigned int i = 0; i < (*it_outputs)->size(); i++)
95         {
96           AnyPtr value = (*(*it_outputs))[i];
97           _out << "      <sample><index>" << processedIndexes[i]<< "</index>";
98           if(value)
99             _out << convertNeutralXml(value->getType(), value);
100           else
101             _out << "<value>None</value>";
102           _out << "      </sample>" << std::endl;
103         }
104         _out << "    </loopOutputPort>" << std::endl;
105       }
106       
107       delete processedData;
108       processedData = NULL;
109     }
110   }
111   _out << "  </node>" << std::endl;
112 }
113
114 void YACS::ENGINE::schemaSaveState(Proc* proc,
115                                   Executor* exec,
116                                   const std::string& xmlSchemaFile)
117 {
118   YACS::BASES::AutoLocker<YACS::BASES::Mutex> alck(&(exec->getTheMutexForSchedulerUpdate()));
119   VisitorSalomeSaveState vss(proc);
120   vss.openFileDump(xmlSchemaFile);
121   proc->accept(&vss);
122   vss.closeFileDump();
123 }