Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/superv.git] / src / GraphBase / DataFlowBase_OutPort.cxx
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_OutPort.cxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SUPERV
27 //  $Header:
28
29 using namespace std;
30 #include "DataFlowBase_OutPort.hxx"
31 #include "DataFlowBase_InPort.hxx"
32
33 //GraphBase::OutPort::~OutPort() {
34 // In GraphExecutor::OutPort
35 //  int i ;
36 //  for ( i = 1 ; i <= _InPortsSize  ; i++ ) {
37 //    if ( !_InPorts[ i ]->RemoveLink() )
38 //      cout << "GraphBase::OutPort::~OutPort error "
39 //           << _InPorts[ i ]->PortName() << endl ;
40 //  }
41 //}
42
43 bool GraphBase::OutPort::HasInPort() const {
44   return _InPortsSize != 0 ;
45 }
46
47 const GraphBase::InPort * GraphBase::OutPort::GetInPort(
48                          const GraphBase::InPort * toPort ) {
49   const GraphBase::InPort * anInPort = NULL ;
50   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
51   if ( index > 0 && index <= _InPortsSize ) {
52     anInPort = _InPorts[ index-1 ] ;
53     if ( anInPort != toPort ) {
54   //cout << "GraphBase::OutPort::GetInPort inconsistency toPort "
55 //           << hex << (void *) toPort << " != anInPort " << (void *) anInPort
56 //           << dec << endl ;
57       anInPort = NULL ;
58     }
59   }
60   return anInPort ;
61 }
62
63 SUPERV::Link_var GraphBase::OutPort::InPortObjRef(
64                          const GraphBase::InPort * toPort ) {
65   SUPERV::Link_var aLink = SUPERV::Link::_nil() ;
66   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
67   if ( index > 0 && index <= _InPortsSize ) {
68     aLink = _Links[ index-1 ] ;
69   }
70   return aLink ;
71 }
72
73 bool GraphBase::OutPort::AddInPort( GraphBase::InPort * toPort ) {
74   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
75   if ( index > 0 && index <= _InPortsSize ) {
76     return false ;
77   }
78   _InPortsSize += 1 ;
79   _InPorts.resize( _InPortsSize ) ;
80   _MapOfInPorts[ toPort->NodePortName() ] = _InPortsSize ;
81   _InPorts[ _InPortsSize-1 ] = toPort ;
82   _Links.resize( _InPortsSize ) ;
83   _Links[ _InPortsSize-1 ] = SUPERV::Link::_nil() ;
84   cdebug << "OutPort " << NodeName() << "(" << PortName() << ") --> InPort "
85          << toPort->NodeName() << "(" << toPort->PortName() << ") SwitchPort "
86          << toPort->IsEndSwitch() << endl ;
87   return true ;
88 }
89
90 bool GraphBase::OutPort::AddInPortObjRef( GraphBase::InPort * toPort ,
91                                           SUPERV::Link_var aLink ) {
92   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
93   if ( index <= 0 || index > _InPortsSize ) {
94     return false ;
95   }
96   _Links[ index - 1 ] = aLink ;
97   return true ;
98 }
99
100 bool GraphBase::OutPort::RemoveInPort() {
101   if ( _InPortsSize != 1 ) {
102     return false ;
103   }
104   int index = 1 ;
105   GraphBase::InPort * toPort = _InPorts[ index - 1 ] ;
106   _InPortsSize -= 1 ;
107   _InPorts.resize( _InPortsSize ) ;
108   _Links.resize( _InPortsSize ) ;
109   _MapOfInPorts.erase( toPort->NodePortName() ) ;
110   if ( _InPortsSize == 0 ) {
111     PortStatus(NotConnected ) ;
112     if ( IsSwitch() ) {
113       Kind( SUPERV::InLineParameter ) ;
114     }
115   }
116   if ( toPort->GetOutPort() ) {
117     toPort->RemoveOutPort( false ) ;
118   }
119   cdebug << "OutPort::RemoveInPort " << NodeName() << "( " << PortName() << " " << PortStatus()
120          << " " << Kind() << " ) _InPortsSize " << _InPortsSize << " --> "
121          << toPort->NodeName() << "( " << toPort->PortName() << " " << toPort->PortStatus()
122          << " " << toPort->Kind() << " )" << endl ;
123   return true ;
124 }
125
126 bool GraphBase::OutPort::RemoveInPort( GraphBase::InPort * toPort ) {
127   int i ;
128   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
129   if ( index <= 0 || index > _InPortsSize ) {
130 //JR NPAL14110 09.02.2007 : Not an error with MacroNodes ...
131     //JRcdebug << "GraphBase::OutPort::RemoveInPort Error " << NodeName() << "( " << PortName()<< " ) --> "
132     //JR       << toPort->NodeName() << "( " << toPort->PortName() << " )" << endl ;
133     return false ;
134   }
135   cdebug << "OutPort::RemoveInPort " << NodeName() << "(" << PortName() << ") --> "
136          << _InPorts[ index - 1 ]->NodeName() << "(" << _InPorts[ index - 1 ]->PortName() << ")" << endl ;
137   _InPortsSize -= 1 ;
138   for ( i = index - 1 ; i < _InPortsSize  ; i++ ) {
139     _MapOfInPorts[ _InPorts[ i+1 ]->NodePortName() ] = i + 1 ;
140     _InPorts[ i ] = _InPorts[ i+1 ]  ;
141     _Links[ i ] = _Links[ i+1 ]  ;
142   }
143   _InPorts.resize( _InPortsSize ) ;
144   _Links.resize( _InPortsSize ) ;
145   _MapOfInPorts.erase( toPort->NodePortName() ) ;
146   if ( _InPortsSize == 0 ) {
147     PortStatus(NotConnected ) ;
148     if ( IsSwitch() ) {
149       Kind( SUPERV::InLineParameter ) ;
150     }
151   }
152   if ( toPort->GetOutPort() ) {
153     toPort->RemoveOutPort( false ) ;
154   }
155   return true ;
156 }
157
158 bool GraphBase::OutPort::ReNameInPort( const char* OldNodePortName ,
159                                        const char* NewNodePortName ) {
160   int index = _MapOfInPorts[ OldNodePortName ] ;
161   if ( index <= 0 || index > _InPortsSize ) {
162     cdebug << "GraphBase::OutPort::ReNameInPort Error in Node " << NodeName() << " " << OldNodePortName
163            << " --> " << NewNodePortName << " index " << index << endl ;
164     return false ;
165   }
166   _MapOfInPorts.erase( OldNodePortName ) ;
167   _MapOfInPorts[ NewNodePortName ] = index ;
168   return true ;
169 }
170
171 #if 0
172 bool GraphBase::OutPort::RemoveLinks() {
173   bool RetVal = true ;
174   int i ;
175   for ( i = 0 ; i < _InPortsSize ; i++ ) {
176     MESSAGE( "       to " << *_InPorts[ i ] );
177     RetVal = _InPorts[ i ]->RemoveLink() ;
178     if ( !RetVal )
179       break ;
180     RetVal = RemoveLink( _InPorts[ i ] ) ;
181     if ( !RetVal )
182       break ;
183   }
184   PortStatus(NotConnected ) ;
185   return RetVal ;
186 }
187 #endif
188
189 ostream & operator<< (ostream &f ,const GraphBase::OutPort &P)
190 {
191  f << P.PortName() << ", " << "type : " << P.PortType() << ", " ;
192  f << "kind " << P.Kind() << ", ";
193  f << "Status " << P.PortStatus() << ", ";
194  f << "from Node " << P.NodeName() << ", ";
195   return f;
196 }
197