Salome HOME
b3210bd5e12f4d575c2f01aef288ffb413761cc9
[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::list< YACSEvalInputPort * > YACSEvalYFX::getFreeInputPorts() const
64 {
65   return _pattern->getFreeInputPorts();
66 }
67
68 std::list< YACSEvalOutputPort * > YACSEvalYFX::getFreeOutputPorts() const
69 {
70   return _pattern->getFreeOutputPorts();
71 }
72
73 void YACSEvalYFX::lockPortsForEvaluation(const std::list< YACSEvalOutputPort * >& outputsOfInterest)
74 {
75   std::size_t sz(checkPortsForEvaluation(outputsOfInterest));
76   _pattern->setOutPortsOfInterestForEvaluation(sz,outputsOfInterest);
77   _pattern->generateGraph();
78 }
79
80 void YACSEvalYFX::unlockAll()
81 {
82   std::list< YACSEvalInputPort * > allInputs(getFreeInputPorts());
83   for(std::list< 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   YACSEvalListOfResources *rss(giveResources());
102   if(!rss->isInteractive())
103     throw YACS::Exception("YACSEvalYFX::run : not implemented yet for non interactive !");
104   YACSEvalSession *mySession(session);
105   YACS::AutoCppPtr<YACSEvalSession> loc;
106   if(!session)
107     {
108       throw YACS::Exception("YACSEvalYFX::run : input session in null !");
109       /*loc=new YACSEvalSession;
110       mySession=loc;*/
111     }
112   rss->apply();
113   nbOfBranches=_pattern->assignNbOfBranches();
114   mySession->launch();
115   YACS::ENGINE::Executor exe;
116   //
117   {
118     MyAutoThreadSaver locker;
119     exe.RunW(getUndergroundGeneratedGraph());
120   }
121   return getUndergroundGeneratedGraph()->getState()==YACS::DONE;
122 }
123
124 std::vector<YACSEvalSeqAny *> YACSEvalYFX::getResults() const
125 {
126   return _pattern->getResults();
127 }
128
129 YACS::ENGINE::Proc *YACSEvalYFX::getUndergroundGeneratedGraph() const
130 {
131   return _pattern->getUndergroundGeneratedGraph();
132 }
133
134 YACSEvalYFX::YACSEvalYFX(YACS::ENGINE::Proc *scheme, bool ownScheme):_pattern(0)
135 {
136   _pattern=YACSEvalYFXPattern::FindPatternFrom(scheme,ownScheme);
137 }
138
139 std::size_t YACSEvalYFX::checkPortsForEvaluation(const std::list< YACSEvalOutputPort * >& outputs) const
140 {
141   std::list< YACSEvalInputPort * > allInputs(getFreeInputPorts());
142   std::list< YACSEvalOutputPort * > allOutputs(getFreeOutputPorts());
143   std::size_t sz(std::numeric_limits<std::size_t>::max());
144   for(std::list< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
145     {
146       std::size_t mySz;
147       if(!(*it)->isOKForLock() && !(*it)->hasSequenceOfValuesToEval(mySz))
148         {
149           std::ostringstream oss; oss << "YACSEvalYFX::checkPortsForEvaluation : input port with name \"" << (*it)->getName() << "\" is not set properly !";
150           throw YACS::Exception(oss.str());
151         }
152       if((*it)->hasSequenceOfValuesToEval(mySz))
153         {
154           if(sz==std::numeric_limits<std::size_t>::max())
155             sz=mySz;
156           else
157             {
158               if(sz!=mySz)
159                 {
160                   std::ostringstream oss; oss << "YACSEvalYFX::checkPortsForEvaluation : input port with name \"" << (*it)->getName() << "\" is declared as to be evaluated on array ! But size of array is not the same than the others !";
161                   throw YACS::Exception(oss.str());
162                 }
163             }
164         }
165     }
166   for(std::list< YACSEvalOutputPort * >::const_iterator it=outputs.begin();it!=outputs.end();it++)
167     if(std::find(allOutputs.begin(),allOutputs.end(),*it)==allOutputs.end())
168       throw YACS::Exception("YACSEvalYFX::lockPortsForEvaluation : one of output is not part of this !");
169   std::set< YACSEvalOutputPort * > soutputs(outputs.begin(),outputs.end());
170   if(soutputs.size()!=outputs.size())
171     throw YACS::Exception("YACSEvalYFX::lockPortsForEvaluation : each elt in outputs must appear once !");
172   for(std::list< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
173     (*it)->lock();
174   return sz;
175 }
176
177 YACSEvalYFX::~YACSEvalYFX()
178 {
179   delete _pattern;
180 }