Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / hmi / commandsProc.cxx
1
2 #include <Python.h>
3 #include "commandsProc.hxx"
4
5 #include "Node.hxx"
6 #include "ComposedNode.hxx"
7 #include "Bloc.hxx"
8 #include "Proc.hxx"
9 #include "ElementaryNode.hxx"
10 #include "InlineNode.hxx"
11 #include "ServiceNode.hxx"
12 #include "PythonNode.hxx"
13 #include "CORBANode.hxx"
14 #include "CppNode.hxx"
15 #include "XMLNode.hxx"
16 #include "SalomePythonNode.hxx"
17 #include "XMLNode.hxx"
18 #include "ForLoop.hxx"
19 #include "ForEachLoop.hxx"
20 #include "WhileLoop.hxx"
21 #include "Switch.hxx"
22 #include "OptimizerLoop.hxx"
23 #include "PresetNode.hxx"
24 #include "OutNode.hxx"
25 #include "StudyNodes.hxx"
26 #include "Exception.hxx"
27 #include "DataPort.hxx"
28 #include "InputDataStreamPort.hxx"
29 #include "OutputDataStreamPort.hxx"
30 #include "ComponentDefinition.hxx"
31 #include "SalomeContainer.hxx"
32 #include "SalomeComponent.hxx"
33 #include "TypeCode.hxx"
34
35 #include "guiContext.hxx"
36
37 #include <iostream>
38 #include <string>
39
40 //#define _DEVDEBUG_
41 #include "YacsTrace.hxx"
42
43 using namespace std;
44
45 using namespace YACS;
46 using namespace YACS::ENGINE;
47 using namespace YACS::HMI;
48
49 std::map<int, std::string> ProcInvoc::_typeNameMap;
50
51 // ----------------------------------------------------------------------------
52
53 ProcInvoc::ProcInvoc()
54   : Invocator()
55 {
56   _typeNameMap.clear();
57   _typeNameMap[SALOMEPROC]           = "SALOMEPROC";
58   _typeNameMap[BLOC]                 = "BLOC";
59   _typeNameMap[FOREACHLOOP]          = "FOREACHLOOP";
60   _typeNameMap[OPTIMIZERLOOP]        = "OPTIMIZERLOOP";
61   _typeNameMap[FORLOOP]              = "FORLOOP";
62   _typeNameMap[WHILELOOP]            = "WHILELOOP";
63   _typeNameMap[SWITCH]               = "SWITCH";
64   _typeNameMap[PYTHONNODE]           = "PYTHONNODE";
65   _typeNameMap[PYFUNCNODE]           = "PYFUNCNODE";
66   _typeNameMap[CORBANODE]            = "CORBANODE";
67   _typeNameMap[SALOMENODE]           = "SALOMENODE";
68   _typeNameMap[CPPNODE]              = "CPPNODE";
69   _typeNameMap[SALOMEPYTHONNODE]     = "SALOMEPYTHONNODE";
70   _typeNameMap[XMLNODE]              = "XMLNODE";
71   _typeNameMap[SPLITTERNODE]         = "SPLITTERNODE";
72   _typeNameMap[DFTODSFORLOOPNODE]    = "DFTODSFORLOOPNODE";
73   _typeNameMap[DSTODFFORLOOPNODE]    = "DSTODFFORLOOPNODE";
74   _typeNameMap[PRESETNODE]           = "PRESETNODE";
75   _typeNameMap[OUTNODE]              = "OUTNODE";
76   _typeNameMap[STUDYINNODE]          = "STUDYINNODE";
77   _typeNameMap[STUDYOUTNODE]         = "STUDYOUTNODE";
78   _typeNameMap[INPUTPORT]            = "INPUTPORT";
79   _typeNameMap[OUTPUTPORT]           = "OUTPUTPORT";
80   _typeNameMap[INPUTDATASTREAMPORT]  = "INPUTDATASTREAMPORT";
81   _typeNameMap[OUTPUTDATASTREAMPORT] = "OUTPUTDATASTREAMPORT";
82   _typeNameMap[CONTAINER]            = "CONTAINER";
83   _typeNameMap[COMPONENT]            = "COMPONENT";
84   _typeNameMap[REFERENCE]            = "REFERENCE";
85   _typeNameMap[DATATYPE]             = "DATATYPE";
86 }
87
88 TypeOfElem ProcInvoc::getTypeOfNode(YACS::ENGINE::Node* node)
89 {
90   TypeOfElem nodeType = UNKNOWN;
91   if      (dynamic_cast<YACS::ENGINE::Bloc*>(node))             nodeType = BLOC;
92   else if (dynamic_cast<YACS::ENGINE::PythonNode*>(node))       nodeType = PYTHONNODE;
93   else if (dynamic_cast<YACS::ENGINE::PyFuncNode*>(node))       nodeType = PYFUNCNODE;
94   else if (dynamic_cast<YACS::ENGINE::CORBANode*>(node))        nodeType = CORBANODE;
95   else if (dynamic_cast<YACS::ENGINE::CppNode*>(node))          nodeType = CPPNODE;
96   else if (dynamic_cast<YACS::ENGINE::SalomeNode*>(node))       nodeType = SALOMENODE;
97   else if (dynamic_cast<YACS::ENGINE::SalomePythonNode*>(node)) nodeType = SALOMEPYTHONNODE;
98   else if (dynamic_cast<YACS::ENGINE::XmlNode*>(node))          nodeType = XMLNODE;
99   else if (dynamic_cast<YACS::ENGINE::SplitterNode*>(node))     nodeType = SPLITTERNODE;
100   else if (dynamic_cast<YACS::ENGINE::ForLoop*>(node))          nodeType = FORLOOP;
101   else if (dynamic_cast<YACS::ENGINE::WhileLoop*>(node))        nodeType = WHILELOOP;
102   else if (dynamic_cast<YACS::ENGINE::Switch*>(node))           nodeType = SWITCH;
103   else if (dynamic_cast<YACS::ENGINE::ForEachLoop*>(node))      nodeType = FOREACHLOOP;
104   else if (dynamic_cast<YACS::ENGINE::OptimizerLoop*>(node))    nodeType = OPTIMIZERLOOP;
105   else if (dynamic_cast<YACS::ENGINE::PresetNode*>(node))       nodeType = PRESETNODE;
106   else if (dynamic_cast<YACS::ENGINE::OutNode*>(node))          nodeType = OUTNODE;
107   else if (dynamic_cast<YACS::ENGINE::StudyInNode*>(node))      nodeType = STUDYINNODE;
108   else if (dynamic_cast<YACS::ENGINE::StudyOutNode*>(node))     nodeType = STUDYOUTNODE;
109   return nodeType;
110 }
111
112 TypeOfElem ProcInvoc::getTypeOfPort(YACS::ENGINE::DataPort* port)
113 {
114   TypeOfElem portType = UNKNOWN;
115   if      (dynamic_cast<YACS::ENGINE::InputPort*>(port))            portType = INPUTPORT;
116   else if (dynamic_cast<YACS::ENGINE::OutputPort*>(port))           portType = OUTPUTPORT;
117   else if (dynamic_cast<YACS::ENGINE::InputDataStreamPort*>(port))  portType = INPUTDATASTREAMPORT;
118   else if (dynamic_cast<YACS::ENGINE::OutputDataStreamPort*>(port)) portType = OUTPUTDATASTREAMPORT;
119   return portType;
120 }
121
122 std::string ProcInvoc::getTypeName(TypeOfElem type)
123 {
124   if (_typeNameMap.count(type))
125     return _typeNameMap[type];
126   else
127     return "UNKNOWN";
128 }
129
130 // ----------------------------------------------------------------------------
131
132 CommandAddNodeFromCatalog::CommandAddNodeFromCatalog(YACS::ENGINE::Catalog *catalog,
133                                                      std::string compo,
134                                                      std::string type,
135                                                      std::string position,
136                                                      std::string name,
137                                                      int swCase)
138   : Command(), _catalog(catalog), _compoName(compo), _typeName(type),
139     _position(position), _name(name), _swCase(swCase)
140 {
141   _nodeToClone = 0;
142   if (_compoName.empty())
143     {
144       if (_catalog->_nodeMap.count(_typeName))
145         _nodeToClone = _catalog->_nodeMap[_typeName];
146       else if (_catalog->_composednodeMap.count(_typeName))
147         _nodeToClone = _catalog->_composednodeMap[_typeName];
148     }
149   else
150     if (_catalog->_componentMap.count(_compoName))
151       {
152         YACS::ENGINE::ComponentDefinition* compodef = _catalog->_componentMap[_compoName];
153         if (compodef->_serviceMap.count(_typeName))
154           _nodeToClone = compodef->_serviceMap[_typeName];
155       }
156   if (_nodeToClone)
157     _typeNode = ProcInvoc::getTypeOfNode(_nodeToClone);
158 }
159
160
161 YACS::ENGINE::Node *CommandAddNodeFromCatalog::getNode()
162 {
163   return _node;
164 }
165
166 bool CommandAddNodeFromCatalog::localExecute()
167 {
168   Node *son = 0;
169   try
170     {
171       Proc* proc = GuiContext::getCurrent()->getProc();
172       Node* node = proc;
173       if (!_position.empty()) node = proc->getChildByName(_position);
174       ComposedNode* father =dynamic_cast<ComposedNode*> (node);
175       if (father)
176         {
177           son = _nodeToClone->clone(0);
178           son->setName(_name);
179         }
180       if (son)
181         {
182           TypeOfElem fatherType = ProcInvoc::getTypeOfNode(father);
183           switch (fatherType)
184             {
185             case BLOC:
186               (dynamic_cast<YACS::ENGINE::Bloc*>(father))->edAddChild(son);
187               break;
188             case FORLOOP:
189               (dynamic_cast<YACS::ENGINE::ForLoop*>(father))->edSetNode(son);
190               break;
191             case WHILELOOP:
192               (dynamic_cast<YACS::ENGINE::WhileLoop*>(father))->edSetNode(son);
193               break;
194             case SWITCH:
195               (dynamic_cast<YACS::ENGINE::Switch*>(father))->edSetNode(_swCase,son);
196               break;
197             case FOREACHLOOP:
198               (dynamic_cast<YACS::ENGINE::ForEachLoop*>(father))->edSetNode(son);
199               break;
200             case OPTIMIZERLOOP:
201               (dynamic_cast<YACS::ENGINE::OptimizerLoop*>(father))->edSetNode(son);
202               break;
203             default:
204               assert(0);
205             }
206         }
207       _node = son;
208     }
209   catch (Exception& ex)
210     {
211       DEBTRACE("CommandAddNode::localExecute() : " << ex.what());
212       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
213       if (son) delete son;
214       _node = 0;
215     }
216   return (_node != 0);
217 }
218
219 bool CommandAddNodeFromCatalog::localReverse()
220 {
221 }
222
223 // ----------------------------------------------------------------------------
224
225 CommandRenameNode::CommandRenameNode(std::string position, std::string name)
226   : Command(), _position(position), _name(name)
227 {
228   DEBTRACE("CommandRenameNode::CommandRenameNode " << _position << " " << _name);
229 }
230
231 bool CommandRenameNode::localExecute()
232 {
233   Proc* proc = GuiContext::getCurrent()->getProc();
234   Node* node = proc;
235   try
236     {
237       if (_position != proc->getName()) node = proc->getChildByName(_position);
238       node->setName(_name);
239     }
240   catch (Exception& ex)
241     {
242       DEBTRACE("CommandRenameNode::localExecute() : " << ex.what());
243       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
244       node = 0;
245     }
246   return (node != 0); 
247 }
248
249 bool CommandRenameNode::localReverse()
250 {
251 }
252
253 // ----------------------------------------------------------------------------
254
255 CommandAddDataTypeFromCatalog::CommandAddDataTypeFromCatalog(YACS::ENGINE::Catalog* catalog,
256                                                              std::string typeName)
257   : Command(), _catalog(catalog), _typeName(typeName)
258 {
259   DEBTRACE("CommandAddDataTypeFromCatalog::CommandAddDataTypeFromCatalog: " << typeName);
260 }
261
262 YACS::ENGINE::TypeCode *CommandAddDataTypeFromCatalog::getTypeCode()
263 {
264
265   if (GuiContext::getCurrent()->getProc()->typeMap.count(_typeName))
266     return GuiContext::getCurrent()->getProc()->typeMap[_typeName];
267   else return 0;
268 }
269
270 bool CommandAddDataTypeFromCatalog::localExecute()
271 {
272   Proc* proc = GuiContext::getCurrent()->getProc();
273   if (proc->typeMap.count(_typeName))
274     {
275       DEBTRACE("typecode already existing in proc: " << _typeName);
276       //GuiContext::getCurrent()->_lastErrorMessage = "typecode already existing in proc: " + _typeName;
277       //return false;
278       return true;
279     }
280   else
281     if (_catalog->_typeMap.count(_typeName))
282       {
283         DEBTRACE("typecode found in catalog, cloned: " << _typeName);
284         proc->typeMap[_typeName] = _catalog->_typeMap[_typeName]->clone();
285         return true;
286       }
287   GuiContext::getCurrent()->_lastErrorMessage = "typecode not found in catalog: " + _typeName;
288   return false;
289 }
290
291 bool CommandAddDataTypeFromCatalog::localReverse()
292 {
293 }
294
295
296 // ----------------------------------------------------------------------------
297
298 CommandAddInputPortFromCatalog::CommandAddInputPortFromCatalog(YACS::ENGINE::Catalog *catalog,
299                                                                std::string type,
300                                                                std::string node,
301                                                                std::string name)
302   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
303 {
304   _inputPort = 0;
305 }
306
307 YACS::ENGINE::InputPort *CommandAddInputPortFromCatalog::getInputPort()
308 {
309   return _inputPort;
310 }
311
312 bool CommandAddInputPortFromCatalog::localExecute()
313 {
314   InputPort *son = 0;
315   try
316     {
317       Proc* proc = GuiContext::getCurrent()->getProc();
318       Node* node = proc->getChildByName(_node);
319       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
320       if (father)
321         {
322           if (_catalog->_typeMap.count(_typePort))
323             son = father->edAddInputPort(_name, _catalog->_typeMap[_typePort]);
324           else
325             {
326               DEBTRACE(_typePort << " not found in catalog");
327               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
328             }
329         }
330       _inputPort = son;
331     }
332   catch (Exception& ex)
333     {
334       DEBTRACE("CommandAddInputPortFromCatalog::localExecute() : " << ex.what());
335       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
336       if (son) delete son;
337       _inputPort = 0;
338     }
339   return (_inputPort != 0);
340 }
341
342 bool CommandAddInputPortFromCatalog::localReverse()
343 {
344 }
345  
346 // ----------------------------------------------------------------------------
347
348 CommandAddOutputPortFromCatalog::CommandAddOutputPortFromCatalog(YACS::ENGINE::Catalog *catalog,
349                                                                  std::string type,
350                                                                  std::string node,
351                                                                  std::string name)
352   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
353 {
354   _outputPort = 0;
355 }
356
357 YACS::ENGINE::OutputPort *CommandAddOutputPortFromCatalog::getOutputPort()
358 {
359   return _outputPort;
360 }
361
362 bool CommandAddOutputPortFromCatalog::localExecute()
363 {
364   OutputPort *son = 0;
365   try
366     {
367       Proc* proc = GuiContext::getCurrent()->getProc();
368       Node* node = proc->getChildByName(_node);
369       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
370       if (father)
371         {
372           if (_catalog->_typeMap.count(_typePort))
373             son = father->edAddOutputPort(_name, _catalog->_typeMap[_typePort]);
374           else
375             {
376               DEBTRACE(_typePort << " not found in catalog");
377               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
378             }
379         }
380       _outputPort = son;
381     }
382   catch (Exception& ex)
383     {
384       DEBTRACE("CommandAddOutputPortFromCatalog::localExecute() : " << ex.what());
385       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
386       if (son) delete son;
387       _outputPort = 0;
388     }
389   return (_outputPort != 0);
390 }
391
392 bool CommandAddOutputPortFromCatalog::localReverse()
393 {
394 }
395
396 // ----------------------------------------------------------------------------
397
398 CommandAddIDSPortFromCatalog::CommandAddIDSPortFromCatalog(YACS::ENGINE::Catalog *catalog,
399                                                            std::string type,
400                                                            std::string node,
401                                                            std::string name)
402   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
403 {
404   _IDSPort = 0;
405 }
406
407 YACS::ENGINE::InputDataStreamPort *CommandAddIDSPortFromCatalog::getIDSPort()
408 {
409   return _IDSPort;
410 }
411
412 bool CommandAddIDSPortFromCatalog::localExecute()
413 {
414   InputDataStreamPort *son = 0;
415   try
416     {
417       Proc* proc = GuiContext::getCurrent()->getProc();
418       Node* node = proc->getChildByName(_node);
419       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
420       if (father)
421         {
422           if (_catalog->_typeMap.count(_typePort))
423             son = father->edAddInputDataStreamPort(_name, _catalog->_typeMap[_typePort]);
424           else
425             {
426               DEBTRACE(_typePort << " not found in catalog");
427               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
428             }
429         }
430       _IDSPort = son;
431     }
432   catch (Exception& ex)
433     {
434       DEBTRACE("CommandAddIDSPortFromCatalog::localExecute() : " << ex.what());
435       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
436       if (son) delete son;
437       _IDSPort = 0;
438     }
439   return (_IDSPort != 0);
440 }
441
442 bool CommandAddIDSPortFromCatalog::localReverse()
443 {
444 }
445  
446 // ----------------------------------------------------------------------------
447
448 CommandAddODSPortFromCatalog::CommandAddODSPortFromCatalog(YACS::ENGINE::Catalog *catalog,
449                                                            std::string type,
450                                                            std::string node,
451                                                            std::string name)
452   : Command(), _catalog(catalog), _typePort(type), _node(node), _name(name)
453 {
454   _ODSPort = 0;
455 }
456
457 YACS::ENGINE::OutputDataStreamPort *CommandAddODSPortFromCatalog::getODSPort()
458 {
459   return _ODSPort;
460 }
461
462 bool CommandAddODSPortFromCatalog::localExecute()
463 {
464   OutputDataStreamPort *son = 0;
465   try
466     {
467       Proc* proc = GuiContext::getCurrent()->getProc();
468       Node* node = proc->getChildByName(_node);
469       ElementaryNode* father =dynamic_cast<ElementaryNode*> (node);
470       if (father)
471         {
472           if (_catalog->_typeMap.count(_typePort))
473             son = father->edAddOutputDataStreamPort(_name, _catalog->_typeMap[_typePort]);
474           else
475             {
476               DEBTRACE(_typePort << " not found in catalog");
477               GuiContext::getCurrent()->_lastErrorMessage = _typePort + " not found in catalog";
478             }
479         }
480       _ODSPort = son;
481     }
482   catch (Exception& ex)
483     {
484       DEBTRACE("CommandAddODSPortFromCatalog::localExecute() : " << ex.what());
485       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
486       if (son) delete son;
487       _ODSPort = 0;
488     }
489   return (_ODSPort != 0);
490 }
491
492 bool CommandAddODSPortFromCatalog::localReverse()
493 {
494 }
495
496 // ----------------------------------------------------------------------------
497
498 CommandDestroy::CommandDestroy(std::string position,  Subject* subject)
499   : Command(), _position(position), _subject(subject)
500 {
501   DEBTRACE("CommandDestroy::CommandDestroy");
502   //todo: store what is needed to recreate the subject
503 }
504
505 bool CommandDestroy::localExecute()
506 {
507   DEBTRACE("CommandDestroy::localExecute");
508   try
509     {
510       Subject::erase(_subject);
511       _subject = 0;
512       return true; 
513     }
514   catch (Exception& ex)
515     {
516       DEBTRACE("CommandDestroy::localExecute() : " << ex.what());
517       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
518       return false;
519     }
520 }
521
522 bool CommandDestroy::localReverse()
523 {
524 }
525  
526 // ----------------------------------------------------------------------------
527
528 CommandAddLink::CommandAddLink(std::string outNode, std::string outPort,
529                                std::string inNode, std::string inPort)
530   : Command(), _outNode(outNode), _outPort(outPort), _inNode(inNode), _inPort(inPort)
531 {
532   DEBTRACE("CommandAddLink::CommandAddLink "<<outNode<<"."<<outPort<<"->"<<inNode<<"."<<inPort);
533 }
534
535 bool CommandAddLink::localExecute()
536 {
537   DEBTRACE(_outNode<<"."<<_outPort<<"->"<<_inNode<<"."<<_inPort);
538   try
539     {
540       Proc* proc = GuiContext::getCurrent()->getProc();
541       Node* outn = proc->getChildByName(_outNode);
542       Node* inn = proc->getChildByName(_inNode);
543       OutPort* outp = outn->getOutPort(_outPort);
544       InPort* inp = inn->getInPort(_inPort);
545       ComposedNode *cla = ComposedNode::getLowestCommonAncestor(outn->getFather(),inn->getFather());
546       DEBTRACE(cla->getName());
547       if (dynamic_cast<OutputDataStreamPort*>(outp))
548         cla->edAddLink(outp,inp);
549       else
550         cla->edAddDFLink(outp,inp);
551       return true;
552     }
553   catch (Exception& ex)
554     {
555       DEBTRACE("CommandAddLink::localExecute() : " << ex.what());
556       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
557       return false;
558     }
559 }
560
561 bool CommandAddLink::localReverse()
562 {
563 }
564
565 // ----------------------------------------------------------------------------
566
567 CommandAddControlLink::CommandAddControlLink(std::string outNode, std::string inNode)
568   : Command(), _outNode(outNode), _inNode(inNode)
569 {
570   DEBTRACE("CommandAddControlLink::CommandAddControlLink "<<outNode<<"-->>"<<inNode);
571 }
572
573 bool CommandAddControlLink::localExecute()
574 {
575   try
576     {
577       Proc* proc = GuiContext::getCurrent()->getProc();
578       Node* outn = proc;
579       if (! _outNode.empty())
580         outn = proc->getChildByName(_outNode);
581       Node* inn = proc;
582       if (! _inNode.empty())
583         inn = proc->getChildByName(_inNode);
584       ComposedNode *cla = ComposedNode::getLowestCommonAncestor(outn,inn);
585       DEBTRACE(cla->getName());
586       cla->edAddCFLink(outn,inn);
587       return true;
588     }
589   catch (Exception& ex)
590     {
591       DEBTRACE("CommandAddControlLink::localExecute() : " << ex.what());
592       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
593       return false;
594     }
595 }
596
597 bool CommandAddControlLink::localReverse()
598 {
599 }
600
601 // ----------------------------------------------------------------------------
602
603 CommandAddContainer::CommandAddContainer(std::string name,
604                                          std::string refContainer)
605   : Command(), _name(name), _containerToClone(refContainer), _container(0)
606 {
607   DEBTRACE("CommandAddContainer::CommandAddContainer " << name << " " << refContainer);
608 }
609
610 bool CommandAddContainer::localExecute()
611 {
612   try
613     {
614       Proc* proc = GuiContext::getCurrent()->getProc();
615       if (proc->containerMap.count(_name))
616         {
617           GuiContext::getCurrent()->_lastErrorMessage = "There is already a container with that name";
618           return false;
619         }
620       Container *container = new SalomeContainer();
621       if (! _containerToClone.empty())
622         {
623           if (proc->containerMap.count(_containerToClone))
624             {
625               Container *ref = proc->containerMap[_containerToClone];
626               assert(ref);
627               container->setProperties(ref->getProperties());
628             }
629           else
630             {
631               GuiContext::getCurrent()->_lastErrorMessage = "There is no reference container to clone properties";
632               return false;
633             }
634         }
635       _container = container;
636       _container->setName(_name);
637       proc->containerMap[_name] = _container;
638       return true;
639     }
640   catch (Exception& ex)
641     {
642       DEBTRACE("CommandAddContainer::localExecute() : " << ex.what());
643       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
644       return false;
645     }
646 }
647
648 bool CommandAddContainer::localReverse()
649 {
650 }
651
652 YACS::ENGINE::Container* CommandAddContainer::getContainer()
653 {
654   return _container;
655 }
656
657 // ----------------------------------------------------------------------------
658
659 CommandSetContainerProperties::CommandSetContainerProperties(std::string container,
660                                                              std::map<std::string,std::string> properties)
661   : Command(), _container(container), _properties(properties)
662 {
663   DEBTRACE("CommandSetContainerProperties::CommandSetContainerProperties " << container);
664 }
665
666 bool CommandSetContainerProperties::localExecute()
667 {
668   try
669     {
670       Proc* proc = GuiContext::getCurrent()->getProc();
671       if (proc->containerMap.count(_container))
672         {
673           Container *ref = proc->containerMap[_container];
674           assert(ref);
675           ref->setProperties(_properties);
676           return true;
677         }
678       GuiContext::getCurrent()->_lastErrorMessage = "container not found: " + _container;
679       return false;
680     }
681   catch (Exception& ex)
682     {
683       DEBTRACE("CommandSetContainerProperties::localExecute() : " << ex.what());
684       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
685       return false;
686     }
687 }
688
689 bool CommandSetContainerProperties::localReverse()
690 {
691 }
692
693 // ----------------------------------------------------------------------------
694
695 CommandSetDSPortProperties::CommandSetDSPortProperties(std::string node, std::string port, bool isInport,
696                                                        std::map<std::string,std::string> properties)
697   : Command(), _nodeName(node), _portName(port), _isInport(isInport), _properties(properties)
698 {
699   DEBTRACE("CommandSetDSPortProperties::CommandSetDSPortProperties " << node << "." << port << " " << isInport);
700 }
701       
702 bool CommandSetDSPortProperties::localExecute()
703 {
704   try
705     {
706       Proc* proc = GuiContext::getCurrent()->getProc();
707       Node* node = proc->getChildByName(_nodeName);
708       DataStreamPort* DSPort = 0;
709       if (_isInport)
710         DSPort = node->getInputDataStreamPort(_portName);
711       else
712         DSPort = node->getOutputDataStreamPort(_portName);
713       DSPort->setProperties(_properties);
714       return true;
715     }
716   catch (Exception& ex)
717     {
718       DEBTRACE("CommandSetDSPortProperties::localExecute() : " << ex.what());
719       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
720       return false;
721     }
722 }
723       
724 bool CommandSetDSPortProperties::localReverse()
725 {
726 }
727
728 // ----------------------------------------------------------------------------
729
730 CommandSetFuncNodeFunctionName::CommandSetFuncNodeFunctionName(std::string node, std::string funcName)
731   : Command(), _nodeName(node), _funcName(funcName)
732 {
733   DEBTRACE("CommandSetFuncNodeFunctionName::CommandSetFuncNodeFunctionName " << node << " " <<funcName);
734 }
735
736 bool CommandSetFuncNodeFunctionName::localExecute()
737 {
738   try
739     {
740       Proc* proc = GuiContext::getCurrent()->getProc();
741       Node* node = proc->getChildByName(_nodeName);
742       if (_funcName.empty())
743         {
744           GuiContext::getCurrent()->_lastErrorMessage = "InlineFuncNode function name empty: " + _nodeName;
745           return false;
746         }
747       if (YACS::ENGINE::InlineFuncNode* funcNode = dynamic_cast<YACS::ENGINE::InlineFuncNode*>(node))
748         {
749           funcNode->setFname(_funcName);
750           return true;
751         }
752       else
753         {
754           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineFuncNode: " + _nodeName;
755           return false;
756         }
757     }
758   catch (Exception& ex)
759     {
760       DEBTRACE("CommandSetFuncNodeFunctionName::localExecute() : " << ex.what());
761       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
762       return false;
763     }  
764 }
765
766 bool CommandSetFuncNodeFunctionName::localReverse()
767 {
768 }
769
770 // ----------------------------------------------------------------------------
771
772 CommandSetInlineNodeScript::CommandSetInlineNodeScript(std::string node, std::string script)
773   : Command(), _nodeName(node), _script(script)
774 {
775   DEBTRACE("CommandSetInlineNodeScript::CommandSetInlineNodeScript " << node << " " <<script);
776 }
777
778 bool CommandSetInlineNodeScript::localExecute()
779 {
780   try
781     {
782       Proc* proc = GuiContext::getCurrent()->getProc();
783       Node* node = proc->getChildByName(_nodeName);
784       if (_script.empty())
785         {
786           GuiContext::getCurrent()->_lastErrorMessage = "InlineNode script empty: " + _nodeName;
787           return false;
788         }
789       if (YACS::ENGINE::InlineNode* inlineNode = dynamic_cast<YACS::ENGINE::InlineNode*>(node))
790         {
791           inlineNode->setScript(_script);
792           return true;
793         }
794       else
795         {
796           GuiContext::getCurrent()->_lastErrorMessage = "node is not an InlineNode: " + _nodeName;
797           return false;
798         }
799     }
800   catch (Exception& ex)
801     {
802       DEBTRACE("CommandSetInlineNodeScript::localExecute() : " << ex.what());
803       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
804       return false;
805     }  
806 }
807
808 bool CommandSetInlineNodeScript::localReverse()
809 {
810 }
811
812 // ----------------------------------------------------------------------------
813
814 CommandAddComponentInstance::CommandAddComponentInstance(std::string compoName)
815   : Command(), _compoName(compoName), _compoInst(0)
816 {
817   DEBTRACE("CommandAddComponentInstance::CommandAddComponentInstance " <<_compoName);
818 }
819
820 bool CommandAddComponentInstance::localExecute()
821 {
822   try
823     {
824       Proc* proc = GuiContext::getCurrent()->getProc();
825       _compoInst = new SalomeComponent(_compoName);
826       pair<string,int> key = pair<string,int>(_compoName, _compoInst->getNumId());
827       proc->componentInstanceMap[key] = _compoInst;
828       return true;
829     }
830   catch (Exception& ex)
831     {
832       DEBTRACE("CommandAddComponentInstance::localExecute() : " << ex.what());
833       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
834       return false;
835     }
836 }
837
838 bool CommandAddComponentInstance::localReverse()
839 {
840 }
841
842 YACS::ENGINE::ComponentInstance* CommandAddComponentInstance::getComponentInstance()
843 {
844   return _compoInst;
845 }
846
847   
848 // ----------------------------------------------------------------------------
849
850 CommandAssociateComponentToContainer::CommandAssociateComponentToContainer(std::pair<std::string,int> key,
851                                                                            std::string container)
852   : Command(), _key(key), _container(container)
853 {
854   DEBTRACE("CommandAssociateComponentToContainer::CommandAssociateComponentToContainer "
855            << key.first << " " << key.second << " " << container);
856 }
857
858 bool CommandAssociateComponentToContainer::localExecute()
859 {
860   try
861     {
862       Proc* proc = GuiContext::getCurrent()->getProc();
863       if (proc->containerMap.count(_container))
864         {
865           Container *cont = proc->containerMap[_container];
866           if (proc->componentInstanceMap.count(_key))
867             {
868               ComponentInstance *compo = proc->componentInstanceMap[_key];
869               compo->setContainer(cont);
870               return true;
871             }
872           else
873             GuiContext::getCurrent()->_lastErrorMessage = "Component instance not found: " + _key.first;
874         }
875       else
876         GuiContext::getCurrent()->_lastErrorMessage = "Container not found: " + _container;
877       return false;
878     }
879   catch (Exception& ex)
880     {
881       DEBTRACE("CommandAssociateComponentToContainer::localExecute() : " << ex.what());
882       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
883       return false;
884     }
885 }
886
887 bool CommandAssociateComponentToContainer::localReverse()
888 {
889 }
890
891 // ----------------------------------------------------------------------------
892
893 CommandAssociateServiceToComponent::CommandAssociateServiceToComponent(std::string service,
894                                                                        std::pair<std::string,int> key)
895   : Command(), _service(service), _key(key)
896 {
897   DEBTRACE("CommandAssociateServiceToComponent::CommandAssociateServiceToComponent "
898            << service << " " << key.first << " " << key.second);
899 }
900
901 bool CommandAssociateServiceToComponent::localExecute()
902 {
903   try
904     {
905       Proc* proc = GuiContext::getCurrent()->getProc();
906       if (_service == proc->getName()) return false; // proc is not an elementary node
907       Node* node = proc->getChildByName(_service);
908       if (ServiceNode *service = dynamic_cast<ServiceNode*>(node))
909         {
910           if (proc->componentInstanceMap.count(_key))
911             {
912               ComponentInstance *compo = proc->componentInstanceMap[_key];
913               service->setComponent(compo);
914               return true;
915             }
916           else
917             GuiContext::getCurrent()->_lastErrorMessage = "Component instance not found: " + _key.first;
918         }
919       else
920         GuiContext::getCurrent()->_lastErrorMessage = "Node is note a service node: " + _service;
921       return false;
922     }
923   catch (Exception& ex)
924     {
925       DEBTRACE("CommandAssociateServiceToComponent::localExecute() : " << ex.what());
926       GuiContext::getCurrent()->_lastErrorMessage = ex.what();
927       return false;
928     }
929 }
930
931 bool CommandAssociateServiceToComponent::localReverse()
932 {
933 }
934
935 // ----------------------------------------------------------------------------