Salome HOME
9037dae68e884b88f94bcae21b8d33c5797958e2
[modules/yacs.git] / src / ydfx_gui / YDFXResourceModel.cxx
1 // Copyright (C) 2017-2023  CEA, EDF
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 "YDFXResourceModel.hxx"
21
22 #include "YACSEvalYFX.hxx"
23 #include "YACSEvalResource.hxx"
24 //#include "ResourcesManager.hxx" 
25
26 #include <algorithm>
27 #include <sstream>
28 #include <cstdlib>
29
30 YDFXResourceModel::YDFXResourceModel(YACSEvalYFX* eval)
31 : AbstractResourceModel(),
32   _eval(eval)
33 {
34 }
35
36 YDFXResourceModel::~YDFXResourceModel()
37 {
38 }
39
40 bool YDFXResourceModel::getParallelizeStatus()const
41 {
42   return _eval->getParallelizeStatus();
43 }
44
45 void YDFXResourceModel::setParallelizeStatus(bool v)
46 {
47   _eval->setParallelizeStatus(v);
48 }
49
50 std::string YDFXResourceModel::getRemoteDir()const
51 {
52   return getClusterParams().getRemoteWorkingDir();
53 }
54
55 void YDFXResourceModel::setRemoteDir(const std::string& v)
56 {
57   getClusterParams().setRemoteWorkingDir(v);
58 }
59
60 std::string YDFXResourceModel::getDefaultRemoteDir(std::string machine)const
61 {
62   return "/tmp";
63 }
64
65 std::string YDFXResourceModel::getLocalDir()const
66 {
67   return getClusterParams().getLocalWorkingDir();
68 }
69
70 void YDFXResourceModel::setLocalDir(const std::string& v)
71 {
72   getClusterParams().setLocalWorkingDir(v);
73 }
74
75 std::string YDFXResourceModel::getWckey()const
76 {
77   return getClusterParams().getWCKey();
78 }
79
80 void YDFXResourceModel::setWckey(const std::string& v)
81 {
82   getClusterParams().setWCKey(v);
83 }
84
85 int YDFXResourceModel::getMaxDurationMinutes()const
86 {
87   int minutes;
88   int hours;
89   getMaxDuration(hours, minutes);
90   return minutes;
91 }
92
93 void YDFXResourceModel::setMaxDurationMinutes(int v)
94 {
95   int minutes;
96   int hours;
97   getMaxDuration(hours, minutes);
98   setMaxDuration(hours, v);
99 }
100
101 int YDFXResourceModel::getMaxDurationHours()const
102 {
103   int minutes;
104   int hours;
105   getMaxDuration(hours, minutes);
106   return hours;
107 }
108
109 void YDFXResourceModel::setMaxDurationHours(int v)
110 {
111   int minutes;
112   int hours;
113   getMaxDuration(hours, minutes);
114   setMaxDuration(v, minutes);
115 }
116
117 unsigned int YDFXResourceModel::getNbprocs()const
118 {
119   return getClusterParams().getNbProcs();
120 }
121
122 void YDFXResourceModel::setNbprocs(unsigned int v)
123 {
124   getClusterParams().setNbProcs(v);
125 }
126
127 std::string YDFXResourceModel::getWantedMachine()const
128 {
129   const std::string default_machine("localhost");
130   std::vector<std::string> chosenMachines = resources()->getAllChosenMachines();
131   std::vector<std::string> fittingMachines = resources()->getAllFittingMachines();
132   std::string wantedMachine="";
133   std::vector<std::string>::const_iterator it;
134   bool foundMachine = false;
135   it = chosenMachines.begin();
136   // take the first chosen machine which is also fitting.
137   while(!foundMachine && it!=chosenMachines.end())
138   {
139     if(std::find(fittingMachines.begin(), fittingMachines.end(), *it)
140        != fittingMachines.end())
141     {
142       foundMachine = true;
143       wantedMachine = *it;
144     }
145   }
146   if(!foundMachine)
147   {
148     // take localhost if fitting
149     if(std::find(fittingMachines.begin(), fittingMachines.end(),
150                  default_machine) != fittingMachines.end())
151     {
152       foundMachine = true;
153       wantedMachine = default_machine;
154     }
155     else if(fittingMachines.size() > 0)
156     {
157       foundMachine = true;
158       wantedMachine = fittingMachines[0];
159     }
160   }
161   return wantedMachine;
162 }
163
164 void YDFXResourceModel::setWantedMachine(const std::string& v)
165 {
166   resources()->setWantedMachine(v);
167 }
168
169 std::vector<std::string> YDFXResourceModel::getFittingMachines()const
170 {
171   return resources()->getAllFittingMachines();
172 }
173
174 const std::list<std::string>& YDFXResourceModel::getInFiles()const
175 {
176   return getClusterParams().getInFiles();
177 }
178
179 std::list<std::string>& YDFXResourceModel::getInFiles()
180 {
181   return getClusterParams().getInFiles();
182 }
183   
184 bool YDFXResourceModel::isMachineInteractive(const std::string& machine)const
185 {
186   return resources()->isMachineInteractive(machine);
187 }
188
189 void YDFXResourceModel::reset(YACSEvalYFX* eval)
190 {
191   _eval = eval;
192 }
193
194 YACSEvalListOfResources* YDFXResourceModel::resources()
195 {
196   return _eval->giveResources();
197 }
198
199 const YACSEvalListOfResources* YDFXResourceModel::resources()const
200 {
201   return _eval->giveResources();
202 }
203
204 YACSEvalParamsForCluster& YDFXResourceModel::getClusterParams()
205 {
206   return _eval->giveResources()->getAddParamsForCluster();
207 }
208
209 const YACSEvalParamsForCluster& YDFXResourceModel::getClusterParams()const
210 {
211   return _eval->giveResources()->getAddParamsForCluster();
212 }
213
214 void YDFXResourceModel::getMaxDuration(int& hours, int& minutes)const
215 {
216   std::string duration = getClusterParams().getMaxDuration();
217   hours=0;
218   minutes=0;
219   if(duration.size() > 0)
220   {
221     std::stringstream ss(duration);
222     std::string value;
223     std::getline(ss, value, ':');
224     hours = atoi(value.c_str());
225     std::getline(ss, value, ':');
226     minutes = atoi(value.c_str());
227   }
228   if(hours<0)
229     hours = 0;
230   if(hours>99)
231     hours = 99;
232   if(minutes<0)
233     minutes = 0;
234   if(minutes > 59)
235     minutes = 59;
236 }
237
238 void YDFXResourceModel::setMaxDuration(int hours, int minutes)
239 {
240   getClusterParams().setMaxDuration(AbstractResourceModel::getMaxDuration());
241 }