Salome HOME
Copyright update 2021
[modules/yacs.git] / src / engine / InGate.cxx
1 // Copyright (C) 2006-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "InGate.hxx"
21 #include "Node.hxx"
22
23 //#define _DEVDEBUG_
24 #include "YacsTrace.hxx"
25
26 #include <algorithm>
27
28 using namespace YACS::ENGINE;
29 using namespace std;
30
31 const char InGate::NAME[]="InGate";
32
33 InGate::InGate(Node *node):Port(node)
34 {
35 }
36
37 InGate::~InGate()
38 {
39 }
40
41 string InGate::getNameOfTypeOfCurrentInstance() const
42 {
43   return NAME;
44 }
45
46 void InGate::edDisconnectAllLinksToMe()
47 {
48   for(list< std::pair<OutGate *, bool> >::iterator iter=_backLinks.begin();iter!=_backLinks.end();iter++)
49     ((*iter).first)->edRemoveInGate(this,false);
50   _backLinks.clear();
51 }
52
53 class ItemCmp
54 {
55 private:
56   OutGate *_itf;
57 public:
58   ItemCmp(OutGate *itf):_itf(itf) { }
59   bool operator()(const std::pair<OutGate *,bool>& elt) const { return elt.first==_itf; }
60 };
61
62 //! Notify this port that an upstream node connected by a control flow link is finished
63 /*!
64  *  Calls the node's gate method : Node::exUpdateState
65  *
66  *  Called by OutGate::exNotifyDone
67  */
68 void InGate::exNotifyFromPrecursor(OutGate *from)
69 {
70   DEBTRACE("InGate::exNotifyFromPrecursor");
71   list< pair<OutGate *, bool> >::iterator iter(std::find_if(_backLinks.begin(),_backLinks.end(),ItemCmp(from)));
72   if(iter==_backLinks.end())
73     throw YACS::Exception("InGate::exNotifyFromPrecursor : precursor not found !");
74   (*iter).second=true;
75   if(exIsReady())
76     _node->exUpdateState();
77 }
78
79 //! Notify this port that an upstream node connected by a control flow link has failed
80 /*!
81  *
82  */
83 void InGate::exNotifyFailed()
84 {
85   if(_node)
86     _node->exFailedState();
87 }
88
89 //! Notify this port that an upstream node connected by a control flow link has been disabled
90 /*!
91  *
92  */
93 void InGate::exNotifyDisabled()
94 {
95   if(_node)
96     _node->exDisabledState();
97 }
98
99 void InGate::edAppendPrecursor(OutGate *from)
100 {
101   list< pair<OutGate *, bool> >::iterator iter(std::find_if(_backLinks.begin(),_backLinks.end(),ItemCmp(from)));
102   if(iter!=_backLinks.end())
103     (*iter).second=false;
104   else
105     _backLinks.push_back(pair<OutGate *, bool>(from,false));
106 }
107
108 void InGate::edRemovePrecursor(OutGate *from)
109 {
110   list< pair<OutGate *, bool> >::iterator iter(std::find_if(_backLinks.begin(),_backLinks.end(),ItemCmp(from)));
111   if(iter!=_backLinks.end())
112     _backLinks.erase(iter);
113 }
114
115 int InGate::getNumberOfBackLinks() const
116 {
117   return _backLinks.size();
118 }
119
120 void InGate::exReset()
121 {
122   for(list< std::pair<OutGate *, bool> >::iterator iter=_backLinks.begin();iter!=_backLinks.end();iter++)
123     (*iter).second=false;
124 }
125
126 bool InGate::exIsReady() const
127 {
128   bool isReady(true);
129   for(list< std::pair<OutGate *, bool> >::const_iterator iter=_backLinks.begin();iter!=_backLinks.end() && isReady;iter++)
130     isReady=(*iter).second;
131   return isReady;
132 }
133
134 std::list<OutGate *> InGate::getBackLinks()
135 {
136   list<OutGate *> listo;
137   for(list< std::pair<OutGate *, bool> >::const_iterator iter=_backLinks.begin();iter!=_backLinks.end();iter++)
138     listo.push_back(iter->first);
139   return listo;
140 }
141
142 void InGate::setPrecursorDone(OutGate *from)
143 {
144   list< std::pair<OutGate *, bool> >::iterator iter(std::find_if(_backLinks.begin(),_backLinks.end(),ItemCmp(from)));
145   if(iter!=_backLinks.end())
146     (*iter).second=true;
147   else
148     throw YACS::Exception("InGate::setPrecursorDone : precursor not found !");
149 }