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