1 // Copyright (C) 2012-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
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"
30 #include "Exception.hxx"
31 #include "parsers.hxx"
40 class MyAutoThreadSaver
43 MyAutoThreadSaver():_save(PyEval_SaveThread()) { }
44 ~MyAutoThreadSaver() { PyEval_RestoreThread(_save); }
50 YACSEvalYFX *YACSEvalYFX::BuildFromFile(const std::string& xmlOfScheme)
52 YACS::ENGINE::RuntimeSALOME::setRuntime();
54 YACS::ENGINE::Proc *scheme(l.load(xmlOfScheme.c_str()));
55 return new YACSEvalYFX(scheme,true);
58 YACSEvalYFX *YACSEvalYFX::BuildFromScheme(YACS::ENGINE::Proc *scheme)
60 return new YACSEvalYFX(scheme,false);
63 std::vector< YACSEvalInputPort * > YACSEvalYFX::getFreeInputPorts() const
65 return _pattern->getFreeInputPorts();
68 std::vector< YACSEvalOutputPort * > YACSEvalYFX::getFreeOutputPorts() const
70 return _pattern->getFreeOutputPorts();
73 void YACSEvalYFX::lockPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputsOfInterest, const std::vector< YACSEvalOutputPort * >& outputsOfInterest)
75 checkPortsForEvaluation(inputsOfInterest,outputsOfInterest);
76 _pattern->setOutPortsOfInterestForEvaluation(outputsOfInterest);
77 _pattern->generateGraph();
80 void YACSEvalYFX::unlockAll()
82 std::vector< YACSEvalInputPort * > allInputs(getFreeInputPorts());
83 for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
85 _pattern->resetOutputsOfInterest();
86 _pattern->resetGeneratedGraph();
89 bool YACSEvalYFX::isLocked() const
91 return _pattern->isLocked();
94 YACSEvalListOfResources *YACSEvalYFX::giveResources()
96 return _pattern->giveResources();
99 bool YACSEvalYFX::run(YACSEvalSession *session, int& nbOfBranches)
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;
109 throw YACS::Exception("YACSEvalYFX::run : input session in null !");
110 /*loc=new YACSEvalSession;
114 nbOfBranches=_pattern->assignNbOfBranches();
116 YACS::ENGINE::Executor exe;
119 MyAutoThreadSaver locker;
120 exe.RunW(getUndergroundGeneratedGraph());
122 return getUndergroundGeneratedGraph()->getState()==YACS::DONE;
125 std::vector<YACSEvalSeqAny *> YACSEvalYFX::getResults() const
127 return _pattern->getResults();
130 YACS::ENGINE::Proc *YACSEvalYFX::getUndergroundGeneratedGraph() const
132 return _pattern->getUndergroundGeneratedGraph();
135 YACSEvalYFX::YACSEvalYFX(YACS::ENGINE::Proc *scheme, bool ownScheme):_pattern(0)
137 _pattern=YACSEvalYFXPattern::FindPatternFrom(scheme,ownScheme);
140 void YACSEvalYFX::checkPortsForEvaluation(const std::vector< YACSEvalInputPort * >& inputs, const std::vector< YACSEvalOutputPort * >& outputs) const
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++)
147 if(inputs2.find(*it)==inputs2.end())
149 if(!(*it)->isOKForLock())
151 std::ostringstream oss; oss << "YACSEvalYFX::checkPortsForEvaluation : input port with name \"" << (*it)->getName() << "\" is not set properly !";
152 throw YACS::Exception(oss.str());
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++)
164 (*it)->declareRandomnessStatus(inputs2.find(*it)!=inputs2.end());
169 YACSEvalYFX::~YACSEvalYFX()