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< YACSEvalOutputPort * >& outputsOfInterest)
75 std::size_t sz(checkPortsForEvaluation(outputsOfInterest));
76 _pattern->setOutPortsOfInterestForEvaluation(sz,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 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;
108 throw YACS::Exception("YACSEvalYFX::run : input session in null !");
109 /*loc=new YACSEvalSession;
113 nbOfBranches=_pattern->assignNbOfBranches();
115 YACS::ENGINE::Executor exe;
118 MyAutoThreadSaver locker;
119 exe.RunW(getUndergroundGeneratedGraph());
121 return getUndergroundGeneratedGraph()->getState()==YACS::DONE;
124 std::vector<YACSEvalSeqAny *> YACSEvalYFX::getResults() const
126 return _pattern->getResults();
129 YACS::ENGINE::Proc *YACSEvalYFX::getUndergroundGeneratedGraph() const
131 return _pattern->getUndergroundGeneratedGraph();
134 YACSEvalYFX::YACSEvalYFX(YACS::ENGINE::Proc *scheme, bool ownScheme):_pattern(0)
136 _pattern=YACSEvalYFXPattern::FindPatternFrom(scheme,ownScheme);
139 std::size_t YACSEvalYFX::checkPortsForEvaluation(const std::vector< YACSEvalOutputPort * >& outputs) const
141 std::vector< YACSEvalInputPort * > allInputs(getFreeInputPorts());
142 std::vector< YACSEvalOutputPort * > allOutputs(getFreeOutputPorts());
143 std::size_t sz(std::numeric_limits<std::size_t>::max());
144 for(std::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
147 if(!(*it)->isOKForLock() && !(*it)->hasSequenceOfValuesToEval(mySz))
149 std::ostringstream oss; oss << "YACSEvalYFX::checkPortsForEvaluation : input port with name \"" << (*it)->getName() << "\" is not set properly !";
150 throw YACS::Exception(oss.str());
152 if((*it)->hasSequenceOfValuesToEval(mySz))
154 if(sz==std::numeric_limits<std::size_t>::max())
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());
166 for(std::vector< 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::vector< YACSEvalInputPort * >::const_iterator it=allInputs.begin();it!=allInputs.end();it++)
177 YACSEvalYFX::~YACSEvalYFX()