Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/superv.git] / src / GraphBase / DataFlowBase_Graph.hxx
1 //  SUPERV GraphBase : contains fondamental classes for Services, Input Ports, Output Ports Links and Nodes.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : DataFlowBase_Graph.hxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 #ifndef _DATAFLOWBASE_GRAPH_HXX
30 #define _DATAFLOWBASE_GRAPH_HXX
31
32 #include "DataFlowBase_FactoryNode.hxx"
33 #include "DataFlowBase_LoopNode.hxx"
34 #include "DataFlowBase_EndOfLoopNode.hxx"
35 #include "DataFlowBase_SwitchNode.hxx"
36 #include "DataFlowBase_EndOfSwitchNode.hxx"
37 #include "DataFlowBase_DataNode.hxx"
38
39 namespace GraphBase {
40
41   class Graph : public DataNode {
42
43     private:
44
45 //      int                 _Graph_prof_debug;
46 //      ofstream          * _Graph_fdebug;
47       CORBA::ORB_ptr      _Orb ;
48 //      SUPERV::Graph_var   _Graph ;
49 //      Graph_Impl        * _GraphImpl ;
50
51 // Map of Nodes of the Graph
52       map< string , int >      _MapOfGraphNodes ;
53       long                     _GraphNodesSize ;
54       vector<bool >            _Sorted ;
55       vector<ComputingNode *>  _GraphNodes ;
56
57 // _SortedNodes[ _LevelsNumber ] : topological sort from 0 to _LevelsNumber
58 // _SortedNodes[ _level ][ _NodesNumber[ _level ] ] : _NodesNumber[ _level ] Nodes in the level
59       int                              _LevelsNumber ;
60       int                              _ThreadsMax ;
61       vector<int >                     _NodesNumber ;
62       vector<vector<ComputingNode *> > _SortedNodes ;
63
64       vector<int >             _CnxInPortsNumber ;
65       vector<int >             _DecrInPortsNumber ;
66
67       long                     _HeadNodesSize ; 
68       vector<ComputingNode *>  _HeadNodes ;
69       long                     _QueueNodesSize ; 
70       vector<ComputingNode *>  _QueueNodes ;
71       bool                     _ParallelExecutionNodes ;
72
73 // For StreamGraphs. It is not the same Service if it belongs to
74 // a different Interface and/or a different Component ...
75       map< string , GraphBase::Service * > _MapOfServiceNames ;
76
77 // For automactic rename of nodes if unique names in graphs
78       map< string , int >      _MapOfServiceInstanceNumber ;
79
80 // Total number of SubGraphs
81       int                              _SubGraphsNumber ;
82 // _SubGraphs[ SubGraphNumero ] [ NodeNumero ]
83 // SubGraphNumero : [ 0 , _SubGraphsSize [ ; NodeNumero : [ 0 , _SubGraphs[ SubGraphNumero ].size() [
84       int                              _SubGraphsSize ;
85       vector<vector<ComputingNode *> > _SubGraphs ;
86
87 // _SubStreamGraphs[ SubStreamGraphNumero ] [ NodeNumero ]
88 // SubStreamGraphNumero : [ 0 , _SubStreamGraphsSize [
89 // NodeNumero : [ 0 , _SubStreamGraphs[ SubStreamGraphNumero ].size() [
90 //      int                              _SubStreamGraphsSize ;
91 //      vector<vector<ComputingNode *> > _SubStreamGraphs ;
92
93     string _Messages ;
94
95     map< string, string > _MapOfComponentNameContainer ; // mkr : PAL13947
96     
97     bool AddLink( GraphBase::ComputingNode *fromNode ,
98                   GraphBase::OutPort *fromPort ,
99                   GraphBase::ComputingNode *toNode ,
100                   GraphBase::InPort *toPort ) ;
101
102     public:
103
104       Graph() ;
105       Graph( CORBA::ORB_ptr ORB ,
106              SALOME_NamingService* ptrNamingService ,
107              const char *DataFlowName ,
108              const SUPERV::KindOfNode DataFlowkind ,
109              int * Graph_prof_debug ,
110              ofstream * Graph_fdebug ) ;
111 //             const char * DebugFileName ) ;
112       Graph( CORBA::ORB_ptr ORB ,
113              SALOME_NamingService* ptrNamingService ,
114              const SALOME_ModuleCatalog::Service& DataFlowService ,
115              const char *DataFlowComponentName ,
116              const char *DataFlowInterfaceName ,
117              const char *DataFlowName ,
118              const SUPERV::KindOfNode DataFlowkind ,
119              const SUPERV::SDate DataFlowFirstCreation ,
120              const SUPERV::SDate DataFlowLastModification ,
121              const char * DataFlowEditorRelease ,
122              const char * DataFlowAuthor ,
123              const char * DataFlowComputer ,
124              const char * DataFlowComment ,
125              int * Graph_prof_debug ,
126              ofstream * Graph_fdebug ) ;
127 //             const char * DebugFileName ) ;
128       virtual ~Graph() ;
129
130 //      void Set_prof_debug( CORBA::ORB_ptr ORB ,
131 //                           const char * DebugFileName ) ;
132       void Set_prof_debug( int * Graph_prof_debug ,
133                            ofstream * Graph_fdebug ) ;
134       int * Graph_prof_debug() {
135             return _prof_debug ; } ;
136       ofstream * Graph_fdebug() { return _fdebug ; } ;
137
138 //      SUPERV::Graph_var ObjRef() const { return _Graph ; } ;
139 //      void SetObjRef( SUPERV::Graph_var aGraph ) {
140 //                      _Graph = aGraph ; } ;
141 //      Graph_Impl * ObjImpl() const { return _GraphImpl ; } ;
142 //      void SetObjImpl( Graph_Impl * aGraphImpl ) {
143 //                       _GraphImpl = aGraphImpl ; } ;
144
145       void AddLinkedNode( const char* FromNodeName , GraphBase::ComputingNode * anOutNode ) {
146            GetChangeGraphNode( FromNodeName )->AddLinkedNode( anOutNode ) ; } ;
147
148       const int GetGraphNodeIndex( const char *name ) {
149             const int index = _MapOfGraphNodes[ name ] - 1 ;
150 //            if ( index >= 0 )
151 //              cdebug << "GetGraphNodeIndex of " << name << " in _MapOfGraphNodes : "
152 //                     << index << " Node " << hex << (void *) _GraphNodes[ index ]
153 //                     << dec << " '" << _GraphNodes[ index ]->Name() << "'" << endl ;
154 #if 0
155            cdebug << "Graph::GetGraphNodeIndex " << _GraphNodesSize << " known nodes" << endl;
156            int i ;
157            for ( i = 0 ; i < _GraphNodesSize ; i++ ) {
158              cdebug << "Node" << i << _GraphNodes[ i ]->Name() << endl ;
159            }
160            map< string , int >::iterator aMapOfGraphNodesIterator ;
161            i = 0 ;
162            for ( aMapOfGraphNodesIterator = _MapOfGraphNodes.begin() ;
163              aMapOfGraphNodesIterator != _MapOfGraphNodes.end() ; aMapOfGraphNodesIterator++ ) {
164              cdebug << "MapOfGraphNodes " << i++ << " " << aMapOfGraphNodesIterator->first
165                     << " --> " << aMapOfGraphNodesIterator->second << " - 1" << endl ;
166            }
167 #endif
168             return index ; } ;
169       void SetGraphNodeIndex( const char *name , const int index ) {
170            _MapOfGraphNodes[ name ] = index + 1 ;
171            cdebug << "SetGraphNodeIndex of " << name << " in _MapOfGraphNodes : "
172                   << index << " Node " << (void *) _GraphNodes[ index ]
173                   << " '" << _GraphNodes[ index ]->Name() << "' MapOfGraphNodessize "
174                   << GetGraphNodeSize() << endl ;
175 #if 1
176            cdebug << "Graph::SetGraphNodeIndex " << _GraphNodesSize << " known nodes" << endl;
177            int i ;
178            for ( i = 0 ; i < _GraphNodesSize ; i++ ) {
179              cdebug << "Node" << i << _GraphNodes[ i ]->Name() << endl ;
180            }
181            map< string , int >::iterator aMapOfGraphNodesIterator ;
182            i = 0 ;
183            for ( aMapOfGraphNodesIterator = _MapOfGraphNodes.begin() ;
184              aMapOfGraphNodesIterator != _MapOfGraphNodes.end() ; aMapOfGraphNodesIterator++ ) {
185              cdebug << "MapOfGraphNodes " << i++ << " " << aMapOfGraphNodesIterator->first
186                     << " --> " << aMapOfGraphNodesIterator->second << " - 1" << endl ;
187            }
188 #endif
189            } ;
190       void DelGraphNodeIndex( const char *name ) {
191            _MapOfGraphNodes.erase( name ) ; } ;
192
193       int GetGraphNodeSize() const {
194           return _MapOfGraphNodes.size() ; }
195       const GraphBase::ComputingNode * GetGraphNode( const int index ) const {
196             const ComputingNode * aNode = GetChangeGraphNode( index ) ;
197             return aNode ; } ;
198       ComputingNode * GetChangeGraphNode( const int index ) const {
199             if ( index >= 0 && index < _GraphNodesSize ) {
200               ComputingNode * aNode = _GraphNodes[ index ] ;
201               return aNode ;
202             }
203             else
204               return NULL ; } ;
205       const ComputingNode * GetGraphNode( const char *name ) {
206             const ComputingNode * aNode = GetChangeGraphNode( name ) ;
207             return aNode ; } ;
208       ComputingNode * GetChangeGraphNode( const char *name ) {
209             int index = GetGraphNodeIndex( name ) ;
210             if ( index >= 0 && index < _GraphNodesSize ) {
211               ComputingNode * aNode = GetChangeGraphNode( index ) ;
212               return aNode ;
213             }
214             else
215               return NULL ; } ;
216
217       const int GraphNodesSize() const {
218             return _GraphNodesSize ; } ;
219       ComputingNode * GraphNodes( int i ) const {
220             return _GraphNodes[ i ] ; } ;
221
222       const int HeadNodesSize() const {
223             return _HeadNodesSize ; } ;
224       ComputingNode * HeadNodes( int i ) const {
225             return _HeadNodes[ i ] ; } ;
226
227       const int QueueNodesSize() const {
228             return _QueueNodesSize ; } ;
229       ComputingNode * QueueNodes( int i ) const {
230             return _QueueNodes[ i ] ; } ;
231
232       const GraphBase::InPort * GetInPort( const char * ToServiceParameterName ) {
233                                 return GraphBase::PortsOfNode::GetInPort( ToServiceParameterName ) ; } ;
234       GraphBase::InPort * GetChangeInPort( const char * ToServiceParameterName ) {
235                           return GraphBase::PortsOfNode::GetChangeInPort( ToServiceParameterName ) ; } ;
236       const GraphBase::OutPort * GetOutPort( const char * FromServiceParameterName ) {
237                                  return GraphBase::PortsOfNode::GetOutPort( FromServiceParameterName ) ; } ;
238       GraphBase::OutPort * GetChangeOutPort( const char * FromServiceParameterName ) {
239                            return GraphBase::PortsOfNode::GetChangeOutPort( FromServiceParameterName ); } ;
240
241       const GraphBase::InPort * GetInPort( const char * ToNodeName ,
242                                           const char * ToServiceParameterName ) {
243             ComputingNode * aNode = GetChangeGraphNode( ToNodeName ) ;
244             if ( aNode ) {
245               return aNode->GetInPort(ToServiceParameterName);
246             }
247             else
248               return NULL ; } ;
249       GraphBase::InPort * GetChangeInPort( const char * ToNodeName ,
250                                           const char * ToServiceParameterName ) {
251             ComputingNode * aNode = GetChangeGraphNode( ToNodeName ) ;
252             if ( aNode ) {
253               return aNode->GetChangeInPort( ToServiceParameterName ) ;
254             }
255             else
256               return NULL ; } ;
257       const GraphBase::OutPort * GetOutPort( const char * FromNodeName ,
258                                     const char * FromServiceParameterName ) {
259             ComputingNode * aNode = GetChangeGraphNode( FromNodeName ) ;
260             if ( aNode ) {
261               return aNode->GetOutPort( FromServiceParameterName ) ;
262             }
263             else
264               return NULL ; } ;
265       GraphBase::OutPort * GetChangeOutPort( const char * FromNodeName ,
266                                             const char * FromServiceParameterName ) {
267             ComputingNode * aNode = GetChangeGraphNode( FromNodeName ) ;
268             if ( aNode ) {
269               return aNode->GetChangeOutPort( FromServiceParameterName );
270             }
271             else
272               return NULL ; } ;
273
274       SUPERV::GraphState PortState( const char* NodeName ,
275                                     const char* ServiceParameterName ) ;
276       bool PortDone( const char* NodeName ,
277                      const char* ServiceParameterName ) {
278         bool aRet = false ;
279         SUPERV::GraphState aState = PortState( NodeName ,
280                                                 ServiceParameterName ) ;
281         if ( aState == SUPERV::DoneState )
282           aRet = true ;
283         return aRet ; } ;
284 //JR 30.03.2005      const CORBA::Any * PortInData( const char* ToNodeName ,
285       const CORBA::Any PortInData( const char* ToNodeName ,
286                                    const char* ToServiceParameterName ) ;
287 //JR 30.03.2005      const CORBA::Any * PortOutData( const char* FromNodeName ,
288       const CORBA::Any PortOutData( const char* FromNodeName ,
289                                     const char* FromServiceParameterName ) ;
290
291       bool LoadXml( CORBA::ORB_ptr Orb , const char* anXmlFile ,
292                     GraphBase::ListOfSGraphs & aListOfDataFlows ) ;
293
294       GraphBase::SNode * GetInfo() const ;
295       GraphBase::ListOfSNodes * GetNodes() const ;
296       GraphBase::ListOfSLinks * GetLinks(bool AllLinks = false ) const ;
297       GraphBase::ListOfSGraphs * GetGraphs() const ;
298       GraphBase::ListOfSLinks * GetDatas() const ;
299
300       bool AddNode( ComputingNode * aNode ) ;
301       bool RemoveNode( const char* NodeName ) ;
302       bool ReNameNode( const char* OldNodeName ,
303                        const char* NewNodeName ) ;
304
305       bool AddLink( const char* FromNodeName ,
306                     const char* FromServiceParameterName ,
307                     const char* ToNodeName ,
308                     const char* ToServiceParameterName ) ;
309 //                    , const CORBA::Any aValue ) ;
310       bool RemoveLink( const char* FromNodeName ,
311                        const char* FromServiceParameterName ,
312                        const char* ToNodeName ,
313                        const char* ToServiceParameterName ) ;
314       bool GetLink( const char* ToNodeName ,
315                     const char* ToServiceParameterName ,
316                     char** FromNodeName ,
317                     char** FromServiceParameterName ) ;
318       GraphBase::SLink * GetLink( GraphBase::ComputingNode * aNode ,
319                                   GraphBase::InPort* toPort ) ;
320
321       bool AddInputData( const char* ToNodeName ,
322                          const char* ToParameterName ,
323                          const CORBA::Any aValue ) ;
324       bool ChangeInputData( const char* ToNodeName ,
325                             const char* ToParameterName ,
326                             const CORBA::Any aValue ) ;
327 //      bool AddInputData( const char* ToNodeName ,
328 //                         const char* ToParameterName ,
329 //                         const CORBA::Any ** aValue ) ;
330
331       bool AddOutputData( const char* FromNodeName ,
332                           const char* FromParameterName ,
333                           const CORBA::Any aValue ) ;
334
335       void SetGraphPorts() ;
336
337       string Messages() ;
338       void ReSetMessages() ;
339       void SetMessages( string anErrorMessage ) ;
340
341       bool CreateService() ;
342       bool InLineServices() ;
343
344       bool Sort( int & SubStreamGraphsNumber ) ;
345       bool ValidGOTO() ; // mkr : PAL12575
346       bool ValidLoops() const ;
347       bool ValidSwitchs() const ;
348       bool ComputingNodes() const ;
349       bool LinkLoopNodes(bool & NewLink ) ;
350       bool DataServerNodes() const ;
351   
352       long LevelMax() {
353            return _LevelsNumber + 1 ; } ;
354
355       map< string , GraphBase::Service * > MapOfServiceNames() ;
356       GraphBase::Service * GetServiceOfMap( char * name ) ;
357       bool SetServiceOfMap( GraphBase::Service * aService ) ;
358       int GetNewServiceInstanceNumber( char * aServiceName ) ;
359
360       SUPERV::ListOfStrings * LevelNodes(long aLevel ) ;
361       long ThreadsMax() {
362            return _ThreadsMax ; } ;
363
364       long SubGraphsNumber() {
365            return _SubGraphsNumber ; } ;
366
367       int NodesNumber(const int aLevel ) {
368           return _NodesNumber[ aLevel ] ; } ;
369       GraphBase::ComputingNode * SortedNodes( const int aLevel , const int index ) {
370              return (_SortedNodes[ aLevel ])[ index ] ; } ;
371
372       bool StartComponent( const int aThread ,
373                            const char * ComputerContainer ,
374                            const char * ComponentName ,
375                            Engines::Container_var & myContainer ,
376                            Engines::Component_var & objComponent ) ;
377
378     // mkr : PAL13947
379     void InsertToMapOfComponentNameContainer( const char * theComponentName, 
380                                               const char * theContainer) {
381       _MapOfComponentNameContainer[ theComponentName ] = theContainer ;
382     }
383
384     // mkr : PAL13947    
385     const char * RetrieveFromMapOfComponentNameContainer( const char * theComponentName ) {
386       if ( _MapOfComponentNameContainer.empty() 
387            ||
388            _MapOfComponentNameContainer.find(theComponentName) == _MapOfComponentNameContainer.end() )
389         return NULL ;
390       
391       return _MapOfComponentNameContainer[ theComponentName ].c_str() ;
392     }
393
394     // mkr : PAL13947
395     void ClearMapOfComponentNameContainer() {
396       if ( !_MapOfComponentNameContainer.empty() )
397         _MapOfComponentNameContainer.clear();
398     }
399
400   };
401   
402 };
403
404 #endif