]> SALOME platform Git repositories - modules/yacs.git/blob - src/evalyfx/YACSEvalYFX.cxx
Salome HOME
a96558401c5956af8d8c7336d746adb08297348c
[modules/yacs.git] / src / evalyfx / YACSEvalYFX.cxx
1 // Copyright (C) 2012-2015  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 class MyAutoThreadSaver
41 {
42 public:
43   MyAutoThreadSaver():_save(PyEval_SaveThread()) { }
44   ~MyAutoThreadSaver() { PyEval_RestoreThread(_save); }
45 private:
46   PyThreadState *_save;
47 };
48
49
50 YACSEvalYFX *YACSEvalYFX::BuildFromFile(const std::string& xmlOfScheme)
51 {
52   YACS::ENGINE::RuntimeSALOME::setRuntime();
53   YACS::YACSLoader l;
54   YACS::ENGINE::Proc *scheme(l.load(xmlOfScheme.c_str()));
55   return new YACSEvalYFX(scheme,true);
56 }
57
58 YACSEvalYFX *YACSEvalYFX::BuildFromScheme(YACS::ENGINE::Proc *scheme)
59 {
60   return new YACSEvalYFX(scheme,false);
61 }
62
63 std::vector< YACSEvalInputPort * > YACSEvalYFX::getFreeInputPorts() const
64 {
65   return _pattern->getFreeInputPorts();
66 }
67
68 std::vector< YACSEvalOutputPort * > YACSEvalYFX::getFreeOutputPorts() const
69 {
70   return _pattern->getFreeOutputPorts();
71 }
72
73 void YACSEvalYFX::lockPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputsOfInterest, const std::vector< YACSEvalOutputPort * >& outputsOfInterest)
74 {
75   checkPortsForEvaluation(inputsOfInterest,outputsOfInterest);
76   _pattern->setOutPortsOfInterestForEvaluation(outputsOfInterest);
77   _pattern->generateGraph();
78 }
79
80 void YACSEvalYFX::unlockAll()
81 {
82   std::vector< YACSEvalInputPort * > allInputs(getFreeInputPorts());
83   for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
84     (*it)->unlock();
85   _pattern->resetOutputsOfInterest();
86   _pattern->resetGeneratedGraph();
87 }
88
89 bool YACSEvalYFX::isLocked() const
90 {
91   return _pattern->isLocked();
92 }
93
94 YACSEvalListOfResources *YACSEvalYFX::giveResources()
95 {
96   return _pattern->giveResources();
97 }
98
99 bool YACSEvalYFX::run(YACSEvalSession *session, int& nbOfBranches)
100 {
101   _pattern->assignRandomVarsInputs();
102   YACSEvalListOfResources *rss(giveResources());
103   if(!rss->isInteractive())
104     throw YACS::Exception("YACSEvalYFX::run : not implemented yet for non interactive !");
105   YACSEvalSession *mySession(session);
106   YACS::AutoCppPtr<YACSEvalSession> loc;
107   if(!session)
108     {
109       throw YACS::Exception("YACSEvalYFX::run : input session in null !");
110       /*loc=new YACSEvalSession;
111       mySession=loc;*/
112     }
113   rss->apply();
114   nbOfBranches=_pattern->assignNbOfBranches();
115   mySession->launch();
116   YACS::ENGINE::Executor exe;
117   //
118   {
119     MyAutoThreadSaver locker;
120     exe.RunW(getUndergroundGeneratedGraph());
121   }
122   return getUndergroundGeneratedGraph()->getState()==YACS::DONE;
123 }
124
125 std::vector<YACSEvalSeqAny *> YACSEvalYFX::getResults() const
126 {
127   return _pattern->getResults();
128 }
129
130 YACS::ENGINE::Proc *YACSEvalYFX::getUndergroundGeneratedGraph() const
131 {
132   return _pattern->getUndergroundGeneratedGraph();
133 }
134
135 YACSEvalYFX::YACSEvalYFX(YACS::ENGINE::Proc *scheme, bool ownScheme):_pattern(0)
136 {
137   _pattern=YACSEvalYFXPattern::FindPatternFrom(scheme,ownScheme);
138 }
139
140 void YACSEvalYFX::checkPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputs, const std::vector< YACSEvalOutputPort * >& outputs) const
141 {
142   std::set<YACSEvalInputPort * > inputs2(inputs.begin(),inputs.end());
143   std::vector< YACSEvalInputPort * > allInputs(getFreeInputPorts());
144   std::vector< YACSEvalOutputPort * > allOutputs(getFreeOutputPorts());
145   for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
146     {
147       if(inputs2.find(*it)==inputs2.end())
148         {
149           if(!(*it)->isOKForLock())
150             {
151               std::ostringstream oss; oss << "YACSEvalYFX::checkPortsForEvaluation : input port with name \"" << (*it)->getName() << "\" is not set properly !";
152               throw YACS::Exception(oss.str());
153             }
154         }
155     }
156   for(std::vector< YACSEvalOutputPort * >::const_iterator it=outputs.begin();it!=outputs.end();it++)
157     if(std::find(allOutputs.begin(),allOutputs.end(),*it)==allOutputs.end())
158       throw YACS::Exception("YACSEvalYFX::lockPortsForEvaluation : one of output is not part of this !");
159   std::set< YACSEvalOutputPort * > soutputs(outputs.begin(),outputs.end());
160   if(soutputs.size()!=outputs.size())
161     throw YACS::Exception("YACSEvalYFX::lockPortsForEvaluation : each elt in outputs must appear once !");
162   for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
163     {
164       (*it)->declareRandomnessStatus(inputs2.find(*it)!=inputs2.end());
165       (*it)->lock();
166     }
167 }
168
169 YACSEvalYFX::~YACSEvalYFX()
170 {
171   delete _pattern;
172 }