1 // Copyright (C) 2006-2021 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
21 #include "DynParaLoop.hxx"
22 #include "InputPort.hxx"
23 #include "OutputPort.hxx"
24 #include "InPropertyPort.hxx"
25 #include "ComposedNode.hxx"
26 #include "Dispatcher.hxx"
27 #include "InputDataStreamPort.hxx"
28 #include "OutputDataStreamPort.hxx"
32 #include "YacsTrace.hxx"
34 using namespace YACS::ENGINE;
37 /*! \class YACS::ENGINE::Node
38 * \brief Base class for all nodes
43 const char Node::SEP_CHAR_IN_PORT[]=".";
46 std::map<int,Node*> Node::idMap;
48 NodeStateNameMap::NodeStateNameMap()
50 insert(make_pair(YACS::READY, "READY"));
51 insert(make_pair(YACS::TOLOAD, "TOLOAD"));
52 insert(make_pair(YACS::LOADED, "LOADED"));
53 insert(make_pair(YACS::TOACTIVATE, "TOACTIVATE"));
54 insert(make_pair(YACS::ACTIVATED, "ACTIVATED"));
55 insert(make_pair(YACS::DESACTIVATED, "DESACTIVATED"));
56 insert(make_pair(YACS::DONE, "DONE"));
57 insert(make_pair(YACS::SUSPENDED, "SUSPENDED"));
58 insert(make_pair(YACS::LOADFAILED, "LOADFAILED"));
59 insert(make_pair(YACS::EXECFAILED, "EXECFAILED"));
60 insert(make_pair(YACS::PAUSE, "PAUSE"));
61 insert(make_pair(YACS::INTERNALERR, "INTERNALERR"));
62 insert(make_pair(YACS::DISABLED, "DISABLED"));
63 insert(make_pair(YACS::FAILED, "FAILED"));
64 insert(make_pair(YACS::ERROR, "ERROR"));
68 Node::Node(const std::string& name):_name(name),_inGate(this),_outGate(this),_father(0),_state(YACS::READY),
69 _implementation(Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME),_modified(1)
71 // Should be protected by lock ??
72 Node::checkValidityOfNodeName(_name);
76 // Every node has an InPropertyPort
77 _inPropertyPort = new InPropertyPort("__InPropertyPort__Node__YACS_", this, Runtime::_tc_propvec);
80 Node::Node(const Node& other, ComposedNode *father):_inGate(this),_outGate(this),_name(other._name),_father(father),
81 _state(YACS::READY),_implementation(other._implementation),
82 _propertyMap(other._propertyMap),_modified(1)
87 // Every node has an InPropertyPort
88 _inPropertyPort = new InPropertyPort("__InPropertyPort__Node__YACS_", this, Runtime::_tc_propvec);
89 _eventReceiver=const_cast<Node *>(&other);
94 delete _inPropertyPort;
98 * initialisation of all input and output ports and gates, for execution
101 void Node::init(bool start)
105 if(_state == YACS::DISABLED)
107 exDisabledState(); // to refresh propagation of DISABLED state
110 setState(YACS::READY);
114 * This method clones \a this by :
116 * - deep copying nodes, links, ports, types
117 * - containers are either deep copied or shallow copied depending on _isAttachedOnCloning attribute.
118 * - component are either deep copied or shallow copied depending on _isAttachedOnCloning attribute.
120 * So \b this \b method \b clone \b is \b dedicated \b for \b DynParaLoop \b class \b or \b subclasses.
121 * It \b should \b not \b be \b used \b elsewhere, because
122 * _isAttachedOnCloning attribute is an attribute in the engine not for GUI/TUI aspects.
123 * For GUI/TUI manipulation cloneWithoutCompAndContDeepCpy method should be used preferably.
125 * \param [in] father - The new father of the returned clone.
126 * \param [in] editionOnly ignored
128 * \sa cloneWithoutCompAndContDeepCpy
130 Node *Node::clone(ComposedNode *father, bool editionOnly) const
132 Node *ret(simpleClone(father,editionOnly));
133 ret->performDuplicationOfPlacement(*this);
138 * This method clones \a this by :
139 * - deep copying nodes, links, ports, types
140 * - shallow copy containers
141 * - shallow copy components
143 * So this method simply ignores isAttachedOnCloning attribute for both containers and components.
144 * So this method is dedicated for the GUI/TUI users.
146 * \param [in] father - The new father of the returned clone.
147 * \param [in] editionOnly ignored
149 Node *Node::cloneWithoutCompAndContDeepCpy(ComposedNode *father, bool editionOnly) const
151 Node *ret(simpleClone(father,editionOnly));
152 ret->performShallowDuplicationOfPlacement(*this);
156 //! Change the name of the node
158 * raise an exception if the name is already used in the scope of its father
159 * \param name : the new name
161 void Node::setName(const std::string& name)
163 Node::checkValidityOfNodeName(name);
166 if(_father->isNameAlreadyUsed(name))
168 if ( _father->getChildByName(name) != this )
170 std::string what("Name ");
172 what+=" already exists in the scope of "; what+=_father->getName();
173 throw Exception(what);
181 * get the set of all nodes connected to the outGate
184 list<Node *> Node::getOutNodes() const
187 list<InGate *> inGates=_outGate.edSetInGate();
188 for(list<InGate *>::iterator iter=inGates.begin();iter!=inGates.end();iter++)
189 ret.push_back((*iter)->getNode());
193 bool Node::exIsControlReady() const
195 return _inGate.exIsReady();
198 //! Update the node state
200 * \note : Update the '_state' attribute.
201 * Typically called by 'this->_inGate' when 'this->_inGate' is ready.
203 * Called by InGate::exNotifyFromPrecursor
205 void Node::exUpdateState()
207 if(_state==YACS::DISABLED)return;
208 if(_inGate.exIsReady())
209 setState(YACS::TOACTIVATE);
212 //! Notify this node that its execution has failed
214 * The node goes in FAILED state and
215 * propagate the notification through the outGate port
218 void Node::exFailedState()
220 DEBTRACE( "Node::exFailedState: " << getName() );
221 setState(YACS::FAILED);
222 _outGate.exNotifyFailed();
225 //! Notify this node that it has been disabled
227 * The node goes in DISABLED state and
228 * propagate the notification through the outGate port
231 void Node::exDisabledState()
233 DEBTRACE( "Node::exDisabledState: " << getName() );
234 setState(YACS::DISABLED);
235 _outGate.exNotifyDisabled();
238 InPort *Node::getInPort(const std::string& name) const
243 ret=getInputPort(name);
247 ret=getInputDataStreamPort(name);
253 Node::getInPropertyPort() const
255 return _inPropertyPort;
259 Node::getInputPort(const std::string& name) const
261 if (name == "__InPropertyPort__Node__YACS_")
262 return _inPropertyPort;
265 std::string what("Node::getInputPort : the port with name "); what+=name; what+=" does not exist on the current level";
266 throw Exception(what);
271 * \note: Contrary to getOutputPort method, this method returns the output port at highest level, possible.
272 * That is to say in some ComposedNode, like ForEachLoop or Switch, an outport inside 'this' is seen differently than the true outport.
274 OutPort *Node::getOutPort(const std::string& name) const
279 ret=getOutputPort(name);
283 ret=getOutputDataStreamPort(name);
288 std::list<InPort *> Node::getSetOfInPort() const
291 list<InputPort *> data=getSetOfInputPort();
292 ret.insert(ret.end(),data.begin(),data.end());
293 list<InputDataStreamPort *> ds=getSetOfInputDataStreamPort();
294 ret.insert(ret.end(),ds.begin(),ds.end());
298 std::list<OutPort *> Node::getSetOfOutPort() const
301 list<OutputPort *> data=getSetOfOutputPort();
302 ret.insert(ret.end(),data.begin(),data.end());
303 list<OutputDataStreamPort *> ds=getSetOfOutputDataStreamPort();
304 ret.insert(ret.end(),ds.begin(),ds.end());
309 * gets a set of the composed nodes that constitute the ascendancy of this node, starting from root
310 * or from a particular ancestor
311 * \b WARNING : returned set is not sorted !
312 * @param levelToStop composed node which is the oldest ancestor required
313 * @return ascendancy, direct father first in set.
316 std::list<ComposedNode *> Node::getAllAscendanceOf(ComposedNode *levelToStop) const
318 list<ComposedNode *> ret;
319 if(this==levelToStop)
321 for(ComposedNode *iter=_father;iter!=levelToStop && iter!=0; iter=iter->_father)
326 bool Node::operator>(const Node& other) const
328 const ComposedNode *iter=other._father;
329 while(iter!=0 && iter!=this)
334 bool Node::operator<(const Node& other) const
336 const ComposedNode *iter=_father;
337 while(iter!=0 && iter!=(&other))
339 return iter==(&other);
343 * @return Implementation of node: C++, Python, CORBA...
344 * _implementation is set by a derived class in a Runtime
345 * it normally applies only to elementaryNodes and it is used by Ports to select Data Converters.
346 * Potential problem with Ports attached to composed Nodes...
349 string Node::getImplementation() const
351 return _implementation;
354 //! Becomes deprecated soon. Replaced by ComposedNode::CheckConsistency.
355 set<InputPort *> Node::edGetSetOfUnitializedInputPort() const
357 set<InputPort *> setOfUnitializedInputPort;
358 list<InputPort *> allOfInputPorts=getSetOfInputPort();
359 for(list<InputPort *>::const_iterator iter=allOfInputPorts.begin();iter!=allOfInputPorts.end();iter++)
361 if ( ! (*iter)->edIsInitialized() )
362 setOfUnitializedInputPort.insert(*iter);
364 return setOfUnitializedInputPort;
367 //! Becomes deprecated soon. Replaced by ComposedNode::CheckConsistency.
368 bool Node::edAreAllInputPortInitialized() const
370 set<InputPort *> setOfUnitializedInputPort = edGetSetOfUnitializedInputPort();
371 return ( setOfUnitializedInputPort.size() == 0);
375 * Called typically by Bloc to notify failure on potentially next nodes on the same scope of 'this'
377 void Node::exForwardFailed()
379 _outGate.exNotifyFailed();
383 * Called typically by Bloc to activate potentially next nodes on the same scope of 'this'
385 void Node::exForwardFinished()
387 DEBTRACE("Node::exForwardFinished");
388 _outGate.exNotifyDone();
392 * Called typically by ComposedNode to correctly update DF/CF/DS links
394 void Node::edDisconnectAllLinksWithMe()
396 _inGate.edDisconnectAllLinksToMe();
397 _outGate.edDisconnectAllLinksFromMe();
400 Proc *Node::getProc()
404 return _father->getProc();
407 const Proc * Node::getProc() const
411 return _father->getProc();
414 DynParaLoop *Node::getClosestDPLAmongAncestors() const
418 ComposedNode *iter(_father);
421 DynParaLoop *iter2(dynamic_cast<DynParaLoop *>(iter));
430 ComposedNode *Node::getRootNode() const
433 throw Exception("No root node");
434 ComposedNode *iter=_father;
437 return (ComposedNode *)iter;
441 * checks validity of ports name, that must not contain a particular character '?'
442 * USAGE NOT CLEAR, not used so far, when are those characters set ?
445 void Node::checkValidityOfPortName(const std::string& name)
447 if(name.find(SEP_CHAR_IN_PORT, 0 )!=string::npos)
449 string what("Port name "); what+=name; what+="not valid because it contains character "; what+=SEP_CHAR_IN_PORT;
450 throw Exception(what);
454 void Node::checkValidityOfNodeName(const std::string& name)
456 if(name.find(ComposedNode::SEP_CHAR_BTW_LEVEL,0)!=string::npos)
458 string what("Node name "); what+=name; what+="not valid because it contains character "; what+=ComposedNode::SEP_CHAR_BTW_LEVEL;
459 throw Exception(what);
464 * @note : Check that 'node1' and 'node2' have exactly the same father
465 * @exception : If 'node1' and 'node2' have NOT exactly the same father
467 ComposedNode *Node::checkHavingCommonFather(Node *node1, Node *node2)
469 if(node1!=0 && node2!=0)
471 if(node1->_father==node2->_father)
472 return node1->_father;
474 throw Exception("check failed : nodes have not the same father");
477 const std::string Node::getId() const
479 std::string id=getRootNode()->getName();
480 if(getRootNode() != this)
481 id= id+'.'+ getRootNode()->getChildName(this);
482 string::size_type debut =id.find_first_of('.');
483 while(debut != std::string::npos){
485 debut=id.find_first_of('.',debut);
490 void Node::setProperty(const std::string& name, const std::string& value)
492 DEBTRACE("Node::setProperty " << name << " " << value);
493 _propertyMap[name]=value;
496 std::string Node::getProperty(const std::string& name)
498 std::map<std::string,std::string>::iterator it=_propertyMap.find(name);
500 if(it != _propertyMap.end())
503 return _father->getProperty(name);
508 std::map<std::string,std::string> Node::getProperties()
510 std::map<std::string,std::string> amap=_propertyMap;
513 std::map<std::string,std::string> fatherMap=_father->getProperties();
514 amap.insert(fatherMap.begin(),fatherMap.end());
520 void Node::setProperties(std::map<std::string,std::string> properties)
522 _propertyMap.clear();
523 _propertyMap=properties;
526 //! Return the node state in the context of its father
528 * \return the effective node state
530 * The node state is stored in a private attribute _state.
531 * This state is relative to its father state : a node with a
532 * TOACTIVATE state with a father node in a READY state is not
533 * to activate. Its effective state is only READY.
534 * This method returns the effective state of the node taking
535 * into account that of its father.
537 YACS::StatesForNode Node::getEffectiveState() const
539 if(!_father) //the root node
541 if(_state==YACS::DISABLED)
542 return YACS::DISABLED;
543 return _father->getEffectiveState(this);
546 //! Return the effective state of a node in the context of this one (its father)
548 * \param node: the node which effective state is queried
549 * \return the effective node state
551 YACS::StatesForNode Node::getEffectiveState(const Node* node) const
553 if(node->getState()==YACS::DISABLED)
554 return YACS::DISABLED;
556 YACS::StatesForNode effectiveState=getEffectiveState();
557 switch(effectiveState)
561 case YACS::TOACTIVATE:
564 return YACS::DISABLED;
568 return node->getState();
572 //! Return the color associated to a state
574 * \param state : the node state
575 * \return the associated color
577 std::string Node::getColorState(YACS::StatesForNode state) const
587 case YACS::TOACTIVATE:
589 case YACS::ACTIVATED:
606 //! Dump to the input stream a dot representation of the node
608 * \param os : the input stream
610 void Node::writeDot(std::ostream &os) const
612 os << getId() << "[fillcolor=\"" ;
613 YACS::StatesForNode state=getEffectiveState();
614 os << getColorState(state);
615 os << "\" label=\"" << getImplementation() << "Node:" ;
616 os << getQualifiedName() <<"\"];\n";
619 //! same as Node::getName() in most cases, but differs for children of switch
621 * used by writeDot to distinguish children of switch, by adding a prefix to the name.
622 * prefix is built on case id.
625 std::string Node::getQualifiedName() const
628 return _father->getMyQualifiedName(this);
632 //! return node instance identifiant, unique for each node instance
634 * node instance identifiant is used to check if to nodes pointers refers to the same instance
641 //! Sets the given state for node.
642 /*! It is strongly recommended to use this function if you want to
643 * change the state of the node, instead of direct access to _state field (_state = ...).
645 void Node::setState(YACS::StatesForNode theState)
647 DEBTRACE("Node::setState: " << getName() << " " << theState);
649 // emit notification to all observers registered with the dispatcher on any change of the node's state
653 std::vector<std::pair<std::string,int> > Node::getDPLScopeInfo(ComposedNode *gfn)
655 std::vector< std::pair<std::string,int> > ret;
657 ComposedNode *work(getFather());
658 while(work!=gfn && work!=0)
660 DynParaLoop *workc(dynamic_cast<DynParaLoop *>(work));
663 std::pair<std::string,int> p(gfn->getChildName(workc),workc->getBranchIDOfNode(work2));
667 work=work->getFather();
673 * Method called by the Executor only if the executor is sensitive of scope of DynParaLoop.
674 * This method is virtual and empty because by default nothing is done.
676 void Node::applyDPLScope(ComposedNode *gfn)
680 //! emit notification to all observers registered with the dispatcher
682 * The dispatcher is unique and can be obtained by getDispatcher()
684 void Node::sendEvent(const std::string& event)
686 DEBTRACE("Node::sendEvent " << event);
687 Dispatcher *disp(Dispatcher::getDispatcher());
690 disp->dispatch(this,event);
694 disp->dispatchFromClone(_eventReceiver,event,this);
698 //! emit notification to all observers registered with the dispatcher
700 * The dispatcher is unique and can be obtained by getDispatcher()
702 void Node::sendEvent2(const std::string& event, void *something)
704 Dispatcher* disp=Dispatcher::getDispatcher();
705 disp->dispatch2(this,event,something);
709 * For use only when loading a previously saved execution
712 void YACS::ENGINE::StateLoader(Node* node, YACS::StatesForNode state)
714 node->setState(state);
717 //! indicates if the node is valid (returns 1) or not (returns 0)
719 * This method is useful when editing a schema. It has no meaning in execution.
720 * When a node is edited, its modified method must be called so when isValid is called, its state
721 * is updated (call to edUpdateState) before returning the validity check
727 if(_state > YACS::INVALID)
733 //! update the status of the node
735 * Only useful when editing a schema
736 * Do nothing in base Node : to implement in derived classes
738 void Node::edUpdateState()
740 DEBTRACE("Node::edUpdateState(): " << _modified);
744 //! returns a string that contains an error report if the node is in error
748 std::string Node::getErrorReport()
750 if(getState()==YACS::DISABLED)
751 return "<error node= "+getName()+ "state= DISABLED/>\n";
753 YACS::StatesForNode effectiveState=getEffectiveState();
755 DEBTRACE("Node::getErrorReport: " << getName() << " " << effectiveState << " " << _errorDetails);
756 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR &&
757 effectiveState != YACS::FAILED && effectiveState != YACS::INTERNALERR)
760 std::string report="<error node= " ;
761 report=report + getName() ;
762 switch(effectiveState)
765 report=report+" state= INVALID";
768 report=report+" state= ERROR";
771 report=report+" state= FAILED";
773 case YACS::INTERNALERR:
774 report=report+" state= INTERNALERR";
779 report=report + ">\n" ;
780 report=report+_errorDetails;
781 report=report+"\n</error>";
785 //! returns a string that contains the name of the container log file if it exists
787 * Do nothing here. To subclass
789 std::string Node::getContainerLog()
794 //! Sets Node in modified state and its father if it exists
798 void Node::modified()
800 DEBTRACE("Node::modified() " << getName());
806 //! Put this node into TOLOAD state when possible
810 void Node::ensureLoading()
812 if(_state == YACS::READY)
813 setState(YACS::TOLOAD);
816 //! Return the name of a state
820 std::string Node::getStateName(YACS::StatesForNode state)
822 static NodeStateNameMap nodeStateNameMap;
823 return nodeStateNameMap[state];
826 //! Stop all pending activities of the node
828 * This method should be called when a Proc is finished and must be deleted from the YACS server
830 void Node::shutdown(int level)
835 //! Clean the node in case of not clean exit
837 * This method should be called on a control-C or sigterm
839 void Node::cleanNodes()
843 //! Reset the node state depending on the parameter level
844 void Node::resetState(int level)
846 DEBTRACE("Node::resetState " << getName() << "," << level << "," << _state);
847 if(_state==YACS::ERROR || _state==YACS::FAILED || _state==YACS::ACTIVATED)
849 setState(YACS::READY);
850 InGate* inGate = getInGate();
851 std::list<OutGate*> backlinks = inGate->getBackLinks();
852 for (std::list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
854 Node* fromNode = (*io)->getNode();
855 if(fromNode->getState() == YACS::DONE)
857 inGate->setPrecursorDone(*io);