Salome HOME
Merge branch 'V7_dev'
[modules/yacs.git] / src / evalyfx / YACSEvalYFX.cxx
1 // Copyright (C) 2012-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 "YACSEvalYFX.hxx"
22 #include "YACSEvalYFXPattern.hxx"
23 #include "YACSEvalAutoPtr.hxx"
24 #include "YACSEvalResource.hxx"
25 #include "YACSEvalSession.hxx"
26 #include "YACSEvalPort.hxx"
27 #include "RuntimeSALOME.hxx"
28 #include "Executor.hxx"
29 #include "Proc.hxx"
30 #include "Exception.hxx"
31 #include "parsers.hxx"
32
33 #include <algorithm>
34 #include <sstream>
35 #include <limits>
36 #include <set>
37
38 #include <Python.h>
39
40 YACSEvalYFX *YACSEvalYFX::BuildFromFile(const std::string& xmlOfScheme)
41 {
42   YACS::ENGINE::RuntimeSALOME::setRuntime();
43   YACS::YACSLoader l;
44   YACS::ENGINE::Proc *scheme(l.load(xmlOfScheme.c_str()));
45   return new YACSEvalYFX(scheme,true);
46 }
47
48 YACSEvalYFX *YACSEvalYFX::BuildFromScheme(YACS::ENGINE::Proc *scheme)
49 {
50   return new YACSEvalYFX(scheme,false);
51 }
52
53 std::vector< YACSEvalInputPort * > YACSEvalYFX::getFreeInputPorts() const
54 {
55   return _pattern->getFreeInputPorts();
56 }
57
58 std::vector< YACSEvalOutputPort * > YACSEvalYFX::getFreeOutputPorts() const
59 {
60   return _pattern->getFreeOutputPorts();
61 }
62
63 void YACSEvalYFX::lockPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputsOfInterest, const std::vector< YACSEvalOutputPort * >& outputsOfInterest)
64 {
65   checkPortsForEvaluation(inputsOfInterest,outputsOfInterest);
66   _pattern->setOutPortsOfInterestForEvaluation(outputsOfInterest);
67 }
68
69 void YACSEvalYFX::unlockAll()
70 {
71   std::vector< YACSEvalInputPort * > allInputs(getFreeInputPorts());
72   for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
73     (*it)->unlock();
74   _pattern->resetOutputsOfInterest();
75   _pattern->resetGeneratedGraph();
76 }
77
78 bool YACSEvalYFX::isLocked() const
79 {
80   return _pattern->isLocked();
81 }
82
83 YACSEvalListOfResources *YACSEvalYFX::giveResources()
84 {
85   return _pattern->giveResources();
86 }
87
88 bool YACSEvalYFX::run(YACSEvalSession *session, int& nbOfBranches)
89 {
90   _pattern->generateGraph();
91   if(!session)
92     {
93       throw YACS::Exception("YACSEvalYFX::run : input session in null !");
94     }
95   session->launch();
96   YACSEvalListOfResources *rss(giveResources());
97   rss->checkOKForRun();
98   _pattern->assignRandomVarsInputs();
99   rss->apply();
100   nbOfBranches=_pattern->assignNbOfBranches();
101   return _pattern->go(_params.getStopASAPAfterErrorStatus(),session);
102 }
103
104 void YACSEvalYFX::registerObserver(YACSEvalObserver *observer)
105 {
106   if(!_pattern)
107     throw YACS::Exception("YACSEvalYFX::registerObserver : no pattern !");
108   _pattern->registerObserver(observer);
109 }
110
111 YACSEvalObserver *YACSEvalYFX::getObserver()
112 {
113   if(!_pattern)
114     throw YACS::Exception("YACSEvalYFX::getObserver : no pattern !");
115   return _pattern->getObserver();
116 }
117
118 std::string YACSEvalYFX::getErrorDetailsInCaseOfFailure() const
119 {
120   return _pattern->getErrorDetailsInCaseOfFailure();
121 }
122
123 std::string YACSEvalYFX::getStatusOfRunStr() const
124 {
125   return _pattern->getStatusOfRunStr();
126 }
127
128 std::vector<YACSEvalSeqAny *> YACSEvalYFX::getResults() const
129 {
130   return _pattern->getResults();
131 }
132
133 std::vector<YACSEvalSeqAny *> YACSEvalYFX::getResultsInCaseOfFailure(std::vector<unsigned int>& passedIds) const
134 {
135   return _pattern->getResultsInCaseOfFailure(passedIds);
136 }
137
138 YACS::ENGINE::Proc *YACSEvalYFX::getUndergroundGeneratedGraph() const
139 {
140   return _pattern->getUndergroundGeneratedGraph();
141 }
142
143 void YACSEvalYFX::setParallelizeStatus(bool newVal)
144 {
145   _pattern->setParallelizeStatus(newVal);
146 }
147
148 bool YACSEvalYFX::getParallelizeStatus() const
149 {
150   return _pattern->getParallelizeStatus();
151 }
152
153 YACSEvalYFX::YACSEvalYFX(YACS::ENGINE::Proc *scheme, bool ownScheme):_pattern(0)
154 {
155   _pattern=YACSEvalYFXPattern::FindPatternFrom(this,scheme,ownScheme);
156 }
157
158 void YACSEvalYFX::checkPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputs, const std::vector< YACSEvalOutputPort * >& outputs) const
159 {
160   std::set<YACSEvalInputPort * > inputs2(inputs.begin(),inputs.end());
161   std::vector< YACSEvalInputPort * > allInputs(getFreeInputPorts());
162   std::vector< YACSEvalOutputPort * > allOutputs(getFreeOutputPorts());
163   for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
164     {
165       if(inputs2.find(*it)==inputs2.end())
166         {
167           if(!(*it)->isOKForLock())
168             {
169               std::ostringstream oss; oss << "YACSEvalYFX::checkPortsForEvaluation : input port with name \"" << (*it)->getName() << "\" is not set properly !";
170               throw YACS::Exception(oss.str());
171             }
172         }
173     }
174   for(std::vector< YACSEvalOutputPort * >::const_iterator it=outputs.begin();it!=outputs.end();it++)
175     if(std::find(allOutputs.begin(),allOutputs.end(),*it)==allOutputs.end())
176       throw YACS::Exception("YACSEvalYFX::lockPortsForEvaluation : one of output is not part of this !");
177   std::set< YACSEvalOutputPort * > soutputs(outputs.begin(),outputs.end());
178   if(soutputs.size()!=outputs.size())
179     throw YACS::Exception("YACSEvalYFX::lockPortsForEvaluation : each elt in outputs must appear once !");
180   for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
181     {
182       (*it)->declareRandomnessStatus(inputs2.find(*it)!=inputs2.end());
183       (*it)->lock();
184     }
185 }
186
187 YACSEvalYFX::~YACSEvalYFX()
188 {
189   delete _pattern;
190 }