Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / hmi / commandsProc.cxx
1 // Copyright (C) 2006-2013  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.
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 <Python.h>
21 #include "commandsProc.hxx"
22
23 #include "Node.hxx"
24 #include "ComposedNode.hxx"
25 #include "Bloc.hxx"
26 #include "Proc.hxx"
27 #include "ElementaryNode.hxx"
28 #include "InlineNode.hxx"
29 #include "ServiceNode.hxx"
30 #include "PythonNode.hxx"
31 #include "CORBANode.hxx"
32 #include "CppNode.hxx"
33 #include "XMLNode.hxx"
34 #include "SalomePythonNode.hxx"
35 #include "XMLNode.hxx"
36 #include "ForLoop.hxx"
37 #include "ForEachLoop.hxx"
38 #include "WhileLoop.hxx"
39 #include "Switch.hxx"
40 #include "OptimizerLoop.hxx"
41 #include "PresetNode.hxx"
42 #include "OutNode.hxx"
43 #include "StudyNodes.hxx"
44 #include "Exception.hxx"
45 #include "DataPort.hxx"
46 #include "InputDataStreamPort.hxx"
47 #include "OutputDataStreamPort.hxx"
48 #include "StudyPorts.hxx"
49 #include "PresetPorts.hxx"
50 #include "ComponentDefinition.hxx"
51 #include "SalomeContainer.hxx"
52 #include "SalomeComponent.hxx"
53 #include "TypeCode.hxx"
54 #include "RuntimeSALOME.hxx"
55 #include "TypeConversions.hxx"
56
57 #include "guiContext.hxx"
58
59 #include <iostream>
60 #include <sstream>
61 #include <string>
62
63 //#define _DEVDEBUG_
64 #include "YacsTrace.hxx"
65
66 using namespace std;
67
68 using namespace YACS;
69 using namespace YACS::ENGINE;
70 using namespace YACS::HMI;
71
72
73 static std::map<int, std::string> createErrorMsgMap()
74 {
75   std::map<int, std::string> m;
76   m[1] = "\nUse the [Ctrl] Drag_N_Drop method if you want to create a input/output link without the associated control link";
77   return m;
78 }
79
80 std::map<int, std::string> ErrorMsg = createErrorMsgMap();
81
82 void setErrorMsg(YACS::Exception& ex)
83 {
84   DEBTRACE("errorNumber= "<<ex.errNumber);
85   if(ex.errNumber > 0 && ErrorMsg.count(ex.errNumber) != 0)
86     {
87       DEBTRACE(ErrorMsg[ex.errNumber]);
88       GuiContext::getCurrent()->_lastErrorMessage = ex.what() + ErrorMsg[ex.errNumber];
89     }
90   else
91     GuiContext::getCurrent()->_lastErrorMessage = ex.what();
92 }
93
94 std::map<int, std::string> ProcInvoc::_typeNameMap;
95
96 // ----------------------------------------------------------------------------
97
98 ProcInvoc::ProcInvoc()
99   : Invocator()
100 {
101   _typeNameMap.clear();
102   _typeNameMap[SALOMEPROC]           = "SALOMEPROC";
103   _typeNameMap[BLOC]                 = "BLOC";
104   _typeNameMap[FOREACHLOOP]          = "FOREACHLOOP";
105   _typeNameMap[OPTIMIZERLOOP]        = "OPTIMIZERLOOP";
106   _typeNameMap[FORLOOP]              = "FORLOOP";
107   _typeNameMap[WHILELOOP]            = "WHILELOOP";
108   _typeNameMap[SWITCH]               = "SWITCH";
109   _typeNameMap[PYTHONNODE]           = "PYTHONNODE";
110   _typeNameMap[PYFUNCNODE]           = "PYFUNCNODE";
111   _typeNameMap[CORBANODE]            = "CORBANODE";
112   _typeNameMap[SALOMENODE]           = "SALOMENODE";
113   _typeNameMap[CPPNODE]              = "CPPNODE";
114   _typeNameMap[SALOMEPYTHONNODE]     = "SALOMEPYTHONNODE";
115   _typeNameMap[XMLNODE]              = "XMLNODE";
116   _typeNameMap[SPLITTERNODE]         = "SPLITTERNODE";
117   _typeNameMap[DFTODSFORLOOPNODE]    = "DFTODSFORLOOPNODE";
118   _typeNameMap[DSTODFFORLOOPNODE]    = "DSTODFFORLOOPNODE";
119   _typeNameMap[PRESETNODE]           = "PRESETNODE";
120   _typeNameMap[OUTNODE]              = "OUTNODE";
121   _typeNameMap[STUDYINNODE]          = "STUDYINNODE";
122   _typeNameMap[STUDYOUTNODE]         = "STUDYOUTNODE";
123   _typeNameMap[INPUTPORT]            = "INPUTPORT";
124   _typeNameMap[OUTPUTPORT]           = "OUTPUTPORT";
125   _typeNameMap[INPUTDATASTREAMPORT]  = "INPUTDATASTREAMPORT";
126   _typeNameMap[OUTPUTDATASTREAMPORT] = "OUTPUTDATASTREAMPORT";
127   _typeNameMap[DATALINK]             = "DATALINK";
128   _typeNameMap[CONTROLLINK]          = "CONTROLLINK";
129   _typeNameMap[CONTAINER]            = "CONTAINER";
130   _typeNameMap[COMPONENT]            = "COMPONENT";
131   _typeNameMap[REFERENCE]            = "REFERENCE";
132   _typeNameMap[DATATYPE]             = "DATATYPE";
133 }
134
135 TypeOfElem ProcInvoc::getTypeOfNode(YACS::ENGINE::Node* node)
136 {
137   TypeOfElem nodeType = UNKNOWN;
138   if      (dynamic_cast<YACS::ENGINE::Bloc*>(node))             nodeType = BLOC;
139   else if (dynamic_cast<YACS::ENGINE::PythonNode*>(node))       nodeType = PYTHONNODE;
140   else if (dynamic_cast<YACS::ENGINE::PyFuncNode*>(node))       nodeType = PYFUNCNODE;
141   else if (dynamic_cast<YACS::ENGINE::CORBANode*>(node))        nodeType = CORBANODE;
142   else if (dynamic_cast<YACS::ENGINE::CppNode*>(node))          nodeType = CPPNODE;
143   else if (dynamic_cast<YACS::ENGINE::SalomeNode*>(node))       nodeType = SALOMENODE;
144   else if (dynamic_cast<YACS::ENGINE::SalomePythonNode*>(node)) nodeType = SALOMEPYTHONNODE;
145   else if (dynamic_cast<YACS::ENGINE::XmlNode*>(node))          nodeType = XMLNODE;
146   else if (dynamic_cast<YACS::ENGINE::SplitterNode*>(node))     nodeType = SPLITTERNODE;
147   else if (dynamic_cast<YACS::ENGINE::ForLoop*>(node))          nodeType = FORLOOP;
148   else if (dynamic_cast<YACS::ENGINE::WhileLoop*>(node))        nodeType = WHILELOOP;
149   else if (dynamic_cast<YACS::ENGINE::Switch*>(node))           nodeType = SWITCH;
150   else if (dynamic_cast<YACS::ENGINE::ForEachLoop*>(node))      nodeType = FOREACHLOOP;
151   else if (dynamic_cast<YACS::ENGINE::OptimizerLoop*>(node))    nodeType = OPTIMIZERLOOP;
152   else if (dynamic_cast<YACS::ENGINE::PresetNode*>(node))       nodeType = PRESETNODE;
153   else if (dynamic_cast<YACS::ENGINE::OutNode*>(node))          nodeType = OUTNODE;
154   else if (dynamic_cast<YACS::ENGINE::StudyInNode*>(node))      nodeType = STUDYINNODE;
155   else if (dynamic_cast<YACS::ENGINE::StudyOutNode*>(node))     nodeType = STUDYOUTNODE;
156   return nodeType;
157 }
158
159 TypeOfElem ProcInvoc::getTypeOfPort(YACS::ENGINE::DataPort* port)
160 {
161   TypeOfElem portType = UNKNOWN;
162   if      (dynamic_cast<YACS::ENGINE::InputPort*>(port))            portType = INPUTPORT;
163   else if (dynamic_cast<YACS::ENGINE::OutputPort*>(port))           portType = OUTPUTPORT;
164   else if (dynamic_cast<YACS::ENGINE::InputDataStreamPort*>(port))  portType = INPUTDATASTREAMPORT;
165   else if (dynamic_cast<YACS::ENGINE::OutputDataStreamPort*>(port)) portType = OUTPUTDATASTREAMPORT;
166   return portType;
167 }
168
169 std::string ProcInvoc::getTypeName(TypeOfElem type)
170 {
171   if (_typeNameMap.count(type))
172     return _typeNameMap[type];
173   else
174     return "UNKNOWN";
175 }
176
177 // ----------------------------------------------------------------------------
178
179 CommandAddNodeFromCatalog::CommandAddNodeFromCatalog(YACS::ENGINE::Catalog *catalog,
180                                                      std::string compo,
181                                                      std::string type,
182                                                      std::string position,
183                                                      std::string name,
184                                                      bool newCompoInst,
185                                                      int swCase)
186   : Command(), _catalog(catalog), _compoName(compo), _typeName(type),
187     _position(position), _name(name), _newCompoInst(newCompoInst), _swCase(swCase)
188 {
189   DEBTRACE("CommandAddNodeFromCatalog " << compo << " " << type <<  " " << position <<  " " << name);
190   _node=0;
191   _snode=0;
192 }
193
194 std::string CommandAddNodeFromCatalog::dump()
195 {
196   string ret ="CommandAddNodeFromCatalog " + _compoName + " " + _typeName + " " +  _position + " " + _name;
197   return ret;
198 }
199
200 YACS::ENGINE::Node *CommandAddNodeFromCatalog::getNode()
201 {
202   return _node;
203 }
204
205 YACS::HMI::SubjectNode *CommandAddNodeFromCatalog::getSubjectNode()
206 {
207   return _snode;
208 }
209
210 bool CommandAddNodeFromCatalog::localExecute()
211 {
212   DEBTRACE("CommandAddNodeFromCatalog::localExecute");
213
214   Node *son = 0;
215   Node *nodeToClone = 0;
216   try
217     {
218       if (_compoName.empty())
219         {
220           if (_catalog->_nodeMap.count(_typeName))
221             nodeToClone = _catalog->_nodeMap[_typeName];
222           else if (_catalog->_composednodeMap.count(_typeName))
223             nodeToClone = _catalog->_composednodeMap[_typeName];
224         }
225       else
226         if (_catalog->_componentMap.count(_compoName))
227           {
228             YACS::ENGINE::ComponentDefinition* compodef = _catalog->_componentMap[_compoName];
229             if (compodef->_serviceMap.count(_typeName))
230               nodeToClone = compodef->_serviceMap[_typeName];
231           }
232       if (nodeToClone)
233         _typeNode = ProcInvoc::getTypeOfNode(nodeToClone);
234
235       GuiContext::getCurrent()->setCurrentCatalog(_catalog);
236       Proc* proc = GuiContext::getCurrent()->getProc();
237       Node* node = proc;
238       ServiceNode *service = 0;
239
240       if (!_position.empty()) node = proc->getChildByName(_position);
241       ComposedNode* father =dynamic_cast<ComposedNode*> (node);
242       if (father && nodeToClone)
243         {
244           son = nodeToClone->clone(0);
245           son->setName(_name);
246           service = dynamic_cast<ServiceNode*>(son);
247         }
248
249       // Node creation eventually reusing old component instance
250       ComponentInstance *compo = 0;
251       if (service)
252         compo = service->getComponent();
253
254       if(compo)
255         {
256           std::string compoName=compo->getCompoName();
257           DEBTRACE(compoName);
258           std::string compoInstName=compo->getInstanceName();
259           DEBTRACE(compoInstName);
260           if(!_newCompoInst)
261             {
262               ComponentInstance *lastcompo = GuiContext::getCurrent()->_mapOfLastComponentInstance[compoName];
263               DEBTRACE(lastcompo);
264               if(lastcompo)
265                 {
266                   DEBTRACE(lastcompo->getInstanceName());
267                   service->setComponent(lastcompo); // use the last component instance of the same type and not a new instance
268                 }
269               else
270                 GuiContext::getCurrent()->_mapOfLastComponentInstance[compoName]=compo;
271             }
272           else
273             GuiContext::getCurrent()->_mapOfLastComponentInstance[compoName]=compo;
274         }
275
276       if (son)
277         {
278           TypeOfElem fatherType = ProcInvoc::getTypeOfNode(father);
279           switch (fatherType)
280             {
281             case BLOC:
282               (dynamic_cast<YACS::ENGINE::Bloc*>(father))->edAddChild(son);
283               break;
284             case FORLOOP:
285               (dynamic_cast<YACS::ENGINE::ForLoop*>(father))->edSetNode(son);
286               break;
287             case WHILELOOP:
288               (dynamic_cast<YACS::ENGINE::WhileLoop*>(father))->edSetNode(son);
289               break;
290             case SWITCH:
291               (dynamic_cast<YACS::ENGINE::Switch*>(father))->edSetNode(_swCase,son);
292               break;
293             case FOREACHLOOP:
294               (dynamic_cast<YACS::ENGINE::ForEachLoop*>(father))->edSetNode(son);
295               break;
296             case OPTIMIZERLOOP:
297               (dynamic_cast<YACS::ENGINE::OptimizerLoop*>(father))->edSetNode(son);
298               break;
299             default:
300               YASSERT(0);
301             }
302         }
303       _node = son;
304       if (!_node)
305         throw YACS::Exception("node from catalog is not correct: check if catalog is up to date!");
306
307       SubjectComposedNode *sfather = dynamic_cast<SubjectComposedNode*>(GuiContext::getCurrent()->_mapOfSubjectNode[father]);
308       _snode= sfather->addSubjectNode(_node, "", _catalog, _compoName, _typeName);
309       _snode->loadChildren();
310       _snode->loadLinks();
311     }
312   catch (Exception& ex)
313     {
314       DEBTRACE("CommandAddNode::localExecute() : " << ex.what());
315       setErrorMsg(ex);
316       if (son) delete son;
317       _node = 0;
318     }
319   return (_node != 0);
320 }
321
322 bool CommandAddNodeFromCatalog::localReverse()
323 {
324   DEBTRACE("CommandAddNodeFromCatalog::localReverse");
325   try
326     {
327       Proc* proc = GuiContext::getCurrent()->getProc();
328       string nodeName;
329       if (_position.empty())
330         nodeName= _name;
331       else
332         nodeName = _position + "." + _name;
333       DEBTRACE(nodeName);
334       _node = proc->getChildByName(nodeName);
335       YASSERT(_node);
336       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(_node));
337       _snode = GuiContext::getCurrent()->_mapOfSubjectNode[_node];
338
339       Subject *father=_snode->getParent();
340       DEBTRACE(father);
341       DEBTRACE(father->getName());
342       Subject::erase(_snode);
343       _snode= 0;
344       _node = 0;
345       if (father)
346         {
347           DEBTRACE("REMOVE");
348           father->select(true);
349           father->update(REMOVE,0,0);
350         }
351       return true;
352     }
353   catch (Exception& ex)
354     {
355       DEBTRACE("CommandAddNodeFromCatalog::localReverse(): " << ex.what());
356       setErrorMsg(ex);
357       return false;
358     }
359 }
360
361 // ----------------------------------------------------------------------------
362
363 CommandReparentNode::CommandReparentNode(std::string position,
364                                          std::string newParent)
365   : Command(), _position(position), _newParent(newParent)
366 {
367   DEBTRACE("CommandReparentNode::CommandReparentNode " << _position << " " << _newParent);
368   _oldParent = "";
369 }
370
371 std::string CommandReparentNode::dump()
372 {
373   string ret ="CommandReparentNode " + _position + " " + _newParent;
374   return ret;
375 }
376
377 bool CommandReparentNode::localExecute()
378 {
379   DEBTRACE("CommandReparentNode::localExecute");
380   Proc* proc = GuiContext::getCurrent()->getProc();
381   Node* node = 0;
382   try
383     {
384       if (_position == proc->getName())
385         throw YACS::Exception("Reparent the proc (main bloc) is impossible");
386       node = proc->getChildByName(_position);
387       ComposedNode *oldFather = node->getFather();
388       ComposedNode *newFather = proc;
389       Node *newF = 0;
390       if (_newParent != proc->getName())
391         {
392           newF = proc->getChildByName(_newParent);
393           newFather = dynamic_cast<ComposedNode*>(newF);
394         }
395       if (!newFather)
396         throw YACS::Exception("new parent must be a composed node");
397       if (oldFather == newFather)
398         throw YACS::Exception("no need to reparent to the same parent");
399       if (ComposedNode *cnode = dynamic_cast<ComposedNode*>(node))
400         if (cnode->isInMyDescendance(newFather))
401           throw YACS::Exception("reparent a node to one of it's children is impossible");
402       if (Loop *loop = dynamic_cast<Loop*>(newFather))
403         if (!loop->edGetDirectDescendants().empty())
404           throw YACS::Exception("Already a node in a new parent of Loop type");
405       if (DynParaLoop * dpl = dynamic_cast<DynParaLoop*>(newFather))
406         if (dpl->getExecNode() != NULL)
407           throw YACS::Exception("Already an execution node in the new parent of type dynamic loop");
408       Node *nodeSameName = 0;
409       try
410         {
411           nodeSameName = newFather->getChildByName(node->getName());
412         }
413       catch (Exception& e)
414         {
415         }
416       if (nodeSameName)
417         throw YACS::Exception("there is already a child of same name in the new parent");
418       SubjectNode * snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
419       Subject *subo = GuiContext::getCurrent()->_mapOfSubjectNode[oldFather];
420       Subject *subn = GuiContext::getCurrent()->_mapOfSubjectNode[newFather];
421       SubjectComposedNode* sop = dynamic_cast<SubjectComposedNode*>(subo);
422       SubjectComposedNode* snp = dynamic_cast<SubjectComposedNode*>(subn);
423       //save existing links
424       snode->saveLinks();
425       snode->removeExternalLinks();
426       snode->removeExternalControlLinks();
427       sop->houseKeepingAfterCutPaste(true, snode);
428       oldFather->edRemoveChild(node);
429       newFather->edAddChild(node);
430       snp->houseKeepingAfterCutPaste(false, snode);
431       //restore links
432       snode->restoreLinks();
433       if (oldFather == proc) _oldParent = proc->getName();
434       else _oldParent = proc->getChildName(oldFather);
435       _newpos = proc->getChildName(node);
436       sop->update(CUT, ProcInvoc::getTypeOfNode(node), snode);
437       snp->update(PASTE, ProcInvoc::getTypeOfNode(node), snode);
438       snode->recursiveUpdate(RENAME, 0, snode);
439       snode->_parent = snp;
440     }
441   catch (Exception& ex)
442     {
443       DEBTRACE("CommandReparentNode::localExecute() : " << ex.what());
444       setErrorMsg(ex);
445       node = 0;
446     }
447   catch (...)
448     {
449       GuiContext::getCurrent()->_lastErrorMessage = "Unknown exception";
450       node = 0;
451     }
452   return (node != 0); 
453 }
454
455 bool CommandReparentNode::localReverse()
456 {
457   DEBTRACE("CommandReparentNode::localReverse " << _newpos << " " << _oldParent);
458   Proc* proc = GuiContext::getCurrent()->getProc();
459   Node* node = 0;
460   try
461     {
462       node = proc->getChildByName(_newpos);
463       ComposedNode *father = node->getFather();
464       ComposedNode *oldFather = proc;
465       Node *oldF = 0;
466       if (_oldParent != proc->getName())
467         {
468           oldF = proc->getChildByName(_oldParent);
469           oldFather = dynamic_cast<ComposedNode*>(oldF);
470         }
471       SubjectNode * snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
472       Subject *subn = GuiContext::getCurrent()->_mapOfSubjectNode[father];
473       Subject *subo = GuiContext::getCurrent()->_mapOfSubjectNode[oldFather];
474       SubjectComposedNode* snp = dynamic_cast<SubjectComposedNode*>(subn);
475       SubjectComposedNode* sop = dynamic_cast<SubjectComposedNode*>(subo);
476       //save existing links
477       snode->saveLinks();
478       snode->removeExternalLinks();
479       snode->removeExternalControlLinks();
480       snp->houseKeepingAfterCutPaste(true, snode);
481       father->edRemoveChild(node);
482       oldFather->edAddChild(node);
483       sop->houseKeepingAfterCutPaste(false, snode);
484       //restore links
485       snode->restoreLinks();
486       snp->update(CUT, ProcInvoc::getTypeOfNode(node), snode);
487       sop->update(PASTE, ProcInvoc::getTypeOfNode(node), snode);
488       snode->recursiveUpdate(RENAME, 0, snode);
489       snode->_parent = sop;
490     }
491   catch (Exception& ex)
492     {
493       DEBTRACE("CommandReparentNode::localReverse() : " << ex.what());
494       setErrorMsg(ex);
495       node = 0;
496     }
497   catch (...)
498     {
499       GuiContext::getCurrent()->_lastErrorMessage = "Unknown exception";
500       node = 0;
501     }
502   return (node != 0); 
503 }
504
505 // ----------------------------------------------------------------------------
506 CommandPutInComposedNode::CommandPutInComposedNode(std::string position,
507                                                    std::string newParent,
508                                                    std::string type,
509                                                    bool toSaveRestoreLinks)
510   : Command(), _position(position), _newParent(newParent), _type(type), _toSaveRestoreLinks(toSaveRestoreLinks)
511 {
512   DEBTRACE("CommandPutInComposedNode::CommandPutInComposedNode " << _position << " " << _newParent);
513   _newpos ="";
514 }
515
516 std::string CommandPutInComposedNode::dump()
517 {
518   string save = _toSaveRestoreLinks ? "true" : "false";
519   string ret ="CommandPutInComposedNode " + _position + " " + _newParent + " " + _type + " " + save;
520   return ret;
521 }
522
523
524 bool CommandPutInComposedNode::localExecute()
525 {
526   DEBTRACE("CommandPutInComposedNode::localExecute");
527   Proc* proc = GuiContext::getCurrent()->getProc();
528   Node* node = 0;
529   try
530     {
531       if (_position == proc->getName())
532         throw YACS::Exception("You cannot put the proc (main bloc) in a " + _type);
533       node = proc->getChildByName(_position);
534       ComposedNode *oldFather = node->getFather();
535       SubjectNode * snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
536       Subject *subo = GuiContext::getCurrent()->_mapOfSubjectNode[oldFather];
537       SubjectComposedNode* sop = dynamic_cast<SubjectComposedNode*>(subo);
538       if (_toSaveRestoreLinks)
539         snode->saveLinks(); //save existing links
540       //remove external links
541       snode->removeExternalLinks();
542       snode->removeExternalControlLinks();
543       //remove subject node from subject old father
544       sop->houseKeepingAfterCutPaste(true, snode);
545       //remove node from old father
546       oldFather->edRemoveChild(node);
547       //refresh node views
548       sop->update(CUT, ProcInvoc::getTypeOfNode(node), snode);
549
550       // try to find a node with the given name:
551       //   success: use it as target composed node
552       //   fail:    create such a node and use it
553       std::list<Node*> children = proc->getChildren();
554       Node* composednode = 0;
555       SubjectNode *scomposednode = 0;
556       for (list<Node*>::iterator it = children.begin(); it != children.end(); ++it)
557         {
558           if ( _newParent == (*it)->getName() )
559             {
560               //get an existing ComposedNode with name _newParent
561               composednode = (*it);
562               break;
563             }
564         }
565       // target node was found
566       if ( composednode )
567         {
568           scomposednode = GuiContext::getCurrent()->_mapOfSubjectNode[composednode];
569         }
570       // creation of target node
571       else 
572         {
573           //create a ComposedNode (type _type) with name _newParent
574           YACS::ENGINE::Catalog *catalog = YACS::ENGINE::getSALOMERuntime()->getBuiltinCatalog();
575           Node* nodeToClone = catalog->_composednodeMap[_type];
576           composednode = nodeToClone->clone(0);
577           composednode->setName(_newParent);
578           //add the new composednode as child of oldfather
579           oldFather->edAddChild(composednode);
580           //create the subject composednode
581           scomposednode = sop->addSubjectNode(composednode,"",catalog,"",_type);
582         }
583
584       //add the old node as child of new composednode
585       (dynamic_cast<YACS::ENGINE::ComposedNode*>(composednode))->edAddChild(node);
586       _newpos = proc->getChildName(node);
587       //add the subject node to subject composednode
588       (dynamic_cast<SubjectComposedNode*>(scomposednode))->houseKeepingAfterCutPaste(false, snode);
589       snode->setParent(scomposednode);
590       if (_toSaveRestoreLinks)
591         snode->restoreLinks(); //restore links
592       //refresh all views
593       scomposednode->update(PASTE, ProcInvoc::getTypeOfNode(node), snode);
594       snode->recursiveUpdate(RENAME, 0, snode);
595       snode->select(true);
596     }
597   catch (Exception& ex)
598     {
599       DEBTRACE("CommandPutInComposedNode::localExecute() : " << ex.what());
600       setErrorMsg(ex);
601       node = 0;
602     }
603   catch (...)
604     {
605       GuiContext::getCurrent()->_lastErrorMessage = "Unknown exception";
606       node = 0;
607     }
608   return (node != 0);
609 }
610
611 bool CommandPutInComposedNode::localReverse()
612 {
613   DEBTRACE("CommandPutInComposedNode::localReverse");
614   Proc* proc = GuiContext::getCurrent()->getProc();
615   Node* node = 0;
616   try
617     {
618       YASSERT(_newpos != proc->getName())
619       node = proc->getChildByName(_newpos);
620       ComposedNode *oldFather = node->getFather();
621       SubjectNode * snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
622       Subject *subo = GuiContext::getCurrent()->_mapOfSubjectNode[oldFather];
623       SubjectComposedNode* sop = dynamic_cast<SubjectComposedNode*>(subo);
624       //save existing links
625       snode->saveLinks();
626       //remove external links
627       snode->removeExternalLinks();
628       snode->removeExternalControlLinks();
629       //remove subject node from subject old father
630       sop->houseKeepingAfterCutPaste(true, snode);
631       //remove node from old father
632       oldFather->edRemoveChild(node);
633       //refresh node views, temporary paste in proc to keep widgets associated to node
634       sop->update(CUT, ProcInvoc::getTypeOfNode(node), snode);
635       GuiContext::getCurrent()->getSubjectProc()->update(PASTE, ProcInvoc::getTypeOfNode(node), snode);
636
637       //remove composed node oldFather
638       ComposedNode *oldGrandFather = oldFather->getFather();
639       Subject *subog = GuiContext::getCurrent()->_mapOfSubjectNode[oldGrandFather];
640       SubjectComposedNode* sogp = dynamic_cast<SubjectComposedNode*>(subog);
641       Subject::erase(sop);
642
643        //add the old node as child of new composednode
644       oldGrandFather->edAddChild(node);
645       _newpos = proc->getChildName(node);
646       //add the subject node to subject composednode
647       sogp->houseKeepingAfterCutPaste(false, snode);
648       snode->setParent(sogp);
649       //restore links
650       snode->restoreLinks();
651       //refresh all views
652       GuiContext::getCurrent()->getSubjectProc()->update(CUT, ProcInvoc::getTypeOfNode(node), snode);
653       sogp->update(PASTE, ProcInvoc::getTypeOfNode(node), snode);
654       snode->recursiveUpdate(RENAME, 0, snode);
655       snode->select(true);
656     }
657   catch (Exception& ex)
658     {
659       DEBTRACE("CommandPutInComposedNode::localReverse() : " << ex.what());
660       setErrorMsg(ex);
661       node = 0;
662     }      
663   return (node != 0);
664 }
665 // ----------------------------------------------------------------------------
666
667 CommandCopyNode::CommandCopyNode(YACS::ENGINE::Proc *fromproc,
668                                  std::string position,
669                                  std::string newParent,
670                                  int swCase)
671   : Command(), _fromproc(fromproc), _position(position), _newParent(newParent), _newName(""), _clone(0), _case(swCase)
672 {
673   DEBTRACE("CommandCopyNode::CommandCopyNode " << _position << " " << _newParent);
674 }
675
676 std::string CommandCopyNode::dump()
677 {
678   string ret ="CommandCopyNode " + _position + " " + _newParent;
679   return ret;
680 }
681
682 YACS::ENGINE::Node *CommandCopyNode::getNode()
683 {
684   return _clone;
685 }
686
687 bool CommandCopyNode::localExecute()
688 {
689   DEBTRACE("CommandCopyNode::localExecute");
690   Proc* proc = GuiContext::getCurrent()->getProc();
691   Node* node = 0;
692   try
693     {
694       if (_position == _fromproc->getName())
695         throw YACS::Exception("Copy the proc (main bloc) is impossible");
696       node = _fromproc->getChildByName(_position);
697       ComposedNode *oldFather = node->getFather();
698       ComposedNode *newFather = proc;
699       Node *newF = 0;
700       if (_newParent != proc->getName())
701         {
702           newF = proc->getChildByName(_newParent);
703           newFather = dynamic_cast<ComposedNode*>(newF);
704         }
705       if (!newFather)
706         throw YACS::Exception("new parent must be a composed node");
707       if (Loop *loop = dynamic_cast<Loop*>(newFather))
708         if (!loop->edGetDirectDescendants().empty())
709           throw YACS::Exception("Already a node in a new parent of Loop type");
710       //_clone = node->clone(newFather);
711       _clone = node->clone(0);
712       if (!_clone)
713         throw YACS::Exception("Node cannot be cloned");
714       int nodeSuffix = -1;
715       bool sameName = true;
716       stringstream s;
717       do
718         {
719           s.str("");
720           s << node->getName();
721           if (nodeSuffix >= 0) // --- first try <0 means without suffix
722             s << nodeSuffix;
723           DEBTRACE(s.str());
724           try
725             {
726               Node *nodeSameName = newFather->getChildByName(s.str());
727             }
728           catch (Exception& e)
729             {
730               sameName = false;
731             }
732           nodeSuffix++;
733         }
734       while(sameName);
735       _clone->setName(s.str());
736       _newName = _clone->getName();
737
738       if (YACS::ENGINE::Switch* theswitch = dynamic_cast<YACS::ENGINE::Switch*>(newFather))
739         {
740           DEBTRACE("father is a switch " << newFather->getName());
741           int theCase=_case;
742           if(theswitch->edGetNode(_case))
743             {
744               //the case is already used. Try another one
745               theCase=theswitch->getMaxCase()+1;
746             }
747           theswitch->edSetNode(theCase,_clone);
748         }
749       else
750         newFather->edAddChild(_clone);
751
752       _newName = _clone->getQualifiedName();
753       DEBTRACE(_newName);
754
755       SubjectNode *sub = GuiContext::getCurrent()->_mapOfSubjectNode[newFather];
756       SubjectComposedNode *snp = dynamic_cast<SubjectComposedNode*>(sub);
757       SubjectNode *son = snp->addSubjectNode(_clone);
758       son->loadChildren();
759       son->loadLinks();
760     }
761   catch (Exception& ex)
762     {
763       DEBTRACE("CommandCopyNode::localExecute() : " << ex.what());
764       setErrorMsg(ex);
765       _clone = 0;
766     }
767   return (_clone != 0); 
768 }
769
770 bool CommandCopyNode::localReverse()
771 {
772   DEBTRACE("CommandCopyNode::localReverse " << _position << " " << _newParent);
773   try
774     {
775       Proc* proc = GuiContext::getCurrent()->getProc();
776       string nodeName = _newParent + "." + _newName;
777       if (_newParent == proc->getName())
778         nodeName = _newName;
779       DEBTRACE(nodeName);
780       _clone = proc->getChildByName(nodeName);
781
782       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(_clone));
783       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[_clone];
784       Subject *father = snode->getParent();
785       Subject::erase(snode);
786       _clone = 0;
787       if (father)
788         {
789           DEBTRACE("REMOVE");
790           father->select(true);
791           father->update(REMOVE,0,0);
792         }
793       return true;
794     }
795   catch (Exception& ex)
796     {
797       DEBTRACE("CommandCopyNode::localReverse(): " << ex.what());
798       setErrorMsg(ex);
799       return false;
800     }
801   return true;
802 }
803
804 // ----------------------------------------------------------------------------
805
806 CommandRenameNode::CommandRenameNode(std::string position, std::string name)
807   : Command(), _position(position), _name(name)
808 {
809   DEBTRACE("CommandRenameNode::CommandRenameNode " << _position << " " << _name);
810   _oldName ="";
811   _newpos="";
812 }
813
814 std::string CommandRenameNode::dump()
815 {
816   string ret ="CommandRenameNode " + _position + " " + _name;
817   return ret;
818 }
819
820 bool CommandRenameNode::localExecute()
821 {
822   DEBTRACE("CommandRenameNode::localExecute");
823   Proc* proc = GuiContext::getCurrent()->getProc();
824   Node* node = proc;
825   try
826     {
827       if (_position != proc->getName()) node = proc->getChildByName(_position);
828       _oldName = node->getName();
829       node->setName(_name);
830       if (node == proc)
831         _newpos = _name;
832       else
833         _newpos = proc->getChildName(node);
834       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
835       if (snode)
836         snode->recursiveUpdate(RENAME, 0, snode);
837     }
838   catch (Exception& ex)
839     {
840       DEBTRACE("CommandRenameNode::localExecute() : " << ex.what());
841       setErrorMsg(ex);
842       node = 0;
843     }
844   return (node != 0); 
845 }
846
847 bool CommandRenameNode::localReverse()
848 {
849   DEBTRACE("CommandRenameNode::localReverse");
850   Proc* proc = GuiContext::getCurrent()->getProc();
851   Node* node = proc;
852   try
853     {
854       if (_newpos != proc->getName()) node = proc->getChildByName(_newpos);
855       YASSERT(node->getName() == _name);
856       node->setName(_oldName);
857       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
858       if (snode)
859         snode->recursiveUpdate(RENAME, 0, snode);
860     }
861   catch (Exception& ex)
862     {
863       DEBTRACE("CommandRenameNode::localReverse() : " << ex.what());
864       setErrorMsg(ex);
865       node = 0;
866     }
867   return (node != 0); 
868 }
869
870 // ----------------------------------------------------------------------------
871
872 CommandRenameContainer::CommandRenameContainer(std::string oldName, std::string newName)
873   : Command(), _oldName(oldName), _newName(newName)
874 {
875   DEBTRACE("CommandRenameContainer::CommandRenameContainer " << _oldName << " " << _newName);
876 }
877
878 std::string CommandRenameContainer::dump()
879 {
880   string ret ="CommandRenameContainer " +_oldName + " " + _newName;
881   return ret;
882 }
883
884 bool CommandRenameContainer::localExecute()
885 {
886   DEBTRACE("CommandRenameContainer::localExecute");
887   Proc* proc = GuiContext::getCurrent()->getProc();
888   Container *container = 0;
889   try
890     {
891       YASSERT(proc->containerMap.count(_oldName));
892       container = proc->containerMap[_oldName];
893       if (proc->containerMap.count(_newName))
894         {
895           GuiContext::getCurrent()->_lastErrorMessage = "Container name already existing";
896           return 0;
897         }
898       proc->containerMap.erase(_oldName);
899       container->setName(_newName);
900       proc->containerMap[_newName] = container;
901       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(container));
902       SubjectContainer *scont = GuiContext::getCurrent()->_mapOfSubjectContainer[container]; 
903       scont-> update(RENAME, 0, scont);
904       scont->notifyComponentsChange(ASSOCIATE, CONTAINER, scont);
905     }
906   catch (Exception& ex)
907     {
908       DEBTRACE("CommandRenameContainer::localExecute() : " << ex.what());
909       setErrorMsg(ex);
910       container = 0;
911     }
912   return (container != 0); 
913 }
914
915 bool CommandRenameContainer::localReverse()
916 {
917   DEBTRACE("CommandRenameContainer::localReverse");
918   Proc* proc = GuiContext::getCurrent()->getProc();
919   Container *container = 0;
920   try
921     {
922       YASSERT(proc->containerMap.count(_newName));
923       container = proc->containerMap[_newName];
924       proc->containerMap.erase(_newName);
925       container->setName(_oldName);
926       proc->containerMap[_oldName] = container;
927       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(container));
928       SubjectContainer *scont = GuiContext::getCurrent()->_mapOfSubjectContainer[container]; 
929       scont-> update(RENAME, 0, scont);
930       scont->notifyComponentsChange(ASSOCIATE, CONTAINER, scont);
931     }
932   catch (Exception& ex)
933     {
934       DEBTRACE("CommandRenameContainer::localReverse() : " << ex.what());
935       setErrorMsg(ex);
936       container = 0;
937     }
938   return (container != 0); 
939 }
940
941 // ----------------------------------------------------------------------------
942
943 CommandRenameInDataPort::CommandRenameInDataPort(std::string position,
944                                                  std::string oldName,
945                                                  std::string newName, TypeOfElem portType)
946   : Command(), _position(position), _oldName(oldName), _newName(newName), _portType(portType)
947 {
948   DEBTRACE("CommandRenameInDataPort::CommandRenameInDataPort "
949            << _position << " " << _oldName<< " " << _newName);
950 }
951
952 std::string CommandRenameInDataPort::dump()
953 {
954   string ret ="CommandRenameInDataPort " + _position + " " + _oldName + " " + _newName;
955   return ret;
956 }
957
958 bool CommandRenameInDataPort::localExecute()
959 {
960   DEBTRACE("CommandRenameInDataPort::localExecute");
961   Proc* proc = GuiContext::getCurrent()->getProc();
962   Node* node = proc;
963   try
964     {
965       if (_position != proc->getName()) node = proc->getChildByName(_position);
966       InPort * port = 0;
967
968       try
969         {
970           if(_portType==INPUTPORT)
971             port = node->getInputPort(_newName);
972           else
973             port = node->getInputDataStreamPort(_newName);
974         }
975       catch (Exception& e) {} // --- raised when no existing port with _newName
976       if (port)
977         throw Exception("there is already a port with the new name");
978
979       if(_portType==INPUTPORT)
980         port = node->getInputPort(_oldName);
981       else
982         port = node->getInputDataStreamPort(_oldName);
983       port->setName(_newName);
984       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(port));
985       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[port];
986       sport->update(RENAME, 0, sport);
987     }
988   catch (Exception& ex)
989     {
990       DEBTRACE("CommandRenameInDataPort::localExecute() : " << ex.what());
991       setErrorMsg(ex);
992       node = 0;
993     }
994   return (node != 0); 
995 }
996
997 bool CommandRenameInDataPort::localReverse()
998 {
999   DEBTRACE("CommandRenameInDataPort::localReverse");
1000   Proc* proc = GuiContext::getCurrent()->getProc();
1001   Node* node = proc;
1002   try
1003     {
1004       if (_position != proc->getName()) node = proc->getChildByName(_position);
1005       InPort * port = 0;
1006
1007       try
1008         {
1009           if(_portType==INPUTPORT)
1010             port = node->getInputPort(_oldName);
1011           else
1012             port = node->getInputDataStreamPort(_oldName);
1013         }
1014       catch (Exception& e) {} // --- raised when no existing port with _newName
1015       if (port)
1016         throw Exception("there is already a port with the old name");
1017
1018       if(_portType==INPUTPORT)
1019         port = node->getInputPort(_newName);
1020       else
1021         port = node->getInputDataStreamPort(_newName);
1022       port->setName(_oldName);
1023       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(port));
1024       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[port];
1025       sport->update(RENAME, 0, sport);
1026     }
1027   catch (Exception& ex)
1028     {
1029       DEBTRACE("CommandRenameInDataPort::localReverse() : " << ex.what());
1030       setErrorMsg(ex);
1031       node = 0;
1032     }
1033   return (node != 0); 
1034 }
1035
1036 // ----------------------------------------------------------------------------
1037
1038 CommandRenameOutDataPort::CommandRenameOutDataPort(std::string position,
1039                                                    std::string oldName,
1040                                                    std::string newName, TypeOfElem portType)
1041   : Command(), _position(position), _oldName(oldName), _newName(newName), _portType(portType)
1042 {
1043   DEBTRACE("CommandRenameOutDataPort::CommandRenameOutDataPort "
1044            << _position << " " << _oldName<< " " << _newName);
1045 }
1046
1047 std::string CommandRenameOutDataPort::dump()
1048 {
1049   string ret ="CommandRenameOutDataPort "  + _position + " " + _oldName + " " + _newName;
1050   return ret;
1051 }
1052
1053 bool CommandRenameOutDataPort::localExecute()
1054 {
1055   DEBTRACE("CommandRenameOutDataPort::localExecute");
1056   Proc* proc = GuiContext::getCurrent()->getProc();
1057   Node* node = proc;
1058   try
1059     {
1060       if (_position != proc->getName()) node = proc->getChildByName(_position);
1061       OutPort * port = 0;
1062       try
1063         {
1064           if(_portType==OUTPUTPORT)
1065             port = node->getOutputPort(_newName);
1066           else
1067             port = node->getOutputDataStreamPort(_newName);
1068         }
1069       catch (Exception& e) {} // --- raised when no existing port with _newName
1070       if (port)
1071         throw Exception("there is already a port with the new name");
1072
1073       if(_portType==OUTPUTPORT)
1074         port = node->getOutputPort(_oldName);
1075       else
1076         port = node->getOutputDataStreamPort(_oldName);
1077       port->setName(_newName);
1078       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(port));
1079       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[port];
1080       sport->update(RENAME, 0, sport);
1081     }
1082   catch (Exception& ex)
1083     {
1084       DEBTRACE("CommandRenameOutDataPort::localExecute() : " << ex.what());
1085       setErrorMsg(ex);
1086       node = 0;
1087     }
1088   return (node != 0); 
1089 }
1090
1091 bool CommandRenameOutDataPort::localReverse()
1092 {
1093   DEBTRACE("CommandRenameOutDataPort::localReverse");
1094   Proc* proc = GuiContext::getCurrent()->getProc();
1095   Node* node = proc;
1096   try
1097     {
1098       if (_position != proc->getName()) node = proc->getChildByName(_position);
1099       OutPort * port = 0;
1100       try
1101         {
1102           if(_portType==OUTPUTPORT)
1103             port = node->getOutputPort(_oldName);
1104           else
1105             port = node->getOutputDataStreamPort(_oldName);
1106         }
1107       catch (Exception& e) {} // --- raised when no existing port with _newName
1108       if (port)
1109         throw Exception("there is already a port with the old name");
1110
1111       if(_portType==OUTPUTPORT)
1112         port = node->getOutputPort(_newName);
1113       else
1114         port = node->getOutputDataStreamPort(_newName);
1115       port->setName(_oldName);
1116       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(port));
1117       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[port];
1118       sport->update(RENAME, 0, sport);
1119     }
1120   catch (Exception& ex)
1121     {
1122       DEBTRACE("CommandRenameOutDataPort::localReverse() : " << ex.what());
1123       setErrorMsg(ex);
1124       node = 0;
1125     }
1126   return (node != 0); 
1127 }
1128
1129 // ----------------------------------------------------------------------------
1130
1131 CommandAddDataTypeFromCatalog::CommandAddDataTypeFromCatalog(YACS::ENGINE::Catalog* catalog,
1132                                                              std::string typeName)
1133   : Command(), _catalog(catalog), _typeName(typeName)
1134 {
1135   DEBTRACE("CommandAddDataTypeFromCatalog::CommandAddDataTypeFromCatalog: " << typeName);
1136 }
1137
1138 std::string CommandAddDataTypeFromCatalog::dump()
1139 {
1140   string ret ="CommandAddDataTypeFromCatalog " + _typeName;
1141   return ret;
1142 }
1143
1144 YACS::ENGINE::TypeCode *CommandAddDataTypeFromCatalog::getTypeCode()
1145 {
1146
1147   if (GuiContext::getCurrent()->getProc()->typeMap.count(_typeName))
1148     return GuiContext::getCurrent()->getProc()->typeMap[_typeName];
1149   else return 0;
1150 }
1151
1152 bool CommandAddDataTypeFromCatalog::localExecute()
1153 {
1154   DEBTRACE("CommandAddDataTypeFromCatalog::localExecute");
1155   Proc* proc = GuiContext::getCurrent()->getProc();
1156   if (proc->typeMap.count(_typeName))
1157     {
1158       DEBTRACE("typecode already existing in proc: " << _typeName);
1159       GuiContext::getCurrent()->_lastErrorMessage = "typeCode already existing in proc: " + _typeName;
1160       return false;
1161     }
1162   else
1163     if (_catalog->_typeMap.count(_typeName))
1164       {
1165         DEBTRACE("typecode found in catalog, cloned: " << _typeName);
1166         proc->typeMap[_typeName] = _catalog->_typeMap[_typeName]->clone();
1167         proc->typeMap[_typeName]->incrRef();
1168         SubjectProc *sproc = GuiContext::getCurrent()->getSubjectProc();
1169         SubjectDataType *son = sproc->addSubjectDataType(getTypeCode(), _typeName);
1170         return (son!=0);
1171       }
1172   GuiContext::getCurrent()->_lastErrorMessage = "typecode not found in catalog: " + _typeName;
1173   return false;
1174 }
1175
1176 bool CommandAddDataTypeFromCatalog::localReverse()
1177 {
1178   DEBTRACE("CommandAddDataTypeFromCatalog::localReverse");
1179   try
1180     {
1181       SubjectProc *sproc = GuiContext::getCurrent()->getSubjectProc();
1182       sproc->removeSubjectDataType(_typeName);
1183       return true;
1184     }
1185   catch (Exception& ex)
1186     {
1187       DEBTRACE("CommandAddDataTypeFromCatalog::localReverse(): " << ex.what());
1188       setErrorMsg(ex);
1189       return 0;
1190     }
1191 }
1192
1193
1194 // ----------------------------------------------------------------------------
1195
1196 CommandAddInputPortFromCatalog::CommandAddInputPortFromCatalog(YACS::ENGINE::Catalog *catalog,
1197                                                                std::string type,
1198                                                                std::string node,
1199                                                                std::string name)
1200   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
1201 {
1202   _inputPort = 0;
1203   _sip = 0;
1204 }
1205
1206 std::string CommandAddInputPortFromCatalog::dump()
1207 {
1208   string ret ="CommandAddInputPortFromCatalog " + _typePort + " " + _node + " " + _name;
1209   return ret;
1210 }
1211
1212 YACS::ENGINE::InputPort *CommandAddInputPortFromCatalog::getInputPort()
1213 {
1214   return _inputPort;
1215 }
1216  
1217 SubjectInputPort* CommandAddInputPortFromCatalog::getSubjectInputPort()
1218 {
1219   return _sip;
1220 }
1221
1222 bool CommandAddInputPortFromCatalog::localExecute()
1223 {
1224   DEBTRACE("CommandAddInputPortFromCatalog::localExecute");
1225   InputPort *son = 0;
1226   try
1227     {
1228       Proc* proc = GuiContext::getCurrent()->getProc();
1229       Node* node = proc->getChildByName(_node);
1230       ElementaryNode* father = dynamic_cast<ElementaryNode*>(node);
1231       if (father)
1232         {
1233           //try proc types and then catalog if not in proc
1234           if(proc->typeMap.count(_typePort))
1235             son = father->edAddInputPort(_name, proc->typeMap[_typePort]);
1236           else if (_catalog->_typeMap.count(_typePort))
1237             son = father->edAddInputPort(_name, _catalog->_typeMap[_typePort]);
1238           else
1239             {
1240               DEBTRACE(_typePort << " not found in catalog " << _catalog);
1241               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
1242             }
1243         }
1244       _inputPort = son;
1245       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1246       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1247       _sip = snode->addSubjectInputPort(son, _name);
1248       snode->update(SYNCHRO,0,0); // when output port tab in node edition is visible
1249     }
1250   catch (Exception& ex)
1251     {
1252       DEBTRACE("CommandAddInputPortFromCatalog::localExecute() : " << ex.what());
1253       setErrorMsg(ex);
1254       if (son) delete son;
1255       _inputPort = 0;
1256     }
1257   return (_inputPort != 0);
1258 }
1259
1260 bool CommandAddInputPortFromCatalog::localReverse()
1261 {
1262   DEBTRACE("CommandAddInputPortFromCatalog::localReverse");
1263   try
1264     {
1265       Proc* proc = GuiContext::getCurrent()->getProc();
1266       Node *node = proc->getChildByName(_node);
1267       ElementaryNode* enode = dynamic_cast<ElementaryNode*>(node);
1268       YASSERT(enode);      
1269       _inputPort = enode->getInputPort(_name);
1270       YASSERT(_inputPort);
1271       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(_inputPort));
1272       _sip = dynamic_cast<SubjectInputPort*>(GuiContext::getCurrent()->_mapOfSubjectDataPort[_inputPort]);
1273       YASSERT(_sip);
1274
1275       Subject *father=_sip->getParent();
1276       Subject::erase(_sip);
1277       _sip = 0;
1278       _inputPort = 0;
1279       if (father)
1280         {
1281           DEBTRACE("REMOVE");
1282           father->select(true);
1283           father->update(REMOVE,0,0);
1284         }
1285       return true;
1286     }
1287   catch (Exception& ex)
1288     {
1289       DEBTRACE("CommandAddInputPortFromCatalog::localReverse(): " << ex.what());
1290       setErrorMsg(ex);
1291       return false;
1292     }
1293 }
1294
1295 // ----------------------------------------------------------------------------
1296
1297 CommandAddOutputPortFromCatalog::CommandAddOutputPortFromCatalog(YACS::ENGINE::Catalog *catalog,
1298                                                                  std::string type,
1299                                                                  std::string node,
1300                                                                  std::string name)
1301   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
1302 {
1303   _outputPort = 0;
1304   _sop = 0;
1305 }
1306
1307 std::string CommandAddOutputPortFromCatalog::dump()
1308 {
1309   string ret ="CommandAddOutputPortFromCatalog " + _typePort + " " + _node + " " + _name;
1310   return ret;
1311 }
1312
1313 YACS::ENGINE::OutputPort *CommandAddOutputPortFromCatalog::getOutputPort()
1314 {
1315   return _outputPort;
1316 }
1317
1318 SubjectOutputPort* CommandAddOutputPortFromCatalog::getSubjectOutputPort()
1319 {
1320   return _sop;
1321 }
1322
1323 bool CommandAddOutputPortFromCatalog::localExecute()
1324 {
1325   DEBTRACE("CommandAddOutputPortFromCatalog::localExecute");
1326   OutputPort *son = 0;
1327   try
1328     {
1329       Proc* proc = GuiContext::getCurrent()->getProc();
1330       Node* node = proc->getChildByName(_node);
1331       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
1332       if (father)
1333         {
1334           //try proc types and then catalog if not in proc
1335           if(proc->typeMap.count(_typePort))
1336             son = father->edAddOutputPort(_name, proc->typeMap[_typePort]);
1337           else if (_catalog->_typeMap.count(_typePort))
1338             son = father->edAddOutputPort(_name, _catalog->_typeMap[_typePort]);
1339           else
1340             {
1341               DEBTRACE(_typePort << " not found in catalog");
1342               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
1343             }
1344         }
1345       _outputPort = son;
1346       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1347       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1348       _sop = snode->addSubjectOutputPort(son, _name);
1349       snode->update(SYNCHRO,0,0); // when input port tab in node edition is visible
1350     }
1351   catch (Exception& ex)
1352     {
1353       DEBTRACE("CommandAddOutputPortFromCatalog::localExecute() : " << ex.what());
1354       setErrorMsg(ex);
1355       if (son) delete son;
1356       _outputPort = 0;
1357     }
1358   return (_outputPort != 0);
1359 }
1360
1361 bool CommandAddOutputPortFromCatalog::localReverse()
1362 {
1363   DEBTRACE("CommandAddOutputPortFromCatalog::localReverse");
1364   try
1365     {
1366       Proc* proc = GuiContext::getCurrent()->getProc();
1367       Node *node = proc->getChildByName(_node);
1368       ElementaryNode* enode = dynamic_cast<ElementaryNode*>(node);
1369       YASSERT(enode);      
1370       _outputPort = enode->getOutputPort(_name);
1371       YASSERT(_outputPort);
1372       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(_outputPort));
1373       _sop = dynamic_cast<SubjectOutputPort*>(GuiContext::getCurrent()->_mapOfSubjectDataPort[_outputPort]);
1374       YASSERT(_sop);
1375
1376       Subject *father=_sop->getParent();
1377       Subject::erase(_sop);
1378       _sop = 0;
1379       _outputPort = 0;
1380       if (father)
1381         {
1382           DEBTRACE("REMOVE");
1383           father->select(true);
1384           father->update(REMOVE,0,0);
1385         }
1386       return true;
1387     }
1388   catch (Exception& ex)
1389     {
1390       DEBTRACE("CommandAddOutputPortFromCatalog::localReverse(): " << ex.what());
1391       setErrorMsg(ex);
1392       return false;
1393     }
1394 }
1395
1396 // ----------------------------------------------------------------------------
1397
1398 CommandAddIDSPortFromCatalog::CommandAddIDSPortFromCatalog(YACS::ENGINE::Catalog *catalog,
1399                                                            std::string type,
1400                                                            std::string node,
1401                                                            std::string name)
1402   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
1403 {
1404   _IDSPort = 0;
1405   _sip = 0;
1406 }
1407
1408 std::string CommandAddIDSPortFromCatalog::dump()
1409 {
1410   string ret ="CommandAddIDSPortFromCatalog " + _typePort + " " + _node + " " + _name;
1411   return ret;
1412 }
1413
1414 YACS::ENGINE::InputDataStreamPort *CommandAddIDSPortFromCatalog::getIDSPort()
1415 {
1416   DEBTRACE("CommandAddIDSPortFromCatalog");
1417   return _IDSPort;
1418 }
1419
1420 SubjectInputDataStreamPort* CommandAddIDSPortFromCatalog::getSubjectIDSPort()
1421 {
1422   return _sip;
1423 }
1424
1425 bool CommandAddIDSPortFromCatalog::localExecute()
1426 {
1427   DEBTRACE("CommandAddIDSPortFromCatalog::localExecute");
1428   InputDataStreamPort *son = 0;
1429   try
1430     {
1431       Proc* proc = GuiContext::getCurrent()->getProc();
1432       Node* node = proc->getChildByName(_node);
1433       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
1434       if (father)
1435         {
1436           if (_catalog->_typeMap.count(_typePort))
1437             son = father->edAddInputDataStreamPort(_name, _catalog->_typeMap[_typePort]);
1438           else
1439             {
1440               DEBTRACE(_typePort << " not found in catalog");
1441               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
1442             }
1443         }
1444       _IDSPort = son;
1445       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1446       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1447       _sip = snode->addSubjectIDSPort(son, _name);
1448     }
1449   catch (Exception& ex)
1450     {
1451       DEBTRACE("CommandAddIDSPortFromCatalog::localExecute() : " << ex.what());
1452       setErrorMsg(ex);
1453       if (son) delete son;
1454       _IDSPort = 0;
1455     }
1456   return (_IDSPort != 0);
1457 }
1458
1459 bool CommandAddIDSPortFromCatalog::localReverse()
1460 {
1461   DEBTRACE("CommandAddIDSPortFromCatalog::localReverse");
1462   try
1463     {
1464       Proc* proc = GuiContext::getCurrent()->getProc();
1465       Node *node = proc->getChildByName(_node);
1466       ElementaryNode* enode = dynamic_cast<ElementaryNode*>(node);
1467       YASSERT(enode);      
1468       _IDSPort = enode->getInputDataStreamPort(_name);
1469       YASSERT(_IDSPort);
1470       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(_IDSPort));
1471       _sip = dynamic_cast<SubjectInputDataStreamPort*>(GuiContext::getCurrent()->_mapOfSubjectDataPort[_IDSPort]);
1472       YASSERT(_sip);
1473
1474       Subject *father=_sip->getParent();
1475       Subject::erase(_sip);
1476       _IDSPort =0;
1477       _sip = 0;
1478       if (father)
1479         {
1480           DEBTRACE("REMOVE");
1481           father->select(true);
1482           father->update(REMOVE,0,0);
1483         }
1484       return true;
1485     }
1486   catch (Exception& ex)
1487     {
1488       DEBTRACE("CommandAddIDSPortFromCatalog::localReverse(): " << ex.what());
1489       setErrorMsg(ex);
1490       return false;
1491     }
1492 }
1493  
1494 // ----------------------------------------------------------------------------
1495
1496 CommandAddODSPortFromCatalog::CommandAddODSPortFromCatalog(YACS::ENGINE::Catalog *catalog,
1497                                                            std::string type,
1498                                                            std::string node,
1499                                                            std::string name)
1500   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
1501 {
1502   DEBTRACE("CommandAddODSPortFromCatalog");
1503   _ODSPort = 0;
1504   _sop = 0;
1505 }
1506
1507 std::string CommandAddODSPortFromCatalog::dump()
1508 {
1509   string ret ="CommandAddODSPortFromCatalog " + _typePort + " " + _node + " " + _name;
1510   return ret;
1511 }
1512
1513 YACS::ENGINE::OutputDataStreamPort *CommandAddODSPortFromCatalog::getODSPort()
1514 {
1515   return _ODSPort;
1516 }
1517
1518 SubjectOutputDataStreamPort* CommandAddODSPortFromCatalog::getSubjectODSPort()
1519 {
1520   return _sop;
1521 }
1522
1523 bool CommandAddODSPortFromCatalog::localExecute()
1524 {
1525   DEBTRACE("CommandAddODSPortFromCatalog::localExecute");
1526   OutputDataStreamPort *son = 0;
1527   try
1528     {
1529       Proc* proc = GuiContext::getCurrent()->getProc();
1530       Node* node = proc->getChildByName(_node);
1531       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
1532       if (father)
1533         {
1534           if (_catalog->_typeMap.count(_typePort))
1535             son = father->edAddOutputDataStreamPort(_name, _catalog->_typeMap[_typePort]);
1536           else
1537             {
1538               DEBTRACE(_typePort << " not found in catalog");
1539               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
1540             }
1541         }
1542       _ODSPort = son;
1543       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1544       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1545       _sop = snode->addSubjectODSPort(son, _name);
1546     }
1547   catch (Exception& ex)
1548     {
1549       DEBTRACE("CommandAddODSPortFromCatalog::localExecute() : " << ex.what());
1550       setErrorMsg(ex);
1551       if (son) delete son;
1552       _ODSPort = 0;
1553     }
1554   return (_ODSPort != 0);
1555 }
1556
1557 bool CommandAddODSPortFromCatalog::localReverse()
1558 {
1559   DEBTRACE("CommandAddODSPortFromCatalog::localReverse");
1560   try
1561     {
1562       Proc* proc = GuiContext::getCurrent()->getProc();
1563       Node *node = proc->getChildByName(_node);
1564       ElementaryNode* enode = dynamic_cast<ElementaryNode*>(node);
1565       YASSERT(enode);      
1566       _ODSPort = enode->getOutputDataStreamPort(_name);
1567       YASSERT(_ODSPort);
1568       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(_ODSPort));
1569       _sop = dynamic_cast<SubjectOutputDataStreamPort*>(GuiContext::getCurrent()->_mapOfSubjectDataPort[_ODSPort]);
1570       YASSERT(_sop);
1571
1572       Subject *father=_sop->getParent();
1573       Subject::erase(_sop);
1574       _sop = 0;
1575       _ODSPort = 0;
1576       if (father)
1577         {
1578           DEBTRACE("REMOVE");
1579           father->select(true);
1580           father->update(REMOVE,0,0);
1581         }
1582       return true;
1583     }
1584   catch (Exception& ex)
1585     {
1586       DEBTRACE("CommandAddODSPortFromCatalog::localReverse(): " << ex.what());
1587       setErrorMsg(ex);
1588       return false;
1589     }
1590 }
1591
1592 // ----------------------------------------------------------------------------
1593
1594 /*! move up or down a port in the list of ports of a node.
1595  *  if isUp = 0, move down one step, if isUp = n>0, move up n steps.
1596  */
1597 CommandOrderInputPorts::CommandOrderInputPorts(std::string node,
1598                                                std::string port,
1599                                                int isUp)
1600   : Command(), _node(node), _port(port), _isUp(isUp), _rank(-1)
1601 {
1602   DEBTRACE("CommandOrderInputPorts");
1603 }
1604
1605 std::string CommandOrderInputPorts::dump()
1606 {
1607   ostringstream s;
1608   s << _isUp;
1609   string ret ="CommandOrderInputPorts " + _node + " " + _port + " " + s.str();
1610   return ret;
1611 }
1612
1613 bool CommandOrderInputPorts::localExecute()
1614 {
1615   DEBTRACE("CommandOrderInputPorts::localExecute " << _node << " " << _port  << " " << _isUp);
1616   ElementaryNode* father = 0;
1617   try
1618     {
1619       Proc* proc = GuiContext::getCurrent()->getProc();
1620       Node* node = proc->getChildByName(_node);
1621       father = dynamic_cast<ElementaryNode*> (node);
1622       if (!father) return false;
1623       InputPort *portToMove = father->getInputPort(_port);
1624       DEBTRACE(portToMove->getName());
1625
1626       list<InputPort*> plist = father->getSetOfInputPort();
1627       list<InputPort*>::iterator pos = find(plist.begin(), plist.end(), portToMove);
1628
1629       int isUp = _isUp;
1630       if (isUp)
1631         {
1632           if(pos == plist.begin())
1633             pos=plist.end(); // --- cycle
1634           else
1635             do { pos--; isUp--; } while (isUp);
1636         }
1637       else
1638         {
1639           pos++;
1640           if (pos == plist.end())
1641             pos = plist.begin(); // --- cycle
1642           else
1643             pos++; // --- insert before the 2nd next port
1644         }
1645
1646       InputPort *portBefore = 0;
1647       if (pos != plist.end())
1648         portBefore = (*pos);
1649
1650       plist.remove(portToMove);
1651       if (portBefore)
1652         {
1653           DEBTRACE(portBefore->getName());
1654           pos = find(plist.begin(), plist.end(), portBefore);
1655           _rank = 0;
1656           for (list<InputPort*>::iterator it = plist.begin(); it != pos; ++it)
1657             _rank++;
1658           plist.insert(pos, portToMove);
1659         }
1660       else
1661         {
1662           _rank = plist.size();
1663           plist.push_back(portToMove);
1664         }
1665       father->edOrderInputPorts(plist);
1666       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1667       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1668       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[portToMove];
1669       snode->update(ORDER, true, sport);
1670       snode->update(SYNCHRO, true, sport); // --- synchronise edition and scene
1671     }
1672   catch (Exception& ex)
1673     {
1674       DEBTRACE("CommandOrderInputPorts::localExecute() : " << ex.what());
1675       setErrorMsg(ex);
1676       father = 0;
1677     }
1678   return (father != 0);
1679 }
1680
1681 bool CommandOrderInputPorts::localReverse()
1682 {
1683   DEBTRACE("CommandOrderInputPorts::localReverse " << _node << " " << _port  << " " << _isUp);
1684   ElementaryNode* father = 0;
1685   try
1686     {
1687       int isUpRev = -_isUp;
1688       if (isUpRev == 0) isUpRev =1;
1689       Proc* proc = GuiContext::getCurrent()->getProc();
1690       Node* node = proc->getChildByName(_node);
1691       father = dynamic_cast<ElementaryNode*> (node);
1692       if (!father) return false;
1693       InputPort *portToMove = father->getInputPort(_port);
1694       DEBTRACE(portToMove->getName());
1695
1696       list<InputPort*> plist = father->getSetOfInputPort();
1697       list<InputPort*>::iterator pos = find(plist.begin(), plist.end(), portToMove);
1698
1699       if (isUpRev>0)
1700         {
1701           if(pos == plist.begin())
1702             pos=plist.end(); // --- cycle
1703           else
1704             do { pos--; isUpRev--; } while (isUpRev);
1705         }
1706       else
1707         {
1708           pos++;
1709           if (pos == plist.end())
1710             pos = plist.begin(); // --- cycle
1711           else
1712             do { pos++; isUpRev++; } while (isUpRev<0);// --- insert before the 2nd next port
1713         }
1714
1715       InputPort *portBefore = 0;
1716       if (pos != plist.end())
1717         portBefore = (*pos);
1718
1719       plist.remove(portToMove);
1720       if (portBefore)
1721         {
1722           DEBTRACE(portBefore->getName());
1723           pos = find(plist.begin(), plist.end(), portBefore);
1724           _rank = 0;
1725           for (list<InputPort*>::iterator it = plist.begin(); it != pos; ++it)
1726             _rank++;
1727           plist.insert(pos, portToMove);
1728         }
1729       else
1730         {
1731           _rank = plist.size();
1732           plist.push_back(portToMove);
1733         }
1734       father->edOrderInputPorts(plist);
1735       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1736       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1737       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[portToMove];
1738       snode->update(ORDER, true, sport);
1739       snode->update(SYNCHRO, true, sport); // --- synchronise edition and scene
1740     }
1741   catch (Exception& ex)
1742     {
1743       DEBTRACE("CommandOrderInputPorts::localExecute() : " << ex.what());
1744       setErrorMsg(ex);
1745       father = 0;
1746     }
1747   return (father != 0);
1748 }
1749
1750 // ----------------------------------------------------------------------------
1751
1752 /*! move up or down a port in the list of ports of a node.
1753  *  if isUp = 0, move down one step, if isUp = n>0, move up n steps.
1754  */
1755 CommandOrderOutputPorts::CommandOrderOutputPorts(std::string node,
1756                                                  std::string port,
1757                                                  int isUp)
1758   : Command(), _node(node), _port(port), _isUp(isUp), _rank(-1)
1759 {
1760   DEBTRACE("CommandOrderOutputPorts");
1761 }
1762
1763 std::string CommandOrderOutputPorts::dump()
1764 {
1765   ostringstream s;
1766   s << _isUp;
1767   string ret ="CommandOrderOutputPorts " + _node + " " + _port + " " + s.str();
1768   return ret;
1769 }
1770
1771 bool CommandOrderOutputPorts::localExecute()
1772 {
1773   DEBTRACE("CommandOrderOutputPorts::localExecute " << _node << " " << _port  << " " << _isUp);
1774   ElementaryNode* father = 0;
1775   try
1776     {
1777       Proc* proc = GuiContext::getCurrent()->getProc();
1778       Node* node = proc->getChildByName(_node);
1779       father = dynamic_cast<ElementaryNode*> (node);
1780       if (!father) return false;
1781       OutputPort *portToMove = father->getOutputPort(_port);
1782       DEBTRACE(portToMove->getName());
1783
1784       list<OutputPort*> plist = father->getSetOfOutputPort();
1785       list<OutputPort*>::iterator pos = find(plist.begin(), plist.end(), portToMove);
1786
1787       int isUp = _isUp;
1788       if (isUp)
1789         {
1790           if(pos == plist.begin())
1791             pos=plist.end(); // --- cycle
1792           else
1793             do { pos--; isUp--; } while (isUp);
1794         }
1795       else
1796         {
1797           pos++;
1798           if (pos == plist.end())
1799             pos = plist.begin(); // --- cycle
1800           else
1801             pos++; // --- insert before the 2nd next port
1802         }
1803
1804       OutputPort *portBefore = 0;
1805       if (pos != plist.end())
1806         portBefore = (*pos);
1807
1808       plist.remove(portToMove);
1809       if (portBefore)
1810         {
1811           DEBTRACE(portBefore->getName());
1812           pos = find(plist.begin(), plist.end(), portBefore);
1813           _rank = 0;
1814           for (list<OutputPort*>::iterator it = plist.begin(); it != pos; ++it)
1815             _rank++;
1816           plist.insert(pos, portToMove);
1817         }
1818       else
1819         {
1820           _rank = plist.size();
1821           plist.push_back(portToMove);
1822         }
1823       father->edOrderOutputPorts(plist);
1824       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1825       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1826       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[portToMove];
1827       snode->update(ORDER, false, sport);
1828       snode->update(SYNCHRO, false, sport); // --- synchronise edition and scene
1829     }
1830   catch (Exception& ex)
1831     {
1832       DEBTRACE("CommandOrderOutputPorts::localExecute() : " << ex.what());
1833       setErrorMsg(ex);
1834       father = 0;
1835     }
1836   return (father != 0);
1837 }
1838
1839 bool CommandOrderOutputPorts::localReverse()
1840 {
1841   DEBTRACE("CommandOrderOutputPorts::localReverse " << _node << " " << _port  << " " << _isUp);
1842   ElementaryNode* father = 0;
1843   try
1844     {
1845       int isUpRev = -_isUp;
1846       if (isUpRev == 0) isUpRev =1;
1847       Proc* proc = GuiContext::getCurrent()->getProc();
1848       Node* node = proc->getChildByName(_node);
1849       father = dynamic_cast<ElementaryNode*> (node);
1850       if (!father) return false;
1851       OutputPort *portToMove = father->getOutputPort(_port);
1852       DEBTRACE(portToMove->getName());
1853
1854       list<OutputPort*> plist = father->getSetOfOutputPort();
1855       list<OutputPort*>::iterator pos = find(plist.begin(), plist.end(), portToMove);
1856
1857       if (isUpRev>0)
1858         {
1859           if(pos == plist.begin())
1860             pos=plist.end(); // --- cycle
1861           else
1862             do { pos--; isUpRev--; } while (isUpRev);
1863         }
1864       else
1865         {
1866           pos++;
1867           if (pos == plist.end())
1868             pos = plist.begin(); // --- cycle
1869           else
1870             do { pos++; isUpRev++; } while (isUpRev<0);// --- insert before the 2nd next port
1871         }
1872
1873       OutputPort *portBefore = 0;
1874       if (pos != plist.end())
1875         portBefore = (*pos);
1876
1877       plist.remove(portToMove);
1878       if (portBefore)
1879         {
1880           DEBTRACE(portBefore->getName());
1881           pos = find(plist.begin(), plist.end(), portBefore);
1882           _rank = 0;
1883           for (list<OutputPort*>::iterator it = plist.begin(); it != pos; ++it)
1884             _rank++;
1885           plist.insert(pos, portToMove);
1886         }
1887       else
1888         {
1889           _rank = plist.size();
1890           plist.push_back(portToMove);
1891         }
1892       father->edOrderOutputPorts(plist);
1893       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1894       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1895       SubjectDataPort *sport = GuiContext::getCurrent()->_mapOfSubjectDataPort[portToMove];
1896       snode->update(ORDER, false, sport);
1897       snode->update(SYNCHRO, false, sport); // --- synchronise edition and scene
1898     }
1899   catch (Exception& ex)
1900     {
1901       DEBTRACE("CommandOrderOutputPorts::localExecute() : " << ex.what());
1902       setErrorMsg(ex);
1903       father = 0;
1904     }
1905   return (father != 0);
1906 }
1907
1908 // ----------------------------------------------------------------------------
1909
1910 CommandDestroy::CommandDestroy(TypeOfElem elemType,
1911                                std::string startnode, std::string startport, TypeOfElem startportType,
1912                                std::string endnode, std::string endport, TypeOfElem endportType)
1913   : Command(), _elemType(elemType), _startnode(startnode), _startport(startport),
1914     _endnode(endnode), _endport(endport), _startportType(startportType), _endportType(endportType)
1915 {
1916   DEBTRACE("CommandDestroy::CommandDestroy");
1917   _normalReverse = false;
1918 }
1919
1920 std::string CommandDestroy::dump()
1921 {
1922   string ret ="CommandDestroy " + ProcInvoc::getTypeName(_elemType);
1923   ret += " " + _startnode + " " + _startport;
1924   ret += " " + _endnode + " " + _endport;
1925   return ret;
1926 }
1927
1928 bool CommandDestroy::localExecute()
1929 {
1930   DEBTRACE("CommandDestroy::localExecute");
1931   try
1932     {
1933       Proc* proc = GuiContext::getCurrent()->getProc();
1934       Subject *subject = 0;
1935       Subject *father  = 0;
1936       switch (_elemType)
1937         {
1938         case SALOMEPROC:
1939         case BLOC:
1940         case FOREACHLOOP:
1941         case OPTIMIZERLOOP:
1942         case FORLOOP:
1943         case WHILELOOP:
1944         case SWITCH:
1945         case PYTHONNODE:
1946         case PYFUNCNODE:
1947         case CORBANODE:
1948         case SALOMENODE:
1949         case CPPNODE:
1950         case SALOMEPYTHONNODE:
1951         case XMLNODE:
1952         case SPLITTERNODE:
1953         case DFTODSFORLOOPNODE:
1954         case DSTODFFORLOOPNODE:
1955         case PRESETNODE:
1956         case OUTNODE:
1957         case STUDYINNODE:
1958         case STUDYOUTNODE:
1959           {
1960             Node* node = proc;
1961             if (!_startnode.empty()) node = proc->getChildByName(_startnode);
1962             YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
1963             subject = GuiContext::getCurrent()->_mapOfSubjectNode[node];
1964             father  = subject->getParent();
1965           }
1966           break;
1967         case INPUTPORT:
1968           {
1969             Node* node = proc->getChildByName(_startnode);
1970             InPort* inp = node->getInputPort(_startport);
1971             YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(inp));
1972             subject = GuiContext::getCurrent()->_mapOfSubjectDataPort[inp];
1973             father  = subject->getParent();
1974           }
1975           break;
1976         case INPUTDATASTREAMPORT:
1977           {
1978             Node* node = proc->getChildByName(_startnode);
1979             InPort* inp = node->getInputDataStreamPort(_startport);
1980             YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(inp));
1981             subject = GuiContext::getCurrent()->_mapOfSubjectDataPort[inp];
1982             father  = subject->getParent();
1983           }
1984           break;
1985         case OUTPUTPORT:
1986           {
1987             Node* node = proc->getChildByName(_startnode);
1988             OutPort* outp = node->getOutputPort(_startport);
1989             YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(outp));
1990             subject = GuiContext::getCurrent()->_mapOfSubjectDataPort[outp];
1991             father  = subject->getParent();
1992           }
1993           break;
1994         case OUTPUTDATASTREAMPORT:
1995           {
1996             Node* node = proc->getChildByName(_startnode);
1997             OutPort* outp = node->getOutputDataStreamPort(_startport);
1998             YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(outp));
1999             subject = GuiContext::getCurrent()->_mapOfSubjectDataPort[outp];
2000             father  = subject->getParent();
2001           }
2002           break;
2003         case DATALINK:
2004           {
2005             Node* outn = proc->getChildByName(_startnode);
2006             Node* inn = proc->getChildByName(_endnode);
2007
2008             OutPort* outp;
2009             InPort* inp;
2010
2011             if(_startportType == OUTPUTPORT)
2012               outp = outn->getOutputPort(_startport);
2013             else
2014               outp = outn->getOutputDataStreamPort(_startport);
2015
2016             if(_endportType == INPUTPORT)
2017               inp = inn->getInputPort(_endport);
2018             else
2019               inp = inn->getInputDataStreamPort(_endport);
2020
2021             pair<OutPort*,InPort*> keymap = pair<OutPort*,InPort*>(outp,inp);
2022             YASSERT(GuiContext::getCurrent()->_mapOfSubjectLink.count(keymap));
2023             subject = GuiContext::getCurrent()->_mapOfSubjectLink[keymap];
2024             father  = subject->getParent();
2025           }
2026           break;
2027         case CONTROLLINK:
2028           {
2029             Node* outn = proc->getChildByName(_startnode);
2030             Node* inn = proc->getChildByName(_endnode);
2031             pair<Node*,Node*> keymap = pair<Node*,Node*>(outn,inn);
2032             YASSERT(GuiContext::getCurrent()->_mapOfSubjectControlLink.count(keymap));
2033             subject = GuiContext::getCurrent()->_mapOfSubjectControlLink[keymap];
2034             father  = subject->getParent();
2035           }
2036           break;
2037         case CONTAINER:
2038           {
2039             Container *container = proc->containerMap[_startnode];
2040             subject = GuiContext::getCurrent()->_mapOfSubjectContainer[container];
2041             break;
2042           }
2043         case COMPONENT:
2044         case REFERENCE:
2045         case DATATYPE:
2046         case UNKNOWN:
2047           throw Exception("Command Destroy not implemented for that type");
2048           break;
2049         }
2050       YASSERT(subject);
2051       Subject::erase(subject);
2052       if (father) 
2053         {
2054           father->select(true);
2055           father->update(REMOVE, 0, 0);
2056         }
2057       subject = 0;
2058       return true; 
2059     }
2060   catch (Exception& ex)
2061     {
2062       DEBTRACE("CommandDestroy::localExecute() : " << ex.what());
2063       setErrorMsg(ex);
2064       return false;
2065     }
2066 }
2067
2068 bool CommandDestroy::localReverse()
2069 {
2070   DEBTRACE("CommandDestroy::localReverse");
2071   //! nothing to do here, all is done in subcommands
2072   return true;
2073 }
2074  
2075 // ----------------------------------------------------------------------------
2076
2077 CommandSetInPortValue::CommandSetInPortValue(std::string node,
2078                                              std::string port,
2079                                              std::string value)
2080   : Command(), _node(node), _port(port), _value(value)
2081 {
2082   DEBTRACE("CommandSetInPortValue::CommandSetInPortValue " << node << " " << port << " " << value);
2083   _oldValue = "";
2084 }
2085
2086 std::string CommandSetInPortValue::dump()
2087 {
2088   string ret ="CommandSetInPortValue " + _node + " " + _port + " " + _value;
2089   return ret;
2090 }
2091     
2092 bool CommandSetInPortValue::localExecute()
2093 {
2094   DEBTRACE("CommandSetInPortValue::localExecute");
2095   InputPort* inp ;
2096   InputPresetPort *inpp = 0;
2097   InputStudyPort *insp = 0;
2098   DataNode *dnode = 0;
2099   SubjectDataPort *sinp = 0;
2100   try
2101     {
2102       Proc* proc = GuiContext::getCurrent()->getProc();
2103       Node* node = proc->getChildByName(_node);
2104       inp = node->getInputPort(_port);
2105       inpp = dynamic_cast<InputPresetPort*>(inp);
2106       insp = dynamic_cast<InputStudyPort*>(inp);
2107       dnode = dynamic_cast<DataNode*>(node);
2108       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(inp));
2109       sinp = GuiContext::getCurrent()->_mapOfSubjectDataPort[inp];
2110     }
2111   catch (Exception& ex)
2112     {
2113       DEBTRACE("CommandSetInPortValue::localExecute() : " << ex.what());
2114       setErrorMsg(ex);
2115       return false;
2116     }
2117
2118   if(insp && dnode)
2119     {
2120       //It's a study port
2121       _oldValue = insp->getAsString();
2122       DEBTRACE("old value="<< _oldValue);
2123       dnode->setData(insp, _value );
2124       sinp->update(SETVALUE, 0, sinp);
2125       return true;
2126     }
2127
2128   PyObject *result;
2129   PyGILState_STATE gstate = PyGILState_Ensure();
2130   try
2131     {
2132       _oldValue = inp->getAsString();
2133       if (_oldValue == "None") _oldValue = "";
2134       DEBTRACE("old value="<< _oldValue);
2135       std::string strval;
2136       if (inp->edGetType()->kind() == YACS::ENGINE::String || inp->edGetType()->isA(Runtime::_tc_file))
2137         strval = "\"" + _value + "\"";
2138       else
2139         strval = _value;
2140       result = YACS::ENGINE::getSALOMERuntime()->convertStringToPyObject(strval.c_str());
2141       inp->edInit("Python", result);
2142       Py_DECREF(result);
2143
2144       PyGILState_Release(gstate);
2145       sinp->update(SETVALUE, 0, sinp);
2146       return true;
2147     }
2148   catch (Exception& ex)
2149     {
2150       DEBTRACE("CommandSetInPortValue::localExecute() : " << ex.what());
2151       //Py_DECREF(result);
2152       PyGILState_Release(gstate);
2153       setErrorMsg(ex);
2154       return false;
2155     }
2156 }
2157
2158 bool CommandSetInPortValue::localReverse()
2159 {
2160   DEBTRACE("CommandSetInPortValue::localReverse");
2161   InputPort* inp ;
2162   InputPresetPort *inpp = 0;
2163   InputStudyPort *insp = 0;
2164   DataNode *dnode = 0;
2165   SubjectDataPort *sinp = 0;
2166   try
2167     {
2168       Proc* proc = GuiContext::getCurrent()->getProc();
2169       Node* node = proc->getChildByName(_node);
2170       inp = node->getInputPort(_port);
2171       inpp = dynamic_cast<InputPresetPort*>(inp);
2172       insp = dynamic_cast<InputStudyPort*>(inp);
2173       dnode = dynamic_cast<DataNode*>(node);
2174       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(inp));
2175       sinp = GuiContext::getCurrent()->_mapOfSubjectDataPort[inp];
2176     }
2177   catch (Exception& ex)
2178     {
2179       DEBTRACE("CommandSetInPortValue::localExecute() : " << ex.what());
2180       setErrorMsg(ex);
2181       return false;
2182     }
2183
2184   if(insp && dnode)
2185     {
2186       //It's a study port
2187       _value = insp->getAsString();
2188       DEBTRACE("value="<< _value);
2189       DEBTRACE("old value="<< _oldValue);
2190       dnode->setData(insp, _oldValue );
2191       sinp->update(SETVALUE, 0, sinp);
2192       return true;
2193     }
2194
2195   PyObject *result = Py_None;
2196   PyGILState_STATE gstate = PyGILState_Ensure();
2197   try
2198     {
2199       _value = inp->getAsString();
2200       DEBTRACE("value="<< _value);
2201       DEBTRACE("old value="<< _oldValue);
2202       if (!_oldValue.empty())
2203         {
2204           std::string strval;
2205           if (inp->edGetType()->kind() == YACS::ENGINE::String || inp->edGetType()->isA(Runtime::_tc_file))
2206             strval = "\"" + _oldValue + "\"";
2207           else
2208             strval = _oldValue;
2209           result = YACS::ENGINE::getSALOMERuntime()->convertStringToPyObject(strval.c_str());
2210         }
2211       inp->edInit("Python", result);
2212       Py_DECREF(result);
2213       PyGILState_Release(gstate);
2214       sinp->update(SETVALUE, 0, sinp);
2215       return true;
2216     }
2217   catch (Exception& ex)
2218     {
2219       DEBTRACE("CommandSetInPortValue::localExecute() : " << ex.what());
2220       //Py_DECREF(result);
2221       PyGILState_Release(gstate);
2222       setErrorMsg(ex);
2223       return false;
2224     }
2225 }
2226
2227 // ----------------------------------------------------------------------------
2228
2229 CommandSetOutPortValue::CommandSetOutPortValue(std::string node,
2230                                                std::string port,
2231                                                std::string value)
2232   : Command(), _node(node), _port(port), _value(value)
2233 {
2234   DEBTRACE("CommandSetOutPortValue::CommandSetOutPortValue " << node << " " << port << " " << value);
2235   _oldValue = "";
2236 }
2237  
2238 std::string CommandSetOutPortValue::dump()
2239 {
2240   string ret ="CommandSetOutPortValue " + _node + " " + _port + " " + _value;
2241   return ret;
2242 }
2243    
2244 bool CommandSetOutPortValue::localExecute()
2245 {
2246   DEBTRACE("CommandSetOutPortValue::localExecute");
2247   OutputPresetPort *outpp = 0;
2248   OutputStudyPort *outsp = 0;
2249   DataNode *dnode = 0;
2250   SubjectDataPort *soutp = 0;
2251   try
2252     {
2253       Proc* proc = GuiContext::getCurrent()->getProc();
2254       Node* node = proc->getChildByName(_node);
2255       OutputPort* outp = node->getOutputPort(_port);
2256       outpp = dynamic_cast<OutputPresetPort*>(outp);
2257       outsp = dynamic_cast<OutputStudyPort*>(outp);
2258       dnode = dynamic_cast<DataNode*>(node);
2259       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(outp));
2260       soutp = GuiContext::getCurrent()->_mapOfSubjectDataPort[outp];
2261     }
2262   catch (Exception& ex)
2263     {
2264       DEBTRACE("CommandSetOutPortValue::localExecute() : " << ex.what());
2265       setErrorMsg(ex);
2266       return false;
2267     }
2268
2269   if (!outpp && !outsp)
2270     {
2271       DEBTRACE("Set value on output port only possible on a presetPort or a studyPort");
2272       GuiContext::getCurrent()->_lastErrorMessage = "Set value on output port only possible on a presetPort or a studyPort";
2273       return false;
2274     }
2275
2276   if (!dnode)
2277     {
2278       DEBTRACE("Set value on output port only possible on a dataNode");
2279       GuiContext::getCurrent()->_lastErrorMessage = "Set value on output port only possible on a dataNode";
2280       return false;
2281     }
2282
2283   if(outsp)
2284     {
2285       //It's a study port
2286       _oldValue = outsp->getAsString();
2287       DEBTRACE("old value="<< _oldValue);
2288       dnode->setData(outsp, _value );
2289       soutp->update(SETVALUE, 0, soutp);
2290      return true;
2291     }
2292
2293   PyObject *result;
2294   try
2295     {
2296       _oldValue = outpp->getAsString();
2297       if (_oldValue == "None") _oldValue = "";
2298       DEBTRACE("old value="<< _oldValue);
2299       std::string strval;
2300       if (outpp->edGetType()->kind() == YACS::ENGINE::String || outpp->edGetType()->isA(Runtime::_tc_file))
2301         strval = "\"" + _value + "\"";
2302       else
2303         strval = _value;
2304       result = YACS::ENGINE::getSALOMERuntime()->convertStringToPyObject(strval.c_str());
2305     }
2306   catch (Exception& ex)
2307     {
2308       DEBTRACE("CommandSetOutPortValue::localExecute() : " << ex.what());
2309       setErrorMsg(ex);
2310       return false;
2311     }
2312
2313   string val;
2314   PyGILState_STATE gstate = PyGILState_Ensure();
2315   try
2316     {
2317       DEBTRACE(PyObject_Str(result));
2318       val = convertPyObjectXml(outpp->edGetType(), result);
2319       DEBTRACE(val);
2320       dnode->setData(outpp, val );
2321       soutp->update(SETVALUE, 0, soutp);
2322     }
2323   catch (Exception& ex)
2324     {
2325       DEBTRACE("CommandSetOutPortValue::localExecute() : " << ex.what());
2326       setErrorMsg(ex);
2327       Py_DECREF(result);
2328       PyGILState_Release(gstate);
2329       return false;
2330     }
2331
2332   Py_DECREF(result);
2333   PyGILState_Release(gstate);
2334   return true;
2335 }
2336
2337 bool CommandSetOutPortValue::localReverse()
2338 {
2339   DEBTRACE("CommandSetOutPortValue::localReverse");
2340   OutputPresetPort *outpp = 0;
2341   OutputStudyPort *outsp = 0;
2342   DataNode *dnode = 0;
2343   SubjectDataPort *soutp = 0;
2344   try
2345     {
2346       Proc* proc = GuiContext::getCurrent()->getProc();
2347       Node* node = proc->getChildByName(_node);
2348       OutputPort* outp = node->getOutputPort(_port);
2349       outpp = dynamic_cast<OutputPresetPort*>(outp);
2350       outsp = dynamic_cast<OutputStudyPort*>(outp);
2351       dnode = dynamic_cast<DataNode*>(node);
2352       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(outp));
2353       soutp = GuiContext::getCurrent()->_mapOfSubjectDataPort[outp];
2354     }
2355   catch (Exception& ex)
2356     {
2357       DEBTRACE("CommandSetOutPortValue::localExecute() : " << ex.what());
2358       setErrorMsg(ex);
2359       return false;
2360     }
2361
2362   if (!outpp && !outsp)
2363     {
2364       DEBTRACE("Set value on output port only possible on a presetPort or a studyPort");
2365       GuiContext::getCurrent()->_lastErrorMessage = "Set value on output port only possible on a presetPort or a studyPort";
2366       return false;
2367     }
2368
2369   if (!dnode)
2370     {
2371       DEBTRACE("Set value on output port only possible on a dataNode");
2372       GuiContext::getCurrent()->_lastErrorMessage = "Set value on output port only possible on a dataNode";
2373       return false;
2374     }
2375
2376   if(outsp)
2377     {
2378       //It's a study port
2379       _value = outsp->getAsString();
2380       DEBTRACE("value="<< _value);
2381       DEBTRACE("old value="<< _oldValue);
2382       dnode->setData(outsp, _oldValue );
2383       soutp->update(SETVALUE, 0, soutp);
2384       return true;
2385     }
2386
2387   PyObject *result = Py_None;
2388   try
2389     {
2390       _value = outpp->getAsString();
2391       DEBTRACE("value="<< _value);
2392       DEBTRACE("old value="<< _oldValue);
2393       if (!_oldValue.empty())
2394         {
2395           std::string strval;
2396           if (outpp->edGetType()->kind() == YACS::ENGINE::String || outpp->edGetType()->isA(Runtime::_tc_file))
2397             strval = "\"" + _value + "\"";
2398           else
2399             strval = _value;
2400           result = YACS::ENGINE::getSALOMERuntime()->convertStringToPyObject(strval.c_str());
2401         }
2402     }
2403   catch (Exception& ex)
2404     {
2405       DEBTRACE("CommandSetOutPortValue::localExecute() : " << ex.what());
2406       setErrorMsg(ex);
2407       return false;
2408     }
2409
2410   string val;
2411   PyGILState_STATE gstate = PyGILState_Ensure();
2412   try
2413     {
2414       DEBTRACE(PyObject_Str(result));
2415       val = convertPyObjectXml(outpp->edGetType(), result);
2416       DEBTRACE(val);
2417       dnode->setData(outpp, val );
2418       soutp->update(SETVALUE, 0, soutp);
2419     }
2420   catch (Exception& ex)
2421     {
2422       DEBTRACE("CommandSetOutPortValue::localExecute() : " << ex.what());
2423       setErrorMsg(ex);
2424       Py_DECREF(result);
2425       PyGILState_Release(gstate);
2426       return false;
2427     }
2428
2429   Py_DECREF(result);
2430   PyGILState_Release(gstate);
2431   return true;
2432 }
2433  
2434 // ----------------------------------------------------------------------------
2435
2436 CommandSetSwitchSelect::CommandSetSwitchSelect(std::string aSwitch,
2437                                                std::string value)
2438   : Command(), _switch(aSwitch), _value(value)
2439 {
2440   DEBTRACE("CommandSetSwitchSelect::CommandSetSwitchSelect");
2441   _oldValue = "0";
2442 }
2443
2444 std::string CommandSetSwitchSelect::dump()
2445 {
2446   string ret ="CommandSetSwitchSelect " + _switch + " " + _value;
2447   return ret;
2448 }
2449     
2450 bool CommandSetSwitchSelect::localExecute()
2451 {
2452   DEBTRACE("CommandSetSwitchSelect::localExecute");
2453   try
2454     {
2455       Proc* proc = GuiContext::getCurrent()->getProc();
2456       Switch* aSwitch = dynamic_cast<Switch*>(proc->getChildByName(_switch));
2457       InputPort *condPort = aSwitch->edGetConditionPort();
2458       _oldValue = condPort->getAsString();
2459       if (_oldValue == "None") _oldValue = "0";
2460       int val = atoi(_value.c_str());
2461       condPort->edInit(val);
2462       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(aSwitch));
2463       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[aSwitch];
2464       snode->update(SETSELECT, 0, snode);
2465     }
2466   catch (Exception& ex)
2467     {
2468       DEBTRACE("CommandSetSwitchSelect::localExecute() : " << ex.what());
2469       setErrorMsg(ex);
2470       return false;
2471     }
2472 }
2473
2474 bool CommandSetSwitchSelect::localReverse()
2475 {
2476   DEBTRACE("CommandSetSwitchSelect::localReverse");
2477   try
2478     {
2479       Proc* proc = GuiContext::getCurrent()->getProc();
2480       Switch* aSwitch = dynamic_cast<Switch*>(proc->getChildByName(_switch));
2481       InputPort *condPort = aSwitch->edGetConditionPort();
2482       int val = atoi(_oldValue.c_str());
2483       condPort->edInit(val);
2484       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(aSwitch));
2485       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[aSwitch];
2486       snode->update(SETSELECT, 0, snode);
2487     }
2488   catch (Exception& ex)
2489     {
2490       DEBTRACE("CommandSetSwitchSelect::localReverse() : " << ex.what());
2491       setErrorMsg(ex);
2492       return false;
2493     }
2494 }
2495
2496 // ----------------------------------------------------------------------------
2497
2498 CommandSetSwitchCase::CommandSetSwitchCase(std::string aSwitch,
2499                                            std::string node,
2500                                            std::string value)
2501   : Command(), _switch(aSwitch), _node(node), _value(value)
2502 {
2503   DEBTRACE("CommandSetSwitchCase::CommandSetSwitchCase");
2504   _oldValue = 0;
2505   _oldNode = "";
2506 }
2507
2508 std::string CommandSetSwitchCase::dump()
2509 {
2510   string ret ="CommandSetSwitchCase " + _switch + " " + _node + " " + _value;
2511   return ret;
2512 }
2513
2514 bool CommandSetSwitchCase::localExecute()
2515 {
2516   DEBTRACE("CommandSetSwitchCase::localExecute");
2517   try
2518     {
2519       DEBTRACE("CommandSetSwitchCase::localExecute");
2520       Proc* proc = GuiContext::getCurrent()->getProc();
2521       Switch* aSwitch = dynamic_cast<Switch*>(proc->getChildByName(_switch));
2522       Node* node = proc->getChildByName(_node);
2523       int val = atoi(_value.c_str());
2524       if (aSwitch->edGetNode(val))
2525         {
2526           throw YACS::Exception("Set Switch Case impossible: value already used");
2527         }
2528       int oldVal = aSwitch->getRankOfNode(node);
2529       aSwitch->edChangeCase(oldVal,val);
2530       _oldValue = oldVal;
2531       _oldNode = proc->getChildName(node);
2532       DEBTRACE("CommandSetSwitchCase::localExecute OK " << val);
2533       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(aSwitch));
2534       SubjectNode *ssw = GuiContext::getCurrent()->_mapOfSubjectNode[aSwitch];
2535       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
2536       ssw->update(SETCASE, val, snode);
2537       snode->recursiveUpdate(RENAME, 0, snode);
2538       return true;
2539     }
2540   catch (Exception& ex)
2541     {
2542       DEBTRACE("CommandSetSwitchCase::localExecute() : " << ex.what());
2543       setErrorMsg(ex);
2544       return false;
2545     }
2546 }
2547
2548 bool CommandSetSwitchCase::localReverse()
2549 {
2550   DEBTRACE("CommandSetSwitchCase::localReverse");
2551   try
2552     {
2553       DEBTRACE("CommandSetSwitchCase::localReverse");
2554       Proc* proc = GuiContext::getCurrent()->getProc();
2555       Switch* aSwitch = dynamic_cast<Switch*>(proc->getChildByName(_switch));
2556       Node* node = proc->getChildByName(_oldNode);
2557       int val = _oldValue;
2558       if (aSwitch->edGetNode(val))
2559         {
2560           throw YACS::Exception("Set Switch Case impossible: value already used");
2561         }
2562       int oldVal = aSwitch->getRankOfNode(node);
2563       aSwitch->edChangeCase(oldVal,val);
2564       DEBTRACE("CommandSetSwitchCase::localReverse OK " << val);
2565       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(aSwitch));
2566       SubjectNode *ssw = GuiContext::getCurrent()->_mapOfSubjectNode[aSwitch];
2567       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
2568       ssw->update(SETCASE, val, snode);
2569       snode->recursiveUpdate(RENAME, 0, snode);
2570       return true;
2571     }
2572   catch (Exception& ex)
2573     {
2574       DEBTRACE("CommandSetSwitchCase::localReverse() : " << ex.what());
2575       setErrorMsg(ex);
2576       return false;
2577     }
2578 }
2579
2580 // ----------------------------------------------------------------------------
2581
2582 CommandSetForLoopSteps::CommandSetForLoopSteps(std::string forLoop,
2583                                                std::string value)
2584   : Command(), _forLoop(forLoop), _value(value)
2585 {
2586   DEBTRACE("CommandSetForLoopSteps::CommandSetForLoopSteps");
2587   _oldValue = 0;
2588 }
2589
2590
2591 std::string CommandSetForLoopSteps::dump()
2592 {
2593   string ret ="CommandSetForLoopSteps " + _forLoop + " " + _value;
2594   return ret;
2595 }
2596     
2597 bool CommandSetForLoopSteps::localExecute()
2598 {
2599   DEBTRACE("CommandSetForLoopSteps::localExecute");
2600   try
2601     {
2602       DEBTRACE("CommandSetForLoopSteps::localExecute");
2603       Proc* proc = GuiContext::getCurrent()->getProc();
2604       ForLoop* forLoop = dynamic_cast<ForLoop*>(proc->getChildByName(_forLoop));
2605       InputPort *nbSteps = forLoop->edGetNbOfTimesInputPort();
2606       _oldValue = atoi(forLoop->edGetNbOfTimesInputPort()->getAsString().c_str());
2607       int val = atoi(_value.c_str());
2608       nbSteps->edInit(val);
2609       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(forLoop));
2610       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[forLoop];
2611       snode->update(SETVALUE, 0, snode);
2612       return true;
2613     }
2614   catch (Exception& ex)
2615     {
2616       DEBTRACE("CommandSetForLoopSteps::localExecute() : " << ex.what());
2617       setErrorMsg(ex);
2618       return false;
2619     }
2620 }
2621
2622 bool CommandSetForLoopSteps::localReverse()
2623 {
2624   DEBTRACE("CommandSetForLoopSteps::localReverse");
2625   try
2626     {
2627       DEBTRACE("CommandSetForLoopSteps::localReverse");
2628       Proc* proc = GuiContext::getCurrent()->getProc();
2629       ForLoop* forLoop = dynamic_cast<ForLoop*>(proc->getChildByName(_forLoop));
2630       InputPort *nbSteps = forLoop->edGetNbOfTimesInputPort();
2631       nbSteps->edInit(_oldValue);
2632       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(forLoop));
2633       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[forLoop];
2634       snode->update(SETVALUE, 0, snode);
2635       return true;
2636     }
2637   catch (Exception& ex)
2638     {
2639       DEBTRACE("CommandSetForLoopSteps::localReverse() : " << ex.what());
2640       setErrorMsg(ex);
2641       return false;
2642     }
2643 }
2644
2645 // ----------------------------------------------------------------------------
2646
2647 CommandSetWhileCondition::CommandSetWhileCondition(std::string whileLoop,
2648                                                    std::string value)
2649   : Command(), _whileLoop(whileLoop), _value(value)
2650 {
2651   DEBTRACE("CommandSetWhileCondition::CommandSetWhileCondition");
2652   _oldValue = 0;
2653 }
2654
2655 std::string CommandSetWhileCondition::dump()
2656 {
2657   string ret ="CommandSetWhileCondition " + _whileLoop + " " + _value;
2658   return ret;
2659 }
2660
2661 bool CommandSetWhileCondition::localExecute()
2662 {
2663   DEBTRACE("CommandSetWhileCondition::localExecute");
2664   try
2665     {
2666       Proc* proc = GuiContext::getCurrent()->getProc();
2667       WhileLoop* whileLoop = dynamic_cast<WhileLoop*>(proc->getChildByName(_whileLoop));
2668       InputPort *cond = whileLoop->edGetConditionPort();
2669       _oldValue = atoi(whileLoop->edGetConditionPort()->getAsString().c_str());
2670       bool val = atoi(_value.c_str());
2671       cond->edInit(val);
2672       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(whileLoop));
2673       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[whileLoop];
2674       snode->update(SETVALUE, 0, snode);
2675       return true;
2676     }
2677   catch (Exception& ex)
2678     {
2679       DEBTRACE("CommandSetWhileCondition::localExecute() : " << ex.what());
2680       setErrorMsg(ex);
2681       return false;
2682     }
2683 }
2684
2685 bool CommandSetWhileCondition::localReverse()
2686 {
2687   DEBTRACE("CommandSetWhileCondition::localReverse");
2688   try
2689     {
2690       Proc* proc = GuiContext::getCurrent()->getProc();
2691       WhileLoop* whileLoop = dynamic_cast<WhileLoop*>(proc->getChildByName(_whileLoop));
2692       InputPort *cond = whileLoop->edGetConditionPort();
2693       cond->edInit(_oldValue);
2694       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(whileLoop));
2695       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[whileLoop];
2696       snode->update(SETVALUE, 0, snode);
2697       return true;
2698     }
2699   catch (Exception& ex)
2700     {
2701       DEBTRACE("CommandSetWhileCondition::localReverse() : " << ex.what());
2702       setErrorMsg(ex);
2703       return false;
2704     }
2705 }
2706
2707 // ----------------------------------------------------------------------------
2708
2709 CommandSetForEachBranch::CommandSetForEachBranch(std::string forEach,
2710                                                  std::string value)
2711   : Command(), _forEach(forEach), _value(value)
2712 {
2713   DEBTRACE("CommandSetForEachBranch::CommandSetForEachBranch");
2714   _oldValue = 0;
2715 }
2716  
2717 std::string CommandSetForEachBranch::dump()
2718 {
2719   string ret ="CommandSetForEachBranch " + _forEach + " " + _value;
2720   return ret;
2721 }
2722    
2723 bool CommandSetForEachBranch::localExecute()
2724 {
2725   DEBTRACE("CommandSetForEachBranch::localExecute");
2726   try
2727     {
2728       Proc* proc = GuiContext::getCurrent()->getProc();
2729       Node* node=proc->getChildByName(_forEach);
2730       InputPort *nbBranches = node->getInputPort("nbBranches");
2731       _oldValue = atoi(nbBranches->getAsString().c_str());
2732       int val = atoi(_value.c_str());
2733       nbBranches->edInit(val);
2734       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(nbBranches));
2735       SubjectDataPort *spo = GuiContext::getCurrent()->_mapOfSubjectDataPort[static_cast<DataPort*>(nbBranches)];
2736       spo->update(SETVALUE, 0, spo);
2737       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
2738       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
2739       snode->update(SETVALUE, 0, snode);
2740       return true;
2741     }
2742   catch (Exception& ex)
2743     {
2744       DEBTRACE("CommandSetForEachBranch::localExecute() : " << ex.what());
2745       setErrorMsg(ex);
2746       return false;
2747     }
2748 }
2749
2750 bool CommandSetForEachBranch::localReverse()
2751 {
2752   DEBTRACE("CommandSetForEachBranch::localReverse");
2753   try
2754     {
2755       Proc* proc = GuiContext::getCurrent()->getProc();
2756       Node* node=proc->getChildByName(_forEach);
2757       InputPort *nbBranches = node->getInputPort("nbBranches");
2758       nbBranches->edInit(_oldValue);
2759       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(nbBranches));
2760       SubjectDataPort *spo = GuiContext::getCurrent()->_mapOfSubjectDataPort[static_cast<DataPort*>(nbBranches)];
2761       spo->update(SETVALUE, 0, spo);
2762       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
2763       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
2764       snode->update(SETVALUE, 0, snode);
2765       return true;
2766     }
2767   catch (Exception& ex)
2768     {
2769       DEBTRACE("CommandSetForEachBranch::localReverse() : " << ex.what());
2770       setErrorMsg(ex);
2771       return false;
2772     }
2773 }
2774
2775 // ----------------------------------------------------------------------------
2776
2777 CommandSetAlgo::CommandSetAlgo(std::string optimizer, std::string alglib, std::string symbol)
2778   : Command(), _optimizer(optimizer), _alglib(alglib), _symbol(symbol)
2779 {
2780   DEBTRACE("CommandSetAlgo::CommandSetAlgo" << _optimizer << " " << _alglib << " " << _symbol);
2781   _oldAlglib = "";
2782   _oldSymbol = "";
2783 }
2784
2785 std::string CommandSetAlgo::dump()
2786 {
2787   string ret ="CommandSetAlgo " + _optimizer + " " + _alglib + " " + _symbol;
2788   return ret;
2789 }
2790
2791 bool CommandSetAlgo::localExecute()
2792 {
2793   DEBTRACE("CommandSetAlgo::localExecute");
2794   try
2795     {
2796       Proc* proc = GuiContext::getCurrent()->getProc();
2797       OptimizerLoop* loop = dynamic_cast<OptimizerLoop*>(proc->getChildByName(_optimizer));
2798       loop->setAlgorithm(_alglib,_symbol);
2799       _oldAlglib = _alglib;
2800       _oldSymbol = _symbol;
2801       InputPort *port = loop->edGetPortForOutPool();
2802       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(static_cast<DataPort*>(port)));
2803       SubjectDataPort *spo = GuiContext::getCurrent()->_mapOfSubjectDataPort[static_cast<DataPort*>(port)];
2804       spo->update(UPDATE, 0, spo);
2805       OutputPort *oport = loop->edGetSamplePort();
2806       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(static_cast<DataPort*>(oport)));
2807       spo = GuiContext::getCurrent()->_mapOfSubjectDataPort[static_cast<DataPort*>(oport)];
2808       spo->update(UPDATE, 0, spo);
2809       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(loop));
2810       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[loop];
2811       snode->update(SETVALUE, 0, snode);
2812       return true;
2813     }
2814   catch (Exception& ex)
2815     {
2816       DEBTRACE("CommandSetAlgo::localExecute() : " << ex.what());
2817       setErrorMsg(ex);
2818       return false;
2819     }
2820 }
2821
2822 bool CommandSetAlgo::localReverse()
2823 {
2824   DEBTRACE("CommandSetAlgo::localReverse");
2825   try
2826     {
2827       Proc* proc = GuiContext::getCurrent()->getProc();
2828       OptimizerLoop* loop = dynamic_cast<OptimizerLoop*>(proc->getChildByName(_optimizer));
2829       loop->setAlgorithm(_oldAlglib,_oldSymbol);
2830       InputPort *port = loop->edGetPortForOutPool();
2831       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(static_cast<DataPort*>(port)));
2832       SubjectDataPort *spo = GuiContext::getCurrent()->_mapOfSubjectDataPort[static_cast<DataPort*>(port)];
2833       spo->update(UPDATE, 0, spo);
2834       OutputPort *oport = loop->edGetSamplePort();
2835       YASSERT(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(static_cast<DataPort*>(oport)));
2836       spo = GuiContext::getCurrent()->_mapOfSubjectDataPort[static_cast<DataPort*>(oport)];
2837       spo->update(UPDATE, 0, spo);
2838       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(loop));
2839       SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[loop];
2840       snode->update(SETVALUE, 0, snode);
2841       return true;
2842     }
2843   catch (Exception& ex)
2844     {
2845       DEBTRACE("CommandSetAlgo::localReverse() : " << ex.what());
2846       setErrorMsg(ex);
2847       return false;
2848     }
2849   return true;
2850 }
2851
2852 // ----------------------------------------------------------------------------
2853
2854 CommandAddLink::CommandAddLink(std::string outNode, std::string outPort, TypeOfElem outPortType,
2855                                std::string inNode, std::string inPort, TypeOfElem inPortType, bool control)
2856   : Command(), _outNode(outNode), _outPort(outPort), _outPortType(outPortType),
2857                _inNode(inNode), _inPort(inPort), _inPortType(inPortType), _control(control)
2858 {
2859   DEBTRACE("CommandAddLink::CommandAddLink "<<outNode<<"."<<outPort<<"->"<<inNode<<"."<<inPort<<" "<<control);
2860   _controlCreatedWithDF = false;
2861 }
2862
2863 std::string CommandAddLink::dump()
2864 {
2865   string s = "false";
2866   if (_control) s = "true";
2867   string ret ="CommandAddLink " + _outNode + " " + _outPort + " " + _inNode + " " + _inPort + " " + s;
2868   return ret;
2869 }
2870
2871 bool CommandAddLink::localExecute()
2872 {
2873   DEBTRACE("CommandAddLink::localExecute");
2874   DEBTRACE(_outNode<<"."<<_outPort<<"->"<<_inNode<<"."<<_inPort<<" "<<_control);
2875   try
2876     {
2877       Proc* proc = GuiContext::getCurrent()->getProc();
2878       Node* outn = proc->getChildByName(_outNode);
2879       Node* inn = proc->getChildByName(_inNode);
2880       OutPort* outp;
2881       InPort* inp;
2882
2883       // --- is a control link already existing ?
2884       bool preexistingControl = false;
2885       {
2886         Node* outn2=outn;
2887         Node* inn2=inn;
2888         ComposedNode* father = ComposedNode::getLowestCommonAncestor(outn2,inn2);
2889         if(outn2==father || inn2==father)
2890           preexistingControl = true;
2891         else
2892           {
2893             while(outn2->getFather() != father)
2894               outn2 = outn2->getFather();
2895             while(inn2->getFather() != father)
2896               inn2 = inn2->getFather();
2897             OutGate *ogate = outn2->getOutGate();
2898             InGate *igate = inn2->getInGate();
2899             if (ogate->isAlreadyInSet(igate))
2900               preexistingControl = true;
2901           }
2902       }
2903
2904       if(_outPortType == OUTPUTPORT)
2905         outp = outn->getOutputPort(_outPort);
2906       else
2907         outp = outn->getOutputDataStreamPort(_outPort);
2908
2909       if(_inPortType == INPUTPORT)
2910         inp = inn->getInputPort(_inPort);
2911       else
2912         inp = inn->getInputDataStreamPort(_inPort);
2913
2914       ComposedNode *cla = ComposedNode::getLowestCommonAncestor(outn->getFather(),inn->getFather());
2915       DEBTRACE(cla->getName());
2916       if (dynamic_cast<OutputDataStreamPort*>(outp))
2917         cla->edAddLink(outp,inp);
2918       else if(_control)
2919         cla->edAddDFLink(outp,inp);
2920       else
2921         cla->edAddLink(outp,inp);
2922
2923       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(cla));
2924       SubjectNode *sub = GuiContext::getCurrent()->_mapOfSubjectNode[cla];
2925       SubjectComposedNode *scla = dynamic_cast<SubjectComposedNode*>(sub);
2926       DEBTRACE(scla->getName());
2927       SubjectNode *sno = GuiContext::getCurrent()->_mapOfSubjectNode[outn];
2928       SubjectNode *sni = GuiContext::getCurrent()->_mapOfSubjectNode[inn];
2929       SubjectDataPort *subOutport = GuiContext::getCurrent()->_mapOfSubjectDataPort[outp];
2930       SubjectDataPort *subInport = GuiContext::getCurrent()->_mapOfSubjectDataPort[inp];
2931       SubjectLink *slink = scla->addSubjectLink(sno, subOutport, sni, subInport);
2932
2933       // --- if control link, identify the nodes linked and draw the control link if not already existing
2934
2935       if (_control)
2936         {
2937           ComposedNode* father = ComposedNode::getLowestCommonAncestor(outn,inn);
2938           if(outn==father || inn==father) return true;
2939           while(outn->getFather() != father)
2940             outn = outn->getFather();
2941           while(inn->getFather() != father)
2942             inn = inn->getFather();
2943           OutGate *ogate = outn->getOutGate();
2944           InGate *igate = inn->getInGate();
2945           if (ogate->isAlreadyInSet(igate))
2946             {
2947               if (!preexistingControl)
2948                 _controlCreatedWithDF = true;
2949               pair<Node*,Node*> keyLink(outn,inn);
2950               if (!GuiContext::getCurrent()->_mapOfSubjectControlLink.count(keyLink))
2951                 {
2952                   SubjectNode *sfno = GuiContext::getCurrent()->_mapOfSubjectNode[outn];
2953                   SubjectNode *sfni = GuiContext::getCurrent()->_mapOfSubjectNode[inn];
2954                   if (!sfno || !sfni) return true;
2955                   SubjectControlLink *sclink = scla->addSubjectControlLink(sfno, sfni);
2956                 }
2957             }
2958         }
2959       return true;
2960     }
2961   catch (Exception& ex)
2962     {
2963       DEBTRACE("CommandAddLink::localExecute() : " << ex.what());
2964       setErrorMsg(ex);
2965       return false;
2966     }
2967 }
2968
2969 bool CommandAddLink::localReverse()
2970 {
2971   DEBTRACE("CommandAddLink::localReverse");
2972   try
2973     {
2974       SubjectLink *slink =0;
2975       SubjectControlLink *sclink = 0;
2976       Proc* proc = GuiContext::getCurrent()->getProc();
2977       Node* outn = proc->getChildByName(_outNode);
2978       Node* inn = proc->getChildByName(_inNode);
2979       OutPort* outp;
2980       InPort* inp;
2981       if(_outPortType == OUTPUTPORT)
2982         outp = outn->getOutputPort(_outPort);
2983       else
2984         outp = outn->getOutputDataStreamPort(_outPort);
2985       if(_inPortType == INPUTPORT)
2986         inp = inn->getInputPort(_inPort);
2987       else
2988         inp = inn->getInputDataStreamPort(_inPort);
2989       YASSERT(GuiContext::getCurrent()->_mapOfSubjectLink.count(pair<OutPort*,InPort*>(outp,inp)));
2990       slink = GuiContext::getCurrent()->_mapOfSubjectLink[pair<OutPort*,InPort*>(outp,inp)];
2991       if (_controlCreatedWithDF)
2992         {
2993           YASSERT(GuiContext::getCurrent()->_mapOfSubjectControlLink.count(pair<Node*,Node*>(outn,inn)));
2994           sclink = GuiContext::getCurrent()->_mapOfSubjectControlLink[pair<Node*,Node*>(outn,inn)];         
2995         }
2996
2997       Subject *father = slink->getParent();
2998       Subject::erase(slink);
2999       slink = 0;
3000       if (father)
3001         {
3002           DEBTRACE("REMOVE");
3003           father->select(true);
3004           father->update(REMOVE,0,0);
3005         }
3006       if (!sclink)
3007         return true;
3008       father=sclink->getParent();
3009       Subject::erase(sclink);
3010       sclink = 0;
3011       if (father)
3012         {
3013           DEBTRACE("REMOVE");
3014           father->select(true);
3015           father->update(REMOVE,0,0);
3016         }
3017       return true;
3018     }
3019   catch (Exception& ex)
3020     {
3021       DEBTRACE("CommandAddLink::localReverse(): " << ex.what());
3022       setErrorMsg(ex);
3023       return false;
3024     }
3025 }
3026
3027 // ----------------------------------------------------------------------------
3028
3029 CommandAddControlLink::CommandAddControlLink(std::string outNode, std::string inNode)
3030   : Command(), _outNode(outNode), _inNode(inNode)
3031 {
3032   DEBTRACE("CommandAddControlLink::CommandAddControlLink "<<outNode<<"-->>"<<inNode);
3033 }
3034
3035 std::string CommandAddControlLink::dump()
3036 {
3037   string ret ="CommandAddControlLink " + _outNode + " " + _inNode;
3038   return ret;
3039 }
3040
3041 bool CommandAddControlLink::localExecute()
3042 {
3043   DEBTRACE("CommandAddControlLink::localExecute");
3044   try
3045     {
3046       Proc* proc = GuiContext::getCurrent()->getProc();
3047       Node* outn = proc;
3048       if (! _outNode.empty())
3049         outn = proc->getChildByName(_outNode);
3050       Node* inn = proc;
3051       if (! _inNode.empty())
3052         inn = proc->getChildByName(_inNode);
3053       ComposedNode *cla = ComposedNode::getLowestCommonAncestor(outn,inn);
3054       DEBTRACE(cla->getName());
3055       bool ret= cla->edAddCFLink(outn,inn);
3056       if(ret==false)
3057         GuiContext::getCurrent()->_lastErrorMessage = "Link already exists";
3058
3059       cla = ComposedNode::getLowestCommonAncestor(outn->getFather(),
3060                                                   inn->getFather());
3061       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(cla));
3062       SubjectNode *sub = GuiContext::getCurrent()->_mapOfSubjectNode[cla];
3063       SubjectComposedNode *scla = dynamic_cast<SubjectComposedNode*>(sub);
3064       DEBTRACE(scla->getName());
3065       SubjectNode *sno = GuiContext::getCurrent()->_mapOfSubjectNode[outn];
3066       SubjectNode *sni = GuiContext::getCurrent()->_mapOfSubjectNode[inn];
3067       SubjectControlLink *sclink = scla->addSubjectControlLink(sno,sni);
3068       return ret;
3069     }
3070   catch (Exception& ex)
3071     {
3072       DEBTRACE("CommandAddControlLink::localExecute() : " << ex.what());
3073       setErrorMsg(ex);
3074       return false;
3075     }
3076 }
3077
3078 bool CommandAddControlLink::localReverse()
3079 {
3080   DEBTRACE("CommandAddControlLink::localReverse");
3081   try
3082     {
3083       SubjectControlLink *sclink = 0;
3084       Proc* proc = GuiContext::getCurrent()->getProc();
3085       Node* outn = proc->getChildByName(_outNode);
3086       Node* inn = proc->getChildByName(_inNode);
3087       YASSERT(GuiContext::getCurrent()->_mapOfSubjectControlLink.count(pair<Node*,Node*>(outn,inn)));
3088       sclink = GuiContext::getCurrent()->_mapOfSubjectControlLink[pair<Node*,Node*>(outn,inn)];         
3089
3090       Subject *father=sclink->getParent();
3091       Subject::erase(sclink);
3092       sclink = 0;
3093       if (father)
3094         {
3095           DEBTRACE("REMOVE");
3096           father->select(true);
3097           father->update(REMOVE,0,0);
3098         }
3099       return true;
3100     }
3101   catch (Exception& ex)
3102     {
3103       DEBTRACE("CommandAddControlLink::localReverse(): " << ex.what());
3104       setErrorMsg(ex);
3105       return false;
3106     }
3107 }
3108
3109 // ----------------------------------------------------------------------------
3110
3111 CommandAddContainer::CommandAddContainer(std::string name,
3112                                          std::string refContainer)
3113   : Command(), _name(name), _containerToClone(refContainer), _subcont(0)
3114 {
3115   DEBTRACE("CommandAddContainer::CommandAddContainer " << name << " " << refContainer);
3116 }
3117
3118 std::string CommandAddContainer::dump()
3119 {
3120   string ret ="CommandAddContainer " + _name + " " + _containerToClone;
3121   return ret;
3122 }
3123
3124 bool CommandAddContainer::localExecute()
3125 {
3126   DEBTRACE("CommandAddContainer::localExecute");
3127   try
3128     {
3129       Proc* proc = GuiContext::getCurrent()->getProc();
3130       if (proc->containerMap.count(_name))
3131         {
3132           GuiContext::getCurrent()->_lastErrorMessage = "There is already a container with that name";
3133           return false;
3134         }
3135       Container *container = new SalomeContainer();
3136       if (! _containerToClone.empty())
3137         {
3138           if (proc->containerMap.count(_containerToClone))
3139             {
3140               Container *ref = proc->containerMap[_containerToClone];
3141               YASSERT(ref);
3142               container->setProperties(ref->getProperties());
3143             }
3144           else
3145             {
3146               GuiContext::getCurrent()->_lastErrorMessage = "There is no reference container to clone properties";
3147               return false;
3148             }
3149         }
3150       container->setName(_name);
3151       container->setProc(proc);
3152       proc->containerMap[_name] = container;
3153
3154       SubjectProc* sproc = GuiContext::getCurrent()->getSubjectProc();
3155       _subcont = sproc->addSubjectContainer(container, _name);
3156       return true;
3157     }
3158   catch (Exception& ex)
3159     {
3160       DEBTRACE("CommandAddContainer::localExecute() : " << ex.what());
3161       setErrorMsg(ex);
3162       return false;
3163     }
3164 }
3165
3166 bool CommandAddContainer::localReverse()
3167 {
3168   DEBTRACE("CommandAddContainer::localReverse");
3169   try
3170     {
3171       Proc* proc = GuiContext::getCurrent()->getProc();
3172       YASSERT(proc->containerMap.count(_name));
3173       Container *container = proc->containerMap[_name];
3174       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(container));
3175       _subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[container];
3176       YASSERT(!_subcont->isUsed());
3177       SubjectProc* sproc = GuiContext::getCurrent()->getSubjectProc();
3178       sproc->removeSubjectContainer(_subcont);
3179       return true;
3180     }
3181   catch (Exception& ex)
3182     {
3183       DEBTRACE("CommandAddContainer::localExecute() : " << ex.what());
3184       setErrorMsg(ex);
3185       return false;
3186     }
3187 }
3188
3189 // ----------------------------------------------------------------------------
3190
3191 CommandSetNodeProperties::CommandSetNodeProperties(std::string position, std::map<std::string,std::string> properties)
3192   : Command(), _position(position), _properties(properties)
3193 {
3194   DEBTRACE("CommandSetNodeProperties::CommandSetNodeProperties " << position);
3195   _oldProp.clear();
3196 }
3197
3198 std::string CommandSetNodeProperties::dump()
3199 {
3200   string ret ="CommandSetNodeProperties " + _position;
3201   return ret;
3202 }
3203
3204 bool CommandSetNodeProperties::localExecute()
3205 {
3206   DEBTRACE("CommandSetNodeProperties::localExecute");
3207   try
3208     {
3209       Proc* proc = GuiContext::getCurrent()->getProc();
3210       Node* node = proc;
3211
3212       if (!_position.empty()) node = proc->getChildByName(_position);
3213
3214       if (node)
3215         {
3216           _oldProp = node->getPropertyMap();
3217           node->setProperties(_properties);
3218           SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
3219           snode->update(SETVALUE, 0, snode);
3220           return true;
3221         }
3222       GuiContext::getCurrent()->_lastErrorMessage = "node not found: " + _position;
3223       return false;
3224     }
3225   catch (Exception& ex)
3226     {
3227       DEBTRACE("CommandSetNodeProperties::localExecute() : " << ex.what());
3228       setErrorMsg(ex);
3229       return false;
3230     }
3231 }
3232
3233 bool CommandSetNodeProperties::localReverse()
3234 {
3235   DEBTRACE("CommandSetNodeProperties::localReverse");
3236   try
3237     {
3238       Proc* proc = GuiContext::getCurrent()->getProc();
3239       Node* node = proc;
3240
3241       if (!_position.empty()) node = proc->getChildByName(_position);
3242
3243       if (node)
3244         {
3245           node->setProperties(_oldProp);
3246           SubjectNode *snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
3247           snode->update(SETVALUE, 0, snode);
3248           return true;
3249         }
3250       GuiContext::getCurrent()->_lastErrorMessage = "node not found: " + _position;
3251       return false;
3252     }
3253   catch (Exception& ex)
3254     {
3255       DEBTRACE("CommandSetNodeProperties::localReverse() : " << ex.what());
3256       setErrorMsg(ex);
3257       return false;
3258     }
3259 }
3260
3261 // ----------------------------------------------------------------------------
3262
3263 CommandSetComponentInstanceProperties::CommandSetComponentInstanceProperties(std::string compoinstance,
3264                                                                              std::map<std::string,std::string> properties)
3265   : Command(), _compoinstance(compoinstance), _properties(properties)
3266 {
3267   DEBTRACE("CommandSetComponentInstanceProperties::CommandSetComponentInstanceProperties " << compoinstance);
3268   _oldProp.clear();
3269 }
3270
3271 std::string CommandSetComponentInstanceProperties::dump()
3272 {
3273   string ret ="CommandSetComponentInstanceProperties " + _compoinstance;
3274   return ret;
3275 }
3276
3277 bool CommandSetComponentInstanceProperties::localExecute()
3278 {
3279   DEBTRACE("CommandSetComponentInstanceProperties::localExecute");
3280   try
3281     {
3282       Proc* proc = GuiContext::getCurrent()->getProc();
3283       if (proc->componentInstanceMap.count(_compoinstance))
3284         {
3285           ComponentInstance *ref = proc->componentInstanceMap[_compoinstance];
3286           YASSERT(ref);
3287           _oldProp = ref->getProperties();
3288           _oldAnon = ref->isAnonymous();
3289           ref->setProperties(_properties);
3290           ref->setAnonymous(false);
3291           SubjectComponent* subcompo = GuiContext::getCurrent()->_mapOfSubjectComponent[ref];
3292           subcompo->update(SETVALUE, 0, subcompo);
3293           return true;
3294         }
3295       GuiContext::getCurrent()->_lastErrorMessage = "compoinstance not found: " + _compoinstance;
3296       return false;
3297     }
3298   catch (Exception& ex)
3299     {
3300       DEBTRACE("CommandSetComponentInstanceProperties::localExecute() : " << ex.what());
3301       setErrorMsg(ex);
3302       return false;
3303     }
3304 }
3305
3306 bool CommandSetComponentInstanceProperties::localReverse()
3307 {
3308   DEBTRACE("CommandSetComponentInstanceProperties::localReverse");
3309   try
3310     {
3311       Proc* proc = GuiContext::getCurrent()->getProc();
3312       if (proc->componentInstanceMap.count(_compoinstance))
3313         {
3314           ComponentInstance *ref = proc->componentInstanceMap[_compoinstance];
3315           YASSERT(ref);
3316           ref->setProperties(_oldProp);
3317           ref->setAnonymous(_oldAnon);
3318           SubjectComponent* subcompo = GuiContext::getCurrent()->_mapOfSubjectComponent[ref];
3319           subcompo->update(SETVALUE, 0, subcompo);
3320           return true;
3321         }
3322       GuiContext::getCurrent()->_lastErrorMessage = "compoinstance not found: " + _compoinstance;
3323       return false;
3324     }
3325   catch (Exception& ex)
3326     {
3327       DEBTRACE("CommandSetComponentInstanceProperties::localReverse() : " << ex.what());
3328       setErrorMsg(ex);
3329       return false;
3330     }
3331 }
3332
3333 // ----------------------------------------------------------------------------
3334
3335 CommandSetContainerProperties::CommandSetContainerProperties(std::string container,
3336                                                              std::map<std::string,std::string> properties)
3337   : Command(), _container(container), _properties(properties)
3338 {
3339   DEBTRACE("CommandSetContainerProperties::CommandSetContainerProperties " << container);
3340   _oldProp.clear();
3341 }
3342
3343 std::string CommandSetContainerProperties::dump()
3344 {
3345   string ret ="CommandSetContainerProperties " + _container;
3346   return ret;
3347 }
3348
3349 bool CommandSetContainerProperties::localExecute()
3350 {
3351   DEBTRACE("CommandSetContainerProperties::localExecute");
3352   try
3353     {
3354       Proc* proc = GuiContext::getCurrent()->getProc();
3355       if (proc->containerMap.count(_container))
3356         {
3357           Container *ref = proc->containerMap[_container];
3358           YASSERT(ref);
3359           _oldProp = ref->getProperties();
3360           ref->setProperties(_properties);
3361           return true;
3362         }
3363       GuiContext::getCurrent()->_lastErrorMessage = "container not found: " + _container;
3364       return false;
3365     }
3366   catch (Exception& ex)
3367     {
3368       DEBTRACE("CommandSetContainerProperties::localExecute() : " << ex.what());
3369       setErrorMsg(ex);
3370       return false;
3371     }
3372 }
3373
3374 bool CommandSetContainerProperties::localReverse()
3375 {
3376   DEBTRACE("CommandSetContainerProperties::localReverse");
3377   try
3378     {
3379       Proc* proc = GuiContext::getCurrent()->getProc();
3380       if (proc->containerMap.count(_container))
3381         {
3382           Container *ref = proc->containerMap[_container];
3383           YASSERT(ref);
3384           ref->setProperties(_oldProp);
3385           return true;
3386         }
3387       GuiContext::getCurrent()->_lastErrorMessage = "container not found: " + _container;
3388       return false;
3389     }
3390   catch (Exception& ex)
3391     {
3392       DEBTRACE("CommandSetContainerProperties::localReverse() : " << ex.what());
3393       setErrorMsg(ex);
3394       return false;
3395     }
3396 }
3397
3398 // ----------------------------------------------------------------------------
3399
3400 CommandSetDSPortProperties::CommandSetDSPortProperties(std::string node, std::string port, bool isInport,
3401                                                        std::map<std::string,std::string> properties)
3402   : Command(), _nodeName(node), _portName(port), _isInport(isInport), _properties(properties)
3403 {
3404   DEBTRACE("CommandSetDSPortProperties::CommandSetDSPortProperties " << node << "." << port << " " << isInport);
3405   _oldProp.clear();
3406 }
3407       
3408 std::string CommandSetDSPortProperties::dump()
3409 {
3410   string s = "false";
3411   if (_isInport) s = "true";
3412   string ret ="CommandSetDSPortProperties " + _nodeName + " " + _portName + " " + s;
3413   return ret;
3414 }
3415
3416 bool CommandSetDSPortProperties::localExecute()
3417 {
3418   DEBTRACE("CommandSetDSPortProperties::localExecute");
3419   try
3420     {
3421       Proc* proc = GuiContext::getCurrent()->getProc();
3422       Node* node = proc->getChildByName(_nodeName);
3423       DataStreamPort* DSPort = 0;
3424       if (_isInport)
3425         DSPort = node->getInputDataStreamPort(_portName);
3426       else
3427         DSPort = node->getOutputDataStreamPort(_portName);
3428       _oldProp = DSPort->getProperties();
3429       DSPort->setProperties(_properties);
3430       return true;
3431     }
3432   catch (Exception& ex)
3433     {
3434       DEBTRACE("CommandSetDSPortProperties::localExecute() : " << ex.what());
3435       setErrorMsg(ex);
3436       return false;
3437     }
3438 }
3439       
3440 bool CommandSetDSPortProperties::localReverse()
3441 {
3442   DEBTRACE("CommandSetDSPortProperties::localReverse");
3443   try
3444     {
3445       Proc* proc = GuiContext::getCurrent()->getProc();
3446       Node* node = proc->getChildByName(_nodeName);
3447       DataStreamPort* DSPort = 0;
3448       if (_isInport)
3449         DSPort = node->getInputDataStreamPort(_portName);
3450       else
3451         DSPort = node->getOutputDataStreamPort(_portName);
3452       DSPort->setProperties(_oldProp);
3453       return true;
3454     }
3455   catch (Exception& ex)
3456     {
3457       DEBTRACE("CommandSetDSPortProperties::localReverse() : " << ex.what());
3458       setErrorMsg(ex);
3459       return false;
3460     }
3461 }
3462
3463 // ----------------------------------------------------------------------------
3464
3465 CommandSetLinkProperties::CommandSetLinkProperties(std::string startnode, std::string startport, 
3466                                                    std::string endnode, std::string endport,
3467                                                    std::map<std::string,std::string> properties)
3468   : Command(), _startNodeName(startnode), _startPortName(startport), 
3469     _endNodeName(endnode), _endPortName(endport),
3470     _properties(properties)
3471 {
3472   DEBTRACE("CommandSetLinkProperties::CommandSetLinkProperties " );
3473   _oldProp.clear();
3474 }
3475
3476 std::string CommandSetLinkProperties::dump()
3477 {
3478   string ret ="CommandSetLinkProperties " + _startNodeName + " " + _startPortName + " " + _endNodeName + " " + _endPortName;
3479   return ret;
3480 }
3481
3482 bool CommandSetLinkProperties::localExecute()
3483 {
3484   DEBTRACE("CommandSetLinkProperties::localExecute");
3485   try
3486     {
3487       Proc* proc = GuiContext::getCurrent()->getProc();
3488       Node* node;
3489       InputDataStreamPort* inDSPort = 0;
3490       OutputDataStreamPort* outDSPort = 0;
3491
3492       node   = proc->getChildByName(_startNodeName);
3493       outDSPort = node->getOutputDataStreamPort(_startPortName);
3494       outDSPort->setProperties(_properties);
3495
3496       node   = proc->getChildByName(_endNodeName);
3497       inDSPort = node->getInputDataStreamPort(_endPortName);
3498       _oldProp = inDSPort->getProperties();
3499       inDSPort->setProperties(_properties);
3500
3501       std::pair<OutPort*,InPort*> keymap = std::pair<OutPort*,InPort*>(outDSPort,inDSPort);
3502       SubjectLink* subject = GuiContext::getCurrent()->_mapOfSubjectLink[keymap];
3503       subject->update(SETVALUE, 0, subject);
3504       return true;
3505     }
3506   catch (Exception& ex)
3507     {
3508       DEBTRACE("CommandSetDSPortProperties::localExecute() : " << ex.what());
3509       setErrorMsg(ex);
3510       return false;
3511     }
3512 }
3513
3514 bool CommandSetLinkProperties::localReverse()
3515 {
3516   DEBTRACE("CommandSetLinkProperties::localReverse");
3517   try
3518     {
3519       Proc* proc = GuiContext::getCurrent()->getProc();
3520       Node* node;
3521       InputDataStreamPort* inDSPort = 0;
3522       OutputDataStreamPort* outDSPort = 0;
3523
3524       node   = proc->getChildByName(_startNodeName);
3525       outDSPort = node->getOutputDataStreamPort(_startPortName);
3526       outDSPort->setProperties(_properties);
3527
3528       node   = proc->getChildByName(_endNodeName);
3529       inDSPort = node->getInputDataStreamPort(_endPortName);
3530       inDSPort->setProperties(_oldProp);
3531
3532       std::pair<OutPort*,InPort*> keymap = std::pair<OutPort*,InPort*>(outDSPort,inDSPort);
3533       SubjectLink* subject = GuiContext::getCurrent()->_mapOfSubjectLink[keymap];
3534       subject->update(SETVALUE, 0, subject);
3535       return true;
3536     }
3537   catch (Exception& ex)
3538     {
3539       DEBTRACE("CommandSetDSPortProperties::localReverse() : " << ex.what());
3540       setErrorMsg(ex);
3541       return false;
3542     }
3543 }
3544
3545 // ----------------------------------------------------------------------------
3546
3547 CommandSetFuncNodeFunctionName::CommandSetFuncNodeFunctionName(std::string node, std::string funcName)
3548   : Command(), _nodeName(node), _funcName(funcName)
3549 {
3550   DEBTRACE("CommandSetFuncNodeFunctionName::CommandSetFuncNodeFunctionName " << node << " " <<funcName);
3551   _oldName.clear();
3552 }
3553
3554 std::string CommandSetFuncNodeFunctionName::dump()
3555 {
3556   string ret ="CommandSetFuncNodeFunctionName " + _nodeName + " " + _funcName;
3557   return ret;
3558 }
3559
3560 bool CommandSetFuncNodeFunctionName::localExecute()
3561 {
3562   DEBTRACE("CommandSetFuncNodeFunctionName::localExecute");
3563   try
3564     {
3565       Proc* proc = GuiContext::getCurrent()->getProc();
3566       Node* node = proc->getChildByName(_nodeName);
3567       if (_funcName.empty())
3568         {
3569           GuiContext::getCurrent()->_lastErrorMessage = "InlineFuncNode function name empty: " + _nodeName;
3570           return false;
3571         }
3572       if (YACS::ENGINE::InlineFuncNode* funcNode = dynamic_cast<YACS::ENGINE::InlineFuncNode*>(node))
3573         {
3574           _oldName = funcNode->getFname();
3575           funcNode->setFname(_funcName);
3576           return true;
3577         }
3578       else
3579         {
3580           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineFuncNode: " + _nodeName;
3581           return false;
3582         }
3583     }
3584   catch (Exception& ex)
3585     {
3586       DEBTRACE("CommandSetFuncNodeFunctionName::localExecute() : " << ex.what());
3587       setErrorMsg(ex);
3588       return false;
3589     }  
3590 }
3591
3592 bool CommandSetFuncNodeFunctionName::localReverse()
3593 {
3594   DEBTRACE("CommandSetFuncNodeFunctionName::localReverse");
3595   try
3596     {
3597       Proc* proc = GuiContext::getCurrent()->getProc();
3598       Node* node = proc->getChildByName(_nodeName);
3599       if (YACS::ENGINE::InlineFuncNode* funcNode = dynamic_cast<YACS::ENGINE::InlineFuncNode*>(node))
3600         {
3601           funcNode->setFname(_oldName);
3602           return true;
3603         }
3604       else
3605         {
3606           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineFuncNode: " + _nodeName;
3607           return false;
3608         }
3609     }
3610   catch (Exception& ex)
3611     {
3612       DEBTRACE("CommandSetFuncNodeFunctionName::localReverse() : " << ex.what());
3613       setErrorMsg(ex);
3614       return false;
3615     }  
3616 }
3617
3618 // ----------------------------------------------------------------------------
3619
3620 CommandSetInlineNodeScript::CommandSetInlineNodeScript(std::string node, std::string script)
3621   : Command(), _nodeName(node), _script(script)
3622 {
3623   DEBTRACE("CommandSetInlineNodeScript::CommandSetInlineNodeScript " << node << " " <<script);
3624   _oldScript.clear();
3625 }
3626
3627 std::string CommandSetInlineNodeScript::dump()
3628 {
3629   string ret ="CommandSetInlineNodeScript " + _nodeName;
3630   return ret;
3631 }
3632
3633 bool CommandSetInlineNodeScript::localExecute()
3634 {
3635   DEBTRACE("CommandSetInlineNodeScript::localExecute");
3636   try
3637     {
3638       Proc* proc = GuiContext::getCurrent()->getProc();
3639       Node* node = proc->getChildByName(_nodeName);
3640       if (_script.empty())
3641         {
3642           GuiContext::getCurrent()->_lastErrorMessage = "InlineNode script empty: " + _nodeName;
3643           return false;
3644         }
3645       if (YACS::ENGINE::InlineNode* inlineNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
3646         {
3647           _oldScript = inlineNode->getScript();
3648           inlineNode->setScript(_script);
3649           YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
3650           SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
3651           snode->update(SYNCHRO,0,snode);
3652           return true;
3653         }
3654       else
3655         {
3656           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
3657           return false;
3658         }
3659     }
3660   catch (Exception& ex)
3661     {
3662       DEBTRACE("CommandSetInlineNodeScript::localExecute() : " << ex.what());
3663       setErrorMsg(ex);
3664       return false;
3665     }  
3666 }
3667
3668 bool CommandSetInlineNodeScript::localReverse()
3669 {
3670   DEBTRACE("CommandSetInlineNodeScript::localReverse");
3671   try
3672     {
3673       Proc* proc = GuiContext::getCurrent()->getProc();
3674       Node* node = proc->getChildByName(_nodeName);
3675       if (YACS::ENGINE::InlineNode* inlineNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
3676         {
3677           inlineNode->setScript(_oldScript);
3678           YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
3679           SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
3680           snode->update(SYNCHRO,0,snode);
3681           return true;
3682         }
3683       else
3684         {
3685           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
3686           return false;
3687         }
3688     }
3689   catch (Exception& ex)
3690     {
3691       DEBTRACE("CommandSetInlineNodeScript::localExecute() : " << ex.what());
3692       setErrorMsg(ex);
3693       return false;
3694     }  
3695 }
3696
3697 // ----------------------------------------------------------------------------
3698
3699 CommandAddComponentInstance::CommandAddComponentInstance(std::string compoName,
3700                                                          std::string container,
3701                                                          std::string name)
3702   : Command(), _compoName(compoName), _container(container), _name(name), _subcompo(0)
3703 {
3704   DEBTRACE("CommandAddComponentInstance::CommandAddComponentInstance " <<_compoName);
3705 }
3706
3707 std::string CommandAddComponentInstance::dump()
3708 {
3709   string ret ="CommandAddComponentInstance " + _compoName + " " + _container + " " + _name;
3710   return ret;
3711 }
3712
3713 bool CommandAddComponentInstance::localExecute()
3714 {
3715   DEBTRACE("CommandAddComponentInstance::localExecute "<< _compoName << " " << _container << " " << _name);
3716   try
3717     {
3718       Proc* proc = GuiContext::getCurrent()->getProc();
3719       YASSERT(proc->containerMap.count(_container));
3720       Container *cont = proc->containerMap[_container];
3721       ComponentInstance* compoInst = new SalomeComponent(_compoName);
3722       compoInst->setContainer(cont);
3723       proc->addComponentInstance(compoInst, _name);
3724       
3725       SubjectProc *sproc = GuiContext::getCurrent()->getSubjectProc();
3726       _subcompo = sproc->addSubjectComponent(compoInst);
3727       _name = compoInst->getInstanceName();
3728       DEBTRACE(_name);
3729       return true;
3730     }
3731   catch (Exception& ex)
3732     {
3733       DEBTRACE("CommandAddComponentInstance::localExecute() : " << ex.what());
3734       setErrorMsg(ex);
3735       return false;
3736     }
3737 }
3738
3739 bool CommandAddComponentInstance::localReverse()
3740 {
3741   DEBTRACE("CommandAddComponentInstance::localReverse");
3742   try
3743     {
3744       Proc* proc = GuiContext::getCurrent()->getProc();
3745       YASSERT(proc->componentInstanceMap.count(_name));
3746       ComponentInstance *compo = proc->componentInstanceMap[_name];
3747       YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
3748       _subcompo = GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
3749       YASSERT(!_subcompo->hasServices());
3750       Container *cont = compo->getContainer();
3751       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
3752       SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
3753       subcont->detachComponent(_subcompo);
3754       GuiContext::getCurrent()->_mapOfSubjectComponent.erase(compo);
3755       proc->removeComponentInstance(compo);
3756       return true;
3757     }
3758   catch (Exception& ex)
3759     {
3760       DEBTRACE("CommandAddComponentInstance::localReverse() : " << ex.what());
3761       setErrorMsg(ex);
3762       return false;
3763     }
3764 }
3765
3766 // ----------------------------------------------------------------------------
3767 CommandSetExecutionMode::CommandSetExecutionMode(std::string nodeName, std::string mode)
3768   : Command(), _mode(mode),_nodeName(nodeName)
3769 {
3770   DEBTRACE("CommandSetExecutionMode::CommandSetExecutionMode " << nodeName << " " << mode);
3771   _oldmode = "local";
3772 }
3773
3774 std::string CommandSetExecutionMode::dump()
3775 {
3776   string ret ="CommandSetExecutionMode " + _mode + " " + _nodeName;
3777   return ret;
3778 }
3779
3780 bool CommandSetExecutionMode::localExecute()
3781 {
3782   DEBTRACE("CommandSetExecutionMode::localExecute");
3783   try
3784     {
3785       Proc* proc = GuiContext::getCurrent()->getProc();
3786       Node* node = proc->getChildByName(_nodeName);
3787       if (YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
3788         {
3789           _oldmode = pyNode->getExecutionMode();
3790           pyNode->setExecutionMode(_mode);
3791           SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[pyNode];
3792           snode->update(UPDATE, 0, 0);
3793           return true;
3794         }
3795       else
3796         {
3797           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
3798           return false;
3799         }
3800     }
3801   catch (Exception& ex)
3802     {
3803       DEBTRACE("CommandSetExecutionMode::localExecute() : " << ex.what());
3804       setErrorMsg(ex);
3805       return false;
3806     }
3807 }
3808
3809 bool CommandSetExecutionMode::localReverse()
3810 {
3811   DEBTRACE("CommandSetExecutionMode::localReverse");
3812   try
3813     {
3814       if (_oldmode == _mode) return true;
3815       Proc* proc = GuiContext::getCurrent()->getProc();
3816       Node* node = proc->getChildByName(_nodeName);
3817       if (YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
3818         {
3819           pyNode->setExecutionMode(_oldmode);
3820           SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[pyNode];
3821           snode->update(UPDATE, 0, 0);
3822           return true;
3823         }
3824       else
3825         {
3826           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
3827           return false;
3828         }
3829     }
3830   catch (Exception& ex)
3831     {
3832       DEBTRACE("CommandSetExecutionMode::localReverse() : " << ex.what());
3833       setErrorMsg(ex);
3834       return false;
3835     }
3836   return true;
3837 }
3838
3839
3840 // ----------------------------------------------------------------------------
3841
3842 CommandSetContainer::CommandSetContainer(std::string nodeName, std::string container)
3843   : Command(), _container(container),_nodeName(nodeName)
3844 {
3845   DEBTRACE("CommandSetContainer::CommandSetContainer " << nodeName << " " << container);
3846   _oldcont = "DefaultContainer";
3847 }
3848
3849 std::string CommandSetContainer::dump()
3850 {
3851   string ret ="CommandSetContainer " + _container + " " + _nodeName;
3852   return ret;
3853 }
3854
3855 bool CommandSetContainer::localExecute()
3856 {
3857   DEBTRACE("CommandSetContainer::localExecute");
3858   try
3859     {
3860       Proc* proc = GuiContext::getCurrent()->getProc();
3861       if (proc->containerMap.count(_container))
3862         {
3863           Container *cont = proc->containerMap[_container];
3864           Node* node = proc->getChildByName(_nodeName);
3865           if (YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
3866             {
3867               Container* oldcont = pyNode->getContainer();
3868               if(oldcont)
3869                 _oldcont = pyNode->getContainer()->getName();
3870               pyNode->setContainer(cont);
3871               SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[pyNode];
3872               SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
3873               snode->update(ASSOCIATE, 0, subcont);
3874               return true;
3875             }
3876           else
3877             {
3878               GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
3879               return false;
3880             }
3881         }
3882       else
3883         GuiContext::getCurrent()->_lastErrorMessage = "Container not found: " + _container;
3884       return false;
3885     }
3886   catch (Exception& ex)
3887     {
3888       DEBTRACE("CommandSetContainer::localExecute() : " << ex.what());
3889       setErrorMsg(ex);
3890       return false;
3891     }
3892 }
3893
3894 bool CommandSetContainer::localReverse()
3895 {
3896   DEBTRACE("CommandSetContainer::localReverse");
3897   try
3898     {
3899       if (_oldcont == _container) return true;
3900       Proc* proc = GuiContext::getCurrent()->getProc();
3901       if (proc->containerMap.count(_oldcont))
3902         {
3903           Container *cont = proc->containerMap[_oldcont];
3904           Node* node = proc->getChildByName(_nodeName);
3905           if (YACS::ENGINE::InlineNode* pyNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
3906             {
3907               pyNode->setContainer(cont);
3908               SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[pyNode];
3909               SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
3910               snode->update(ASSOCIATE, 0, subcont);
3911               return true;
3912             }
3913           else
3914             {
3915               GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
3916               return false;
3917             }
3918         }
3919       else
3920         GuiContext::getCurrent()->_lastErrorMessage = "Container not found: " + _oldcont;
3921       return false;
3922     }
3923   catch (Exception& ex)
3924     {
3925       DEBTRACE("CommandSetContainer::localReverse() : " << ex.what());
3926       setErrorMsg(ex);
3927       return false;
3928     }
3929   return true;
3930 }
3931
3932
3933   
3934 // ----------------------------------------------------------------------------
3935
3936 CommandAssociateComponentToContainer::CommandAssociateComponentToContainer(std::string instanceName,
3937                                                                            std::string container)
3938   : Command(), _container(container),_instanceName(instanceName)
3939 {
3940   DEBTRACE("CommandAssociateComponentToContainer::CommandAssociateComponentToContainer " << instanceName << " " << container);
3941   _oldcont = "DefaultContainer";
3942 }
3943
3944 std::string CommandAssociateComponentToContainer::dump()
3945 {
3946   string ret ="CommandAssociateComponentToContainer " + _container + " " + _instanceName;
3947   return ret;
3948 }
3949
3950 bool CommandAssociateComponentToContainer::localExecute()
3951 {
3952   DEBTRACE("CommandAssociateComponentToContainer::localExecute");
3953   try
3954     {
3955       Proc* proc = GuiContext::getCurrent()->getProc();
3956       if (proc->containerMap.count(_container))
3957         {
3958           Container *cont = proc->containerMap[_container];
3959           if (proc->componentInstanceMap.count(_instanceName))
3960             {
3961               DEBTRACE(_instanceName);
3962               ComponentInstance *compo = proc->componentInstanceMap[_instanceName];
3963               if (compo->getContainer())
3964                 _oldcont = compo->getContainer()->getName();
3965               compo->setContainer(cont);
3966
3967               YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
3968               SubjectComponent *scomp =  GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
3969               YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
3970               SubjectContainer *subcont =  GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
3971               scomp->addSubjectReference(subcont);
3972               if (scomp->_subRefContainer)
3973                 subcont->moveComponent(scomp->_subRefContainer);
3974               else
3975                 scomp->_subRefContainer = subcont->attachComponent(scomp);
3976               scomp->notifyServicesChange(ASSOCIATE, CONTAINER, subcont);
3977               return true;
3978             }
3979           else
3980             GuiContext::getCurrent()->_lastErrorMessage = "Component instance not found: " + _instanceName;
3981         }
3982       else
3983         GuiContext::getCurrent()->_lastErrorMessage = "Container not found: " + _container;
3984       return false;
3985     }
3986   catch (Exception& ex)
3987     {
3988       DEBTRACE("CommandAssociateComponentToContainer::localExecute() : " << ex.what());
3989       setErrorMsg(ex);
3990       return false;
3991     }
3992 }
3993
3994 bool CommandAssociateComponentToContainer::localReverse()
3995 {
3996   DEBTRACE("CommandAssociateComponentToContainer::localReverse");
3997   try
3998     {
3999       if (_oldcont == _container) return true;
4000       Proc* proc = GuiContext::getCurrent()->getProc();
4001       if (proc->containerMap.count(_oldcont))
4002         {
4003           Container *cont = proc->containerMap[_oldcont];
4004           if (proc->componentInstanceMap.count(_instanceName))
4005             {
4006               DEBTRACE(_instanceName);
4007               ComponentInstance *compo = proc->componentInstanceMap[_instanceName];
4008               compo->setContainer(cont);
4009
4010               YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
4011               SubjectComponent *scomp =  GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
4012               YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
4013               SubjectContainer *subcont =  GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
4014               scomp->addSubjectReference(subcont);
4015               if (scomp->_subRefContainer)
4016                 subcont->moveComponent(scomp->_subRefContainer);
4017               else
4018                 scomp->_subRefContainer = subcont->attachComponent(scomp);
4019               scomp->notifyServicesChange(ASSOCIATE, CONTAINER, subcont);
4020               return true;
4021             }
4022           else
4023             GuiContext::getCurrent()->_lastErrorMessage = "Component instance not found: " + _instanceName;
4024         }
4025       else
4026         GuiContext::getCurrent()->_lastErrorMessage = "Container not found: " + _oldcont;
4027       return false;
4028     }
4029   catch (Exception& ex)
4030     {
4031       DEBTRACE("CommandAssociateComponentToContainer::localReverse() : " << ex.what());
4032       setErrorMsg(ex);
4033       return false;
4034     }
4035   return true;
4036 }
4037
4038 // ----------------------------------------------------------------------------
4039
4040 CommandAssociateServiceToComponent::CommandAssociateServiceToComponent(std::string service,
4041                                                                        std::string instanceName)
4042   : Command(), _service(service), _instanceName(instanceName)
4043 {
4044   DEBTRACE("CommandAssociateServiceToComponent::CommandAssociateServiceToComponent "<< service << " " <<instanceName);
4045   _oldInstance="";
4046 }
4047
4048 std::string CommandAssociateServiceToComponent::dump()
4049 {
4050   string ret ="CommandAssociateServiceToComponent " + _service + " " + _instanceName;
4051   return ret;
4052 }
4053
4054 bool CommandAssociateServiceToComponent::localExecute()
4055 {
4056   DEBTRACE("CommandAssociateServiceToComponent::localExecute");
4057   try
4058     {
4059       Proc* proc = GuiContext::getCurrent()->getProc();
4060       if (_service == proc->getName()) return false; // proc is not an elementary node
4061       Node* node = proc->getChildByName(_service);
4062       if (ServiceNode *service = dynamic_cast<ServiceNode*>(node))
4063         {
4064           if (proc->componentInstanceMap.count(_instanceName))
4065             {
4066               DEBTRACE(_instanceName);
4067               ComponentInstance *compo = proc->componentInstanceMap[_instanceName];
4068               service->setComponent(compo);
4069
4070               YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(service));
4071               SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[service];
4072               SubjectServiceNode *ssnode = dynamic_cast<SubjectServiceNode*>(snode);
4073               YASSERT(ssnode);
4074               YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
4075               SubjectComponent *subCompo = GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
4076               snode->addSubjectReference(subCompo);
4077               if (ssnode->_subRefComponent)
4078                 {
4079                   SubjectComponent* oldcomp = dynamic_cast<SubjectComponent*>(ssnode->_subRefComponent->getParent());
4080                   YASSERT(oldcomp);
4081                   _oldInstance = oldcomp->getName();
4082                   _oldcont = oldcomp->getComponent()->getContainer()->getName();
4083                   subCompo->moveService(ssnode->_subRefComponent);
4084                 }
4085               else
4086                 ssnode->_subRefComponent = subCompo->attachService(ssnode);
4087
4088               return true;
4089             }
4090           else
4091             GuiContext::getCurrent()->_lastErrorMessage = "Component instance not found: " + _instanceName;
4092         }
4093       else
4094         GuiContext::getCurrent()->_lastErrorMessage = "Node is not a service node: " + _service;
4095       return false;
4096     }
4097   catch (Exception& ex)
4098     {
4099       DEBTRACE("CommandAssociateServiceToComponent::localExecute() : " << ex.what());
4100       setErrorMsg(ex);
4101       return false;
4102     }
4103 }
4104
4105 bool CommandAssociateServiceToComponent::localReverse()
4106 {
4107   DEBTRACE("CommandAssociateServiceToComponent::localReverse");
4108   try
4109     {
4110       Proc* proc = GuiContext::getCurrent()->getProc();
4111       if (_service == proc->getName()) return false; // proc is not an elementary node
4112       Node* node = proc->getChildByName(_service);
4113       if (ServiceNode *service = dynamic_cast<ServiceNode*>(node))
4114         {
4115           ComponentInstance *compo;
4116           if (!proc->componentInstanceMap.count(_oldInstance))
4117             {
4118               //component instance does not exist anymore recreate it
4119               ComponentInstance *oldcompo = service->getComponent();
4120               compo = oldcompo->clone();
4121               compo->setName(_oldInstance);
4122               proc->addComponentInstance(compo, _oldInstance);
4123               Container *cont = proc->containerMap[_oldcont];
4124               compo->setContainer(cont);
4125               SubjectProc *sproc = GuiContext::getCurrent()->getSubjectProc();
4126               sproc->addSubjectComponent(compo);
4127             }
4128           else
4129             {
4130               compo = proc->componentInstanceMap[_oldInstance];
4131             }
4132
4133           DEBTRACE(_oldInstance);
4134           service->setComponent(compo);
4135
4136           YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(service));
4137           SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[service];
4138           SubjectServiceNode *ssnode = dynamic_cast<SubjectServiceNode*>(snode);
4139           YASSERT(ssnode);
4140           YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
4141           SubjectComponent *subCompo = GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
4142           snode->addSubjectReference(subCompo);
4143           if (ssnode->_subRefComponent)
4144             subCompo->moveService(ssnode->_subRefComponent);
4145           else
4146             ssnode->_subRefComponent = subCompo->attachService(ssnode);
4147
4148           return true;
4149         }
4150       else
4151         GuiContext::getCurrent()->_lastErrorMessage = "Node is not a service node: " + _service;
4152       return false;
4153     }
4154   catch (Exception& ex)
4155     {
4156       DEBTRACE("CommandAssociateServiceToComponent::localReverse() : " << ex.what());
4157       setErrorMsg(ex);
4158       return false;
4159     }
4160 }
4161
4162 // ----------------------------------------------------------------------------
4163
4164 CommandAddComponentFromCatalog::CommandAddComponentFromCatalog(YACS::ENGINE::Catalog* catalog,
4165                                                                std::string position,
4166                                                                std::string compo,
4167                                                                std::string service)
4168   : Command(), _catalog(catalog), _position(position), _compo(compo), _service(service)
4169 {
4170   DEBTRACE("CommandAddComponentFromCatalog::CommandAddComponentFromCatalog " << position << " " << compo << " " << service);
4171   _nameInProc="";
4172   _createdInstance = false;
4173 }
4174
4175 std::string CommandAddComponentFromCatalog::dump()
4176 {
4177   string ret = "CommandAddComponentFromCatalog " + _position + " " + _compo + " " + _service;
4178   return ret;
4179 }
4180     
4181 bool CommandAddComponentFromCatalog::localExecute()
4182 {
4183   DEBTRACE("CommandAddComponentFromCatalog::localExecute");
4184   try
4185     {
4186       DEBTRACE("_nameInProc: " << _nameInProc);
4187       Proc* proc = GuiContext::getCurrent()->getProc();
4188       Node* node = proc;
4189       if (!_position.empty()) node = proc->getChildByName(_position);
4190       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
4191       SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
4192       SubjectServiceNode *ssnode = dynamic_cast<SubjectServiceNode*>(snode);
4193       YASSERT(ssnode);
4194       if (_catalog->_componentMap.count(_compo))
4195         {
4196           YACS::ENGINE::ComponentDefinition* compodef = _catalog->_componentMap[_compo];
4197           if (compodef->_serviceMap.count(_service))
4198             {
4199               Proc* proc = GuiContext::getCurrent()->getProc();
4200               ComponentInstance *instance =ssnode->_serviceNode->getComponent();
4201               YASSERT(instance);
4202               SubjectComponent* subCompo = 0;
4203               _createdInstance = false;
4204               if(!GuiContext::getCurrent()->_mapOfSubjectComponent.count(instance))
4205                 {
4206                   _createdInstance = true;
4207                   //automatic rename of the component instance by the proc on first execute
4208                   DEBTRACE("name given to proc:" << _nameInProc);
4209                   proc->addComponentInstance(instance,_nameInProc, true);
4210                   _nameInProc= instance->getInstanceName();
4211                   DEBTRACE("name given by proc:" << _nameInProc);
4212                   subCompo = GuiContext::getCurrent()->getSubjectProc()->addSubjectComponent(instance);
4213                 }
4214               else 
4215                 subCompo = GuiContext::getCurrent()->_mapOfSubjectComponent[instance];
4216               YASSERT(subCompo);
4217               ssnode->addSubjectReference(subCompo);
4218               YASSERT(! ssnode->_subRefComponent);
4219               ssnode->_subRefComponent = subCompo->attachService(ssnode);
4220             }
4221         }
4222       return true;
4223     }
4224   catch (Exception& ex)
4225     {
4226       DEBTRACE("CommandAddComponentFromCatalog::localExecute() : " << ex.what());
4227       setErrorMsg(ex);
4228       return false;
4229     }      
4230 }
4231
4232 bool CommandAddComponentFromCatalog::localReverse()
4233 {
4234   DEBTRACE("CommandAddComponentFromCatalog::localReverse");
4235   try
4236     {
4237       Proc* proc = GuiContext::getCurrent()->getProc();
4238       Node* node = proc;
4239       if (!_position.empty()) node = proc->getChildByName(_position);
4240       YASSERT(GuiContext::getCurrent()->_mapOfSubjectNode.count(node));
4241       SubjectNode* snode = GuiContext::getCurrent()->_mapOfSubjectNode[node];
4242       SubjectServiceNode *ssnode = dynamic_cast<SubjectServiceNode*>(snode);
4243       YASSERT(ssnode);
4244
4245       DEBTRACE("_nameInProc: " << _nameInProc);
4246       YASSERT(proc->componentInstanceMap.count(_nameInProc));
4247       ComponentInstance *compo = proc->componentInstanceMap[_nameInProc];
4248       YASSERT(GuiContext::getCurrent()->_mapOfSubjectComponent.count(compo));
4249       SubjectComponent *subCompo = GuiContext::getCurrent()->_mapOfSubjectComponent[compo];
4250
4251       subCompo->detachService(ssnode);
4252       if (subCompo->hasServices())
4253         throw YACS::Exception("Component instance with services attached, not removed");
4254       Container *cont = compo->getContainer();
4255       YASSERT(GuiContext::getCurrent()->_mapOfSubjectContainer.count(cont));
4256       SubjectContainer *subcont = GuiContext::getCurrent()->_mapOfSubjectContainer[cont];
4257       subcont->detachComponent(subCompo);
4258       //remove componentInstance from proc, from context
4259       if (_createdInstance)
4260         {
4261           GuiContext::getCurrent()->_mapOfSubjectComponent.erase(compo);
4262           proc->removeComponentInstance(compo);
4263         }
4264       DEBTRACE("_nameInProc: " << _nameInProc);
4265       return true;
4266     }
4267   catch (Exception& ex)
4268     {
4269       DEBTRACE("CommandAddComponentFromCatalog::localReverse() : " << ex.what());
4270       setErrorMsg(ex);
4271       return false;
4272     }      
4273 }
4274