Salome HOME
MPV: Merge V1_2d
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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
32 //GraphBase::OutPort::~OutPort() {
33 // In GraphExecutor::OutPort
34 //  int i ;
35 //  for ( i = 1 ; i <= _InPortsSize  ; i++ ) {
36 //    if ( !_InPorts[ i ]->RemoveLink() )
37 //      cout << "GraphBase::OutPort::~OutPort error "
38 //           << _InPorts[ i ]->PortName() << endl ;
39 //  }
40 //}
41
42 bool GraphBase::OutPort::HasInPort() const {
43   return _InPortsSize != 0 ;
44 }
45
46 const GraphBase::InPort * GraphBase::OutPort::GetInPort(
47                          const GraphBase::InPort * toPort ) {
48   const GraphBase::InPort * anInPort = NULL ;
49   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
50   if ( index > 0 && index <= _InPortsSize ) {
51     anInPort = _InPorts[ index-1 ] ;
52     if ( anInPort != toPort ) {
53       cout << "GraphBase::OutPort::GetInPort inconsistency toPort "
54            << hex << (void *) toPort << " != anInPort " << (void *) anInPort
55            << dec << endl ;
56       anInPort = NULL ;
57     }
58   }
59   return anInPort ;
60 }
61
62 SUPERV::Link_var GraphBase::OutPort::InPortObjRef(
63                          const GraphBase::InPort * toPort ) {
64   SUPERV::Link_var aLink = SUPERV::Link::_nil() ;
65   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
66   if ( index > 0 && index <= _InPortsSize ) {
67     aLink = _Links[ index-1 ] ;
68   }
69   return aLink ;
70 }
71
72 bool GraphBase::OutPort::AddInPort( GraphBase::InPort * toPort ) {
73   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
74   if ( index > 0 && index <= _InPortsSize ) {
75     return false ;
76   }
77   _InPortsSize += 1 ;
78   _InPorts.resize( _InPortsSize ) ;
79   _MapOfInPorts[ toPort->NodePortName() ] = _InPortsSize ;
80   _InPorts[ _InPortsSize-1 ] = toPort ;
81   _Links.resize( _InPortsSize ) ;
82   _Links[ _InPortsSize-1 ] = SUPERV::Link::_nil() ;
83   cdebug << "OutPort " << NodeName() << "(" << PortName() << ") --> InPort "
84          << toPort->NodeName() << "(" << toPort->PortName() << ") SwitchPort "
85          << toPort->IsEndSwitch() << endl ;
86   return true ;
87 }
88
89 bool GraphBase::OutPort::AddInPortObjRef( GraphBase::InPort * toPort ,
90                                           SUPERV::Link_var aLink ) {
91   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
92   if ( index <= 0 || index > _InPortsSize ) {
93     return false ;
94   }
95   _Links[ index - 1 ] = aLink ;
96   return true ;
97 }
98
99 bool GraphBase::OutPort::RemoveInPort( GraphBase::InPort * toPort ) {
100   int i ;
101   int index = _MapOfInPorts[ toPort->NodePortName() ] ;
102   if ( index <= 0 || index > _InPortsSize ) {
103     cdebug << "GraphBase::OutPort::RemoveInPort Error " << NodePortName()
104            << " --> " << toPort->NodePortName() << endl ;
105     return false ;
106   }
107   _InPortsSize -= 1 ;
108   for ( i = index - 1 ; i < _InPortsSize  ; i++ ) {
109     _MapOfInPorts[ _InPorts[ i+1 ]->NodePortName() ] = i + 1 ;
110     _InPorts[ i ] = _InPorts[ i+1 ]  ;
111     _Links[ i ] = _Links[ i+1 ]  ;
112   }
113   _InPorts.resize( _InPortsSize ) ;
114   _Links.resize( _InPortsSize ) ;
115   _MapOfInPorts.erase( toPort->NodePortName() ) ;
116   if ( _InPortsSize == 0 ) {
117     PortStatus(NotConnected ) ;
118   }
119   toPort->RemoveOutPort() ;
120   return true ;
121 }
122
123 bool GraphBase::OutPort::ReNameInPort( const char* OldNodePortName ,
124                                        const char* NewNodePortName ) {
125   int index = _MapOfInPorts[ OldNodePortName ] ;
126   if ( index <= 0 || index > _InPortsSize ) {
127     cdebug << "GraphBase::OutPort::ReNameLink Error " << OldNodePortName
128            << " --> " << NewNodePortName << " index " << index << endl ;
129     return false ;
130   }
131   _MapOfInPorts.erase( OldNodePortName ) ;
132   _MapOfInPorts[ NewNodePortName ] = index ;
133   return true ;
134 }
135
136 #if 0
137 bool GraphBase::OutPort::RemoveLinks() {
138   bool RetVal = true ;
139   int i ;
140   for ( i = 0 ; i < _InPortsSize ; i++ ) {
141     MESSAGE( "       to " << *_InPorts[ i ] );
142     RetVal = _InPorts[ i ]->RemoveLink() ;
143     if ( !RetVal )
144       break ;
145     RetVal = RemoveLink( _InPorts[ i ] ) ;
146     if ( !RetVal )
147       break ;
148   }
149   PortStatus(NotConnected ) ;
150   return RetVal ;
151 }
152 #endif
153
154 ostream & operator<< (ostream &f ,const GraphBase::OutPort &P)
155 {
156   f << P.PortName() << ", " 
157     << "type : " << P.PortType() << ", " ;
158  f << "kind "
159    << P.Kind() << ", ";
160  f << "from "
161    << P.NodeName() << ", ";
162
163   return f;
164 }
165