]> SALOME platform Git repositories - modules/superv.git/blob - src/GraphBase/DataFlowBase_SwitchNode.cxx
Salome HOME
*** empty log message ***
[modules/superv.git] / src / GraphBase / DataFlowBase_SwitchNode.cxx
1 //  SUPERV GraphBase : contains fondamental classes for Services, Input Ports, Output Ports Links and Nodes.
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : DataFlowBase_SwitchNode.cxx
8 //  Author : Jean Rahuel, CEA
9 //  Module : SUPERV
10 //  $Header:
11
12 using namespace std;
13 //#include <sstream>
14 //#include <iostream>
15
16 #include "DataFlowBase_SwitchNode.hxx"
17 #include "DataFlowBase_EndOfSwitchNode.hxx"
18 #include "DataFlowBase_Graph.hxx"
19
20 GraphBase::SwitchNode::SwitchNode() :
21   GraphBase::GOTONode::GOTONode() {
22
23   cdebug << "GraphBase::SwitchNode::SwitchNode "  << this 
24          << " _Name " << (void *) Name() << " " << Name() << " _Comment "
25          << (void *) Comment() << " "  << Comment() << " "  << endl ;
26
27 }
28
29 GraphBase::SwitchNode::SwitchNode( CORBA::ORB_ptr ORB ,
30                                    SALOME_NamingService* ptrNamingService ,
31                                    const char * FuncName ,
32                                    const SUPERV::ListOfStrings & aPythonFunction ,
33                                    const char *NodeName ,
34                                    const SUPERV::KindOfNode akind ,
35                                    const SUPERV::SDate NodeFirstCreation ,
36                                    const SUPERV::SDate NodeLastModification  ,
37                                    const char * NodeEditorRelease ,
38                                    const char * NodeAuthor ,
39                                    const char * NodeComment ,
40                                    const bool   GeneratedName ,
41                                    const long   X ,
42                                    const long   Y ,
43                                    int * Graph_prof_debug ,
44                                    ofstream * Graph_fdebug ) :
45   GraphBase::GOTONode::GOTONode( ORB , ptrNamingService , FuncName , aPythonFunction ,
46                                  NodeName , akind , NodeFirstCreation ,
47                                  NodeLastModification  , NodeEditorRelease ,
48                                  NodeAuthor , NodeComment , GeneratedName ,
49                                  X , Y , Graph_prof_debug , Graph_fdebug ) {
50
51   cdebug_in << "GraphBase::SwitchNode::SwitchNode "  << this 
52             << "' _Name " << (void *) Name() << " '" << Name() << " _Comment "
53             << (void *) Comment() << " "  << Comment() << " " 
54             << " KindOfNode " << Kind() << " FuncName " << FuncName
55             << " ServiceName " << ServiceName() << " In("
56             << ServiceInParameter().length()
57             << ") Out(" << ServiceOutParameter().length() << ")" << endl ;
58
59   cdebug_out << "GraphBase::SwitchNode::SwitchNode" << endl;
60 }
61
62 GraphBase::SwitchNode::~SwitchNode() {
63   cdebug << "GraphBase::SwitchNode::~SwitchNode "  << this 
64          << " _Name " << (void *) Name() << " " << Name() << " _Comment "
65          << (void *) Comment() << " "  << Comment() << " "  << endl ;
66 //  if ( _ComponentName != NULLSTRING )
67 //    delete [] _ComponentName ;
68 //  delete [] _Name ;
69 //  delete [] _EditorRelease ;
70 //  if ( _Author != NULLSTRING )
71 //    delete [] _Author ;
72 //  if ( _Computer != FACTORYSERVER )
73 //    delete [] _Computer;
74 //  if ( _Comment != NULLSTRING )
75 //    delete [] _Comment;
76 }
77
78 bool GraphBase::SwitchNode::CheckSwitch() const {
79   GraphBase::EndOfSwitchNode * anEndSwitchNode ;
80   anEndSwitchNode = (GraphBase::EndOfSwitchNode * ) CoupledNode() ;
81   cdebug_in << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
82             << anEndSwitchNode->Name() << endl;
83   int i ;
84
85 //All nodes between Switch and EndSwitch will have _BranchOfSwitchDone to false
86 //When we have to execute such a node in a switchbranch, if _BranchOfSwitchDone is true we have
87 // an error ; else we set _BranchOfSwitchDone to true
88   for ( i = 0 ; i < GetNodeOutPortsSize() ; i++ ) {
89     GraphBase::OutPort * anOutPort = GetChangeNodeOutPort( i ) ;
90     if ( anOutPort->IsSwitch() || anOutPort->IsGate() ) {
91       int j ;
92       for ( j = 0 ; j < anOutPort->InPortsSize() ; j++ ) {
93         GraphBase::InPort * anInPort = anOutPort->ChangeInPorts( j ) ;
94         GraphBase::ComputingNode * aNode ;
95         aNode = GraphOfNode()->GetChangeGraphNode( anInPort->NodeName() ) ;
96         if ( aNode && aNode != anEndSwitchNode ) {
97           aNode->InitBranchOfSwitchDone( true , anEndSwitchNode ) ;
98         }
99       }
100     }
101   }
102
103   bool DefaultConnected = false ;
104 //OutPorts of the SwitchNode
105   for ( i = 0 ; i < GetNodeOutPortsSize() ; i++ ) {
106     GraphBase::OutPort * anOutPort = GetChangeNodeOutPort( i ) ;
107     if ( anOutPort->IsSwitch() || anOutPort->IsGate() ) {
108 // We have a branch of the switch
109       cdebug << endl << Name() << "->SwitchNode::CheckSwitch OutPort " << anOutPort->PortName()
110              << " " << anOutPort->PortStatus() << " " << anOutPort->Kind()
111              << " SWITCHBRANCH" << endl ;
112
113 //JR 11.02.2005 : if Default is not connected and if Default is true at execution time, the SwitchNode
114 // execution will be aborted
115 //Behavior : in that case, that means that a SwitchBranch should be a DefaultBranch and it is not the case !
116 //1. DEFAULT_OUTPORT
117       if ( anOutPort->IsGate() ) {
118         if ( anOutPort->InPortsSize() == 0 ) {
119 // DefaultPort of the SwitchNode is not connected
120           cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
121                      << anEndSwitchNode->Name() << " OutPort " << anOutPort->PortName() << " "
122                      << anOutPort->InPortsSize() << " InPortsSize EndSwitch(Default) "
123                      << anEndSwitchNode->GetNodeInGate()->PortStatus() << "WARNING" << endl;
124         }
125         else {
126 // But the DefaultPort of the EndSwitchNode is connected : so there is a branch going to
127 // that DefaultPort
128           cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
129                      << anEndSwitchNode->Name() << " OutPort " << anOutPort->PortName() << " "
130                      << " " << anOutPort->PortStatus() << " " << anOutPort->InPortsSize() << " InPortsSize "
131                      << endl ;
132           DefaultConnected = true ;
133
134           anEndSwitchNode->InitEndSwitchInPortLinked() ;
135
136 // That DefaultBranch have to go directly to the EndSwitchNode
137           GraphBase::InPort * anInPort = anOutPort->ChangeInPorts( 0 ) ;
138           GraphBase::ComputingNode * aNode ;
139           aNode = GraphOfNode()->GetChangeGraphNode( anInPort->NodeName() ) ;
140           if ( aNode != anEndSwitchNode ) {
141             cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
142                        << anEndSwitchNode->Name() << " for OutPort " << anOutPort->PortName()
143                        << " linked to " << aNode->Name() << " WARNING false" << endl;
144             return false ;
145           }
146           cdebug << endl << Name() << " Check of " << anOutPort->PortName() << " switch port branch"
147                  << " to EndSwitchNode" << endl ;
148 // All of InPorts of the EndSwitchNode have to be done :
149           int k ;
150           for ( k = 0 ; k < GetNodeOutPortsSize() ; k++ ) {
151             GraphBase::OutPort * anOutPort = GetChangeNodeOutPort( k ) ;
152             if ( !anOutPort->IsGate() ) {
153               GraphBase::InPort * anInPort ;
154               anInPort = anEndSwitchNode->GetChangeInPort( anOutPort->PortName() ) ;
155               if ( anInPort && !anEndSwitchNode->DecrEndSwitchInPortLinked( anInPort ) ) {
156                 cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
157                            << anEndSwitchNode->Name() << " for port " << anInPort->PortName()
158                            << " WARNING false" << endl;
159                 return false ;
160               }
161               else {
162                 cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
163                            << anEndSwitchNode->Name() << " OutPort " << anOutPort->PortName()
164                            << " " << anOutPort->PortStatus() << " " << anOutPort->Kind()
165                            << " not in EndSwitchNode : IGNORED"
166                            << endl;
167               }
168             }
169             else {
170               cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
171                          << anEndSwitchNode->Name() << " OutPort " << anOutPort->PortName()
172                          << " " << anOutPort->PortStatus() << " " << anOutPort->Kind()
173                          << " SwitchPort or Gate : IGNORED"
174                          << endl;
175             }
176           }
177 // Check that all InPorts of the EndSwitchNode have a value :
178           if ( !anEndSwitchNode->CheckEndSwitchInPortsLinked() ) {
179             cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
180                        << anEndSwitchNode->Name() << " " << aNode->Name()
181                        << " WARNING false" << endl;
182             return false ;
183           }
184         }
185       }
186
187 //2. SWITCH_OUTPORT
188       else {
189 // We have a SwitchPort which is connected to Gates
190 // We initialize the _NodeDoneInBranchOfSwitch field to false for each node of the
191 // InPortsSize() branch(es)
192         int j ;
193         for ( j = 0 ; j < anOutPort->InPortsSize() ; j++ ) {
194           GraphBase::InPort * anInPort = anOutPort->ChangeInPorts( j ) ;
195           GraphBase::ComputingNode * aNode ;
196           aNode = GraphOfNode()->GetChangeGraphNode( anInPort->NodeName() ) ;
197           if ( aNode && aNode != anEndSwitchNode ) {
198             if ( !aNode->InitBranchOfSwitchDone( false , anEndSwitchNode ) ) {
199               cdebug << Name() << "->ComputingNode::InitBranchOfSwitchDone WARNING false"
200                      << " Node common to two or more branchs of switch" << endl ;
201               return false ;
202             }
203           }
204         }
205
206         anEndSwitchNode->InitEndSwitchInPortLinked() ;
207
208 // At first all of not SwitchPort should have to be done also :
209 // Not Switch Branches (Gate not connected to a switch port of a SwitchNode) :
210 // They are always executed except if we execute the DefaultBranch ...
211         cdebug << "1. " << Name() << "->SwitchNode::CheckSwitch at first NOT_SWITCH_BRANCH_OUTPORT FOR "
212                << anOutPort->PortName()
213                << " " << anOutPort->PortStatus() << " " << anOutPort->Kind() << endl ;
214         for ( j = 0 ; j < GetNodeOutPortsSize() ; j++ ) {
215           GraphBase::OutPort * anOutPort = GetChangeNodeOutPort( j ) ;
216           if ( !anOutPort->IsSwitch() && !anOutPort->IsGate() ) {
217             int k ;
218             for ( k = 0 ; k < anOutPort->InPortsSize() ; k++ ) {
219               GraphBase::InPort * anInPort = anOutPort->ChangeInPorts( k ) ;
220               GraphBase::ComputingNode * aNode ;
221               aNode = GraphOfNode()->GetChangeGraphNode( anInPort->NodeName() ) ;
222               if ( aNode ) {
223                 const GraphBase::InPort * anInGatePort = aNode->GetNodeInGate() ;
224                 cdebug << Name() << "->SwitchNode::CheckSwitch InPort" << k << ". "
225                        << anInPort->PortName() << " of " << aNode->Name() << " :" << endl ;
226 // Direct Connexion to the EndSwitchNode : always executed (except for DefaultBranch)
227                 if ( aNode == anEndSwitchNode ) {
228                   if ( !anEndSwitchNode->DecrEndSwitchInPortLinked( anInPort ) ) {
229                     cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
230                                << anEndSwitchNode->Name() << " for port " << anInPort->PortName()
231                                << " WARNING false" << endl;
232                     return false ;
233                   }
234                   cdebug << Name() << "->SwitchNode::CheckSwitch Check of '"
235                          << anOutPort->PortName() << "' " << anOutPort->Kind()
236                          << " NOT switch port Branch to EndSwitchNode "
237                          << aNode->Name() << " is done" << endl ;
238                 }
239 // Check that InGate is not linked from a SwitchPort or a GatePort of the SwitchNode :
240                 else if ( !anInGatePort->GetOutPort() ) {
241                   cdebug << Name() << "->SwitchNode::CheckSwitch Check of '"
242                          << anOutPort->PortName() << "' " << anOutPort->Kind()
243                          << " NOT switch port Branch to node "
244                          << aNode->Name() << " will be done" << endl ;
245                   if ( !aNode->CheckSwitch( anEndSwitchNode ) ) {
246                     cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
247                                << anEndSwitchNode->Name() << " " << aNode->Name()
248                                << " WARNING false" << endl;
249                     return false ;
250                   }
251                   cdebug << Name() << "->SwitchNode::CheckSwitch Check of '"
252                          << anOutPort->PortName() << "' " << anOutPort->Kind()
253                          << " NOT switch port Branch to node "
254                          << aNode->Name() << " is done" << endl ;
255                 }
256                 else {
257                   cdebug << Name() << "->SwitchNode::CheckSwitch Check of '"
258                          << anOutPort->PortName() << "' " << anOutPort->Kind()
259                          << " NOT switch port Branch to node "
260                          << aNode->Name() << " has InGate connected IGNORED for that branch" << endl ;
261                 }
262               }
263             }
264           }
265         }
266
267         cdebug << "2. "<< Name() << "->SwitchNode::CheckSwitch and now SWITCH_BRANCH_OUTPORT "
268                << anOutPort->PortName()
269                << " " << anOutPort->PortStatus() << " " << anOutPort->Kind() << endl ;
270 // And all links from the switchport anOutPort (connected to Gates) will be checked
271         for ( j = 0 ; j < anOutPort->InPortsSize() ; j++ ) {
272           GraphBase::InPort * anInPort = anOutPort->ChangeInPorts( j ) ;
273           GraphBase::ComputingNode * aNode ;
274           aNode = GraphOfNode()->GetChangeGraphNode( anInPort->NodeName() ) ;
275           if ( aNode == NULL ) {
276             cdebug << Name() << "->SwitchNode::CheckSwitch Check of " << anOutPort->PortName()
277                    << " switch port branch "
278                    << aNode->Name() << " ignored" << endl ;
279           }
280
281
282           else {
283 // That branch does not go directly to the EndSwitchNode
284 // Control the OutPorts Values of the SwitchNode through the branch
285             cdebug << Name() << " Check of " << anOutPort->PortName()
286                    << " switch port Branch to node "
287                    << aNode->Name() << endl ;
288             if ( !aNode->CheckSwitch( anEndSwitchNode ) ) {
289               cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
290                          << anEndSwitchNode->Name() << " " << aNode->Name() << " WARNING false"
291                          << endl;
292               return false ;
293             }
294           }
295         }
296 // Check that all InPorts of the EndSwitchNode have a value :
297         if ( !anEndSwitchNode->CheckEndSwitchInPortsLinked() ) {
298           cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
299                      << anEndSwitchNode->Name() << " CheckEndSwitchInPortsLinked WARNING false"
300                      << endl;
301           return false ;
302         }
303         else {
304           cdebug << Name() << "->SwitchNode::CheckSwitch OutPort " << anOutPort->PortName()
305                  << " " << anOutPort->PortStatus() << " " << anOutPort->Kind()
306                  << " SWITCHBRANCH IS OK" << endl << endl ;
307         }
308       }
309     }
310     else {
311       cdebug << Name() << "->SwitchNode::CheckSwitch OutPort " << anOutPort->PortName()
312              << " " << anOutPort->PortStatus() << " " << anOutPort->Kind() << " NOTSWITCHBRANCH"
313              << " IGNORED" << endl ;
314     }
315   }
316   cdebug_out << Name() << "->SwitchNode::CheckSwitch EndSwitchNode "
317              << anEndSwitchNode->Name() << " true" << endl;
318   return true ;
319 }
320