Salome HOME
[PYTHON 3] WIP
[modules/yacs.git] / src / ydfx_gui / YDFXGUIWrap.cxx
1 // Copyright (C) 2016  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 // Author : Anthony Geay (EDF R&D)
20
21 #include "YDFXGUIWrap.hxx"
22
23 #include "YACSEvalYFX.hxx"
24 #include "YACSEvalPort.hxx"
25 #include "YACSEvalSeqAny.hxx"
26
27 #include <limits>
28
29 std::vector< YACSEvalInputPort * > YACSEvalYFXWrap::getFreeInputPorts() const
30 {
31   return _efx->getFreeInputPorts();
32 }
33
34 std::vector< YACSEvalOutputPort * > YACSEvalYFXWrap::getFreeOutputPorts() const
35 {
36   return _efx->getFreeOutputPorts();
37 }
38
39 YACSEvalExecParams *YACSEvalYFXWrap::getParams() const
40 {
41   return _efx->getParams();
42 }
43
44 bool YACSEvalYFXWrap::isLocked() const
45 {
46   return _efx->isLocked();
47 }
48
49 YACSEvalListOfResources *YACSEvalYFXWrap::giveResources()
50 {
51   return _efx->giveResources();
52 }
53
54 class AutoRunningDeclarator
55 {
56 public:
57   AutoRunningDeclarator(YACSEvalYFXWrap *wrap):_wrap(wrap) { _wrap->setRunningStatus(true); }
58   ~AutoRunningDeclarator() { _wrap->setRunningStatus(false); }
59 private:
60   YACSEvalYFXWrap *_wrap;
61 };
62
63 bool YACSEvalYFXWrap::run(YACSEvalSession *session, int& nbOfBranches)
64 {
65   AutoRunningDeclarator ard(this);
66   return _efx->run(session,nbOfBranches);
67 }
68
69 void YACSEvalYFXWrap::registerObserver(YACSEvalObserver *observer)
70 {
71   _efx->registerObserver(observer);
72 }
73
74 void YACSEvalYFXWrap::unlockAll()
75 {
76   _efx->unlockAll();
77   Q_EMIT lockSignal(false);
78 }
79
80 void YACSEvalYFXWrap::lockPortsForEvaluation()
81 {
82   bool lockedOrNot(isLocked());
83   std::vector< YACSEvalInputPort * > inps(getFreeInputPorts()),inps2;
84   foreach(YACSEvalInputPort *inp,inps)
85     {
86       if(inp->isRandomVar())
87         inps2.push_back(inp);
88     }
89   std::vector< YACSEvalOutputPort * > outps(getFreeOutputPorts()),outps2;
90   foreach(YACSEvalOutputPort *outp,outps)
91     {
92       if(outp->isQOfInterest())
93         outps2.push_back(outp);
94     }
95   if(lockedOrNot)
96     _efx->unlockAll();
97   _efx->lockPortsForEvaluation(inps2,outps2);
98   _efx->giveResources();//do not remove this line to generate resource info
99   if(!lockedOrNot)
100     Q_EMIT lockSignal(true);
101 }
102
103 int YACSEvalYFXWrap::getNbOfItems() const
104 {
105   std::vector< YACSEvalInputPort * > inps(getFreeInputPorts());
106   int nbOfRandom(0),refSz(std::numeric_limits<int>::max());
107   foreach(YACSEvalInputPort *inp,inps)
108     {
109       if(!inp->isRandomVar())
110         continue;
111       YACSEvalSeqAny *seq(inp->getSequenceOfValuesToEval());
112       if(!seq)
113         return 0;
114       if(nbOfRandom==0)
115         refSz=seq->size();
116       if(refSz!=seq->size())
117         return 0;
118       nbOfRandom++;
119     }
120   if(nbOfRandom==0)
121     return 0;
122   return refSz;
123 }
124
125 void lockPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputsOfInterest, const std::vector< YACSEvalOutputPort * >& outputsOfInterest);
126
127 YACSEvalYFXWrap::~YACSEvalYFXWrap()
128 {
129   delete _efx;
130 }
131
132 void YACSEvalYFXWrap::setRunningStatus(bool status)
133 {
134   if(_isRunning!=status)
135     Q_EMIT runSignal(status);
136   _isRunning=status;
137 }
138
139 void YACSEvalYFXWrap::updateSequencesStatus()
140 {
141   int nbOfVals;
142   bool newStatus(computeSequencesStatus(nbOfVals));
143   if(_isSeqOfValsSet!=newStatus)
144     {
145       _isSeqOfValsSet=newStatus;
146       Q_EMIT sequencesAreSetSignal(_isSeqOfValsSet);
147     }
148 }
149
150 bool YACSEvalYFXWrap::computeSequencesStatus(int& nbOfVals)
151 {
152   std::vector< YACSEvalInputPort * > inputs(_efx->getFreeInputPorts());
153   std::size_t sz(0),nbOfRandomVars(0);
154   foreach(YACSEvalInputPort *input,inputs)
155     {
156       if(!input->isRandomVar())
157         continue;
158       nbOfRandomVars++;
159       if(!input->hasSequenceOfValuesToEval())
160         return false;
161       YACSEvalSeqAny *seq(input->getSequenceOfValuesToEval());
162       sz=seq->size();
163     }
164   if(nbOfRandomVars==0 || sz==0)
165     return false;
166   foreach(YACSEvalInputPort *input,inputs)
167     {
168       if(!input->isRandomVar())
169         continue;
170       if(sz!=input->getSequenceOfValuesToEval()->size())
171         return false;
172     }
173   nbOfVals=sz;
174   return true;
175 }