Salome HOME
Merge from OCC_development_generic_2006
[modules/superv.git] / src / Supervision / Graph_Impl.cxx
1 //  SUPERV Supervision : contains the implementation of interfaces of SuperVision described in SUPERV.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : Graph_Impl.cxx
25 //  Author : Jean Rahuel
26 //  Module : SUPERV
27 //  $Header: 
28
29 using namespace std;
30 #include <stdio.h>
31 #include <fstream>
32 //#include <sstream>
33 #include <string>
34
35 //#include "utilities.h"
36
37 #include "SALOME_Container_i.hxx"
38
39 #include "StreamGraph_Impl.hxx"
40
41 #include "DataFlowEditor_DataFlow.hxx"
42
43 extern GraphExecutor::FiniteStateMachine * theAutomaton ;
44
45 static void CreateEditor( CORBA::ORB_ptr orb ,
46                           const char *instanceName ,
47                           const char *aDataFlowName ,
48                           const SUPERV::KindOfNode aKindOfNode ,
49                           string & dbgfile ,
50                           GraphEditor::DataFlow **  aDataFlowEditor ) {
51   bool aXmlFile = false ;
52   int lenname = strlen( aDataFlowName ) ;
53   char * theDataFlowName = new char [ lenname+1 ] ;
54   strcpy( theDataFlowName , aDataFlowName ) ;
55   if ( aDataFlowName ) {
56     if ( lenname > 4 && !strcmp( &aDataFlowName[ lenname - 4 ] , ".xml" ) ) {
57       strncpy( theDataFlowName , &aDataFlowName[ 0 ] , lenname-4 ) ;
58       theDataFlowName[ lenname-4 ] = '\0' ;
59       aXmlFile = true ;
60       int i ;
61       for ( i = lenname - 5 ; i >= 0 ; i-- ) {
62         if ( aDataFlowName[ i ] == '/' ) {
63           strncpy( theDataFlowName , &aDataFlowName[ i + 1 ] , lenname-5-i ) ;
64           theDataFlowName[ lenname-5-i ] = '\0' ;
65           break ;
66         }
67       }
68     }
69     else {
70       strcpy( theDataFlowName , &aDataFlowName[ 0 ] ) ;
71     }
72   }
73   
74   string theDataFlowInstanceName = theDataFlowName ;
75
76   // asv : 16.11.04 : creation of log file in /tmp/logs/$USER dir. 
77   // "/tmp/logs/$USER" was created by  runSalome.py -> orbmodule.py.
78   dbgfile = "/tmp/logs/" ;
79   dbgfile += getenv( "USER" ) ;
80   dbgfile += "/" ;
81   dbgfile += instanceName ;
82   dbgfile += "_" ;
83   dbgfile += theDataFlowInstanceName ;
84   dbgfile = dbgfile + "_" + theAutomaton->DbgFileNumber() + ".log" ;
85   FILE* f = fopen ( dbgfile.c_str(), "a" );
86   if ( f ) { // check if file can be opened for writing
87     fclose( f );
88   } 
89   else { // if file can't be opened - use a guaranteed temp file name
90     char* aTempNam = tempnam( NULL, NULL );
91     dbgfile = aTempNam;
92     free ( aTempNam );
93   }
94
95   SALOME_NamingService * NamingService = new SALOME_NamingService( orb ) ;
96   *aDataFlowEditor = new GraphEditor::DataFlow( orb , NamingService ,
97                                                 theDataFlowInstanceName.c_str() , dbgfile.c_str() ,
98                                                 aKindOfNode ) ;
99   MESSAGE( "CreateEditor " << theDataFlowName << " uniquely named " << theDataFlowInstanceName << " created with "
100            << dbgfile.c_str() ) ;
101
102   delete [] theDataFlowName ;
103 }
104
105 static void CreateExecutor( CORBA::ORB_ptr orb ,
106                           const char *instanceName ,
107                           const char *aDataFlowName ,
108                           const SUPERV::KindOfNode aKindOfNode ,
109                           string & dbgfile ,
110                           GraphExecutor::DataFlow **  aDataFlowExecutor ) {
111   int lenname = strlen( aDataFlowName ) ;
112   char * theDataFlowName = new char [ lenname+1 ] ;
113   strcpy( theDataFlowName , aDataFlowName ) ;
114   if ( aDataFlowName ) {
115     strcpy( theDataFlowName , &aDataFlowName[ 0 ] ) ;
116   }
117
118   string theDataFlowInstanceName = theDataFlowName ;
119
120   // asv : 16.11.04 : creation of log file in /tmp/logs/$USER dir. 
121   // "/tmp/logs/$USER" was created by  runSalome.py -> orbmodule.py.
122   dbgfile = "/tmp/logs/" ;
123   dbgfile += getenv( "USER" ) ;
124   dbgfile += "/" ;
125   dbgfile += instanceName ;
126   dbgfile += "_" ;
127   dbgfile += theDataFlowInstanceName ;
128   dbgfile = dbgfile + "_" + theAutomaton->DbgFileNumber() ;
129   ostringstream astr ;
130   astr << theAutomaton->ExecNumber() ;
131   dbgfile += astr.str() ;
132   dbgfile += string( "_Exec.log" ) ;
133   FILE* f = fopen ( dbgfile.c_str(), "a" );
134   if ( f ) { // check if file can be opened for writing
135     fclose( f );
136   } 
137   else { // if file can't be opened - use a guaranteed temp file name
138     char* aTempNam = tempnam( NULL, NULL );
139     dbgfile = aTempNam;
140     free ( aTempNam );
141   }
142
143   SALOME_NamingService * NamingService = new SALOME_NamingService( orb ) ;
144   *aDataFlowExecutor = new GraphExecutor::DataFlow( orb , NamingService ,
145                                                     theDataFlowInstanceName.c_str() , dbgfile.c_str() ,
146                                                     aKindOfNode ) ;
147
148   MESSAGE( "CreateExecutor " << theDataFlowName << " " << theDataFlowInstanceName << " created with "
149            << dbgfile.c_str() ) ;
150
151   delete [] theDataFlowName ;
152 }
153
154 Graph_Impl::Graph_Impl( CORBA::ORB_ptr orb ,
155                         PortableServer::POA_ptr poa ,
156                         PortableServer::ObjectId * contId , 
157                         const char *instanceName ,
158                         const char *interfaceName ,
159                         const char *aDataFlowName ,
160                         const SUPERV::KindOfNode aKindOfNode ) :
161   GNode_Impl( orb , poa , contId , instanceName , interfaceName , aDataFlowName , aKindOfNode ) {
162 //  MESSAGE("Graph_Impl::Graph_Impl activate object instanceName("
163 //          << instanceName << ") interfaceName(" << interfaceName << ") --> "
164 //          << hex << (void *) this << dec )
165 //  beginService( "Graph_Impl::Graph_Impl" );
166   _Orb = CORBA::ORB::_duplicate(orb);
167   _Poa = poa ;
168   _ContId = contId ;
169   _DebugFileName = NULL ;
170   if ( aKindOfNode == SUPERV::DataFlowGraph || aKindOfNode == SUPERV::MacroNode ) {
171 //    MESSAGE( "Graph_Impl::Graph_Impl _poa->activate_object" );
172     _thisObj = this ;
173     _id = _poa->activate_object(_thisObj);
174   }
175   else {
176 //    MESSAGE( "Graph_Impl::Graph_Impl NO _poa->activate_object " );
177   }
178
179   if ( aKindOfNode == SUPERV::DataFlowGraph || aKindOfNode == SUPERV::DataStreamGraph ) {
180     string dbgfile ;
181     GraphEditor::DataFlow * aDataFlowEditor ;
182     CreateEditor( orb , instanceName , aDataFlowName , aKindOfNode ,
183                   dbgfile , &aDataFlowEditor ) ;
184
185     if ( _DebugFileName ) {
186       delete [] _DebugFileName ;
187     }
188     _DebugFileName = new char[ strlen( dbgfile.c_str() )+1 ] ;
189     strcpy( _DebugFileName , dbgfile.c_str() ) ;
190
191     _NamingService = new SALOME_NamingService( orb ) ;
192     DataFlowEditor( aDataFlowEditor ) ;
193     DataFlowEditor()->Graph()->GraphEditor( aDataFlowEditor ) ;
194     DataFlowEditor()->Graph()->SetObjImpl( this ) ;
195   }
196   pthread_mutex_init( &_MutexExecutorWait , NULL ) ;
197 //  DataFlowExecutor( NULL ) ;
198 //  endService( "Graph_Impl::Graph_Impl" );
199 }
200
201 Graph_Impl::Graph_Impl( CORBA::ORB_ptr orb ,
202                         PortableServer::POA_ptr poa ,
203                         PortableServer::ObjectId * contId , 
204                         const char *instanceName ,
205                         const char *interfaceName ,
206                         GraphEditor::DataFlow * aDataFlowEditor ,
207                         GraphEditor::InNode * aDataFlowNode ) :
208   GNode_Impl( orb , poa , contId , instanceName , interfaceName , aDataFlowEditor , aDataFlowNode ) {
209 //  beginService( "Graph_Impl::Graph_Impl" );
210 //  MESSAGE( aDataFlowEditor->Graph()->Name() << " " );
211   if ( aDataFlowEditor->Graph()->IsDataFlowNode() ||
212        ( aDataFlowNode && aDataFlowNode->IsMacroNode() ) ) {
213 //    MESSAGE( "Graph_Impl::Graph_Impl _poa->activate_object" );
214     _thisObj = this ;
215     _id = _poa->activate_object(_thisObj);
216   }
217   else {
218 //    MESSAGE( "Graph_Impl::Graph_Impl NO _poa->activate_object " );
219   }
220   _Orb = CORBA::ORB::_duplicate(orb);
221   _Poa = poa ;
222   _ContId = contId ;
223   _DebugFileName = NULL ;
224   DataFlowEditor( aDataFlowEditor ) ;
225   DataFlowEditor()->Graph()->GraphEditor( aDataFlowEditor ) ;
226   DataFlowEditor()->Graph()->SetObjImpl( this ) ;
227   pthread_mutex_init( &_MutexExecutorWait , NULL ) ;
228 //  DataFlowExecutor( NULL ) ;
229 //  endService( "Graph_Impl::Graph_Impl" );  
230 }
231
232 Graph_Impl::Graph_Impl() {
233 }
234
235 Graph_Impl::~Graph_Impl() {
236   beginService( "Graph_Impl::~Graph_Impl" );
237   endService( "Graph_Impl::~Graph_Impl" );
238 }
239
240 void Graph_Impl::destroy() {
241   beginService( "Graph_Impl::destroy" );
242   if ( DataFlowNode() && DataFlowNode()->ComputingNode()->IsMacroNode() ) {
243     SUPERV::Graph_var aGraph = DataFlowNode()->GOTONode()->MacroObject() ;
244     GNode_Impl::Delete() ;
245     if ( !CORBA::is_nil( aGraph ) ) {
246       aGraph->destroy() ;
247     }
248   }
249   else {
250     SUPERV::Graph_var aGraph = DataFlowEditor()->Graph()->MacroObject() ;
251     GNode_Impl::Delete() ;
252     if ( !CORBA::is_nil( aGraph ) ) {
253       aGraph->destroy() ;
254     }
255   }
256   _poa->deactivate_object(*_id) ;
257 //  CORBA::release(_poa) ;
258   delete(_id) ;
259 //  _thisObj->_remove_ref();
260   endService( "Graph_Impl::destroy" );
261 }
262
263
264 char* Graph_Impl::getIOR() {
265   if ( IsMacro() ) {
266     return CORBA::string_dup( NULLSTRING ) ;
267   }
268   else if ( CORBA::is_nil( myServant ) ) {
269     PortableServer::ObjectId* id = getId();
270     CORBA::Object_var obj;
271     obj = _poa->id_to_reference(*id);
272     myServant = SUPERV::Graph::_narrow(obj);
273   }
274   return(CORBA::string_dup(_Orb->object_to_string(myServant)));
275 }
276
277 SUPERV::Graph_ptr Graph_Impl::Copy() {
278   beginService( "Graph_Impl::Copy" );
279   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
280   if ( !IsMacro() ) {
281     Graph_Impl * myGraph ;
282     myGraph = new Graph_Impl( _Orb , _Poa, _ContId,
283                               instanceName() , interfaceName() ,
284                               DataFlowEditor()->Graph()->Name() , SUPERV::DataFlowGraph ) ;
285     PortableServer::ObjectId * id = myGraph->getId() ;
286     CORBA::Object_var obj = _poa->id_to_reference(*id);
287     iobject = SUPERV::Graph::_narrow(obj) ;
288     GraphBase::ListOfSGraphs * aListOfSGraphs = GetGraphs() ;
289     myGraph->LoadGraphs( aListOfSGraphs ) ;
290   }
291   endService( "Graph_Impl::Copy" );
292   return SUPERV::Graph::_duplicate(iobject) ;
293 }
294
295 void Graph_Impl::ReadOnly() {
296   DataFlowEditor()->ReadOnly() ;
297 }
298
299 SUPERV::INode_ptr Graph_Impl::Node() {
300 //  beginService( "Graph_Impl::Node" );
301   PortableServer::ObjectId * id = getId() ;
302   CORBA::Object_var obj = _poa->id_to_reference(*id);
303   SUPERV::Graph_var iobject ;
304   iobject = SUPERV::Graph::_narrow(obj) ;
305 //  endService( "Graph_Impl::Node" );
306   return SUPERV::Graph::_duplicate(iobject) ;
307 }
308
309 GraphBase::ListOfSGraphs * Graph_Impl::GetGraphs() {
310   return DataFlowEditor()->GetDataFlows() ;
311 }
312
313 bool Graph_Impl::LoadGraphs( GraphBase::ListOfSGraphs * aListOfDataFlows ) {
314   beginService( "Graph_Impl::LoadGraphs" );
315   bool RetVal = false ;
316   if ( DataFlowEditor()->IsEditing() ) {
317 //    RetVal = DataFlowEditor()->LoadDataFlows( aListOfDataFlows ) ;
318     if ( CORBA::is_nil( LoadDataFlows( DataFlowEditor() , aListOfDataFlows , 0 ) ) ) {
319       RetVal = false ;
320     }
321   }
322   endService( "Graph_Impl::LoadGraphs" );
323   return RetVal ;
324 }
325
326 bool Graph_Impl::Import(const char * aXmlFile ) {
327   beginService( "Graph_Impl::Import" );
328   bool RetVal = false ;
329   if ( DataFlowEditor()->IsEditing() && !IsMacro() ) {
330     GraphBase::ListOfSGraphs aListOfDataFlows ;
331     RetVal = DataFlowEditor()->LoadXml( aXmlFile , aListOfDataFlows ) ;
332     if ( RetVal && aXmlFile != NULL ) {
333 //      RetVal = DataFlowEditor()->LoadDataFlows( &aListOfDataFlows ) ;
334       if ( CORBA::is_nil( LoadDataFlows( DataFlowEditor() , &aListOfDataFlows , 0 ) ) ) {
335         RetVal = false ;
336       }
337     }
338   }
339   endService( "Graph_Impl::Import" );
340   return RetVal ;
341 }
342
343 bool Graph_Impl::Export(const char * anXmlFile ) {
344   beginService( "Graph_Impl::Export" );
345   bool RetVal = false ;
346   if ( !IsMacro() ) {
347     char * aFile = new char [ strlen( anXmlFile ) + 5 ] ;
348     strcpy( aFile , anXmlFile ) ;
349     int len = strlen( aFile ) ;
350     if ( !strcmp( &aFile[ len - 4 ] , ".xml" ) ) {
351     }
352     else if ( !strcmp( &aFile[ len - 3 ] , ".py" ) ) {
353       strcpy( &aFile[ len - 3 ] , ".xml" ) ;
354       len = strlen( aFile ) ;
355     }
356     else {
357       strcat( aFile , ".xml" ) ;
358       len = strlen( aFile ) ;
359     }
360     RetVal = DataFlowEditor()->SaveXml( aFile ) ;
361     if ( RetVal ) {
362       strcpy( &aFile[ len - 4 ] , ".py" ) ;
363       RetVal = DataFlowEditor()->SavePy( aFile ) ;
364     }
365     delete [] aFile ;
366   }
367   endService( "Graph_Impl::Export" );
368   return RetVal ;
369 }
370
371 char *  Graph_Impl::SavePY( bool importSuperV ) {
372   beginService( "Graph_Impl::SavePY" );
373   ostringstream fstring ;
374   bool RetVal ;
375   RetVal = DataFlowEditor()->SavePY( fstring , importSuperV ) ;
376   fstring  << ends ;
377   endService( "Graph_Impl::SavePY" );
378   return CORBA::string_dup( fstring.str().c_str() );
379 }
380 SUPERV::CNode_ptr Graph_Impl::CNode( const SALOME_ModuleCatalog::Service &NodeService ) {
381   beginService( "Graph_Impl::CNode" );
382   SUPERV::CNode_var iobject = SUPERV::CNode::_nil() ;
383   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() && !IsMacro() ) {
384     CNode_Impl * myNode = new CNode_Impl( _Orb , _Poa , _ContId ,
385                                           instanceName() , interfaceName() ,
386                                           DataFlowEditor() ,
387                                           NodeService , NULLSTRING ,
388                                           SUPERV::ComputingNode , NULLSTRING ) ;
389     if ( myNode->DataFlowNode() ) {
390       PortableServer::ObjectId * id = myNode->getId() ;
391       CORBA::Object_var obj = _poa->id_to_reference(*id);
392       iobject = SUPERV::CNode::_narrow(obj) ;
393       myNode->SetObjRef( SUPERV::CNode::_duplicate( iobject ) ) ;
394     }
395   }
396   DataFlowEditor()->UnValid() ;
397   endService( "Graph_Impl::CNode" );
398   return SUPERV::CNode::_duplicate( iobject ) ;
399 }
400
401 SUPERV::FNode_ptr Graph_Impl::FNode( const char * NodeComponentName ,
402                                      const char * NodeInterfaceName ,
403                                      const SALOME_ModuleCatalog::Service &NodeService ,
404                                      bool isCimpl ) {
405   beginService( "Graph_Impl::FNode" );
406   SUPERV::FNode_var iobject = SUPERV::FNode::_nil() ;
407   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() && !IsMacro() ) {
408     FNode_Impl * myNode = new FNode_Impl( _Orb , _Poa , _ContId ,
409                                           instanceName() , interfaceName() ,
410                                           DataFlowEditor() ,
411                                           NodeService ,
412                                           NodeComponentName ,
413                                           NodeInterfaceName ,
414                                           NULLSTRING ,
415                                           SUPERV::FactoryNode ,
416                                           isCimpl ) ;
417     if ( myNode->DataFlowNode() ) {
418       PortableServer::ObjectId * id = myNode->getId() ;
419       CORBA::Object_var obj = _poa->id_to_reference(*id);
420       iobject = SUPERV::FNode::_narrow(obj) ;
421       myNode->SetObjRef( SUPERV::FNode::_duplicate( iobject ) ) ;
422     }
423   }
424   DataFlowEditor()->UnValid() ;
425   endService( "Graph_Impl::FNode" );
426   return SUPERV::FNode::_duplicate( iobject ) ;
427 }
428
429 SUPERV::INode_ptr Graph_Impl::INode( const char * FuncName ,
430                                      const SUPERV::ListOfStrings & PythonFuntion ) {
431   beginService( "Graph_Impl::INode" );
432   SUPERV::INode_var iobject = SUPERV::INode::_nil() ;
433   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() && !IsMacro() ) {
434     INode_Impl * myNode = new INode_Impl( _Orb , _Poa , _ContId ,
435                                           instanceName() , interfaceName() ,
436                                           DataFlowEditor() ,
437                                           FuncName , PythonFuntion ,
438                                           SUPERV::InLineNode ) ;
439     if ( myNode->DataFlowNode() ) {
440       PortableServer::ObjectId * id = myNode->getId() ;
441       CORBA::Object_var obj = _poa->id_to_reference(*id);
442       iobject = SUPERV::INode::_narrow(obj) ;
443       myNode->SetObjRef( SUPERV::INode::_duplicate( iobject ) ) ;
444     }
445   }
446   endService( "Graph_Impl::INode" );
447   return SUPERV::INode::_duplicate( iobject ) ;
448 }
449
450
451 SUPERV::GNode_ptr Graph_Impl::GNode( const char * FuncName ,
452                                      const SUPERV::ListOfStrings & PythonFuntion ,
453                                      const char * anInLineNode ) {
454   beginService( "Graph_Impl::GNode" );
455   SUPERV::GNode_var iobject = SUPERV::GNode::_nil() ;
456   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() && !IsMacro() ) {
457     GNode_Impl * myNode = new GNode_Impl( _Orb , _Poa , _ContId ,
458                                           instanceName() , interfaceName() ,
459                                           DataFlowEditor() ,
460                                           FuncName , PythonFuntion ,
461                                           SUPERV::GOTONode ) ;
462     if ( myNode->DataFlowNode() ) {
463       PortableServer::ObjectId * id = myNode->getId() ;
464       CORBA::Object_var obj = _poa->id_to_reference(*id);
465       iobject = SUPERV::GNode::_narrow(obj) ;
466       myNode->SetObjRef( SUPERV::GNode::_duplicate( iobject ) ) ;
467       if ( strlen( anInLineNode ) ) {
468         GraphBase::InLineNode * CoupledINode = (GraphBase::InLineNode * ) DataFlowEditor()->Graph()->GetGraphNode( anInLineNode ) ;
469         if ( anInLineNode ) {
470           myNode->SetCoupled( anInLineNode ) ;
471           Link( myNode->Port( "OutGate" ) , CoupledINode->ObjRef()->Port( "InGate" ) ) ;
472         }
473       }
474     }
475   }
476   DataFlowEditor()->UnValid() ;
477   endService( "Graph_Impl::GNode" );
478   return SUPERV::GNode::_duplicate( iobject ) ;
479 }
480
481 SUPERV::LNode_ptr Graph_Impl::LNode( const char * InitName ,
482                                      const SUPERV::ListOfStrings & InitFunction ,
483                                      const char * MoreName ,
484                                      const SUPERV::ListOfStrings & MoreFunction ,
485                                      const char * NextName ,
486                                      const SUPERV::ListOfStrings & NextFunction ,
487                                      SUPERV::INode_out anEndOfLoop ) {
488   beginService( "Graph_Impl::LNode" );
489   SUPERV::LNode_var iobject = SUPERV::LNode::_nil() ;
490   SUPERV::ELNode_var iendobject = SUPERV::ELNode::_nil() ;
491   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() && !IsMacro() ) {
492     GraphBase::ListOfFuncName FuncNameList ;
493     FuncNameList.resize(3) ;
494     FuncNameList[0] = InitName ;
495     FuncNameList[1] = MoreName ;
496     FuncNameList[2] = NextName ;
497     GraphBase::ListOfPythonFunctions PythonFunctionList ;
498     PythonFunctionList.resize(3) ;
499     PythonFunctionList[0] = &InitFunction ;
500     PythonFunctionList[1] = &MoreFunction ;
501     PythonFunctionList[2] = &NextFunction ;
502     LNode_Impl * myNode = new LNode_Impl( _Orb , _Poa , _ContId ,
503                                           instanceName() , interfaceName() ,
504                                           DataFlowEditor() ,
505                                           FuncNameList , PythonFunctionList ,
506 //                                          InitName , InitFunction ,
507 //                                          MoreName , MoreFunction ,
508 //                                          NextName , NextFunction ,
509                                           SUPERV::LoopNode ) ;
510     if ( myNode->DataFlowNode() ) {
511       PortableServer::ObjectId * id = myNode->getId() ;
512       CORBA::Object_var obj = _poa->id_to_reference(*id);
513       iobject = SUPERV::LNode::_narrow(obj) ;
514       myNode->SetObjRef( SUPERV::LNode::_duplicate( iobject ) ) ;
515
516       SALOME_ModuleCatalog::Service aVoidService = SALOME_ModuleCatalog::Service() ;
517       char * anEndName ;
518       if ( strlen( InitName ) ) {
519         anEndName = new char( 3 + strlen( InitName ) + 1 ) ;
520         strcpy( anEndName , "End" ) ;
521         strcat( anEndName , InitName ) ;
522       }
523       else {
524         anEndName = new char( 3 + strlen( "Loop" ) + 1 ) ;
525         strcpy( anEndName , "EndLoop" ) ;
526       }
527       ELNode_Impl * myEndNode = new ELNode_Impl( _Orb , _Poa , _ContId ,
528                                                  instanceName() , interfaceName() ,
529                                                  DataFlowEditor() ,
530                                                  anEndName ,
531                                                  SUPERV::EndLoopNode ) ;
532       PortableServer::ObjectId * endid = myEndNode->getId() ;
533       CORBA::Object_var endobj = _poa->id_to_reference(*endid);
534       iendobject = SUPERV::ELNode::_narrow(endobj) ;
535       myEndNode->SetObjRef( SUPERV::ELNode::_duplicate( iendobject ) ) ;
536       myNode->SetCoupled( myEndNode->BaseNode()->Name() ) ;
537       myEndNode->SetCoupled( myNode->BaseNode()->Name() ) ;
538 //JR 25.01.2005  That links are already done in SetCoupled above ...
539 //      Link( myNode->Port( "DoLoop" ) , myEndNode->Port( "DoLoop" ) ) ;
540 //      Link( myEndNode->Port( "DoLoop" ) , myNode->Port( "InitLoop" ) ) ;
541 //      Link( myEndNode->Port( "DoLoop" ) , myNode->Port( "DoLoop" ) ) ;
542     }
543   }
544   anEndOfLoop = SUPERV::ELNode::_duplicate( iendobject ) ;
545   DataFlowEditor()->UnValid() ;
546   endService( "Graph_Impl::LNode" );
547   return SUPERV::LNode::_duplicate( iobject ) ;
548 }
549
550 SUPERV::SNode_ptr Graph_Impl::SNode( const char * FuncName ,
551                                      const SUPERV::ListOfStrings & PythonFunction ,
552                                      SUPERV::INode_out anEndOfSwitch ) {
553   beginService( "Graph_Impl::SNode" );
554   SUPERV::SNode_var iobject = SUPERV::SNode::_nil() ;
555   SUPERV::ESNode_var iendobject = SUPERV::ESNode::_nil() ;
556   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() && !IsMacro() ) {
557     SNode_Impl * myNode = new SNode_Impl( _Orb , _Poa , _ContId ,
558                                           instanceName() , interfaceName() ,
559                                           DataFlowEditor() ,
560                                           FuncName , PythonFunction ,
561                                           SUPERV::SwitchNode ) ;
562     if ( myNode->DataFlowNode() ) {
563       PortableServer::ObjectId * id = myNode->getId() ;
564       CORBA::Object_var obj = _poa->id_to_reference(*id);
565       iobject = SUPERV::SNode::_narrow(obj) ;
566       myNode->SetObjRef( SUPERV::SNode::_duplicate( iobject ) ) ;
567
568       string anEndName = "End" ;
569       if ( strlen( FuncName ) ) {
570         anEndName += FuncName ;
571       }
572       else {
573         anEndName += "Switch" ;
574       }
575 //      cout << "Graph_Impl::SNode anEndName " << (void *) FuncName << " " << FuncName
576 //           << " " << strlen(FuncName) << " " << (void *) anEndName.c_str() << " "
577 //           << anEndName.c_str() << endl ;
578       ESNode_Impl * myEndNode = new ESNode_Impl( _Orb , _Poa , _ContId ,
579                                                instanceName() , interfaceName() ,
580                                                DataFlowEditor() ,
581                                                anEndName.c_str() ,
582                                                SUPERV::EndSwitchNode ) ;
583 //      cout << "Graph_Impl::SNode returned anEndName " << (void *) FuncName << " "
584 //           << FuncName << " " << strlen(FuncName) << " " << (void *) anEndName.c_str()
585 //           << " " << anEndName.c_str() << endl ;
586       PortableServer::ObjectId * endid = myEndNode->getId() ;
587       CORBA::Object_var endobj = _poa->id_to_reference(*endid);
588       iendobject = SUPERV::ESNode::_narrow(endobj) ;
589       myEndNode->SetObjRef( SUPERV::ESNode::_duplicate( iendobject ) ) ;
590       myNode->SetCoupled( myEndNode->BaseNode()->Name() ) ;
591       myEndNode->SetCoupled( myNode->BaseNode()->Name() ) ;
592       if ( !Link( myNode->Port( "Default" ) , myEndNode->Port( "Default" ) ) ) {
593         iobject = SUPERV::SNode::_nil() ;
594         iendobject = SUPERV::ESNode::_nil() ;
595       }
596     }
597   }
598   anEndOfSwitch = SUPERV::ESNode::_duplicate( iendobject ) ;
599   DataFlowEditor()->UnValid() ;
600   endService( "Graph_Impl::SNode" );
601   return SUPERV::SNode::_duplicate( iobject ) ;
602 }
603
604 // WARNING : THIS IS COMPLICATED :
605 SUPERV::Graph_var Graph_Impl::LoadDataFlows( GraphEditor::DataFlow * aDataFlowEditor ,
606                                              GraphBase::ListOfSGraphs * aListOfDataFlows ,
607                                              int index ) {
608   beginService( "Graph_Impl::EditorLoadDataFlows" ) ;
609   MESSAGE("Graph_Impl::LoadDataFlows index " << index << " " << (*aListOfDataFlows)[index].Info.theName.c_str() ) ;
610
611   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
612 // That method is recursive :
613 // At first we load the supergraph with index = 0 :
614 // (After we load the graph corresponding to each MacroNode :)
615   if ( !aDataFlowEditor->LoadDataFlow( &(*aListOfDataFlows)[ index ] ) ) {
616     MESSAGE("Graph_Impl::LoadDataFlows failed " << (*aListOfDataFlows)[index].Info.theName.c_str() ) ;
617     return SUPERV::Graph::_duplicate( iobject ) ;
618   }
619 // That graph is not a StreamGraph :
620   else if ( !aDataFlowEditor->Graph()->HasDataStream() ) {
621     aDataFlowEditor->Graph()->Kind( SUPERV::DataFlowGraph ) ;
622 //    aDataFlowEditor->Graph()->SetGraphEditor( aDataFlowEditor ) ;
623     Graph_Impl * myGraph ;
624 // We create a Graph ObjRef for that graph
625     myGraph = new Graph_Impl( _Orb , _Poa, _ContId,
626                               instanceName() , interfaceName() ,
627                               aDataFlowEditor , NULL ) ;
628     PortableServer::ObjectId * id = myGraph->getId() ;
629     CORBA::Object_var obj = _poa->id_to_reference(*id) ;
630     iobject = SUPERV::Graph::_narrow( obj ) ;
631     myGraph->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
632     myGraph->SetObjImpl( (CNode_Impl * ) myGraph ) ;
633     GraphBase::Graph * aGraph = myGraph->DataFlowEditor()->Graph() ;
634     int i ;
635
636 // asv: set Editor for the graph itself and for all its macro nodes
637     aGraph->GraphEditor( aDataFlowEditor );
638
639 // For the supergraph or each graph of MacroNodes we search MacroNodes recursively :
640     for ( i = 0 ; i < aGraph->GraphNodesSize() ; i++ ) {
641       if ( aGraph->GraphNodes( i )->IsMacroNode() ) {
642         GraphBase::GOTONode * aMacroNode = (GraphBase::GOTONode * ) aGraph->GraphNodes( i ) ;
643
644         // asv : 05.11.04 : fix of bug - "insert file" into existing graph with MacroNodes worked incorrectly!
645         // aMacroNode is an "old" node, and here we must analize only "new" nodes, that
646         // came from aListOfDataFlows and were loaded by aDataFlowEditor->LoadDataFlow( &(*aListOfDataFlows)[ index ] )
647         // so here we check if the node is "new" or "old"
648         if ( aMacroNode->GraphEditor() && aMacroNode->CoupledNode() )
649           continue;
650
651         // asv: set Editor for the graph itself and for all its macro nodes
652         aMacroNode->GraphEditor( aDataFlowEditor );
653
654         // Creation of a GraphMacroNode ObjRef in the current editor
655         SUPERV::Graph_var macroiobject = SUPERV::Graph::_nil() ;
656         Graph_Impl * myMacroNode ;
657         myMacroNode = new Graph_Impl( _Orb , _Poa, _ContId,
658                                       instanceName() , interfaceName() ,
659                                       aMacroNode->Name() , SUPERV::MacroNode ) ;
660         id = myMacroNode->getId() ;
661         obj = _poa->id_to_reference(*id);
662         macroiobject = SUPERV::Graph::_narrow( obj ) ;
663         myMacroNode->DataFlowEditor( aDataFlowEditor ) ;
664         GraphEditor::InNode * aDataFlowNode = (GraphEditor::InNode * ) aMacroNode->GetInNode() ;
665         myMacroNode->DataFlowNode( aDataFlowNode ) ;
666         aDataFlowNode->SetObjRef( SUPERV::CNode::_duplicate( SUPERV::CNode::_narrow( obj ) ) ) ;
667         aDataFlowNode->SetObjImpl( myMacroNode ) ;
668
669         char * aCoupledNodeName = aMacroNode->CoupledNodeName() ;
670         MESSAGE( "LoadDataFlows MacroNode " << aMacroNode->Name() << " --> " << aCoupledNodeName
671                  << " to be searched among " << (*aListOfDataFlows).size() << " Graphs" ) ;
672         int index ;
673         bool found = false ;
674         for ( index = 0 ; index < (int ) (*aListOfDataFlows).size() ; index++ ) {
675           MESSAGE( "LoadDataFlows Graph" << index << " " << (*aListOfDataFlows)[index].Info.theName.c_str() ) ;
676           if ( !strcmp( aCoupledNodeName , (*aListOfDataFlows)[index].Info.theName.c_str() ) ) {
677             found = true ;
678             string dbgfile ;
679 // At first create the editor and a StreamGraph
680             GraphEditor::DataFlow * aMacroGraphDataFlowEditor ;
681             CreateEditor( _Orb , instanceName() , aCoupledNodeName , SUPERV::DataStreamGraph ,
682                           dbgfile , &aMacroGraphDataFlowEditor ) ;
683
684             if ( _DebugFileName ) {
685               delete [] _DebugFileName ;
686             }
687             _DebugFileName = new char[ strlen( dbgfile.c_str() )+1 ] ;
688             strcpy( _DebugFileName , dbgfile.c_str() ) ;
689
690             MESSAGE( "RECURSIVE IMPORT OF GRAPHS OF MACRONODES :" << aCoupledNodeName ) ;
691             if ( CORBA::is_nil( LoadDataFlows( aMacroGraphDataFlowEditor , aListOfDataFlows , index ) ) ) {
692               MESSAGE("Graph_Impl::LoadDataFlows failed" ) ;
693               return SUPERV::Graph::_duplicate( iobject ) ;
694             }
695
696 // That graph is not a StreamGraph :
697             else if ( !aMacroGraphDataFlowEditor->Graph()->HasDataStream() ) {
698               Graph_Impl * myMacroGraph ;
699 // The Graph ObjRef for that graph was already created
700               myMacroGraph = (Graph_Impl * ) aMacroGraphDataFlowEditor->Graph()->ObjImpl() ;
701
702               GraphBase::Graph * aMacroGraph = myMacroGraph->DataFlowEditor()->Graph() ;
703               
704               aMacroGraph->GraphMacroLevel( aGraph->GraphMacroLevel() + 1 ) ;
705               aMacroGraph->CoupledNode( aMacroNode ) ;
706               aMacroGraph->MacroObject( SUPERV::Graph::_duplicate( macroiobject ) ) ;
707
708 // Set the GraphMacroNode ObjRef in the MacroNode
709               aMacroNode->GraphMacroLevel( aGraph->GraphMacroLevel() + 1 ) ;
710               aMacroNode->CoupledNode( aMacroGraph ) ;
711               aMacroNode->MacroObject( SUPERV::Graph::_narrow( aMacroGraphDataFlowEditor->Graph()->ObjRef() ) ) ;
712               MESSAGE( "LoadDataFlows aMacroGraph " << aMacroGraph << " " << aMacroGraph->Name()
713                        << " coupled to " << aMacroGraph->CoupledNode() << " "
714                        << aMacroGraph->CoupledNode()->Name() << " Editor " << aMacroGraph->GraphEditor() ) ;
715               MESSAGE( "LoadDataFlows aMacroNode " << aMacroNode << " " << aMacroNode->Name()
716                        << " coupled to " << aMacroNode->CoupledNode() << " "
717                        << aMacroNode->CoupledNode()->Name() << " Editor " << aMacroNode->GraphEditor() ) ;
718               MESSAGE( "LoadDataFlows current Graph " << aGraph << " " << aGraph->Name()
719                        << " coupled to " << aGraph->CoupledNode()
720                        << " Editor " << aGraph->GraphEditor() << " " << aGraph->Name() ) ;
721             }
722             break ;
723           }
724         }
725         if ( !found ) {
726           return SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
727         }
728       }
729     }
730   }
731   else if ( (*aListOfDataFlows).size() == 1 ) {
732     StreamGraph_Impl * myStreamGraph ;
733 // We create a Graph ObjRef for that graph
734     myStreamGraph = new StreamGraph_Impl( _Orb , _Poa, _ContId,
735                                           instanceName() , interfaceName() ,
736                                           aDataFlowEditor , NULL ) ;
737     PortableServer::ObjectId * id = myStreamGraph->getId() ;
738     CORBA::Object_var obj = _poa->id_to_reference(*id) ;
739     SUPERV::StreamGraph_var Streamiobject = SUPERV::StreamGraph::_nil() ;
740     Streamiobject = SUPERV::StreamGraph::_narrow( obj ) ;
741     myStreamGraph->SetObjRef( SUPERV::StreamGraph::_duplicate( Streamiobject ) ) ;
742     myStreamGraph->SetObjImpl( (CNode_Impl * ) myStreamGraph ) ;
743 //    iobject = SUPERV::Graph::_narrow( obj ) ;
744     iobject = SUPERV::Graph::_narrow( Streamiobject ) ;
745   }
746   else {
747     MESSAGE( "Graph_Impl::LoadDataFlows StreamGraph with MacroNodes not yet implemented" ) ;
748   }
749
750   endService( "Graph_Impl::EditorLoadDataFlows" );
751   return SUPERV::Graph::_duplicate( iobject ) ;
752 }
753
754 SUPERV::Graph_var Graph_Impl::LoadDataFlows( GraphExecutor::DataFlow * aDataFlowExecutor ,
755                                              GraphBase::ListOfSGraphs * aListOfDataFlows ,
756                                              int index ) {
757   beginService( "Graph_Impl::ExecutorLoadDataFlows" ) ;
758   MESSAGE("Graph_Impl::LoadDataFlows(Executor) index " << index << " " << (*aListOfDataFlows)[index].Info.theName.c_str() << " aDataFlowExecutor " << aDataFlowExecutor ) ;
759
760   // That method is recursive :
761   // At first we load the supergraph with index = 0 :
762   // (After we load the graph corresponding to each MacroNode :)
763   if ( !aDataFlowExecutor->LoadDataFlow( &(*aListOfDataFlows)[ index ] ) ) {
764     MESSAGE("Graph_Impl::LoadDataFlows(Executor) failed, could not LoadDataFlow(supergraph)" ) ;
765     return SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
766   }
767
768   // That graph is not a StreamGraph :
769   else {
770     aDataFlowExecutor->Graph()->Kind( SUPERV::DataFlowGraph ) ;
771     GraphEditor::DataFlow * aDataFlowEditor ;
772     aDataFlowEditor = DataFlowEditor() ;
773     GraphBase::Graph * anEditorGraph = aDataFlowEditor->Graph() ;
774     GraphBase::Graph * anExecutorGraph = aDataFlowExecutor->Graph() ;
775     if ( aDataFlowEditor->Executor() ) {
776       delete aDataFlowEditor->Executor() ;
777       aDataFlowEditor->Executor( NULL ) ;
778       aDataFlowEditor->Editing() ;
779     }
780     aDataFlowEditor->Executor( aDataFlowExecutor ) ;
781     anExecutorGraph->GraphEditor( aDataFlowEditor ) ;
782
783     if ( !aDataFlowExecutor->Graph()->HasDataStream() ) {
784     // For the supergraph or each graph of MacroNodes we search MacroNodes recursively :
785       int i;
786       for ( i = 0 ; i < anExecutorGraph->GraphNodesSize() ; i++ ) {
787         if ( anExecutorGraph->GraphNodes( i )->IsMacroNode() ) {
788           GraphBase::GOTONode * aMacroNode = (GraphBase::GOTONode * ) anExecutorGraph->GraphNodes( i ) ;
789           aMacroNode->GraphEditor( aDataFlowEditor ) ;
790
791         // get the macro node's editor object
792           char * aMacroGraphName = aMacroNode->CoupledNodeName() ;
793           GraphBase::Graph* aMacroGraph =  (GraphBase::Graph*)((GraphBase::GOTONode *)anEditorGraph->GetGraphNode( aMacroNode->Name() ))->CoupledNode() ;
794         GraphEditor::DataFlow* aMacroGraphEditor = aMacroGraph->GraphEditor();
795
796           MESSAGE( "LoadDataFlows(Executor) MacroNode " << aMacroNode->Name() << " Coupled to " << aMacroGraphName
797                    << " to be searched among " << (*aListOfDataFlows).size() << " Graphs" << endl ) ;
798           int index;
799           bool found = false ;
800           for ( index = 0 ; index < (int ) (*aListOfDataFlows).size() && !found; index++ ) {
801             if ( !strcmp( aMacroGraphName , (*aListOfDataFlows)[index].Info.theName.c_str() ) )
802               found = true;
803           }
804           if ( !found ) {
805             MESSAGE("Graph_Impl::LoadDataFlows(Executor) failed, Macro node's coupled graph \"" << aMacroGraphName << "\" was not found" ) ;
806             return SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
807           }
808           index--; // it was incremented in "for ..." before last check ( !found ).
809         
810         // At first create the editor and a StreamGraph
811           string dbgfile ;
812           GraphExecutor::DataFlow * aMacroGraphExecutor ;
813           CreateExecutor( _Orb , instanceName() , aMacroGraphName , SUPERV::DataStreamGraph ,
814                         dbgfile , &aMacroGraphExecutor );
815
816         // set GraphMacroLevel for Executor's graph
817           GraphBase::Graph* aMacroGraphExe = aMacroGraphExecutor->Graph();
818             aMacroGraphExe->GraphMacroLevel( anExecutorGraph->GraphMacroLevel() + 1 );
819
820         // load data flows in MacroGraph's executor
821           Graph_Impl* aMacroGraphImpl = (Graph_Impl*) aMacroGraphEditor->Graph()->ObjImpl();
822           MESSAGE( "RECURSIVE IMPORT OF GRAPHS OF MACRONODES : " << aMacroGraphName ) ;
823           if ( CORBA::is_nil( aMacroGraphImpl->LoadDataFlows( aMacroGraphExecutor, aListOfDataFlows , index ) ) ) {
824             MESSAGE("Graph_Impl::LoadDataFlows(Executor) failed,  could not LoadDataFlow( MacroNodeGraph \"" <<aMacroGraphName << "\" )" ) ;
825             return SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
826           }
827           
828           // set coupled pair MacroNode <--> MacroGraph
829           aMacroGraphExe->CoupledNode( aMacroNode ) ;
830           aMacroNode->CoupledNode( aMacroGraphExe ) ;
831
832 //JR 04.05.2005 Debug : InPorts values of MacroNodes are like for other nodes
833 //                      InPorts values of MacroGraphs of MacroNodes are done in the Executor
834 // It was probably to try to debug anything but it was a wrong debug and I do not know what bug
835 // ===> folowing code is commented
836 #if 0
837           // initialize MacroNode's InPorts with corresponding "external" values
838           int q ;
839           for ( q = 0 ; q < aMacroNode->GetNodeInPortsSize() ; q++ ) {
840             const GraphBase::InPort * anInPort = aMacroNode->GetNodeInPort( q ) ;
841             if ( anInPort->IsDataConnected() ) {
842               const char* aMacroGraphInPortName = aMacroGraph->GetNodeInPort( q )->PortName();
843 //JR 30.03.2005       if ( !aMacroGraphExecutor->InputOfAny(  aMacroGraphInPortName, *anInPort->GetOutPort()->Value(),false ) ) {
844               if ( !aMacroGraphExecutor->InputOfAny(  aMacroGraphInPortName, anInPort->GetOutPort()->Value(),false ) ) {
845                 return SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
846               }
847               else {
848                 MESSAGE( "LoadDataFlows(Executor) " << aMacroGraph->Name() << "->InputOfAny( " 
849                          << aMacroGraphInPortName << " , Value )" ) ;
850               }
851             }
852           } // end of init MacroNode's InPorts
853 #endif
854           aMacroGraphImpl->Run() ;
855
856         }
857       } // end of for( get nodes of the graph...)
858     }
859   } // end of setting initial graph's structure..
860
861   endService( "Graph_Impl::ExecutorLoadDataFlows" );
862
863   PortableServer::ObjectId * id = getId();
864   CORBA::Object_var obj = _poa->id_to_reference( *id );
865   SUPERV::Graph_var iobject = SUPERV::Graph::_narrow( obj ) ;
866   return SUPERV::Graph::_duplicate( iobject ) ;
867 }
868
869 SUPERV::Graph_ptr Graph_Impl::MNode( const char * aXmlFileName ) {
870   beginService( "Graph_Impl::MNode" );
871   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
872   MESSAGE( "Graph_Impl::MNode( " << aXmlFileName << " )" ) ;
873   if ( !IsMacro() ) {
874     GraphBase::ListOfSGraphs aListOfDataFlows ;
875     string dbgfile ;
876 // At first create the editor and a StreamGraph with the xml file
877     GraphEditor::DataFlow * aDataFlowEditor ;
878     CreateEditor( _Orb , instanceName() , aXmlFileName , SUPERV::DataStreamGraph ,
879                   dbgfile , &aDataFlowEditor ) ;
880
881     if ( _DebugFileName ) {
882       delete [] _DebugFileName ;
883     }
884     _DebugFileName = new char[ strlen( dbgfile.c_str() )+1 ] ;
885     strcpy( _DebugFileName , dbgfile.c_str() ) ;
886
887     int lenname = strlen( aXmlFileName ) ;
888     bool loaded = false ;
889 // Import of the xml file
890     if ( lenname > 4 && !strcmp( &aXmlFileName[ lenname - 4 ] , ".xml" ) ) {
891       loaded = aDataFlowEditor->LoadXml( aXmlFileName , aListOfDataFlows ) ;
892       if ( loaded ) {
893         iobject = MNode( aDataFlowEditor , &aListOfDataFlows ) ;
894       }
895     }
896   }
897   return iobject ;
898 }
899
900 // WARNING : THIS IS COMPLICATED :
901 // I should have to remove duplicated code ...
902 SUPERV::Graph_ptr Graph_Impl::MNode( GraphEditor::DataFlow * aMacroGraphDataFlowEditor ,
903                                      GraphBase::ListOfSGraphs * aListOfDataFlows ) {
904   beginService( "Graph_Impl::MNode" ) ;
905   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
906   SUPERV::Graph_var macroiobject = SUPERV::Graph::_nil() ;
907
908   if ( !IsMacro() ) {
909     MESSAGE( "Graph_Impl::MNode DataFlowEditor->LoadDataFlow" ) ;
910 //    if ( !aMacroGraphDataFlowEditor->LoadDataFlow( &aListOfDataFlows[ 0 ] ) ) {
911     iobject = LoadDataFlows( aMacroGraphDataFlowEditor , aListOfDataFlows , 0 ) ;
912     if ( CORBA::is_nil( iobject ) ) {
913       MESSAGE("Graph_Impl::MNode LoadDataFlow failed" ) ;
914       return false ;
915     }
916
917 // That graph is not a StreamGraph :
918     else if ( !aMacroGraphDataFlowEditor->Graph()->HasDataStream() ) {
919       aMacroGraphDataFlowEditor->Graph()->Kind( SUPERV::DataFlowGraph ) ;
920       aMacroGraphDataFlowEditor->Graph()->GraphEditor( aMacroGraphDataFlowEditor ) ;
921
922       Graph_Impl * myMacroGraph = (Graph_Impl * ) aMacroGraphDataFlowEditor->Graph()->ObjImpl() ;
923 // Creation of a GraphMacroNode in the current editor
924       string aMacroNodeName = string( "Macro_" ) + string( myMacroGraph->Name() ) ;
925       Graph_Impl * myMacroNode ;
926       myMacroNode = new Graph_Impl( _Orb , _Poa, _ContId,
927                                     instanceName() , interfaceName() ,
928                                     aMacroNodeName.c_str() , SUPERV::MacroNode ) ;
929       PortableServer::ObjectId * id = myMacroNode->getId() ;
930       CORBA::Object_var obj = _poa->id_to_reference(*id);
931       macroiobject = SUPERV::Graph::_narrow( obj ) ;
932       myMacroNode->DataFlowEditor( DataFlowEditor() ) ;
933
934 // Creation of the MacroNode itself in the current Graph
935       GraphBase::ListOfFuncName aFuncName ;
936       aFuncName.resize( 1 ) ;
937       aFuncName[0] = "" ;
938       SUPERV::ListOfStrings aListOfStrings ;
939       aListOfStrings.length( 1 ) ;
940       aListOfStrings[0] = "" ;
941       GraphBase::ListOfPythonFunctions aPythonFunction ;
942       aPythonFunction.resize( 1 ) ;
943       aPythonFunction[0] = &aListOfStrings ;
944       SALOME_ModuleCatalog::Service * aMacroNodeService = myMacroGraph->Service() ;
945       GraphEditor::InNode * aDataFlowNode = DataFlowEditor()->AddNode( *aMacroNodeService , "" , "" ,
946                                                                        aMacroNodeName.c_str() , SUPERV::MacroNode ,
947                                                                        aFuncName , aPythonFunction ) ;
948       aDataFlowNode->SetPythonFunction( "" , aListOfStrings ) ;
949       myMacroNode->DataFlowNode( aDataFlowNode ) ;
950       aDataFlowNode->ComputingNode()->SetService( *aMacroNodeService ) ;
951       aDataFlowNode->SetObjRef( SUPERV::CNode::_duplicate( SUPERV::CNode::_narrow( obj ) ) ) ;
952       aDataFlowNode->SetObjImpl( this ) ;
953
954       GraphBase::Graph * aGraph = myMacroGraph->DataFlowEditor()->Graph() ;
955       GraphBase::Graph * aGraphOfMacroGraph = myMacroNode->DataFlowEditor()->Graph() ;
956       GraphBase::Graph * aMacroGraph = myMacroNode->DataFlowNode()->GraphMacroNode() ;
957       aMacroGraph->Kind( SUPERV::MacroNode ) ;
958       aMacroGraph->GraphEditor( DataFlowEditor() ) ;
959 // Creation of the Ports of the MacroNode from the ports of the GraphMacroNode
960       aMacroGraph->SetMacroPorts( aGraph ) ;
961 // Valid ==> creation of Service and the ports of the current Graph
962       DataFlowEditor()->IsValid() ;
963 // Set the Input Datas from the GraphMacroNode to the current Graph
964       aMacroGraph->SetMacroDatas( aGraph , aGraphOfMacroGraph ) ;
965       aMacroGraphDataFlowEditor->UnValid() ;
966 // Set the MacroNode ObjRef in the GraphMacroNode
967       aGraph->CoupledNode( aMacroGraph ) ;
968       aGraph->MacroObject( SUPERV::Graph::_duplicate( macroiobject ) ) ;
969 // Set the GraphMacroNode ObjRef in the MacroNode
970       aDataFlowNode->GraphMacroNode()->GraphMacroLevel( aGraphOfMacroGraph->GraphMacroLevel() + 1 ) ;
971       aMacroGraph->CoupledNode( aGraph ) ;
972 //      aGraphOfMacroGraph->CoupledNode( aGraph ) ;
973       aMacroGraph->MacroObject( SUPERV::Graph::_duplicate( iobject ) ) ;
974 //      aGraphOfMacroGraph->MacroObject( SUPERV::Graph::_duplicate( iobject ) ) ;
975 //    aDataFlowNode->CoupledNode( aGraph ) ;
976 //    aDataFlowNode->ComputingNode()->MacroObject( SUPERV::Graph::_duplicate( iobject ) ) ;
977 // Set the MacroLevel of that graph
978       aGraph->GraphMacroLevel( aGraphOfMacroGraph->GraphMacroLevel() + 1 ) ;
979       aMacroGraph->Coordinates( 0 , 0 ) ;
980       MESSAGE( "DataFlowNode Graph " << this << " DataFlowEditor " << DataFlowEditor() << " aDataFlowNode "
981                << aDataFlowNode << " " << aDataFlowNode->Name() << " created" ) ;
982       MESSAGE( "MNode aGraph " << aGraph << " " << aGraph->Name()
983                << " coupled to " << aGraph->CoupledNode() << " "
984                << aGraph->CoupledNode()->Name() ) ;
985       MESSAGE( "MNode aMacroGraph " << aMacroGraph << " " << aMacroGraph->Name()
986                << " coupled to " << aMacroGraph->CoupledNode() << " "
987                << aMacroGraph->CoupledNode()->Name() ) ;
988       MESSAGE( "MNode aMacroGraph " << myMacroNode->DataFlowEditor()->Graph() << " "
989                << myMacroNode->DataFlowEditor()->Graph()->Name()
990                << " coupled to " << myMacroNode->DataFlowEditor()->Graph()->CoupledNode() ) ;
991     }
992     else {
993       delete aMacroGraphDataFlowEditor ;
994     }
995   }
996   endService( "Graph_Impl::MNode" );
997   return SUPERV::Graph::_duplicate( macroiobject ) ;
998 }
999
1000 // For python supergraph
1001 SUPERV::Graph_ptr Graph_Impl::GraphMNode( SUPERV::Graph_ptr myMacroGraph ) {
1002   SUPERV::Graph_var iobject = myMacroGraph ;
1003   beginService( "Graph_Impl::GraphMNode" ) ;
1004
1005 //  GraphBase::Graph * myMacroGraph = aGraph;
1006   SUPERV::Graph_var macroiobject = SUPERV::StreamGraph::_nil() ;
1007 // Creation of a GraphMacroNode in the current editor
1008   if ( !IsMacro() ) {
1009     string aMacroNodeName = string( "Macro_" ) + string( myMacroGraph->Name() ) ;
1010     //string aMacroNodeName = string( myMacroGraph->CoupledName() ) ;
1011     MESSAGE( "GraphMNode( " << myMacroGraph->Name() << " )" ) ;
1012     Graph_Impl * myMacroNode ;
1013     myMacroNode = new Graph_Impl( _Orb , _Poa, _ContId,
1014                                   instanceName() , interfaceName() ,
1015                                   aMacroNodeName.c_str() , SUPERV::MacroNode ) ;
1016     PortableServer::ObjectId * id = myMacroNode->getId() ;
1017     CORBA::Object_var obj = _poa->id_to_reference(*id);
1018     macroiobject = SUPERV::Graph::_narrow( obj ) ;
1019     myMacroNode->DataFlowEditor( DataFlowEditor() ) ;
1020
1021 // Creation of the MacroNode itself in the current Graph
1022     GraphBase::ListOfFuncName aFuncName ;
1023     aFuncName.resize( 1 ) ;
1024     aFuncName[0] = "" ;
1025     SUPERV::ListOfStrings aListOfStrings ;
1026     aListOfStrings.length( 1 ) ;
1027     aListOfStrings[0] = "" ;
1028     GraphBase::ListOfPythonFunctions aPythonFunction ;
1029     aPythonFunction.resize( 1 ) ;
1030     aPythonFunction[0] = &aListOfStrings ;
1031     SALOME_ModuleCatalog::Service * aMacroNodeService = myMacroGraph->Service() ;
1032     GraphEditor::InNode * aDataFlowNode ;
1033     aDataFlowNode = DataFlowEditor()->AddNode( *aMacroNodeService , "" , "" ,
1034                                                aMacroNodeName.c_str() , SUPERV::MacroNode ,
1035                                                aFuncName , aPythonFunction ) ;
1036     aDataFlowNode->SetPythonFunction( "" , aListOfStrings ) ;
1037     myMacroNode->DataFlowNode( aDataFlowNode ) ;
1038     aDataFlowNode->ComputingNode()->SetService( *aMacroNodeService ) ;
1039     aDataFlowNode->SetObjRef( SUPERV::CNode::_duplicate( SUPERV::CNode::_narrow( obj ) ) ) ;
1040     aDataFlowNode->SetObjImpl( this ) ;
1041
1042     GraphBase::Graph * aSubGraph = NULL ;
1043 //    GraphBase::Graph * aGraph = DataFlowEditor()->Automaton()->MapGraph( myMacroGraph->Name() ) ;
1044     //DataFlowEditor()->Automaton()->GraphBase( &aGraph ) ;
1045 //JR 20.09.2005 Debug SAB_data_1609_bugPAL.py from CEA (Anthony)
1046 //    Graph_Impl * aSubGraphImpl = dynamic_cast<Graph_Impl * >( _Poa->id_to_servant( *myMacroGraph->_Id ) ) ;
1047     myMacroGraph->ping() ;
1048     Graph_Impl * aSubGraphImpl = theAutomaton->GraphImpl() ;
1049     aSubGraph = aSubGraphImpl->DataFlowEditor()->Graph() ;
1050     myMacroGraph->Coupled() ;
1051     GraphBase::Graph * aGraphOfMacroGraph = myMacroNode->DataFlowEditor()->Graph() ;
1052     GraphBase::Graph * aMacroGraph = myMacroNode->DataFlowNode()->GraphMacroNode() ;
1053     aMacroGraph->Kind( SUPERV::MacroNode ) ;
1054     aMacroGraph->GraphEditor( DataFlowEditor() ) ;
1055 // Creation of the Ports of the MacroNode from the ports of the GraphMacroNode
1056     aMacroGraph->SetMacroPorts( aSubGraph ) ;
1057 // Valid ==> creation of Service and the ports of the current Graph
1058     DataFlowEditor()->IsValid() ;
1059 // Set the Input Datas from the GraphMacroNode to the current Graph
1060     aMacroGraph->SetMacroDatas( aSubGraph , aGraphOfMacroGraph ) ;
1061 //    aMacroGraphDataFlowEditor->UnValid() ;
1062     aSubGraph->GraphEditor()->UnValid() ;
1063 // Set the MacroNode ObjRef in the GraphMacroNode
1064     aSubGraph->CoupledNode( aMacroGraph ) ;
1065     aSubGraph->MacroObject( SUPERV::Graph::_duplicate( macroiobject ) ) ;
1066 // Set the GraphMacroNode ObjRef in the MacroNode
1067     aDataFlowNode->GraphMacroNode()->GraphMacroLevel( aGraphOfMacroGraph->GraphMacroLevel() + 1 ) ;
1068     aMacroGraph->CoupledNode( aSubGraph ) ;
1069     aMacroGraph->MacroObject( SUPERV::Graph::_duplicate( iobject ) ) ;
1070 //    aDataFlowNode->CoupledNode( aSubGraph ) ;
1071 //    aDataFlowNode->ComputingNode()->MacroObject( SUPERV::Graph::_duplicate( iobject ) ) ;
1072 // Set the MacroLevel of that graph
1073     aSubGraph->GraphMacroLevel( aGraphOfMacroGraph->GraphMacroLevel() + 1 ) ;
1074     aMacroGraph->Coordinates( 0 , 0 ) ;
1075     MESSAGE( aSubGraph->Name() << "->CoupledNode()->GraphEditor() : " << aSubGraph->CoupledNode()->GraphEditor() ) ;
1076     MESSAGE( aMacroGraph->Name() << "->CoupledNode()->GraphEditor() : "
1077              << aMacroGraph->CoupledNode()->GraphEditor() ) ;
1078     MESSAGE( "DataFlowNode Graph " << this << " DataFlowEditor " << DataFlowEditor() << " aDataFlowNode "
1079              << aDataFlowNode << " " << aDataFlowNode->Name() << " created" ) ;
1080   }
1081   endService( "Graph_Impl::GraphMNode" ) ;
1082   return SUPERV::Graph::_duplicate( macroiobject ) ;
1083 }
1084
1085 SUPERV::Graph_ptr Graph_Impl::FlowObjRef() {
1086   beginService( "Graph_Impl::FlowObjRef" );
1087   SUPERV::Graph_var iobject = SUPERV::Graph::_nil() ;
1088   if ( IsMacro() ) {
1089     iobject = SUPERV::Graph::_narrow( DataFlowNode()->GraphMacroNode()->MacroObject() ) ;
1090   }
1091   else if ( IsGraph() ) {
1092     iobject = SUPERV::Graph::_narrow( DataFlowEditor()->Graph()->MacroObject() ) ;
1093   }
1094   if ( CORBA::is_nil( iobject ) ) {
1095     MESSAGE( "Graph_Impl::FlowObjRef() returns a NIL Object" ) ;
1096   }
1097   endService( "Graph_Impl::FlowObjRef" );
1098   return SUPERV::Graph::_duplicate( iobject ) ;
1099 }
1100
1101 SUPERV::StreamGraph_ptr Graph_Impl::StreamObjRef() {
1102   beginService( "Graph_Impl::StreamObjRef" );
1103   SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil() ;
1104   if ( IsMacro() ) {
1105     iobject = SUPERV::StreamGraph::_narrow( DataFlowNode()->GraphMacroNode()->MacroObject() ) ;
1106   }
1107   else if ( IsGraph() || IsStreamGraph() ) {
1108     iobject = SUPERV::StreamGraph::_narrow( DataFlowEditor()->Graph()->MacroObject() ) ;
1109   }
1110   if ( CORBA::is_nil( iobject ) ) {
1111     MESSAGE( "Graph_Impl::StreamObjRef() returns a NIL Object" ) ;
1112   }
1113   endService( "Graph_Impl::StreamObjRef" );
1114   return SUPERV::StreamGraph::_duplicate( iobject ) ;
1115 }
1116
1117 SUPERV::CNode_ptr Graph_Impl::Node(char const * aNodeName ) {
1118 //  beginService( "Graph_Impl::Node" );
1119   SUPERV::CNode_var iobject = SUPERV::CNode::_nil() ;
1120   /* JR : 13/06/03
1121      if ( CORBA::is_nil( DataFlowEditor()->GetNode( aNodeName )->ObjRef() ) ) {
1122      CNode_Impl * myNode = new CNode_Impl( _Orb , _Poa , _ContId ,
1123      instanceName() , interfaceName() ,
1124      DataFlowEditor() ,
1125      DataFlowEditor()->GetNode( aNodeName ) ) ;
1126      PortableServer::ObjectId * id = myNode->getId() ;
1127      CORBA::Object_var obj = _poa->id_to_reference(*id);
1128      iobject = SUPERV::CNode::_narrow(obj) ;
1129      myNode->SetObjRef( SUPERV::CNode::_duplicate( iobject ) ) ;
1130      }
1131      else {
1132      iobject = DataFlowEditor()->GetNode( aNodeName )->ObjRef() ;
1133   */
1134   if ( !IsMacro() ) {
1135     GraphEditor::InNode * anInNode = DataFlowEditor()->GetNode( aNodeName ) ;
1136     if ( anInNode ) {
1137       if ( CORBA::is_nil( anInNode->ObjRef() ) ) {
1138         SetNodeObjRef( anInNode ) ;
1139       }
1140       iobject = anInNode->ObjRef() ;
1141     }
1142   }
1143 //  endService( "Graph_Impl::Node" );
1144   return SUPERV::CNode::_duplicate( iobject ) ;
1145 }
1146
1147 SUPERV::Link_ptr Graph_Impl::Link( SUPERV::Port_ptr OutPort ,
1148                                    SUPERV::Port_ptr InPort ) {
1149 //  beginService( "Graph_Impl::Link" );
1150   SUPERV::Link_var iobject = SUPERV::Link::_nil() ;
1151   if ( DataFlowEditor()->IsEditing() && !DataFlowEditor()->IsReadOnly() &&
1152        !OutPort->IsDataStream() && !InPort->IsDataStream() && !IsMacro() ) {
1153     MESSAGE( "Graph_Impl::Link( " << OutPort->Node()->Name() << "( " << OutPort->Name() << " ) --> "
1154              << InPort->Node()->Name() << "( " << InPort->Name() << " )" ) ;
1155     GraphEditor::InNode * anInNode = DataFlowEditor()->GetNode( InPort->Node()->Name() ) ;
1156     GraphEditor::InNode * anOutNode = DataFlowEditor()->GetNode( OutPort->Node()->Name() ) ;
1157     if ( anInNode == NULL || anOutNode== NULL ) {
1158       MESSAGE( "Graph_Impl::Link( " << OutPort->Node()->Name() << " " << anOutNode << " , "
1159                << InPort->Node()->Name() << " " << anInNode << " ) Node(s) not found in " << Name() ) ;
1160     }
1161     else {
1162       GraphBase::InPort * anInPort = anInNode->ComputingNode()->GetChangeInPort( InPort->Name() ) ;
1163       GraphBase::OutPort * anOutPort = anOutNode->ComputingNode()->GetChangeOutPort( OutPort->Name() ) ;
1164       if ( anInPort == NULL || anOutPort== NULL ) {
1165         MESSAGE( "Graph_Impl::Link( " << OutPort->Name() << " " << anOutPort << " , "
1166                  << InPort->Name() << " " << anInPort << " ) Port(s) not found" ) ;
1167       }
1168       else if ( CORBA::is_nil( anOutPort->InPortObjRef( anInPort ) ) ) {
1169         const char * DataFlowOutNodeName = OutPort->Node()->Name() ;
1170         GraphEditor::InNode * DataFlowOutNode = DataFlowEditor()->GetNode( DataFlowOutNodeName ) ;
1171         const char * DataFlowInNodeName = InPort->Node()->Name() ;
1172         GraphEditor::InNode * DataFlowInNode = DataFlowEditor()->GetNode( DataFlowInNodeName ) ;
1173         if ( DataFlowOutNode && DataFlowInNode ) {
1174           bool Success ;
1175           Link_Impl * myLink = new Link_Impl( _Orb , _Poa , _ContId ,
1176                                               instanceName() , interfaceName() ,
1177                                               DataFlowEditor() ,
1178                                               DataFlowInNode ,
1179                                               InPort->Name() ,
1180                                               DataFlowOutNode ,
1181                                               OutPort->Name() ,
1182                                               true , false , Success ) ;
1183           if ( Success ) {
1184             PortableServer::ObjectId * id = myLink->getId() ;
1185             CORBA::Object_var obj = _poa->id_to_reference(*id);
1186             iobject = SUPERV::Link::_narrow(obj) ;
1187             anOutPort->AddInPortObjRef( anInPort , SUPERV::Link::_duplicate( iobject ) ) ;
1188           }
1189         }
1190       }
1191       else {
1192         MESSAGE( "Graph_Impl::Link( " << OutPort->Name() << " " << anOutPort << " , "
1193                  << InPort->Name() << " " << anInPort << " ) ObjRef already exists" ) ;
1194         iobject = anOutPort->InPortObjRef( anInPort ) ;
1195       }
1196     }
1197   }
1198   DataFlowEditor()->UnValid() ;
1199 //  endService( "Graph_Impl::Link" );
1200   return SUPERV::Link::_duplicate( iobject ) ;
1201 }
1202
1203 // Only to see MacroNodes with actual GUI ... :
1204 #define GOTOMacroNode 0
1205
1206 void Graph_Impl::SetNodeObjRef( GraphEditor::InNode * anInNode ) {
1207   MESSAGE("Graph_Impl::SetNodeObjRef " << anInNode->Name() << " " << anInNode->Kind() ) ;
1208   CORBA::Object_var obj ;
1209   if ( anInNode->IsComputingNode() ) {
1210     CNode_Impl * myNode = new CNode_Impl( _Orb , _Poa , _ContId ,
1211                                           instanceName() , interfaceName() ,
1212                                           DataFlowEditor() ,
1213                                           anInNode ) ;
1214     PortableServer::ObjectId * id = myNode->getId() ;
1215     obj = _poa->id_to_reference(*id);
1216     SUPERV::CNode_var iobject = SUPERV::CNode::_narrow(obj) ;
1217     myNode->SetObjRef( SUPERV::CNode::_duplicate( iobject ) ) ;
1218   }
1219   else if ( anInNode->IsFactoryNode() ) {
1220     FNode_Impl * myNode = new FNode_Impl( _Orb , _Poa , _ContId ,
1221                                           instanceName() , interfaceName() ,
1222                                           DataFlowEditor() ,
1223                                           anInNode ) ;
1224     PortableServer::ObjectId * id = myNode->getId() ;
1225     obj = _poa->id_to_reference(*id);
1226     SUPERV::FNode_var iobject = SUPERV::FNode::_narrow(obj) ;
1227     myNode->SetObjRef( SUPERV::FNode::_duplicate( iobject ) ) ;
1228   }
1229   else if ( anInNode->IsInLineNode() ) {
1230     INode_Impl * myNode = new INode_Impl( _Orb , _Poa , _ContId ,
1231                                           instanceName() , interfaceName() ,
1232                                           DataFlowEditor() ,
1233                                           anInNode ) ;
1234     PortableServer::ObjectId * id = myNode->getId() ;
1235     obj = _poa->id_to_reference(*id);
1236     SUPERV::INode_var iobject = SUPERV::INode::_narrow(obj) ;
1237     myNode->SetObjRef( SUPERV::INode::_duplicate( iobject ) ) ;
1238   }
1239 #if GOTOMacroNode
1240   else if ( anInNode->IsGOTONode() || anInNode->IsMacroNode() ) {
1241     if ( anInNode->IsMacroNode() ) {
1242       anInNode->ComputingNode()->Kind( SUPERV::GOTONode ) ;
1243     }
1244 #else
1245   else if ( anInNode->IsGOTONode() ) {
1246 #endif
1247     GNode_Impl * myNode = new GNode_Impl( _Orb , _Poa , _ContId ,
1248                                           instanceName() , interfaceName() ,
1249                                           DataFlowEditor() ,
1250                                           anInNode ) ;
1251     PortableServer::ObjectId * id = myNode->getId() ;
1252     obj = _poa->id_to_reference(*id);
1253     SUPERV::GNode_var iobject = SUPERV::GNode::_narrow(obj) ;
1254     myNode->SetObjRef( SUPERV::GNode::_duplicate( iobject ) ) ;
1255   }
1256   else if ( anInNode->IsMacroNode() ) {
1257     Graph_Impl * myNode = new Graph_Impl( _Orb , _Poa , _ContId ,
1258                                           instanceName() , interfaceName() ,
1259                                           DataFlowEditor() ,
1260                                           anInNode ) ;
1261     PortableServer::ObjectId * id = myNode->getId() ;
1262     obj = _poa->id_to_reference(*id);
1263     SUPERV::Graph_var iobject = SUPERV::Graph::_narrow(obj) ;
1264     myNode->SetObjRef( SUPERV::Graph::_duplicate( iobject ) ) ;
1265   }
1266   else if ( anInNode->IsLoopNode() ) {
1267     LNode_Impl * myNode = new LNode_Impl( _Orb , _Poa , _ContId ,
1268                                           instanceName() , interfaceName() ,
1269                                           DataFlowEditor() ,
1270                                           anInNode ) ;
1271     PortableServer::ObjectId * id = myNode->getId() ;
1272     obj = _poa->id_to_reference(*id);
1273     SUPERV::LNode_var iobject = SUPERV::LNode::_narrow(obj) ;
1274     myNode->SetObjRef( SUPERV::LNode::_duplicate( iobject ) ) ;
1275   }
1276   else if ( anInNode->IsEndLoopNode() ) {
1277     ELNode_Impl * myNode = new ELNode_Impl( _Orb , _Poa , _ContId ,
1278                                             instanceName() , interfaceName() ,
1279                                             DataFlowEditor() ,
1280                                             anInNode ) ;
1281     PortableServer::ObjectId * id = myNode->getId() ;
1282     obj = _poa->id_to_reference(*id);
1283     SUPERV::ELNode_var iobject = SUPERV::ELNode::_narrow(obj) ;
1284     myNode->SetObjRef( SUPERV::ELNode::_duplicate( iobject ) ) ;
1285   }
1286   else if ( anInNode->IsSwitchNode() ) {
1287     SNode_Impl * myNode = new SNode_Impl( _Orb , _Poa , _ContId ,
1288                                           instanceName() , interfaceName() ,
1289                                           DataFlowEditor() ,
1290                                           anInNode ) ;
1291     PortableServer::ObjectId * id = myNode->getId() ;
1292     obj = _poa->id_to_reference(*id);
1293     SUPERV::SNode_var iobject = SUPERV::SNode::_narrow(obj) ;
1294     myNode->SetObjRef( SUPERV::SNode::_duplicate( iobject ) ) ;
1295   }
1296   else if ( anInNode->IsEndSwitchNode() ) {
1297     ESNode_Impl * myNode = new ESNode_Impl( _Orb , _Poa , _ContId ,
1298                                             instanceName() , interfaceName() ,
1299                                             DataFlowEditor() ,
1300                                             anInNode ) ;
1301     PortableServer::ObjectId * id = myNode->getId() ;
1302     obj = _poa->id_to_reference(*id);
1303     SUPERV::ESNode_var iobject = SUPERV::ESNode::_narrow(obj) ;
1304     myNode->SetObjRef( SUPERV::ESNode::_duplicate( iobject ) ) ;
1305   }
1306 }
1307
1308 SUPERV::ListOfNodes_var  Graph_Impl::SetNode( SUPERV::ListOfNodes_var RetVal ,
1309                                               GraphBase::ComputingNode * aNode ) {
1310   int index = 0 ;
1311 //  if ( _DataFlowExecutor ) {
1312   if ( DataFlowExecutor() ) {
1313     MESSAGE("Graph_Impl::SetNode " << aNode->Name() << " " << aNode->Kind() << " "
1314             << DataFlowExecutor()->StateName( DataFlowExecutor()->AutomatonState( aNode->Name() ) ) ) ;
1315   }
1316   else {
1317     MESSAGE("Graph_Impl::SetNode " << aNode->Name() << " " << aNode->Kind() ) ;
1318   }
1319   if ( aNode->IsComputingNode() ) {
1320     index = RetVal->CNodes.length() ;
1321     RetVal->CNodes.length( index+1 );
1322   }
1323   else if ( aNode->IsFactoryNode() ) {
1324     index = RetVal->FNodes.length() ;
1325     RetVal->FNodes.length( index+1 );
1326   }
1327   else if ( aNode->IsInLineNode() ) {
1328     index = RetVal->INodes.length() ;
1329     RetVal->INodes.length( index+1 );
1330   }
1331 #if GOTOMacroNode
1332   else if ( aNode->IsGOTONode() || aNode->IsMacroNode() ) {
1333 #else
1334   else if ( aNode->IsGOTONode() ) {
1335 #endif
1336     index = RetVal->GNodes.length() ;
1337     RetVal->GNodes.length( index+1 );
1338   }
1339   else if ( aNode->IsMacroNode() ) {
1340     index = RetVal->Graphs.length() ;
1341     RetVal->Graphs.length( index+1 );
1342   }
1343   else if ( aNode->IsLoopNode() ) {
1344     index = RetVal->LNodes.length() ;
1345     RetVal->LNodes.length( index+1 );
1346   }
1347   else if ( aNode->IsEndLoopNode() ) {
1348     index = RetVal->ELNodes.length() ;
1349     RetVal->ELNodes.length( index+1 );
1350   }
1351   else if ( aNode->IsSwitchNode() ) {
1352     index = RetVal->SNodes.length() ;
1353     RetVal->SNodes.length( index+1 );
1354   }
1355   else if ( aNode->IsEndSwitchNode() ) {
1356     index = RetVal->ESNodes.length() ;
1357     RetVal->ESNodes.length( index+1 );
1358   }
1359   if ( CORBA::is_nil( aNode->ObjRef() ) ) {
1360     SetNodeObjRef( (GraphEditor::InNode * ) aNode->GetInNode() ) ;
1361   }
1362   SUPERV::CNode_var aNodeObjRef = aNode->ObjRef() ;
1363   if ( aNode->IsComputingNode() ) {
1364     RetVal->CNodes[index] = SUPERV::CNode::_duplicate( aNodeObjRef ) ;
1365   }
1366   else if ( aNode->IsFactoryNode() ) {
1367     RetVal->FNodes[index] = SUPERV::FNode::_duplicate( SUPERV::FNode::_narrow( aNodeObjRef ) ) ;
1368   }
1369   else if ( aNode->IsInLineNode() ) {
1370     RetVal->INodes[index] = SUPERV::INode::_duplicate( SUPERV::INode::_narrow( aNodeObjRef ) ) ;
1371   }
1372 #if GOTOMacroNode
1373   else if ( aNode->IsGOTONode() || aNode->IsMacroNode() ) {
1374 #else
1375   else if ( aNode->IsGOTONode() ) {
1376 #endif
1377     RetVal->GNodes[index] = SUPERV::GNode::_duplicate( SUPERV::GNode::_narrow( aNodeObjRef ) ) ;
1378   }
1379   else if ( aNode->IsMacroNode() ) {
1380     RetVal->Graphs[index] = SUPERV::Graph::_duplicate( SUPERV::Graph::_narrow( aNodeObjRef ) ) ;
1381   }
1382   else if ( aNode->IsLoopNode() ) {
1383     RetVal->LNodes[index] = SUPERV::LNode::_duplicate( SUPERV::LNode::_narrow( aNodeObjRef ) ) ;
1384   }
1385   else if ( aNode->IsEndLoopNode() ) {
1386     RetVal->ELNodes[index] = SUPERV::ELNode::_duplicate( SUPERV::ELNode::_narrow( aNodeObjRef ) ) ;
1387   }
1388   else if ( aNode->IsSwitchNode() ) {
1389     RetVal->SNodes[index] = SUPERV::SNode::_duplicate( SUPERV::SNode::_narrow( aNodeObjRef ) ) ;
1390   }
1391    else if ( aNode->IsEndSwitchNode() ) {
1392     RetVal->ESNodes[index] = SUPERV::ESNode::_duplicate( SUPERV::ESNode::_narrow( aNodeObjRef ) ) ;
1393   }
1394  return ( RetVal._retn() ) ;
1395 }
1396
1397 SUPERV::ListOfNodes * Graph_Impl::Nodes() {
1398   SUPERV::ListOfNodes_var RetVal = new SUPERV::ListOfNodes;
1399   beginService( "Graph_Impl::Nodes" );
1400   RetVal->CNodes.length(0) ;
1401   RetVal->FNodes.length(0) ;
1402   RetVal->INodes.length(0) ;
1403   RetVal->LNodes.length(0) ;
1404   RetVal->SNodes.length(0) ;
1405   RetVal->GNodes.length(0) ;
1406   RetVal->Graphs.length(0) ;
1407   if ( !IsMacro() ) {
1408     int i ;
1409     MESSAGE( DataFlowEditor()->Graph()->GetGraphNodeSize() << " Nodes in the Map and "
1410              << DataFlowEditor()->Graph()->GraphNodesSize() << " Nodes int the Vector" ) ;
1411     for ( i = 0 ; i < DataFlowEditor()->Graph()->GraphNodesSize() ; i++ ) {
1412       GraphBase::ComputingNode * aNode = DataFlowEditor()->Graph()->GraphNodes( i ) ;
1413 #if 1
1414       MESSAGE( i << ". Vector : " << aNode->Name() << " Map : "
1415                << DataFlowEditor()->Graph()->GetGraphNode( aNode->Name() )->Name() ) ;
1416 #endif
1417       RetVal = SetNode( RetVal , aNode ) ;
1418     }
1419 #if 0
1420     char * aStateG = "" ;
1421     if ( DataFlowExecutor() ) {
1422       aStateG = (char *) DataFlowExecutor()->StateName( DataFlowExecutor()->AutomatonState() ) ;
1423     }
1424     MESSAGE("Graph_Impl::Nodes GraphState " << aStateG << " CNodes " << RetVal->CNodes.length() ) ;
1425     for ( i = 0 ; i < RetVal->CNodes.length() ; i++ ) {
1426       MESSAGE("           " << RetVal->CNodes[ i ] ->Name() ) ;
1427     }
1428     MESSAGE("FNodes " << RetVal->FNodes.length() ) ;
1429     for ( i = 0 ; i < RetVal->FNodes.length() ; i++ ) {
1430       MESSAGE("           " << RetVal->FNodes[ i ] ->Name() ) ;
1431     }
1432     MESSAGE("INodes " << RetVal->INodes.length() ) ;
1433     for ( i = 0 ; i < RetVal->INodes.length() ; i++ ) {
1434       MESSAGE("           " << RetVal->INodes[ i ] ->Name() ) ;
1435     }
1436     MESSAGE("GNodes " << RetVal->GNodes.length() ) ;
1437     for ( i = 0 ; i < RetVal->GNodes.length() ; i++ ) {
1438       MESSAGE("           " << RetVal->GNodes[ i ] ->Name() ) ;
1439     }
1440     MESSAGE("LNodes " << RetVal->LNodes.length() ) ;
1441     for ( i = 0 ; i < RetVal->LNodes.length() ; i++ ) {
1442       MESSAGE("           " << RetVal->LNodes[ i ] ->Name() ) ;
1443     }
1444     MESSAGE("ELNodes " << RetVal->ELNodes.length() ) ;
1445     for ( i = 0 ; i < RetVal->ELNodes.length() ; i++ ) {
1446       MESSAGE("           " << RetVal->ELNodes[ i ] ->Name() ) ;
1447     }
1448     MESSAGE("SNodes " << RetVal->SNodes.length() ) ;
1449     for ( i = 0 ; i < RetVal->SNodes.length() ; i++ ) {
1450       MESSAGE("           " << RetVal->SNodes[ i ] ->Name() ) ;
1451     }
1452     MESSAGE("ESNodes " << RetVal->ESNodes.length() ) ;
1453     for ( i = 0 ; i < RetVal->ESNodes.length() ; i++ ) {
1454       MESSAGE("           " << RetVal->ESNodes[ i ] ->Name() ) ;
1455     }
1456     MESSAGE("Graphs " << RetVal->Graphs.length() ) ;
1457     for ( i = 0 ; i < RetVal->Graphs.length() ; i++ ) {
1458       MESSAGE("           " << RetVal->Graphs[ i ] ->Name() ) ;
1459     }
1460     if ( DataFlowExecutor() ) {
1461       DataFlowExecutor()->EventList() ;
1462     }
1463 #endif
1464   }
1465   endService( "Graph_Impl::Nodes" );
1466   return ( RetVal._retn() ) ;
1467 }
1468
1469 SUPERV::ListOfLinks * Graph_Impl::GLinks() {
1470   return Links( NULL , NULL ) ;
1471 }
1472
1473 SUPERV::ListOfLinks * Graph_Impl::Links( GraphBase::ComputingNode * theNode ,
1474                                          const char * anInputParam ) {
1475   bool begin = true ;
1476   SUPERV::ListOfLinks_var RetVal = new SUPERV::ListOfLinks ;
1477   RetVal->length( 0 ) ;
1478   if ( !IsMacro() ) {
1479     int i , j , countlink ;
1480     countlink = 0 ;
1481     for ( i = 0 ; i < DataFlowEditor()->Graph()->GraphNodesSize() ; i++ ) {
1482       GraphEditor::InNode * aNode = NULL ;
1483       aNode = (GraphEditor::InNode * ) DataFlowEditor()->Graph()->GraphNodes( i )->GetInNode() ;
1484       bool ToProcess = false ;
1485       if ( theNode == NULL ) {
1486         ToProcess = true ;
1487       }
1488       else {
1489         if ( !strcmp( theNode->Name() , aNode->Name() ) ) {
1490           if ( !theNode->IsEndSwitchNode() ) {
1491             ToProcess = true ;
1492           }
1493         }
1494         else if ( theNode->IsEndSwitchNode() ) {
1495           ToProcess = true ;
1496         }
1497       }
1498       if ( ToProcess ) {
1499         for ( j = 0 ; j < aNode->GetNodeInPortsSize() ; j++ ) {
1500           GraphBase::InPort * anInPort = NULL ;
1501           anInPort = aNode->GetChangeNodeInPort( j ) ;
1502           if ( anInputParam == NULL ||
1503                !strcmp( anInPort->PortName() , anInputParam ) ) {
1504             GraphBase::OutPort * anOutPort = NULL ;
1505             anOutPort = anInPort->GetOutPort() ;
1506             if ( anOutPort && !anOutPort->IsDataStream() ) {
1507               if ( strcmp( anOutPort->NodeName() , Name() ) ) {
1508 //                MESSAGE("Graph_Impl::Links " << anOutPort->NodeName() << "("
1509 //                        << anOutPort->PortName() << ") --> " << aNode->Name() << "("
1510 //                        << anInPort->PortName() << ")" ) ;
1511                 if ( theNode == NULL ||
1512 //PAL8521
1513 //JR 14.02.2005 : Debug : we must see also that links !
1514 //                     ( theNode != NULL && !theNode->IsEndSwitchNode() &&
1515                        !strcmp( theNode->Name() , aNode->Name() ) ) {
1516                   if ( anInPort->IsLoop() || anOutPort->IsLoop() ||
1517                        ( aNode->IsEndLoopNode() && !strcmp( aNode->CoupledNode()->Name() ,
1518                                                             anOutPort->NodeName() ) ) ) {
1519 //                    MESSAGE( "Link " << anOutPort->NodeName() << "("
1520 //                            << anOutPort->PortName() << ") --> " << aNode->Name() << "("
1521 //                            << anInPort->PortName() << ")" << " ignored" ) ;
1522                   }
1523                   else if ( CORBA::is_nil( anOutPort->InPortObjRef( anInPort ) ) ) {
1524                     if ( begin ) {
1525                       beginService( "Graph_Impl::Links" );
1526                       begin = false ;
1527                     }
1528                     GraphEditor::InNode * anOutNode = NULL ;
1529                     anOutNode = (GraphEditor::InNode * ) DataFlowEditor()->Graph()->GetChangeGraphNode( anOutPort->NodeName() )->GetInNode() ;
1530                     if ( anOutNode ) {
1531                       bool Success ;
1532                       Link_Impl * myLink = new Link_Impl( _Orb , _Poa , _ContId ,
1533                                                           instanceName() , interfaceName() ,
1534                                                           DataFlowEditor() ,
1535                                                           aNode ,
1536                                                           anInPort->PortName() ,
1537                                                           anOutNode ,
1538                                                           anOutPort->PortName() ,
1539                                                           false , true , Success ) ;
1540                       if ( Success ) {
1541                         PortableServer::ObjectId * id = myLink->getId() ;
1542                         CORBA::Object_var obj = _poa->id_to_reference(*id);
1543                         SUPERV::Link_var iobject ;
1544                         iobject = SUPERV::Link::_narrow(obj) ;
1545                         RetVal->length( countlink + 1 ) ;
1546                         RetVal[ countlink++ ] = SUPERV::Link::_duplicate( iobject ) ;
1547                         anOutPort->AddInPortObjRef( anInPort , SUPERV::Link::_duplicate( iobject ) ) ;
1548                       }
1549                     }
1550                   }
1551                   else {
1552                     RetVal->length( countlink + 1 ) ;
1553                     RetVal[ countlink++ ] = SUPERV::Link::_duplicate( anOutPort->InPortObjRef( anInPort ) ) ;
1554                   }
1555 //                  MESSAGE( "Link " << anOutPort->NodeName() << "("
1556 //                           << anOutPort->PortName() << ") --> " << aNode->Name() << "("
1557 //                           << anInPort->PortName() << ")" << " selected" ) ;
1558                 }
1559                 else {
1560 //                  MESSAGE( "Link " << anOutPort->NodeName() << "("
1561 //                           << anOutPort->PortName() << ") --> " << aNode->Name() << "("
1562 //                           << anInPort->PortName() << ")" << " skipped" ) ;
1563                 }
1564               }
1565             }
1566           }
1567         }
1568       }
1569       for ( j = 0 ; j < aNode->GetNodeOutPortsSize() ; j++ ) {
1570         GraphBase::OutPort * anOutPort = aNode->GetChangeNodeOutPort( j ) ;
1571 //        MESSAGE( "Graph_Impl::Links " << aNode->Name() << " Out" << j << " " << anOutPort->PortName() << " "
1572 //                 << anOutPort->PortStatus() << " PortConnected to " << anOutPort->InPortsSize() << " InPorts" ) ;
1573         int k ;
1574         for ( k = 0 ; k < anOutPort->InPortsSize() ; k++ ) {
1575           GraphBase::InPort * anInPort = anOutPort->ChangeInPorts( k ) ;
1576 //          MESSAGE( "              -->  In" << k << " " << anInPort->NodeName() << " " << anInPort->PortName()
1577 //                   << " " << anInPort->PortStatus() ) ;
1578           if ( anInPort->IsPortConnected() ) {
1579             GraphBase::ComputingNode * aComputingNode = DataFlowEditor()->Graph()->GetChangeGraphNode( anInPort->NodeName() ) ;
1580             GraphEditor::InNode * toNode = (GraphEditor::InNode * ) aComputingNode->GetInNode() ;
1581             if ( theNode == NULL ||
1582                  !strcmp( theNode->Name() , aNode->Name() ) ) {
1583               if ( !anInPort->IsDataStream() ) {
1584 //PAL8521
1585 //JR 14.02.2005 : Debug : we must see also that links !
1586 //                if ( theNode || ( toNode->IsEndSwitchNode() && !aNode->IsSwitchNode() ) ) {
1587                   if ( anInputParam == NULL ||
1588                        !strcmp( anInPort->PortName() , anInputParam ) ) {
1589                     if ( anInPort->IsLoop() || anOutPort->IsLoop() ||
1590                          ( toNode->IsEndLoopNode() && !strcmp( toNode->CoupledNode()->Name() ,
1591                                                                anOutPort->NodeName() ) ) ) {
1592 //                      MESSAGE( "Link " << anOutPort->NodeName() << "("
1593 //                              << anOutPort->PortName() << ") --> " << toNode->Name() << "("
1594 //                              << anInPort->PortName() << ")" << " ignored" ) ;
1595                     }
1596                     else if ( CORBA::is_nil( anOutPort->InPortObjRef( anInPort ) ) ) {
1597                       if ( begin ) {
1598                         beginService( "Graph_Impl::Links" );
1599                         begin = false ;
1600                       }
1601                       bool Success ;
1602                       Link_Impl * myLink = new Link_Impl( _Orb , _Poa , _ContId ,
1603                                                           instanceName() , interfaceName() ,
1604                                                           DataFlowEditor() ,
1605                                                           toNode ,
1606                                                           anInPort->PortName() ,
1607                                                           aNode ,
1608                                                           anOutPort->PortName() ,
1609                                                           false , true , Success ) ;
1610                       if ( Success ) {
1611                         PortableServer::ObjectId * id = myLink->getId() ;
1612                         CORBA::Object_var obj = _poa->id_to_reference(*id);
1613                         SUPERV::Link_var iobject ;
1614                         iobject = SUPERV::Link::_narrow(obj) ;
1615                         RetVal->length( countlink + 1 ) ;
1616                         RetVal[ countlink++ ] = SUPERV::Link::_duplicate( iobject ) ;
1617                         anOutPort->AddInPortObjRef( anInPort , SUPERV::Link::_duplicate( iobject ) ) ;
1618                       }
1619                     }
1620                     else {
1621                       RetVal->length( countlink + 1 ) ;
1622                       RetVal[ countlink++ ] = SUPERV::Link::_duplicate( SUPERV::Link::_narrow( anOutPort->InPortObjRef( anInPort ) ) ) ;
1623                     }
1624 //                    MESSAGE( "Link " << anOutPort->NodeName() << "("
1625 //                             << anOutPort->PortName() << ") --> " << toNode->Name() << "("
1626 //                             << anInPort->PortName() << ")" << " selected" ) ;
1627                   }
1628                   else {
1629 //                    MESSAGE( "Link " << anOutPort->NodeName() << "("
1630 //                             << anOutPort->PortName() << ") --> " << toNode->Name() << "("
1631 //                             << anInPort->PortName() << ")" << " skipped" ) ;
1632                   }
1633 //              }
1634               }
1635             }
1636           }
1637         }
1638       }
1639     }
1640
1641 #if 0
1642     const char * NodeName = "" ;
1643     const char * InputParamName = "" ;
1644     if ( theNode ) {
1645       NodeName = theNode->Name() ;
1646     }
1647     if ( anInputParam ) {
1648       InputParamName = anInputParam ;
1649     }
1650     MESSAGE( RetVal->length() << " Links of Node " << NodeName << " and of InPort " << InputParamName ) ;
1651     for ( i = 0 ; i < (int ) RetVal->length() ; i++ ) {
1652       MESSAGE( "Link " << RetVal[i]->OutPort()->Node()->Name() << "("
1653                << RetVal[i]->OutPort()->Name() << ") --> "
1654                << RetVal[i]->InPort()->Node()->Name() << "("
1655                << RetVal[i]->InPort()->Name() << ")" ) ;
1656     }
1657 #endif
1658     if ( !begin ) {
1659       endService( "Graph_Impl::Links" );
1660     }
1661   }
1662   return ( RetVal._retn() ) ;
1663 }
1664
1665 Engines::Component_ptr Graph_Impl::ComponentRef( const char * aComputerContainer ,
1666                                                  const char * aComponentName ) {
1667   Engines::Component_var objComponent = Engines::Component::_nil() ;
1668   if ( !IsMacro() ) {
1669     Engines::Container_var myContainer ;
1670     DataFlowEditor()->Graph()->StartComponent( 0 , aComputerContainer , aComponentName ,
1671                                                myContainer , objComponent ) ;
1672   }
1673   return Engines::Component::_duplicate( objComponent ) ;
1674 }
1675
1676 char * Graph_Impl::Messages() {
1677   beginService( "Graph_Impl::Messages" );
1678   string Messages  ;
1679   MESSAGE( "Graph_Impl::Messages IsEditing " << DataFlowEditor()->IsEditing() ) ;
1680   Messages = DataFlowEditor()->Graph()->Messages() ;
1681   DataFlowEditor()->Graph()->ReSetMessages() ;
1682   if ( DataFlowExecutor() ) {
1683     Messages += DataFlowExecutor()->Graph()->Messages() ;
1684     DataFlowExecutor()->Graph()->ReSetMessages() ;
1685   }
1686   endService( "Graph_Impl::Messages" );
1687   return ( CORBA::string_dup( Messages.c_str() ) ) ;
1688 }
1689
1690 bool Graph_Impl::IsValid() {
1691 //  beginService( "Graph_Impl::IsValid" );
1692   bool RetVal = false ;
1693   if ( !IsMacro() ) {
1694     RetVal = DataFlowEditor()->IsValid() ;
1695   }
1696 //  endService( "Graph_Impl::IsValid" );
1697   return RetVal ;
1698 }
1699 bool Graph_Impl::IsExecutable() {
1700 //  beginService( "Graph_Impl::IsExecutable" );
1701   bool RetVal = false ;
1702
1703   // asv : 15.11.04 : added "&& GraphMacroLevel() == 0" -> 
1704   // subgraphs are not executable by themselves, RetVal = false..
1705   if ( !IsMacro() && DataFlowEditor()->Graph()->GraphMacroLevel() == 0 ) {
1706     RetVal = DataFlowEditor()->IsExecutable() ;
1707   }
1708 //  endService( "Graph_Impl::IsExecutable" );
1709   return RetVal ;
1710 }
1711
1712 bool Graph_Impl::IsEditing() {
1713 /*
1714   bool RetVal = false ;
1715   if ( !IsMacro() ) {
1716     RetVal = DataFlowEditor()->IsEditing() ;
1717   }
1718   return RetVal ;
1719 */
1720   if ( IsMacro() )
1721     return false;
1722   return !IsExecuting();
1723 }
1724
1725 bool Graph_Impl::IsExecuting() {
1726 /*
1727   bool RetVal = false ;
1728   if ( !IsMacro() ) {
1729     RetVal = !DataFlowEditor()->IsEditing() ;
1730   }
1731   return RetVal ;
1732 */
1733   return CNode_Impl::IsExecuting();
1734 }
1735
1736 bool Graph_Impl::IsReadOnly() {
1737 //  beginService( "Graph_Impl::IsExecuting" );
1738   bool RetVal = false ;
1739   if ( !IsMacro() ) {
1740     RetVal = DataFlowEditor()->IsReadOnly() ;
1741   }
1742 //  endService( "Graph_Impl::IsExecuting" );
1743   return RetVal ;
1744 }
1745
1746 long Graph_Impl::LevelMax() {
1747 //  beginService( "Graph_Impl::LevelMax" );
1748   long RetVal = 0 ;
1749   if ( !IsMacro() ) {
1750     RetVal = DataFlowEditor()->LevelMax() ;
1751   }
1752 //  endService( "Graph_Impl::LevelMax" );
1753   return RetVal ;
1754 }
1755 SUPERV::ListOfNodes * Graph_Impl::LevelNodes(long aLevel ) {
1756 //  beginService( "Graph_Impl::LevelNodes" );
1757   SUPERV::ListOfNodes_var RetVal = new SUPERV::ListOfNodes;
1758   if ( !IsMacro() ) {
1759     int i ;
1760     SUPERV::ListOfStrings_var Nodes = DataFlowEditor()->LevelNodes( aLevel ) ;
1761 //  RetVal->length( Nodes->length() );
1762     for ( i = 0 ; i < (int ) Nodes->length() ; i++ ) {
1763 //    char * aNode = Nodes[ i ] ;
1764       GraphBase::ComputingNode * aNode = DataFlowEditor()->Graph()->GetChangeGraphNode( Nodes[ i ] ) ;
1765       RetVal = SetNode( RetVal , aNode ) ;
1766 //    cout << "Graph_Impl::LevelNodes( " << aLevel << " ) " << aNode->Name() << endl ;
1767 //    CNode_Impl * myNode = new CNode_Impl( _Orb , _Poa , _ContId ,
1768 //                               instanceName() , interfaceName() ,
1769 //                               DataFlowEditor() ,
1770 //                               (GraphEditor::InNode * ) DataFlowEditor()->GetChangeGraphNode( aNode )->GetInNode() ) ;
1771 //    PortableServer::ObjectId * id = myNode->getId() ;
1772 //    CORBA::Object_var obj = _poa->id_to_reference(*id);
1773 //    SUPERV::CNode_var iobject ;
1774 //    iobject = SUPERV::CNode::_narrow(obj) ;
1775 //    RetVal[i] = SUPERV::CNode::_duplicate( iobject ) ;
1776     }
1777   }
1778 //  endService( "Graph_Impl::LevelNodes" );
1779   return ( RetVal._retn() ) ;
1780 }
1781 long Graph_Impl::ThreadsMax() {
1782 //  beginService( "Graph_Impl::ThreadsMax" );
1783   long RetVal = 0 ;
1784   if ( !IsMacro() ) {
1785     RetVal =  DataFlowEditor()->ThreadsMax() ;
1786   }
1787 //  endService( "Graph_Impl::ThreadsMax" );
1788   return RetVal ;
1789 }
1790 long Graph_Impl::Threads() {
1791 //  beginService( "Node_Impl::Threads" );
1792   long RetVal = false ;
1793   if ( !IsMacro() ) {
1794     RetVal = DataFlowExecutor()->Threads() ;
1795   }
1796 //  endService( "Node_Impl::Threads" );
1797   return RetVal ;
1798 }
1799 long Graph_Impl::SuspendedThreads() {
1800 //  beginService( "Node_Impl::SuspendedThreads" );
1801   long RetVal = false ;
1802   if ( !IsMacro() ) {
1803     RetVal = DataFlowExecutor()->SuspendedThreads() ;
1804   }
1805 //  endService( "Node_Impl::SuspendedThreads" );
1806   return RetVal ;
1807 }
1808
1809 bool Graph_Impl::Begin() {
1810   bool RetVal = false ;
1811   // mkr : IPAL11408 : "Run()" method returns "0" after trying to start graph execution.
1812   // If we call Run() method from Python script two times for one graph, we have to be sure,
1813   // that first execution of this graph ended (i.e. GraphEditor::DataFlow::Editing() method was called).
1814   // Otherwise, the second call of Run() method will return 0. Therefore, we have to waiting for
1815   // the first execution finish. 
1816   // NB! From GUI we cann't run dataflow if its previous execution not finished.
1817   while ( !DataFlowEditor()->IsEditing() ) {} // waiting for the previous execution of this graph ended
1818   if ( DataFlowEditor()->IsEditing() ) {
1819     if ( pthread_mutex_lock( &_MutexExecutorWait ) ) {
1820       perror("pthread_mutex_lock _MutexExecutorWait") ;
1821       exit( 0 ) ;
1822     }
1823     DataFlowEditor()->Graph()->ReSetMessages() ; // ==> Only one set of errors messages ...
1824     if ( DataFlowExecutor() ) {
1825       MESSAGE( "Graph_Impl::Begin " << DataFlowExecutor()->Threads() << " threads" )
1826       Editing(); // just in case it was not called before by GUI..
1827     }
1828     if ( DataFlowEditor()->IsExecutable() ) {
1829       DataFlowEditor()->EditedAfterExecution( false ) ;
1830       GraphBase::ListOfSGraphs * myListOfGraphs = DataFlowEditor()->GetDataFlows() ;
1831
1832       GraphExecutor::DataFlow * aDataFlowExecutor ;
1833       string dbgfile ;
1834       CreateExecutor( _Orb , instanceName() , (*myListOfGraphs)[ 0 ].Info.theName.c_str() , Kind() ,
1835                       dbgfile , &aDataFlowExecutor ) ;
1836
1837       MESSAGE( "Graph_Impl::Begin : DataFlowExecutor created" );
1838       if ( !CORBA::is_nil( LoadDataFlows( aDataFlowExecutor, myListOfGraphs , 0 ) ) ) 
1839         RetVal = true ;
1840   
1841       if ( RetVal )
1842         DataFlowExecutor()->Graph()->SetObjImpl( DataFlowEditor()->Graph()->ObjImpl() ) ;
1843     
1844     }
1845     if ( pthread_mutex_unlock( &_MutexExecutorWait ) ) {
1846       perror("pthread_mutex_unlock _MutexExecutorWait") ;
1847       exit( 0 ) ;
1848     }
1849   }
1850   return RetVal ;
1851 }
1852
1853 /**
1854  * run() : was created in order to reduce code dublication, Run() and Start() do the same thing
1855  *         except for final call to Executor->Run( bool ) - Run() calls with AndSuspend=false
1856  *         and Start() calls with AndSuspend=true flag.  The rest of the actions are the same:
1857  *         1. if a MacroNode's coupled graph (MacroLevel>0) -> simply Run() it.
1858  *         2. if a Supergraph -> create executor, load dataflow in it (done in Begin()), then - Run() it.
1859  */
1860 bool Graph_Impl::run( const bool andSuspend ) {
1861   bool RetVal = false ;
1862
1863   // if Editing or Finished state, and, naturally, 'this' is not a MacroNode. 
1864   if ( ( DataFlowEditor()->IsEditing() || IsDone() ) && !IsMacro() ) {
1865
1866     if ( DataFlowEditor()->Graph()->GraphMacroLevel() ) {
1867       // MacroGraph's Executor was created in recursive function LoadDataflows(Executor), called from
1868       // Begin() of Supergraph.  See Begin() and LoadDataflows(Executor) for details.
1869       RetVal = true ; 
1870     }
1871     else {
1872       // Create Executor instance and its datamodel
1873       RetVal = Begin() ;
1874     }
1875     if ( RetVal ) {
1876       // Set Executing flag to avoid edition operations during execution, EditedAfterExecution=false
1877       DataFlowEditor()->Executing() ;
1878       DataFlowEditor()->EditedAfterExecution( false ); 
1879
1880       // THE MAIN RUN METHOD
1881       RetVal = DataFlowExecutor()->Run( andSuspend );
1882     }
1883   }
1884   return RetVal ;
1885
1886 }
1887
1888 /**
1889  * Run() - called on "Execute" command
1890  */
1891 bool Graph_Impl::Run() {
1892   beginService( "Graph_Impl::Run" );
1893   bool RetVal = run( /*andSuspend=*/false ) ;
1894   MESSAGE( "Graph_Impl::Run " << DataFlowEditor()->Graph()->Name() << " RetVal " << RetVal ) ;
1895   endService( "Graph_Impl::Run" );
1896   return RetVal ;
1897 }
1898
1899 /**
1900  * Run() - called on "Execute step-by-step" command
1901  */
1902 bool Graph_Impl::Start() {
1903   beginService( "Graph_Impl::Start" );
1904   bool RetVal = run( /*andSuspend=*/true ) ;
1905   MESSAGE( "Graph_Impl::Start " << DataFlowEditor()->Graph()->Name() << " RetVal " << RetVal ) ;
1906   endService( "Graph_Impl::Start" );
1907   return RetVal ;
1908 }
1909
1910 bool Graph_Impl::EventNoW( SUPERV::CNode_out aNode ,
1911                            SUPERV::GraphEvent & anEvent ,
1912                            SUPERV::GraphState & aState ) {
1913   bool RetVal = false ;
1914   char * aNodeName = NULL ;
1915   SUPERV::GraphEvent theEvent = SUPERV::UndefinedEvent ;
1916   SUPERV::GraphState theState = SUPERV::UndefinedState ;
1917   if ( pthread_mutex_lock( &_MutexExecutorWait ) ) {
1918     perror("pthread_mutex_lock _MutexExecutorWait") ;
1919     exit( 0 ) ;
1920   }
1921   if ( DataFlowExecutor() && !IsMacro() &&
1922        ( DataFlowExecutor()->GetListSize() || !DataFlowEditor()->IsEditing() ) ) {
1923     RetVal = DataFlowExecutor()->Event( & aNodeName , theEvent , theState , false ) ;
1924 //  endService( "Graph_Impl::Event" );
1925     if ( strlen( aNodeName ) ) {
1926       if ( strcmp( aNodeName , Name() ) ) {
1927         aNode = Node( aNodeName ) ;
1928       }
1929       else {
1930         aNode = Node() ;
1931       }
1932     }
1933     else {
1934       aNode = SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
1935     }
1936 //    cout << "Graph_Impl::EventNoW " << aNode->Name() << " QSize " << DataFlowExecutor()->GetListSize() << endl ;
1937   }
1938   else {
1939 //    cout << "Graph_Impl::EventNoW NO DataFlowExecutor() or QSize=0 " << endl ;
1940   }
1941   if ( pthread_mutex_unlock( &_MutexExecutorWait ) ) {
1942     perror("pthread_mutex_unlock _MutexExecutorWait") ;
1943     exit( 0 ) ;
1944   }
1945   anEvent = theEvent ;
1946   aState = theState ;
1947   return RetVal ;
1948 }
1949
1950 bool Graph_Impl::Event( SUPERV::CNode_out aNode ,
1951                         SUPERV::GraphEvent & anEvent ,
1952                         SUPERV::GraphState & aState ) {
1953 //  beginService( "Graph_Impl::Event" );
1954   bool RetVal = false ;
1955   char * aNodeName = NULL ;
1956   SUPERV::GraphEvent theEvent = SUPERV::UndefinedEvent ;
1957   SUPERV::GraphState theState = SUPERV::UndefinedState ;
1958   if ( pthread_mutex_lock( &_MutexExecutorWait ) ) {
1959     perror("pthread_mutex_lock _MutexExecutorWait") ;
1960     exit( 0 ) ;
1961   }
1962   if ( DataFlowExecutor() && !IsMacro() &&
1963        ( DataFlowExecutor()->GetListSize() || !DataFlowEditor()->IsEditing() ) ){
1964     RetVal = DataFlowExecutor()->Event( & aNodeName , theEvent , theState ) ;
1965 //  endService( "Graph_Impl::Event" );
1966     if ( strlen( aNodeName ) ) {
1967       if ( strcmp( aNodeName , Name() ) ) {
1968         aNode = Node( aNodeName ) ;
1969       }
1970       else {
1971         aNode = Node() ;
1972       }
1973     }
1974     else {
1975       aNode = SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
1976     }
1977 //    cout << "Graph_Impl::Event " << aNode->Name() << " QSize " << DataFlowExecutor()->GetListSize() << endl ;
1978   }
1979   else {
1980 //    cout << "Graph_Impl::Event NO DataFlowExecutor() or QSize=0 " << endl ;
1981   }
1982   if ( pthread_mutex_unlock( &_MutexExecutorWait ) ) {
1983     perror("pthread_mutex_unlock _MutexExecutorWait") ;
1984     exit( 0 ) ;
1985   }
1986   anEvent = theEvent ;
1987   aState = theState ;
1988
1989   return RetVal ;
1990 }
1991
1992 bool Graph_Impl::EventW( SUPERV::CNode_out aNode ,
1993                          SUPERV::GraphEvent & anEvent ,
1994                          SUPERV::GraphState & aState ) {
1995 //  beginService( "Graph_Impl::EventW" );
1996   bool RetVal = false ;
1997   char * aNodeName = NULL ;
1998   SUPERV::GraphEvent theEvent = SUPERV::UndefinedEvent ;
1999   SUPERV::GraphState theState = SUPERV::UndefinedState ;
2000   if ( pthread_mutex_lock( &_MutexExecutorWait ) ) {
2001     perror("pthread_mutex_lock _MutexExecutorWait") ;
2002     exit( 0 ) ;
2003   }
2004   if ( DataFlowExecutor() && !IsMacro() &&
2005        ( DataFlowExecutor()->GetListSize() || !DataFlowEditor()->IsEditing() ) ) {
2006     RetVal = DataFlowExecutor()->EventW( & aNodeName , theEvent , theState ) ;
2007     if ( RetVal && strcmp( aNodeName , Name() ) ) {
2008       aNode = Node( aNodeName ) ;
2009     }
2010     else {
2011       aNode = Node() ;
2012     }
2013   }
2014   if ( pthread_mutex_unlock( &_MutexExecutorWait ) ) {
2015     perror("pthread_mutex_unlock _MutexExecutorWait") ;
2016     exit( 0 ) ;
2017   }
2018   anEvent = theEvent ;
2019   aState = theState ;
2020 //  endService( "Graph_Impl::EventW" );
2021   return RetVal ;
2022 }
2023
2024 long Graph_Impl::EventQSize() {
2025 //  beginService( "Graph_Impl::EventQSize" );
2026   long QSize = -1 ;
2027   if ( pthread_mutex_lock( &_MutexExecutorWait ) ) {
2028     perror("pthread_mutex_lock _MutexExecutorWait") ;
2029     exit( 0 ) ;
2030   }
2031   if ( DataFlowExecutor() && !IsMacro() ) {
2032     QSize = DataFlowExecutor()->EventQSize() ;
2033   }
2034   if ( pthread_mutex_unlock( &_MutexExecutorWait ) ) {
2035     perror("pthread_mutex_unlock _MutexExecutorWait") ;
2036     exit( 0 ) ;
2037   }
2038 //  endService( "Graph_Impl::EventQSize" );
2039   return QSize ;
2040 }
2041
2042 long Graph_Impl::LastLevelDone() {
2043 //  beginService( "Graph_Impl::LastLevelDone" );
2044   if ( pthread_mutex_lock( &_MutexExecutorWait ) ) {
2045     perror("pthread_mutex_lock _MutexExecutorWait") ;
2046     exit( 0 ) ;
2047   }
2048   long RetVal = 0 ;
2049   if ( DataFlowExecutor() && !IsMacro() ) {
2050     RetVal = DataFlowExecutor()->LastLevelDone() ;
2051   }
2052   if ( pthread_mutex_unlock( &_MutexExecutorWait ) ) {
2053     perror("pthread_mutex_unlock _MutexExecutorWait") ;
2054     exit( 0 ) ;
2055   }
2056 //  endService( "Graph_Impl::LastLevelDone" );
2057   return RetVal ;
2058 }
2059
2060 long Graph_Impl::SubGraphsNumber() {
2061 //  beginService( "Graph_Impl::SubGraphsNumber" );
2062   long RetVal = 0 ;
2063   if ( DataFlowEditor()->IsExecutable() && !IsMacro() ) {
2064     RetVal = DataFlowEditor()->SubGraphsNumber() ;
2065   }
2066 //  endService( "Graph_Impl::SubGraphsNumber" );
2067   return RetVal ;
2068 }
2069
2070 SUPERV::ListOfNodes * Graph_Impl::SubGraphsNodes( const long aSubGraphNumber ) {
2071   beginService( "Graph_Impl::SubGraphsNodes" );
2072   SUPERV::ListOfNodes_var RetVal = new SUPERV::ListOfNodes ;
2073   if ( DataFlowEditor()->IsEditing() && !IsMacro() ) {
2074     SUPERV::ListOfNodes * aGraphNodes = Nodes() ;
2075     int i ;
2076 // ComputingNodes
2077     for ( i = 0 ; i < (int ) aGraphNodes->CNodes.length() ; i++ ) {
2078       SUPERV::CNode_var aNode = (aGraphNodes->CNodes)[ i ] ;
2079       if ( aNode->SubGraph() == aSubGraphNumber ) {
2080         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2081       }
2082     }
2083 // FactoryNodes
2084     for ( i = 0 ; i < (int ) aGraphNodes->FNodes.length() ; i++ ) {
2085       SUPERV::FNode_var aNode = (aGraphNodes->FNodes)[ i ] ;
2086       if ( aNode->SubGraph() == aSubGraphNumber ) {
2087         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2088       }
2089     }
2090 // InLineNodes
2091     for ( i = 0 ; i < (int ) aGraphNodes->INodes.length() ; i++ ) {
2092       SUPERV::INode_var aNode = (aGraphNodes->INodes)[ i ] ;
2093       if ( aNode->SubGraph() == aSubGraphNumber ) {
2094         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2095       }
2096     }
2097 // GOTONodes
2098     for ( i = 0 ; i < (int ) aGraphNodes->GNodes.length() ; i++ ) {
2099       SUPERV::GNode_var aNode = (aGraphNodes->GNodes)[ i ] ;
2100       if ( aNode->SubGraph() == aSubGraphNumber ) {
2101         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2102       }
2103     }
2104 // LoopNodes
2105     for ( i = 0 ; i < (int ) aGraphNodes->LNodes.length() ; i++ ) {
2106       SUPERV::LNode_var aNode = (aGraphNodes->LNodes)[ i ] ;
2107       if ( aNode->SubGraph() == aSubGraphNumber ) {
2108         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2109       }
2110     }
2111 // EndLoopNodes
2112     for ( i = 0 ; i < (int ) aGraphNodes->ELNodes.length() ; i++ ) {
2113       SUPERV::ELNode_var aNode = (aGraphNodes->ELNodes)[ i ] ;
2114       if ( aNode->SubGraph() == aSubGraphNumber ) {
2115         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2116       }
2117     }
2118 // SwitchNodes
2119     for ( i = 0 ; i < (int ) aGraphNodes->SNodes.length() ; i++ ) {
2120       SUPERV::SNode_var aNode = (aGraphNodes->SNodes)[ i ] ;
2121       if ( aNode->SubGraph() == aSubGraphNumber ) {
2122         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2123       }
2124     }
2125 // EndSwitchNodes
2126     for ( i = 0 ; i < (int ) aGraphNodes->ESNodes.length() ; i++ ) {
2127       SUPERV::ESNode_var aNode = (aGraphNodes->ESNodes)[ i ] ;
2128       if ( aNode->SubGraph() == aSubGraphNumber ) {
2129         RetVal = SetNode( RetVal , DataFlowEditor()->Graph()->GetChangeGraphNode( aNode->Name() ) ) ;
2130       }
2131     }
2132   }
2133   endService( "Graph_Impl::SubGraphsNodes" );
2134   return ( RetVal._retn() ) ;
2135 }
2136
2137 bool Graph_Impl::Merge(const SUPERV::Graph_ptr aGraph ) {
2138 //  beginService( "Graph_Impl::Merge" );
2139   bool RetVal = true ;
2140   if ( !IsMacro() ) {
2141     map< string , int > aMapOfNodes ;
2142     RetVal = Merge( aGraph , aMapOfNodes ) ;
2143   }
2144 //  endService( "Graph_Impl::Merge" );
2145   return RetVal ;
2146 }
2147
2148 bool Graph_Impl::Merge(const SUPERV::Graph_ptr aGraph , map< string , int > & aMapOfNodes ) {
2149   beginService( "Graph_Impl::Merge" );
2150   bool RetVal = true ;
2151   if ( DataFlowEditor()->IsEditing() && !IsMacro() ) {
2152     SUPERV::ListOfNodes * aGraphNodes = aGraph->Nodes() ;
2153     int i ;
2154     SUPERV::Port_ptr aPort ;
2155 //    SUPERV::StreamPort_ptr aStreamPort ;
2156 // ComputingNodes
2157     for ( i = 0 ; i < (int ) aGraphNodes->CNodes.length() ; i++ ) {
2158       SUPERV::CNode_var aNode = (aGraphNodes->CNodes)[ i ] ;
2159       SUPERV::CNode_ptr myNode = CNode( *(aNode->Service()) ) ;
2160       if ( !CORBA::is_nil( myNode ) ) {
2161         myNode->SetName( aNode->Name() ) ;
2162         myNode->SetAuthor( aNode->Author() ) ;
2163         myNode->SetComment( aNode->Comment() ) ;
2164         myNode->Coords( aNode->X() , aNode->Y() ) ;
2165         string * aNodetheName = new string( aNode->Name() ) ;
2166         aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( myNode->Name() ) ;
2167         delete aNodetheName ;
2168         RetVal = true ;
2169       }
2170       else {
2171         RetVal = false ;
2172         break ;
2173       }
2174 // The following informations are now in the service from SALOME_ModuleCatalog
2175 //      SUPERV::ListOfStreamPorts myStreamPorts = *(aNode->StreamPorts()) ;
2176 //      int j ;
2177 //      for ( j = 0 ; j < (int ) myStreamPorts.length() ; j++ ) {
2178 //        if ( myStreamPorts[ j ]->IsInput() && myStreamPorts[ j ]->IsDataStream() ) {
2179 //          aStreamPort = myNode->InStreamPort( myStreamPorts[ j ]->Name() , myStreamPorts[ j ]->Type() ) ;
2180 //        }
2181 //        else if ( myStreamPorts[ j ]->IsDataStream() ) {
2182 //          aStreamPort = myNode->OutStreamPort( myStreamPorts[ j ]->Name() , myStreamPorts[ j ]->Type() ) ;
2183 //        }
2184 //      }
2185     }
2186 // FactoryNodes
2187     if ( RetVal ) {
2188       for ( i = 0 ; i < (int ) aGraphNodes->FNodes.length() ; i++ ) {
2189         SUPERV::FNode_var aNode = (aGraphNodes->FNodes)[ i ] ;
2190         SUPERV::FNode_ptr myNode = FNode( aNode->GetComponentName() ,
2191                                           aNode->GetInterfaceName() ,
2192                                           *(aNode->Service()) ,
2193                                           aNode->IsCimpl() ) ; // mkr : PAL11273
2194         if ( !CORBA::is_nil( myNode ) ) {
2195           myNode->SetName( aNode->Name() ) ;
2196           myNode->SetAuthor( aNode->Author() ) ;
2197           myNode->SetComment( aNode->Comment() ) ;
2198           myNode->Coords( aNode->X() , aNode->Y() ) ;
2199           string * aNodetheName = new string( aNode->Name() ) ;
2200           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( myNode->Name() ) ;
2201           delete aNodetheName ;
2202           RetVal = true ;
2203         }
2204         else {
2205           RetVal = false ;
2206           break ;
2207         }
2208 // The following informations are now in the service from SALOME_ModuleCatalog
2209 //        SUPERV::ListOfStreamPorts myStreamPorts = *(aNode->StreamPorts()) ;
2210 //        int j ;
2211 //        for ( j = 0 ; j < (int ) myStreamPorts.length() ; j++ ) {
2212 //          if ( myStreamPorts[ j ]->IsInput() && myStreamPorts[ j ]->IsDataStream() ) {
2213 //            aStreamPort = myNode->InStreamPort( myStreamPorts[ j ]->Name() , myStreamPorts[ j ]->Type() ) ;
2214 //          }
2215 //          else if ( myStreamPorts[ j ]->IsDataStream() ) {
2216 //            aStreamPort = myNode->OutStreamPort( myStreamPorts[ j ]->Name() , myStreamPorts[ j ]->Type() ) ;
2217 //          }
2218 //        }
2219       }
2220     }
2221 // InLineNodes
2222     if ( RetVal ) {
2223       for ( i = 0 ; i < (int ) aGraphNodes->INodes.length() ; i++ ) {
2224         SUPERV::INode_var aNode = (aGraphNodes->INodes)[ i ] ;
2225         SUPERV::INode_ptr myNode = INode( aNode->PyFuncName() , *(aNode->PyFunction()) ) ;
2226         if ( !CORBA::is_nil( myNode ) ) {
2227           myNode->SetName( aNode->Name() ) ;
2228           myNode->SetAuthor( aNode->Author() ) ;
2229           myNode->SetComment( aNode->Comment() ) ;
2230           myNode->Coords( aNode->X() , aNode->Y() ) ;
2231           string * aNodetheName = new string( aNode->Name() ) ;
2232           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( myNode->Name() ) ;
2233           delete aNodetheName ;
2234           SUPERV::ListOfPorts myPorts = *(aNode->Ports()) ;
2235           int j ;
2236           for ( j = 0 ; j < (int ) myPorts.length() ; j++ ) {
2237             if ( myPorts[ j ]->IsInput() ) {
2238               aPort = myNode->InPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2239             }
2240             else {
2241               aPort = myNode->OutPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2242             }
2243           }
2244           if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
2245             SUPERV::ListOfStreamPorts myStreamPorts = *(aNode->StreamPorts()) ;
2246             for ( j = 0 ; j < (int ) myStreamPorts.length() ; j++ ) {
2247               if ( myStreamPorts[ j ]->IsInput() ) {
2248                 aPort = myNode->InStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2249               }
2250               else {
2251                 aPort = myNode->OutStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2252               }
2253             }
2254           }
2255           RetVal = true ;
2256         }
2257         else {
2258           RetVal = false ;
2259           break ;
2260         }
2261       }
2262     }
2263 // GOTONodes
2264     if ( RetVal ) {
2265       for ( i = 0 ; i < (int ) aGraphNodes->GNodes.length() ; i++ ) {
2266         SUPERV::GNode_var aNode = (aGraphNodes->GNodes)[ i ] ;
2267         SUPERV::GNode_ptr myNode = GNode( aNode->PyFuncName() , *(aNode->PyFunction()) , aNode->Coupled()->Name() ) ;
2268         if ( !CORBA::is_nil( myNode ) ) {
2269           myNode->SetName( aNode->Name() ) ;
2270           myNode->SetAuthor( aNode->Author() ) ;
2271           myNode->SetComment( aNode->Comment() ) ;
2272           myNode->Coords( aNode->X() , aNode->Y() ) ;
2273           string * aNodetheName = new string( aNode->Name() ) ;
2274           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( myNode->Name() ) ;
2275           delete aNodetheName ;
2276           SUPERV::ListOfPorts myPorts = *(aNode->Ports()) ;
2277           int j ;
2278           for ( j = 0 ; j < (int ) myPorts.length() ; j++ ) {
2279             if ( myPorts[ j ]->IsInput() ) {
2280               aPort = myNode->InPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2281             }
2282             else {
2283               aPort = myNode->OutPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2284             }
2285           }
2286           if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
2287             SUPERV::ListOfStreamPorts myStreamPorts = *(aNode->StreamPorts()) ;
2288             for ( j = 0 ; j < (int ) myStreamPorts.length() ; j++ ) {
2289               if ( myStreamPorts[ j ]->IsInput() ) {
2290                 aPort = myNode->InStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2291               }
2292               else {
2293                 aPort = myNode->OutStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2294               }
2295             }
2296           }
2297           RetVal = true ;
2298         }
2299         else {
2300           RetVal = false ;
2301           break ;
2302         }
2303       }
2304     }
2305 // LoopNodes
2306     if ( RetVal ) {
2307       for ( i = 0 ; i < (int ) aGraphNodes->LNodes.length() ; i++ ) {
2308         SUPERV::LNode_var aNode = (aGraphNodes->LNodes)[ i ] ;
2309         SUPERV::INode_ptr anEndOfLoop ;
2310         SUPERV::LNode_ptr myNode = LNode( aNode->PyInitName() ,
2311                                           *(aNode->PyInit()) ,
2312                                           aNode->PyMoreName() ,
2313                                           *(aNode->PyMore()) ,
2314                                           aNode->PyNextName() ,
2315                                           *(aNode->PyNext()) ,
2316                                           anEndOfLoop ) ;
2317         if ( !CORBA::is_nil( myNode ) ) {
2318           myNode->SetName( aNode->Name() ) ;
2319           myNode->SetAuthor( aNode->Author() ) ;
2320           myNode->SetComment( aNode->Comment() ) ;
2321           myNode->Coords( aNode->X() , aNode->Y() ) ;
2322           string * aNodetheName = new string( aNode->Name() ) ;
2323           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( myNode->Name() ) ;
2324           delete aNodetheName ;
2325           SUPERV::ListOfPorts myPorts = *(aNode->Ports()) ;
2326           int j ;
2327           for ( j = 0 ; j < (int ) myPorts.length() ; j++ ) {
2328             if ( myPorts[ j ]->IsInput() ) {
2329               aPort = myNode->InPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2330             }
2331             else {
2332               aPort = myNode->OutPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2333             }
2334           }
2335           if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
2336             SUPERV::ListOfStreamPorts myStreamPorts = *(aNode->StreamPorts()) ;
2337             for ( j = 0 ; j < (int ) myStreamPorts.length() ; j++ ) {
2338               if ( myStreamPorts[ j ]->IsInput() ) {
2339                 aPort = myNode->InStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2340               }
2341               else {
2342                 aPort = myNode->OutStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2343               }
2344             }
2345           }
2346           SUPERV::INode_ptr myEndOfLoop = aNode->Coupled() ;
2347           anEndOfLoop->SetName( myEndOfLoop->Name() ) ;
2348           anEndOfLoop->SetAuthor( myEndOfLoop->Author() ) ;
2349           anEndOfLoop->SetComment( myEndOfLoop->Comment() ) ;
2350           anEndOfLoop->Coords( myEndOfLoop->X() , myEndOfLoop->Y() ) ;
2351           anEndOfLoop->SetPyFunction( myEndOfLoop->PyFuncName() , *(myEndOfLoop->PyFunction()) ) ;
2352           aNodetheName = new string( myEndOfLoop->Name() ) ;
2353           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( anEndOfLoop->Name() ) ;
2354           if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
2355             SUPERV::ListOfStreamPorts myStreamLoopPorts = *(myEndOfLoop->StreamPorts()) ;
2356             for ( j = 0 ; j < (int ) myStreamLoopPorts.length() ; j++ ) {
2357               if ( myStreamLoopPorts[ j ]->IsInput() ) {
2358                 aPort = myNode->InStreamPort( myStreamLoopPorts[ j ]->Name() , StringToDataStreamType( myStreamLoopPorts[ j ]->Type() ) , myStreamLoopPorts[ j ]->Dependency() ) ;
2359               }
2360               else {
2361                 aPort = myNode->OutStreamPort( myStreamLoopPorts[ j ]->Name() , StringToDataStreamType( myStreamLoopPorts[ j ]->Type() ) , myStreamLoopPorts[ j ]->Dependency() ) ;
2362               }
2363             }
2364           }
2365           delete aNodetheName ;
2366           RetVal = true ;
2367         }
2368         else {
2369           RetVal = false ;
2370           break ;
2371         }
2372       }
2373     }
2374 // SwitchNodes
2375     if ( RetVal ) {
2376       for ( i = 0 ; i < (int ) aGraphNodes->SNodes.length() ; i++ ) {
2377         SUPERV::SNode_var aNode = (aGraphNodes->SNodes)[ i ] ;
2378         SUPERV::INode_ptr anEndOfSwitch ;
2379         SUPERV::SNode_ptr myNode = SNode( aNode->PyFuncName() , *(aNode->PyFunction()) , anEndOfSwitch ) ;
2380         if ( !CORBA::is_nil( myNode ) ) {
2381           myNode->SetName( aNode->Name() ) ;
2382           myNode->SetAuthor( aNode->Author() ) ;
2383           myNode->SetComment( aNode->Comment() ) ;
2384           myNode->Coords( aNode->X() , aNode->Y() ) ;
2385           string * aNodetheName = new string( aNode->Name() ) ;
2386           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( myNode->Name() ) ;
2387           delete aNodetheName ;
2388           SUPERV::ListOfPorts myPorts = *(aNode->Ports()) ;
2389           int j ;
2390           for ( j = 0 ; j < (int ) myPorts.length() ; j++ ) {
2391             if ( myPorts[ j ]->IsInput() ) {
2392               aPort = myNode->InPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2393             }
2394             else {
2395               aPort = myNode->OutPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ;
2396             }
2397           }
2398           if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
2399             SUPERV::ListOfStreamPorts myStreamPorts = *(aNode->StreamPorts()) ;
2400             for ( j = 0 ; j < (int ) myStreamPorts.length() ; j++ ) {
2401               if ( myStreamPorts[ j ]->IsInput() ) {
2402                 aPort = myNode->InStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2403               }
2404               else {
2405                 aPort = myNode->OutStreamPort( myStreamPorts[ j ]->Name() , StringToDataStreamType( myStreamPorts[ j ]->Type() ) , myStreamPorts[ j ]->Dependency() ) ;
2406               }
2407             }
2408           }
2409           SUPERV::INode_ptr myEndOfSwitch = aNode->Coupled() ;
2410           anEndOfSwitch->SetName( myEndOfSwitch->Name() ) ;
2411           anEndOfSwitch->SetAuthor( myEndOfSwitch->Author() ) ;
2412           anEndOfSwitch->SetComment( myEndOfSwitch->Comment() ) ;
2413           anEndOfSwitch->Coords( myEndOfSwitch->X() , myEndOfSwitch->Y() ) ;
2414           anEndOfSwitch->SetPyFunction( myEndOfSwitch->PyFuncName() , *(myEndOfSwitch->PyFunction()) ) ;
2415           aNodetheName = new string( myEndOfSwitch->Name() ) ;
2416           aMapOfNodes[ *aNodetheName ] = DataFlowEditor()->Graph()->GetGraphNodeIndex( anEndOfSwitch->Name() ) ;
2417           delete aNodetheName ;
2418           myPorts = *(myEndOfSwitch->Ports()) ;
2419           for ( j = 0 ; j < (int ) myPorts.length() ; j++ ) {
2420             if ( myPorts[ j ]->IsInput() ) {
2421               aPort = anEndOfSwitch->InPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ; // mkr : IPAL11394 (add port to !EndSwitch! node)
2422             }
2423             else {
2424               aPort = anEndOfSwitch->OutPort( myPorts[ j ]->Name() , myPorts[ j ]->Type() ) ; // mkr : IPAL11394 (add port to !EndSwitch! node)
2425             }
2426           }
2427           if ( DataFlowEditor()->Graph()->IsDataStreamNode() ) {
2428             SUPERV::ListOfStreamPorts myStreamSwitchPorts = *(myEndOfSwitch->StreamPorts()) ;
2429             for ( j = 0 ; j < (int ) myStreamSwitchPorts.length() ; j++ ) {
2430               if ( myStreamSwitchPorts[ j ]->IsInput() ) {
2431                 aPort = anEndOfSwitch->InStreamPort( myStreamSwitchPorts[ j ]->Name() , StringToDataStreamType( myStreamSwitchPorts[ j ]->Type() ) , myStreamSwitchPorts[ j ]->Dependency() ) ; // mkr : IPAL11394 (add port to !EndSwitch! node)
2432               }
2433               else {
2434                 aPort = anEndOfSwitch->OutStreamPort( myStreamSwitchPorts[ j ]->Name() , StringToDataStreamType( myStreamSwitchPorts[ j ]->Type() ) , myStreamSwitchPorts[ j ]->Dependency() ) ; // mkr : IPAL11394 (add port to !EndSwitch! node)
2435               }
2436             }
2437           }
2438           RetVal = true ;
2439         }
2440         else {
2441           RetVal = false ;
2442           break ;
2443         }
2444       }
2445     }
2446     if ( RetVal ) {
2447       SUPERV::ListOfLinks * aGraphLinks = aGraph->GLinks() ;
2448       SUPERV::ListOfPorts * aGraphPorts = aGraph->Ports() ;
2449 //      cout << "Graph_Impl::Merge " << aGraphLinks->length() << " links " << aGraphPorts->length() << " GraphPorts"
2450 //           << endl ;
2451       for ( i = 0 ; i < (int ) aGraphLinks->length() ; i++ ) {
2452         SUPERV::Link_var aLink = (*aGraphLinks)[ i ] ;
2453         SUPERV::Port_var OutPort = aLink->OutPort() ;
2454         SUPERV::Port_var InPort = aLink->InPort() ;
2455         string * aLinkFromNodeName = new string( OutPort->Node()->Name() ) ;
2456         string * aLinkToNodeName = new string( InPort->Node()->Name() ) ;
2457         RetVal = DataFlowEditor()->AddLink( DataFlowEditor()->Graph()->GetGraphNode( aMapOfNodes[ aLinkFromNodeName->c_str() ] )->Name() ,
2458                                            OutPort->Name() ,
2459                                            DataFlowEditor()->Graph()->GetGraphNode( aMapOfNodes[ aLinkToNodeName->c_str() ] )->Name() ,
2460                                            InPort->Name() ) ;
2461         if ( RetVal ) {
2462           int j ;
2463           for ( j = 1 ; j <= aLink->CoordsSize() ; j++ ) {
2464             long X , Y ;
2465             RetVal = aLink->Coords( j , X , Y ) ;
2466             if ( !RetVal )
2467               break ;
2468             RetVal = DataFlowEditor()->AddLinkCoord( DataFlowEditor()->Graph()->GetGraphNode( aMapOfNodes[ aLinkFromNodeName->c_str() ] )->Name() ,
2469                                                      OutPort->Name() ,
2470                                                      DataFlowEditor()->Graph()->GetGraphNode( aMapOfNodes[ aLinkToNodeName->c_str() ] )->Name() ,
2471                                                      InPort->Name() ,
2472                                                      j , X , Y ) ;
2473             if ( !RetVal ) {
2474               break ;
2475             }
2476           }
2477         }
2478         delete aLinkFromNodeName ;
2479         delete aLinkToNodeName ;
2480         if ( !RetVal ) {
2481           break ;
2482         }
2483       }
2484       if ( RetVal ) {
2485         for ( i = 0 ; i < (int ) aGraphPorts->length() ; i++ ) {
2486           SUPERV::Port_var aPort = (*aGraphPorts)[ i ] ;
2487           if ( !aPort->IsGate() ) {
2488             MESSAGE( "Graph_Impl::Merge " << i << ". " << aPort->Node()->Name() << " " << aPort->Name() ) ;
2489             char * aPortName = aPort->Name() ;
2490             char * aNodeName = new char[ strlen( aPortName ) + 1 ] ;
2491             strcpy( aNodeName , aPortName ) ;
2492 //            char * thePortName = strchr( aNodeName , '\\' ) ;
2493             char * thePortName = aNodeName ;
2494             while ( ( thePortName = strchr( thePortName , '_' ) ) ) {
2495               if ( thePortName[1] == '_' ) {
2496                 thePortName[ 0 ] = '\0' ;
2497                 break ;
2498               }
2499               else {
2500                 thePortName = &thePortName[2] ;
2501               }
2502             }
2503             bool hasinput = aGraph->Node( aNodeName )->Port( thePortName + 2 )->HasInput() ;
2504 //            cout << "Graph_Impl::Merge " << " aNodeName " << aNodeName << " aPort " << thePortName + 1
2505 //                 << " HasInput " << hasinput << endl ;
2506             if ( hasinput ) {
2507               RetVal = DataFlowEditor()->AddInputData( DataFlowEditor()->Graph()->GetGraphNode( aMapOfNodes[ aNodeName ] )->Name() ,
2508                                                        thePortName + 2 ,
2509                                                        *(aPort->ToAny()) ) ;
2510             }
2511             delete [] aNodeName ;
2512             if ( !RetVal ) {
2513               break ;
2514             }
2515           }
2516         }
2517       }
2518     }
2519   }
2520   MESSAGE( "Graph_Impl::Merge returns " << RetVal ) ;
2521   endService( "Graph_Impl::Merge" );
2522   return RetVal ;
2523 }
2524
2525 SUPERV::StreamGraph_ptr Graph_Impl::ToStreamGraph() {
2526   SUPERV::StreamGraph_var iobject = SUPERV::StreamGraph::_nil() ;
2527   beginService( "Graph_Impl::ToStreamGraph" );
2528   if ( IsStreamGraph() && !IsMacro() ) {
2529 //  StreamGraph_Impl * myStreamGraph = new StreamGraph_Impl( _Orb , _Poa , _ContId ,
2530 //                                          instanceName() , interfaceName() ) ;
2531 //  PortableServer::ObjectId * id = myStreamGraph->getId() ;
2532 //  CORBA::Object_var obj = _poa->id_to_reference(*id);
2533     iobject = SUPERV::StreamGraph::_narrow( ObjRef() ) ;
2534     if ( CORBA::is_nil( iobject ) ) {
2535       MESSAGE( "ToStreamGraph of " << Name() << " (IsStreamGraph) --> nil reference" ) ;
2536     }
2537   }
2538   else {
2539     MESSAGE( "ToStreamGraph of " << Name() << " (IsNOTStreamGraph) --> nil reference" ) ;
2540   }
2541   endService( "Graph_Impl::ToStreamGraph" );
2542   return SUPERV::StreamGraph::_duplicate( iobject ) ;
2543 }
2544
2545 /**
2546  * Destroy Executor and use only Editor and its data model
2547  */
2548 void Graph_Impl::Editing() {
2549   if ( DataFlowEditor() && DataFlowExecutor() ) {
2550     delete DataFlowExecutor() ;
2551     DataFlowEditor()->Executor( NULL );
2552   }
2553 }
2554