1 // Copyright (C) 2006-2008 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
20 #include "SALOME_LifeCycleCORBA.hxx"
21 #include "SALOME_NamingService.hxx"
23 #include "GuiExecutor.hxx"
24 #include "GuiObserver_i.hxx"
25 #include "QtGuiContext.hxx"
27 #include "RuntimeSALOME.hxx"
31 #include "InputPort.hxx"
32 #include "OutputPort.hxx"
33 #include "Executor.hxx"
37 #include "YacsTrace.hxx"
40 using namespace YACS::HMI;
41 using namespace YACS::ENGINE;
43 GuiExecutor::GuiExecutor(YACS::ENGINE::Proc* proc)
45 DEBTRACE("GuiExecutor::GuiExecutor");
48 _context = QtGuiContext::getQtCurrent();
50 _engineRef = YACS_ORB::YACS_Gen::_nil();
51 _procRef = YACS_ORB::ProcExec::_nil();
52 _observerRef = YACS_ORB::Observer::_nil();
54 _execMode = YACS::CONTINUE;
59 _isStopOnError = false;
62 _breakpointList.clear();
66 GuiExecutor::~GuiExecutor()
68 DEBTRACE("GuiExecutor::~GuiExecutor");
72 void GuiExecutor::startResumeDataflow(bool initialize)
74 DEBTRACE("GuiExecutor::runDataflow " << initialize);
75 if (CORBA::is_nil(_engineRef))
77 DEBTRACE("Create YACS ORB engine!");
78 YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime();
79 CORBA::ORB_ptr orb = runTime->getOrb();
80 SALOME_NamingService namingService(orb);
81 SALOME_LifeCycleCORBA lcc(&namingService);
82 Engines::Component_var comp = lcc.FindOrLoad_Component("YACSContainer", "YACS" );
83 _engineRef =YACS_ORB::YACS_Gen::_narrow(comp);
84 assert(!CORBA::is_nil(_engineRef));
87 checkEndOfDataflow(); // --- to allow change of the _isRunning state
93 _procRef->setExecMode(getCurrentExecMode());
94 _procRef->resumeCurrentBreakPoint();
102 if (CORBA::is_nil(_procRef))
104 DEBTRACE("init _procRef");
105 _procRef = _engineRef->LoadProc(_context->getFileName().toAscii());
106 registerStatusObservers();
107 DEBTRACE("_procRef init");
111 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
113 _procRef->setExecMode(getCurrentExecMode());
116 if (_loadStateFile.empty())
118 DEBTRACE("Run from scratch!");
123 DEBTRACE("Run from STATE!");
126 _procRef->RunFromState(_loadStateFile.c_str());
130 DEBTRACE("Runtime error: execution from the loaded state failed")
136 bool GuiExecutor::checkEndOfDataflow(bool display)
138 DEBTRACE("GuiExecutor::checkEndOfDataFlow");
141 if (CORBA::is_nil(_procRef))
143 DEBTRACE("Runtime error: connection lost on a running scheme");
147 if (_procRef->isNotFinished())
149 DEBTRACE("Remote Execution Already running...");
155 // --- TODO: cleaning on server ...
161 void GuiExecutor::killDataflow()
163 DEBTRACE("GuiExecutor::killDataflow");
165 _procRef->stopExecution();
168 void GuiExecutor::suspendDataflow()
170 DEBTRACE("GuiExecutor::suspendDataflow");
172 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
175 void GuiExecutor::resumeDataflow()
177 DEBTRACE("GuiExecutor::resumeDataflow");
180 _procRef->setExecMode(getCurrentExecMode());
181 _procRef->resumeCurrentBreakPoint();
185 void GuiExecutor::stopDataflow()
187 DEBTRACE("GuiExecutor::stopDataflow");
189 _procRef->stopExecution();
192 void GuiExecutor::resetDataflow()
194 DEBTRACE("GuiExecutor::resetDataflow");
196 _procRef->stopExecution();
200 void GuiExecutor::setStepByStepMode()
202 DEBTRACE("GuiExecutor::setStepByStepMode");
203 _execMode = YACS::STEPBYSTEP;
205 _procRef->setExecMode(YACS_ORB::STEPBYSTEP);
208 void GuiExecutor::setContinueMode()
210 DEBTRACE("GuiExecutor::setContinueMode");
211 _execMode = YACS::CONTINUE;
213 _procRef->setExecMode(YACS_ORB::CONTINUE);
216 void GuiExecutor::setBreakpointMode()
218 DEBTRACE("GuiExecutor::setBreakpointMode");
219 _execMode = YACS::STOPBEFORENODES;
221 _procRef->setExecMode(YACS_ORB::STOPBEFORENODES);
224 void GuiExecutor::setStopOnError(bool aMode)
226 DEBTRACE("GuiExecutor::setStopOnError " << aMode);
229 _procRef->setStopOnError(aMode,
230 (string("/tmp/dumpStateOnError_")
231 + getenv("USER") + string(".xml")).c_str());
232 _isStopOnError = true;
236 void GuiExecutor::unsetStopOnError()
238 DEBTRACE("GuiExecutor::unsetStopOnError");
241 _procRef->unsetStopOnError();
242 _isStopOnError = false;
247 void GuiExecutor::saveState(const std::string& xmlFile)
249 DEBTRACE("GuiExecutor::saveState " << xmlFile);
250 bool StartFinish = (getExecutorState() == YACS::NOTYETINITIALIZED ||
251 getExecutorState() == YACS::FINISHED);
253 !(CORBA::is_nil(_procRef)) && StartFinish )
254 _procRef->saveState(xmlFile.c_str());
257 void GuiExecutor::setLoadStateFile(std::string xmlFile)
259 DEBTRACE("GuiExecutor::setLoadStateFile " << xmlFile);
260 _loadStateFile = xmlFile;
264 YACS_ORB::executionMode GuiExecutor::getCurrentExecMode()
266 DEBTRACE("GuiExecutor::getCurrentExecMode");
269 case YACS::CONTINUE: return YACS_ORB::CONTINUE;
270 case YACS::STEPBYSTEP: return YACS_ORB::STEPBYSTEP;
271 case YACS::STOPBEFORENODES: return YACS_ORB::STOPBEFORENODES;
272 default: return YACS_ORB::CONTINUE;
276 int GuiExecutor::getExecutorState()
278 DEBTRACE("GuiExecutor::getExecutorState");
279 if (_isRunning || !CORBA::is_nil(_procRef))
280 return _procRef->getExecutorState();
281 else if (CORBA::is_nil(_procRef))
282 return YACS::NOTYETINITIALIZED;
284 return YACS::FINISHED;
288 void GuiExecutor::setBreakpointList(std::list<std::string> breakpointList)
290 DEBTRACE("GuiExecutor::setBreakpointList");
291 _breakpointList.clear();
292 _breakpointList = breakpointList;
294 if ((_execMode == YACS::CONTINUE) && ! _breakpointList.empty())
296 QtGuiContext::getQtCurrent()->getGMain()->_breakpointsModeAct->setChecked(true);
301 void GuiExecutor::addBreakpoint(std::string breakpoint)
303 DEBTRACE("addBreakpoint " << breakpoint);
304 _breakpointList.push_back(breakpoint);
306 if ((_execMode == YACS::CONTINUE) && ! _breakpointList.empty())
308 QtGuiContext::getQtCurrent()->getGMain()->_breakpointsModeAct->setChecked(true);
313 void GuiExecutor::removeBreakpoint(std::string breakpoint)
315 DEBTRACE("removeBreakpoint " << breakpoint);
316 _breakpointList.remove(breakpoint);
320 void GuiExecutor::setNextStepList(std::list<std::string> nextStepList)
322 DEBTRACE("GuiExecutor::setNextStepList");
325 YACS_ORB::stringArray listOfNextStep;
326 listOfNextStep.length(nextStepList.size());
328 for (list<string>::iterator it = nextStepList.begin(); it != nextStepList.end(); ++it)
329 listOfNextStep[i++] = (*it).c_str();
330 _procRef->setStepsToExecute(listOfNextStep);
334 void GuiExecutor::registerStatusObservers()
336 DEBTRACE("GuiExecutor::registerStatusObservers");
337 if (CORBA::is_nil(_procRef))
339 DEBTRACE("Runtime error (yacsgui): Lost connection on YACS executor");
342 if (CORBA::is_nil(_observerRef))
344 _serv = new GuiObserver_i(_proc);
345 _serv->SetImpl(this);
346 _observerRef = _serv->_this();
349 _serv->SetRemoteProc(_procRef);
350 _serv->setConversion();
352 std::list<Node*> aNodeSet = _proc->getAllRecursiveConstituents();
353 for ( std::list<Node*>::iterator it = aNodeSet.begin(); it != aNodeSet.end(); it++ )
355 _procRef->addObserver(_observerRef, _serv->getEngineId((*it)->getNumId()) , "status");
357 _procRef->addObserver(_observerRef, _serv->getEngineId(_proc->getNumId()) , "executor");
362 void GuiExecutor::setEngineRef(YACS_ORB::YACS_Gen_ptr ref)
364 DEBTRACE("GuiExecutor::setEngineRef");
368 std::string GuiExecutor::getErrorDetails(YACS::ENGINE::Node* node)
370 DEBTRACE("GuiExecutor::getErrorDetails");
373 int engineId=_serv->getEngineId(node->getNumId());
374 return _procRef->getErrorDetails(engineId);
379 std::string GuiExecutor::getErrorReport(YACS::ENGINE::Node* node)
381 DEBTRACE("GuiExecutor::getErrorReport");
384 int engineId=_serv->getEngineId(node->getNumId());
385 return _procRef->getErrorReport(engineId);
390 std::string GuiExecutor::getContainerLog()
392 DEBTRACE("GuiExecutor::getContainerLog");
394 if (!CORBA::is_nil(_engineRef))
396 Engines::Container_var cont= _engineRef->GetContainerRef();
397 CORBA::String_var logname = cont->logfilename();
400 std::string::size_type pos = msg.find(":");
401 msg=msg.substr(pos+1);
406 std::string GuiExecutor::getContainerLog(YACS::ENGINE::Node* node)
408 DEBTRACE("GuiExecutor::getContainerLog(YACS::ENGINE::Node* node)");
412 int engineId=_serv->getEngineId(node->getNumId());
413 CORBA::String_var logname = _procRef->getContainerLog(engineId);
415 std::string::size_type pos = msg.find(":");
416 msg=msg.substr(pos+1);
421 bool GuiExecutor::event(QEvent *e)
423 DEBTRACE("GuiExecutor::event");
424 YACSEvent *yev = dynamic_cast<YACSEvent*>(e);
425 if (!yev) return false;
426 int numid = yev->getYACSEvent().first;
427 string event = yev->getYACSEvent().second;
428 DEBTRACE("<" << numid << "," << event << ">");
429 if (event == "executor") // --- Executor notification: state
431 int execState = _procRef->getExecutorState();
432 list<string> nextSteps;
433 if ((execState == YACS::WAITINGTASKS) || (execState == YACS::PAUSED))
435 YACS_ORB::stringArray_var nstp = _procRef->getTasksToLoad();
436 for (CORBA::ULong i=0; i<nstp->length(); i++)
437 nextSteps.push_back(nstp[i].in());
438 if (execState == YACS::PAUSED)
441 SubjectProc *sproc = GuiContext::getCurrent()->getSubjectProc();
442 sproc->setExecState(execState);
443 // theRunMode->onNotifyNextSteps(nextSteps);
445 else // --- Node notification
447 if (! _serv->_engineToGuiMap.count(numid))
449 int state = _procRef->getNodeState(numid);
450 int iGui = _serv->_engineToGuiMap[numid];
451 assert(GuiContext::getCurrent()->_mapOfExecSubjectNode.count(iGui));
452 SubjectNode *snode = GuiContext::getCurrent()->_mapOfExecSubjectNode[iGui];
453 DEBTRACE("node " << snode->getName() << " state=" << state);
454 snode->setExecState(state);
456 YACS::ENGINE::Node *node = snode->getNode();
457 list<InputPort*> inports = node->getLocalInputPorts();
458 list<InputPort*>::iterator iti = inports.begin();
459 for ( ; iti != inports.end(); ++iti)
461 string val = _procRef->getInPortValue(numid, (*iti)->getName().c_str());
462 DEBTRACE("node " << snode->getName() << " inport " << (*iti)->getName()
463 << " value " << val);
464 assert(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(*iti));
465 SubjectDataPort* port = GuiContext::getCurrent()->_mapOfSubjectDataPort[*iti];
466 port->setExecValue(val);
467 port->update(YACS::HMI::UPDATEPROGRESS, 0, port);
469 list<OutputPort*> outports = node->getLocalOutputPorts();
470 list<OutputPort*>::iterator ito = outports.begin();
471 for ( ; ito != outports.end(); ++ito)
473 string val = _procRef->getOutPortValue(numid, (*ito)->getName().c_str());
474 DEBTRACE("node " << snode->getName() << " outport " << (*ito)->getName()
475 << " value " << val);
476 assert(GuiContext::getCurrent()->_mapOfSubjectDataPort.count(*ito));
477 SubjectDataPort* port = GuiContext::getCurrent()->_mapOfSubjectDataPort[*ito];
478 port->setExecValue(val);
479 port->update(YACS::HMI::UPDATEPROGRESS, 0, port);
486 void GuiExecutor::setBPList()
488 DEBTRACE("GuiExecutor::setBPList");
491 YACS_ORB::stringArray listOfBreakPoints;
492 listOfBreakPoints.length(_breakpointList.size());
494 for (list<string>::iterator it = _breakpointList.begin(); it != _breakpointList.end(); ++it)
495 listOfBreakPoints[i++] = (*it).c_str();
496 _procRef->setListOfBreakPoints(listOfBreakPoints);