1 // Copyright (C) 2006-2012 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.
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
21 #include "SALOME_LifeCycleCORBA.hxx"
22 #include "SALOME_NamingService.hxx"
24 #include "GuiExecutor.hxx"
25 #include "GuiObserver_i.hxx"
26 #include "QtGuiContext.hxx"
28 #include "RuntimeSALOME.hxx"
32 #include "InputPort.hxx"
33 #include "OutputPort.hxx"
34 #include "Executor.hxx"
38 #include "YacsTrace.hxx"
41 using namespace YACS::HMI;
42 using namespace YACS::ENGINE;
44 GuiExecutor::GuiExecutor(YACS::ENGINE::Proc* proc)
46 DEBTRACE("GuiExecutor::GuiExecutor");
49 _context = QtGuiContext::getQtCurrent();
51 _engineRef = YACS_ORB::YACS_Gen::_nil();
52 _procRef = YACS_ORB::ProcExec::_nil();
53 _observerRef = YACS_ORB::Observer::_nil();
55 _execMode = YACS::CONTINUE;
60 _isStopOnError = false;
64 _breakpointList.clear();
68 GuiExecutor::~GuiExecutor()
70 DEBTRACE("GuiExecutor::~GuiExecutor");
73 void GuiExecutor::closeContext()
75 DEBTRACE("GuiExecutor::closeContext");
79 void GuiExecutor::startResumeDataflow(bool initialize)
81 DEBTRACE("GuiExecutor::runDataflow " << initialize);
84 DEBTRACE("context closed");
87 if (CORBA::is_nil(_engineRef))
89 DEBTRACE("Create YACS ORB engine!");
90 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
91 CORBA::ORB_ptr orb = runTime->getOrb();
92 SALOME_NamingService namingService(orb);
93 SALOME_LifeCycleCORBA lcc(&namingService);
94 ostringstream containerName;
95 containerName << "localhost/YACSContainer" << QtGuiContext::getQtCurrent()->getStudyId();
96 Engines::EngineComponent_var comp = lcc.FindOrLoad_Component(containerName.str().c_str(), "YACS" );
97 _engineRef =YACS_ORB::YACS_Gen::_narrow(comp);
98 YASSERT(!CORBA::is_nil(_engineRef));
101 checkEndOfDataflow(); // --- to allow change of the _isRunning state
107 _procRef->setExecMode(getCurrentExecMode());
108 _procRef->resumeCurrentBreakPoint();
109 _isSuspended = false;
116 if (CORBA::is_nil(_procRef))
118 DEBTRACE("init _procRef");
119 _procRef = _engineRef->LoadProc(_context->getFileName().toAscii());
120 registerStatusObservers();
121 DEBTRACE("_procRef init");
124 YASSERT(!CORBA::is_nil(_procRef));
127 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
129 _procRef->setExecMode(getCurrentExecMode());
132 if (_loadStateFile.empty())
134 DEBTRACE("Run from scratch!");
139 DEBTRACE("Run from STATE!");
142 _procRef->RunFromState(_loadStateFile.c_str());
146 DEBTRACE("Runtime error: execution from the loaded state failed")
152 bool GuiExecutor::checkEndOfDataflow(bool display)
154 DEBTRACE("GuiExecutor::checkEndOfDataFlow");
157 if (CORBA::is_nil(_procRef))
159 DEBTRACE("Runtime error: connection lost on a running scheme");
163 if (_procRef->isNotFinished())
165 DEBTRACE("Remote Execution Already running...");
171 // --- TODO: cleaning on server ...
177 void GuiExecutor::killDataflow()
179 DEBTRACE("GuiExecutor::killDataflow");
181 _procRef->stopExecution();
184 void GuiExecutor::suspendDataflow()
186 DEBTRACE("GuiExecutor::suspendDataflow");
188 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
191 void GuiExecutor::resumeDataflow()
193 DEBTRACE("GuiExecutor::resumeDataflow");
196 _procRef->setExecMode(getCurrentExecMode());
197 _procRef->resumeCurrentBreakPoint();
201 void GuiExecutor::stopDataflow()
203 DEBTRACE("GuiExecutor::stopDataflow");
205 _procRef->stopExecution();
208 void GuiExecutor::resetDataflow()
210 DEBTRACE("GuiExecutor::resetDataflow");
212 checkEndOfDataflow();
214 _procRef->stopExecution();
215 checkEndOfDataflow();
220 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
221 //full reset: set all nodes in error to READY state and start execution
222 _procRef->RestartFromState("");
227 void GuiExecutor::setStepByStepMode()
229 DEBTRACE("GuiExecutor::setStepByStepMode");
230 _execMode = YACS::STEPBYSTEP;
232 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
235 void GuiExecutor::setContinueMode()
237 DEBTRACE("GuiExecutor::setContinueMode");
238 _execMode = YACS::CONTINUE;
240 _procRef->setExecMode(YACS_ORB::CONTINUE);
243 void GuiExecutor::setBreakpointMode()
245 DEBTRACE("GuiExecutor::setBreakpointMode");
246 _execMode = YACS::STOPBEFORENODES;
248 _procRef->setExecMode(YACS_ORB::STOPBEFORENODES);
251 void GuiExecutor::setStopOnError(bool aMode)
253 DEBTRACE("GuiExecutor::setStopOnError " << aMode);
257 _procRef->setStopOnError(aMode, (getenv("TEMP") + string("\\dumpStateOnError_") + getenv("USER") + string(".xml")).c_str());
259 _procRef->setStopOnError(aMode, (string("/tmp/dumpStateOnError_") + getenv("USER") + string(".xml")).c_str());
261 _isStopOnError = true;
265 void GuiExecutor::unsetStopOnError()
267 DEBTRACE("GuiExecutor::unsetStopOnError");
270 _procRef->unsetStopOnError();
271 _isStopOnError = false;
276 void GuiExecutor::saveState(const std::string& xmlFile)
278 DEBTRACE("GuiExecutor::saveState " << xmlFile);
279 bool StartFinish = (getExecutorState() == YACS::NOTYETINITIALIZED ||
280 getExecutorState() == YACS::FINISHED);
282 !(CORBA::is_nil(_procRef)) && StartFinish )
283 _procRef->saveState(xmlFile.c_str());
286 void GuiExecutor::setLoadStateFile(std::string xmlFile)
288 DEBTRACE("GuiExecutor::setLoadStateFile " << xmlFile);
289 _loadStateFile = xmlFile;
293 YACS_ORB::executionMode GuiExecutor::getCurrentExecMode()
295 DEBTRACE("GuiExecutor::getCurrentExecMode");
298 case YACS::CONTINUE: return YACS_ORB::CONTINUE;
299 case YACS::STEPBYSTEP: return YACS_ORB::STEPBYSTEP;
300 case YACS::STOPBEFORENODES: return YACS_ORB::STOPBEFORENODES;
301 default: return YACS_ORB::CONTINUE;
305 int GuiExecutor::getExecutorState()
307 DEBTRACE("GuiExecutor::getExecutorState");
308 if (_isRunning || !CORBA::is_nil(_procRef))
309 return _procRef->getExecutorState();
310 else if (CORBA::is_nil(_procRef))
311 return YACS::NOTYETINITIALIZED;
313 return YACS::FINISHED;
317 void GuiExecutor::setBreakpointList(std::list<std::string> breakpointList)
319 DEBTRACE("GuiExecutor::setBreakpointList");
322 DEBTRACE("context closed");
325 _breakpointList.clear();
326 _breakpointList = breakpointList;
328 if ((_execMode == YACS::CONTINUE) && ! _breakpointList.empty())
330 _context->getGMain()->_breakpointsModeAct->setChecked(true);
335 void GuiExecutor::addBreakpoint(std::string breakpoint)
337 DEBTRACE("addBreakpoint " << breakpoint);
340 DEBTRACE("context closed");
343 _breakpointList.push_back(breakpoint);
345 if ((_execMode == YACS::CONTINUE) && ! _breakpointList.empty())
347 _context->getGMain()->_breakpointsModeAct->setChecked(true);
352 void GuiExecutor::removeBreakpoint(std::string breakpoint)
354 DEBTRACE("removeBreakpoint " << breakpoint);
355 _breakpointList.remove(breakpoint);
359 void GuiExecutor::setNextStepList(std::list<std::string> nextStepList)
361 DEBTRACE("GuiExecutor::setNextStepList");
364 YACS_ORB::stringArray listOfNextStep;
365 listOfNextStep.length(nextStepList.size());
367 for (list<string>::iterator it = nextStepList.begin(); it != nextStepList.end(); ++it)
368 listOfNextStep[i++] = (*it).c_str();
369 _procRef->setStepsToExecute(listOfNextStep);
373 void GuiExecutor::registerStatusObservers()
375 DEBTRACE("GuiExecutor::registerStatusObservers");
376 if (CORBA::is_nil(_procRef))
378 DEBTRACE("Runtime error (yacsgui): Lost connection on YACS executor");
381 if (CORBA::is_nil(_observerRef))
383 _serv = new GuiObserver_i(_proc);
384 _serv->SetImpl(this);
385 _observerRef = _serv->_this();
388 _serv->SetRemoteProc(_procRef);
389 _serv->setConversion();
391 std::list<Node*> aNodeSet = _proc->getAllRecursiveConstituents();
392 for ( std::list<Node*>::iterator it = aNodeSet.begin(); it != aNodeSet.end(); it++ )
394 _procRef->addObserver(_observerRef, _serv->getEngineId((*it)->getNumId()) , "status");
396 _procRef->addObserver(_observerRef, _serv->getEngineId(_proc->getNumId()) , "executor");
401 void GuiExecutor::setEngineRef(YACS_ORB::YACS_Gen_ptr ref)
403 DEBTRACE("GuiExecutor::setEngineRef");
407 std::string GuiExecutor::getErrorDetails(YACS::ENGINE::Node* node)
409 DEBTRACE("GuiExecutor::getErrorDetails");
412 int engineId=_serv->getEngineId(node->getNumId());
413 return _procRef->getErrorDetails(engineId);
418 std::string GuiExecutor::getErrorReport(YACS::ENGINE::Node* node)
420 DEBTRACE("GuiExecutor::getErrorReport");
423 int engineId=_serv->getEngineId(node->getNumId());
424 return _procRef->getErrorReport(engineId);
429 std::string GuiExecutor::getContainerLog()
431 DEBTRACE("GuiExecutor::getContainerLog");
433 if (!CORBA::is_nil(_engineRef))
435 Engines::Container_var cont= _engineRef->GetContainerRef();
436 CORBA::String_var logname = cont->logfilename();
439 std::string::size_type pos = msg.find(":");
440 msg=msg.substr(pos+1);
445 std::string GuiExecutor::getContainerLog(YACS::ENGINE::Node* node)
447 DEBTRACE("GuiExecutor::getContainerLog(YACS::ENGINE::Node* node)");
451 int engineId=_serv->getEngineId(node->getNumId());
452 CORBA::String_var logname = _procRef->getContainerLog(engineId);
454 std::string::size_type pos = msg.find(":");
455 msg=msg.substr(pos+1);
460 void GuiExecutor::shutdownProc()
462 DEBTRACE("GuiExecutor::shutdownProc " << _shutdownLevel << "," << _isRunning);
463 checkEndOfDataflow();
465 _procRef->shutdownProc(_shutdownLevel);
468 void GuiExecutor::setInPortValue(YACS::ENGINE::DataPort* port, std::string value)
470 DEBTRACE("GuiExecutor::setInPortValue");
472 YACS::ENGINE::Node* node = port->getNode();
473 YACS::ENGINE::ComposedNode* rootNode = node->getRootNode();
475 std::string nodeName;
477 nodeName = node->getName();
479 nodeName = rootNode->getChildName(node);
481 std::string msg = _procRef->setInPortValue(nodeName.c_str(), port->getName().c_str(), value.c_str());
484 bool GuiExecutor::event(QEvent *e)
486 DEBTRACE("GuiExecutor::event");
487 YACSEvent *yev = dynamic_cast<YACSEvent*>(e);
488 if (!yev) return false;
489 int numid = yev->getYACSEvent().first;
490 string event = yev->getYACSEvent().second;
491 DEBTRACE("<" << numid << "," << event << ">");
494 DEBTRACE("context closed");
497 if (event == "executor") // --- Executor notification: state
499 int execState = _procRef->getExecutorState();
500 list<string> nextSteps;
501 if ((execState == YACS::WAITINGTASKS) || (execState == YACS::PAUSED))
503 YACS_ORB::stringArray_var nstp = _procRef->getTasksToLoad();
504 for (CORBA::ULong i=0; i<nstp->length(); i++)
505 nextSteps.push_back(nstp[i].in());
506 if (execState == YACS::PAUSED)
509 SubjectProc *sproc = _context->getSubjectProc();
510 sproc->update(YACS::HMI::UPDATEPROGRESS, execState, sproc);
512 else // --- Node notification
514 if (! _serv->_engineToGuiMap.count(numid))
516 int state = _procRef->getNodeState(numid);
517 int iGui = _serv->_engineToGuiMap[numid];
518 YASSERT(_context->_mapOfExecSubjectNode.count(iGui));
519 SubjectNode *snode = _context->_mapOfExecSubjectNode[iGui];
520 DEBTRACE("node " << snode->getName() << " state=" << state);
522 YACS::ENGINE::Node *node = snode->getNode();
523 list<InputPort*> inports = node->getLocalInputPorts();
524 list<InputPort*>::iterator iti = inports.begin();
525 for ( ; iti != inports.end(); ++iti)
527 string val = _procRef->getInPortValue(numid, (*iti)->getName().c_str());
528 DEBTRACE("node " << snode->getName() << " inport " << (*iti)->getName()
529 << " value " << val);
530 YASSERT(_context->_mapOfSubjectDataPort.count(*iti));
531 SubjectDataPort* port = _context->_mapOfSubjectDataPort[*iti];
532 port->setExecValue(val);
533 port->update(YACS::HMI::UPDATEPROGRESS, 0, port);
535 list<OutputPort*> outports = node->getLocalOutputPorts();
536 list<OutputPort*>::iterator ito = outports.begin();
537 for ( ; ito != outports.end(); ++ito)
539 string val = _procRef->getOutPortValue(numid, (*ito)->getName().c_str());
540 DEBTRACE("node " << snode->getName() << " outport " << (*ito)->getName()
541 << " value " << val);
542 YASSERT(_context->_mapOfSubjectDataPort.count(*ito));
543 SubjectDataPort* port = _context->_mapOfSubjectDataPort[*ito];
544 port->setExecValue(val);
545 port->update(YACS::HMI::UPDATEPROGRESS, 0, port);
547 snode->update(YACS::HMI::UPDATEPROGRESS, state, snode);
553 void GuiExecutor::setBPList()
555 DEBTRACE("GuiExecutor::setBPList");
558 YACS_ORB::stringArray listOfBreakPoints;
559 listOfBreakPoints.length(_breakpointList.size());
561 for (list<string>::iterator it = _breakpointList.begin(); it != _breakpointList.end(); ++it)
562 listOfBreakPoints[i++] = (*it).c_str();
563 _procRef->setListOfBreakPoints(listOfBreakPoints);
567 YACS::ExecutorState GuiExecutor::updateSchema(string jobState)
569 YACS::ExecutorState execState = YACS::NOTYETINITIALIZED;
573 std::list<Node*> aNodeSet = _proc->getAllRecursiveConstituents();
574 for ( std::list<Node*>::iterator it = aNodeSet.begin(); it != aNodeSet.end(); it++ ){
576 numid = (*it)->getNumId();
578 state = _proc->getNodeState(numid);
579 SubjectNode *snode = _context->_mapOfExecSubjectNode[numid];
581 YACS::ENGINE::Node *node = snode->getNode();
582 list<InputPort*> inports = node->getLocalInputPorts();
583 list<InputPort*>::iterator iti = inports.begin();
584 for ( ; iti != inports.end(); ++iti)
586 string val = _proc->getInPortValue(numid, (*iti)->getName().c_str());
587 YASSERT(_context->_mapOfSubjectDataPort.count(*iti));
588 SubjectDataPort* port = _context->_mapOfSubjectDataPort[*iti];
589 port->setExecValue(val);
590 port->update(YACS::HMI::UPDATEPROGRESS, 0, port);
592 list<OutputPort*> outports = node->getLocalOutputPorts();
593 list<OutputPort*>::iterator ito = outports.begin();
594 for ( ; ito != outports.end(); ++ito)
596 string val = _proc->getOutPortValue(numid, (*ito)->getName().c_str());
597 YASSERT(_context->_mapOfSubjectDataPort.count(*ito));
598 SubjectDataPort* port = _context->_mapOfSubjectDataPort[*ito];
599 port->setExecValue(val);
600 port->update(YACS::HMI::UPDATEPROGRESS, 0, port);
602 snode->update(YACS::HMI::UPDATEPROGRESS, state, snode);
604 state = _proc->getRootNode()->getEffectiveState();
607 case YACS::ACTIVATED:
608 if(jobState!="RUNNING")
609 execState = YACS::FINISHED;
611 execState = YACS::RUNNING;
615 execState = YACS::FINISHED;
617 case YACS::SUSPENDED:
618 execState = YACS::PAUSED;
621 SubjectProc *sproc = _context->getSubjectProc();
622 sproc->update(YACS::HMI::UPDATEPROGRESS, execState, sproc);