1 // Copyright (C) 2006-2016 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 ??
75 // Every node has an InPropertyPort
76 _inPropertyPort = new InPropertyPort("__InPropertyPort__Node__YACS_", this, Runtime::_tc_propvec);
79 Node::Node(const Node& other, ComposedNode *father):_inGate(this),_outGate(this),_name(other._name),_father(father),
80 _state(YACS::READY),_implementation(other._implementation),
81 _propertyMap(other._propertyMap),_modified(1)
86 // Every node has an InPropertyPort
87 _inPropertyPort = new InPropertyPort("__InPropertyPort__Node__YACS_", this, Runtime::_tc_propvec);
92 delete _inPropertyPort;
96 * initialisation of all input and output ports and gates, for execution
99 void Node::init(bool start)
103 if(_state == YACS::DISABLED)
105 exDisabledState(); // to refresh propagation of DISABLED state
108 setState(YACS::READY);
112 * This method clones \a this by :
114 * - deep copying nodes, links, ports, types
115 * - containers are either deep copied or shallow copied depending on _isAttachedOnCloning attribute.
116 * - component are either deep copied or shallow copied depending on _isAttachedOnCloning attribute.
118 * So \b this \b method \b clone \b is \b dedicated \b for \b DynParaLoop \b class \b or \b subclasses.
119 * It \b should \b not \b be \b used \b elsewhere, because
120 * _isAttachedOnCloning attribute is an attribute in the engine not for GUI/TUI aspects.
121 * For GUI/TUI manipulation cloneWithoutCompAndContDeepCpy method should be used preferably.
123 * \param [in] father - The new father of the returned clone.
124 * \param [in] editionOnly ignored
126 * \sa cloneWithoutCompAndContDeepCpy
128 Node *Node::clone(ComposedNode *father, bool editionOnly) const
130 Node *ret(simpleClone(father,editionOnly));
131 ret->performDuplicationOfPlacement(*this);
136 * This method clones \a this by :
137 * - deep copying nodes, links, ports, types
138 * - shallow copy containers
139 * - shallow copy components
141 * So this method simply ignores isAttachedOnCloning attribute for both containers and components.
142 * So this method is dedicated for the GUI/TUI users.
144 * \param [in] father - The new father of the returned clone.
145 * \param [in] editionOnly ignored
147 Node *Node::cloneWithoutCompAndContDeepCpy(ComposedNode *father, bool editionOnly) const
149 Node *ret(simpleClone(father,editionOnly));
150 ret->performShallowDuplicationOfPlacement(*this);
154 //! Change the name of the node
156 * raise an exception if the name is already used in the scope of its father
157 * \param name : the new name
159 void Node::setName(const std::string& name)
163 if(_father->isNameAlreadyUsed(name))
165 if ( _father->getChildByName(name) != this )
167 std::string what("Name ");
169 what+=" already exists in the scope of "; what+=_father->getName();
170 throw Exception(what);
178 * get the set of all nodes connected to the outGate
181 list<Node *> Node::getOutNodes() const
184 list<InGate *> inGates=_outGate.edSetInGate();
185 for(list<InGate *>::iterator iter=inGates.begin();iter!=inGates.end();iter++)
186 ret.push_back((*iter)->getNode());
190 bool Node::exIsControlReady() const
192 return _inGate.exIsReady();
195 //! Update the node state
197 * \note : Update the '_state' attribute.
198 * Typically called by 'this->_inGate' when 'this->_inGate' is ready.
200 * Called by InGate::exNotifyFromPrecursor
202 void Node::exUpdateState()
204 if(_state==YACS::DISABLED)return;
205 if(_inGate.exIsReady())
206 setState(YACS::TOACTIVATE);
209 //! Notify this node that its execution has failed
211 * The node goes in FAILED state and
212 * propagate the notification through the outGate port
215 void Node::exFailedState()
217 DEBTRACE( "Node::exFailedState: " << getName() );
218 setState(YACS::FAILED);
219 _outGate.exNotifyFailed();
222 //! Notify this node that it has been disabled
224 * The node goes in DISABLED state and
225 * propagate the notification through the outGate port
228 void Node::exDisabledState()
230 DEBTRACE( "Node::exDisabledState: " << getName() );
231 setState(YACS::DISABLED);
232 _outGate.exNotifyDisabled();
235 InPort *Node::getInPort(const std::string& name) const throw(YACS::Exception)
240 ret=getInputPort(name);
244 ret=getInputDataStreamPort(name);
250 Node::getInPropertyPort() const throw(YACS::Exception)
252 return _inPropertyPort;
256 Node::getInputPort(const std::string& name) const throw(YACS::Exception)
258 if (name == "__InPropertyPort__Node__YACS_")
259 return _inPropertyPort;
262 std::string what("Node::getInputPort : the port with name "); what+=name; what+=" does not exist on the current level";
263 throw Exception(what);
268 * \note: Contrary to getOutputPort method, this method returns the output port at highest level, possible.
269 * That is to say in some ComposedNode, like ForEachLoop or Switch, an outport inside 'this' is seen differently than the true outport.
271 OutPort *Node::getOutPort(const std::string& name) const throw(YACS::Exception)
276 ret=getOutputPort(name);
280 ret=getOutputDataStreamPort(name);
285 std::list<InPort *> Node::getSetOfInPort() const
288 list<InputPort *> data=getSetOfInputPort();
289 ret.insert(ret.end(),data.begin(),data.end());
290 list<InputDataStreamPort *> ds=getSetOfInputDataStreamPort();
291 ret.insert(ret.end(),ds.begin(),ds.end());
295 std::list<OutPort *> Node::getSetOfOutPort() const
298 list<OutputPort *> data=getSetOfOutputPort();
299 ret.insert(ret.end(),data.begin(),data.end());
300 list<OutputDataStreamPort *> ds=getSetOfOutputDataStreamPort();
301 ret.insert(ret.end(),ds.begin(),ds.end());
306 * gets a set of the composed nodes that constitute the ascendancy of this node, starting from root
307 * or from a particular ancestor
308 * \b WARNING : returned set is not sorted !
309 * @param levelToStop composed node which is the oldest ancestor required
310 * @return ascendancy, direct father first in set.
313 std::list<ComposedNode *> Node::getAllAscendanceOf(ComposedNode *levelToStop) const
315 list<ComposedNode *> ret;
316 if(this==levelToStop)
318 for(ComposedNode *iter=_father;iter!=levelToStop && iter!=0; iter=iter->_father)
323 bool Node::operator>(const Node& other) const
325 const ComposedNode *iter=other._father;
326 while(iter!=0 && iter!=this)
331 bool Node::operator<(const Node& other) const
333 const ComposedNode *iter=_father;
334 while(iter!=0 && iter!=(&other))
336 return iter==(&other);
340 * @return Implementation of node: C++, Python, CORBA...
341 * _implementation is set by a derived class in a Runtime
342 * it normally applies only to elementaryNodes and it is used by Ports to select Data Converters.
343 * Potential problem with Ports attached to composed Nodes...
346 string Node::getImplementation() const
348 return _implementation;
351 //! Becomes deprecated soon. Replaced by ComposedNode::CheckConsistency.
352 set<InputPort *> Node::edGetSetOfUnitializedInputPort() const
354 set<InputPort *> setOfUnitializedInputPort;
355 list<InputPort *> allOfInputPorts=getSetOfInputPort();
356 for(list<InputPort *>::const_iterator iter=allOfInputPorts.begin();iter!=allOfInputPorts.end();iter++)
358 if ( ! (*iter)->edIsInitialized() )
359 setOfUnitializedInputPort.insert(*iter);
361 return setOfUnitializedInputPort;
364 //! Becomes deprecated soon. Replaced by ComposedNode::CheckConsistency.
365 bool Node::edAreAllInputPortInitialized() const
367 set<InputPort *> setOfUnitializedInputPort = edGetSetOfUnitializedInputPort();
368 return ( setOfUnitializedInputPort.size() == 0);
372 * Called typically by Bloc to notify failure on potentially next nodes on the same scope of 'this'
374 void Node::exForwardFailed()
376 _outGate.exNotifyFailed();
380 * Called typically by Bloc to activate potentially next nodes on the same scope of 'this'
382 void Node::exForwardFinished()
384 DEBTRACE("Node::exForwardFinished");
385 _outGate.exNotifyDone();
389 * Called typically by ComposedNode to correctly update DF/CF/DS links
391 void Node::edDisconnectAllLinksWithMe()
393 _inGate.edDisconnectAllLinksToMe();
394 _outGate.edDisconnectAllLinksFromMe();
397 Proc *Node::getProc()
401 return _father->getProc();
404 const Proc * Node::getProc() const
408 return _father->getProc();
411 DynParaLoop *Node::getClosestDPLAmongAncestors() const
415 ComposedNode *iter(_father);
418 DynParaLoop *iter2(dynamic_cast<DynParaLoop *>(iter));
427 ComposedNode *Node::getRootNode() const throw(YACS::Exception)
430 throw Exception("No root node");
431 ComposedNode *iter=_father;
434 return (ComposedNode *)iter;
438 * checks validity of ports name, that must not contain a particular character '?'
439 * USAGE NOT CLEAR, not used so far, when are those characters set ?
442 void Node::checkValidityOfPortName(const std::string& name) throw(YACS::Exception)
444 if(name.find(SEP_CHAR_IN_PORT, 0 )!=string::npos)
446 string what("Port name "); what+=name; what+="not valid because it contains character "; what+=SEP_CHAR_IN_PORT;
447 throw Exception(what);
452 * @note : Check that 'node1' and 'node2' have exactly the same father
453 * @exception : If 'node1' and 'node2' have NOT exactly the same father
455 ComposedNode *Node::checkHavingCommonFather(Node *node1, Node *node2) throw(YACS::Exception)
457 if(node1!=0 && node2!=0)
459 if(node1->_father==node2->_father)
460 return node1->_father;
462 throw Exception("check failed : nodes have not the same father");
465 const std::string Node::getId() const
467 std::string id=getRootNode()->getName();
468 if(getRootNode() != this)
469 id= id+'.'+ getRootNode()->getChildName(this);
470 string::size_type debut =id.find_first_of('.');
471 while(debut != std::string::npos){
473 debut=id.find_first_of('.',debut);
478 void Node::setProperty(const std::string& name, const std::string& value)
480 DEBTRACE("Node::setProperty " << name << " " << value);
481 _propertyMap[name]=value;
484 std::string Node::getProperty(const std::string& name)
486 std::map<std::string,std::string>::iterator it=_propertyMap.find(name);
488 if(it != _propertyMap.end())
491 return _father->getProperty(name);
496 std::map<std::string,std::string> Node::getProperties()
498 std::map<std::string,std::string> amap=_propertyMap;
501 std::map<std::string,std::string> fatherMap=_father->getProperties();
502 amap.insert(fatherMap.begin(),fatherMap.end());
508 void Node::setProperties(std::map<std::string,std::string> properties)
510 _propertyMap.clear();
511 _propertyMap=properties;
514 //! Return the node state in the context of its father
516 * \return the effective node state
518 * The node state is stored in a private attribute _state.
519 * This state is relative to its father state : a node with a
520 * TOACTIVATE state with a father node in a READY state is not
521 * to activate. Its effective state is only READY.
522 * This method returns the effective state of the node taking
523 * into account that of its father.
525 YACS::StatesForNode Node::getEffectiveState() const
527 if(!_father) //the root node
529 if(_state==YACS::DISABLED)
530 return YACS::DISABLED;
531 return _father->getEffectiveState(this);
534 //! Return the effective state of a node in the context of this one (its father)
536 * \param node: the node which effective state is queried
537 * \return the effective node state
539 YACS::StatesForNode Node::getEffectiveState(const Node* node) const
541 if(node->getState()==YACS::DISABLED)
542 return YACS::DISABLED;
544 YACS::StatesForNode effectiveState=getEffectiveState();
545 switch(effectiveState)
549 case YACS::TOACTIVATE:
552 return YACS::DISABLED;
556 return node->getState();
560 //! Return the color associated to a state
562 * \param state : the node state
563 * \return the associated color
565 std::string Node::getColorState(YACS::StatesForNode state) const
575 case YACS::TOACTIVATE:
577 case YACS::ACTIVATED:
594 //! Dump to the input stream a dot representation of the node
596 * \param os : the input stream
598 void Node::writeDot(std::ostream &os) const
600 os << getId() << "[fillcolor=\"" ;
601 YACS::StatesForNode state=getEffectiveState();
602 os << getColorState(state);
603 os << "\" label=\"" << getImplementation() << "Node:" ;
604 os << getQualifiedName() <<"\"];\n";
607 //! same as Node::getName() in most cases, but differs for children of switch
609 * used by writeDot to distinguish children of switch, by adding a prefix to the name.
610 * prefix is built on case id.
613 std::string Node::getQualifiedName() const
616 return _father->getMyQualifiedName(this);
620 //! return node instance identifiant, unique for each node instance
622 * node instance identifiant is used to check if to nodes pointers refers to the same instance
629 //! Sets the given state for node.
630 /*! It is strongly recommended to use this function if you want to
631 * change the state of the node, instead of direct access to _state field (_state = ...).
633 void Node::setState(YACS::StatesForNode theState)
635 DEBTRACE("Node::setState: " << getName() << " " << theState);
637 // emit notification to all observers registered with the dispatcher on any change of the node's state
641 std::vector<std::pair<std::string,int> > Node::getDPLScopeInfo(ComposedNode *gfn)
643 std::vector< std::pair<std::string,int> > ret;
645 ComposedNode *work(getFather());
646 while(work!=gfn && work!=0)
648 DynParaLoop *workc(dynamic_cast<DynParaLoop *>(work));
651 std::pair<std::string,int> p(gfn->getChildName(workc),workc->getBranchIDOfNode(work2));
655 work=work->getFather();
661 * Method called by the Executor only if the executor is sensitive of scope of DynParaLoop.
662 * This method is virtual and empty because by default nothing is done.
664 void Node::applyDPLScope(ComposedNode *gfn)
668 //! emit notification to all observers registered with the dispatcher
670 * The dispatcher is unique and can be obtained by getDispatcher()
672 void Node::sendEvent(const std::string& event)
674 DEBTRACE("Node::sendEvent " << event);
675 Dispatcher* disp=Dispatcher::getDispatcher();
676 disp->dispatch(this,event);
679 //! emit notification to all observers registered with the dispatcher
681 * The dispatcher is unique and can be obtained by getDispatcher()
683 void Node::sendEvent2(const std::string& event, void *something)
685 Dispatcher* disp=Dispatcher::getDispatcher();
686 disp->dispatch2(this,event,something);
690 * For use only when loading a previously saved execution
693 void YACS::ENGINE::StateLoader(Node* node, YACS::StatesForNode state)
695 node->setState(state);
698 //! indicates if the node is valid (returns 1) or not (returns 0)
700 * This method is useful when editing a schema. It has no meaning in execution.
701 * When a node is edited, its modified method must be called so when isValid is called, its state
702 * is updated (call to edUpdateState) before returning the validity check
708 if(_state > YACS::INVALID)
714 //! update the status of the node
716 * Only useful when editing a schema
717 * Do nothing in base Node : to implement in derived classes
719 void Node::edUpdateState()
721 DEBTRACE("Node::edUpdateState(): " << _modified);
725 //! returns a string that contains an error report if the node is in error
729 std::string Node::getErrorReport()
731 if(getState()==YACS::DISABLED)
732 return "<error node= "+getName()+ "state= DISABLED/>\n";
734 YACS::StatesForNode effectiveState=getEffectiveState();
736 DEBTRACE("Node::getErrorReport: " << getName() << " " << effectiveState << " " << _errorDetails);
737 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR &&
738 effectiveState != YACS::FAILED && effectiveState != YACS::INTERNALERR)
741 std::string report="<error node= " ;
742 report=report + getName() ;
743 switch(effectiveState)
746 report=report+" state= INVALID";
749 report=report+" state= ERROR";
752 report=report+" state= FAILED";
754 case YACS::INTERNALERR:
755 report=report+" state= INTERNALERR";
760 report=report + ">\n" ;
761 report=report+_errorDetails;
762 report=report+"\n</error>";
766 //! returns a string that contains the name of the container log file if it exists
768 * Do nothing here. To subclass
770 std::string Node::getContainerLog()
775 //! Sets Node in modified state and its father if it exists
779 void Node::modified()
781 DEBTRACE("Node::modified() " << getName());
787 //! Put this node into TOLOAD state when possible
791 void Node::ensureLoading()
793 if(_state == YACS::READY)
794 setState(YACS::TOLOAD);
797 //! Return the name of a state
801 std::string Node::getStateName(YACS::StatesForNode state)
803 static NodeStateNameMap nodeStateNameMap;
804 return nodeStateNameMap[state];
807 //! Stop all pending activities of the node
809 * This method should be called when a Proc is finished and must be deleted from the YACS server
811 void Node::shutdown(int level)
816 //! Clean the node in case of not clean exit
818 * This method should be called on a control-C or sigterm
820 void Node::cleanNodes()
824 //! Reset the node state depending on the parameter level
825 void Node::resetState(int level)
827 DEBTRACE("Node::resetState " << getName() << "," << level << "," << _state);
828 if(_state==YACS::ERROR || _state==YACS::FAILED || _state==YACS::ACTIVATED)
830 setState(YACS::READY);
831 InGate* inGate = getInGate();
832 std::list<OutGate*> backlinks = inGate->getBackLinks();
833 for (std::list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
835 Node* fromNode = (*io)->getNode();
836 if(fromNode->getState() == YACS::DONE)
838 inGate->setPrecursorDone(*io);