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 ComposedNode *Node::getRootNode() const throw(YACS::Exception)
414 throw Exception("No root node");
415 ComposedNode *iter=_father;
418 return (ComposedNode *)iter;
422 * checks validity of ports name, that must not contain a particular character '?'
423 * USAGE NOT CLEAR, not used so far, when are those characters set ?
426 void Node::checkValidityOfPortName(const std::string& name) throw(YACS::Exception)
428 if(name.find(SEP_CHAR_IN_PORT, 0 )!=string::npos)
430 string what("Port name "); what+=name; what+="not valid because it contains character "; what+=SEP_CHAR_IN_PORT;
431 throw Exception(what);
436 * @note : Check that 'node1' and 'node2' have exactly the same father
437 * @exception : If 'node1' and 'node2' have NOT exactly the same father
439 ComposedNode *Node::checkHavingCommonFather(Node *node1, Node *node2) throw(YACS::Exception)
441 if(node1!=0 && node2!=0)
443 if(node1->_father==node2->_father)
444 return node1->_father;
446 throw Exception("check failed : nodes have not the same father");
449 const std::string Node::getId() const
451 std::string id=getRootNode()->getName();
452 if(getRootNode() != this)
453 id= id+'.'+ getRootNode()->getChildName(this);
454 string::size_type debut =id.find_first_of('.');
455 while(debut != std::string::npos){
457 debut=id.find_first_of('.',debut);
462 void Node::setProperty(const std::string& name, const std::string& value)
464 DEBTRACE("Node::setProperty " << name << " " << value);
465 _propertyMap[name]=value;
468 std::string Node::getProperty(const std::string& name)
470 std::map<std::string,std::string>::iterator it=_propertyMap.find(name);
472 if(it != _propertyMap.end())
475 return _father->getProperty(name);
480 std::map<std::string,std::string> Node::getProperties()
482 std::map<std::string,std::string> amap=_propertyMap;
485 std::map<std::string,std::string> fatherMap=_father->getProperties();
486 amap.insert(fatherMap.begin(),fatherMap.end());
492 void Node::setProperties(std::map<std::string,std::string> properties)
494 _propertyMap.clear();
495 _propertyMap=properties;
498 //! Return the node state in the context of its father
500 * \return the effective node state
502 * The node state is stored in a private attribute _state.
503 * This state is relative to its father state : a node with a
504 * TOACTIVATE state with a father node in a READY state is not
505 * to activate. Its effective state is only READY.
506 * This method returns the effective state of the node taking
507 * into account that of its father.
509 YACS::StatesForNode Node::getEffectiveState() const
511 if(!_father) //the root node
513 if(_state==YACS::DISABLED)
514 return YACS::DISABLED;
515 return _father->getEffectiveState(this);
518 //! Return the effective state of a node in the context of this one (its father)
520 * \param node: the node which effective state is queried
521 * \return the effective node state
523 YACS::StatesForNode Node::getEffectiveState(const Node* node) const
525 if(node->getState()==YACS::DISABLED)
526 return YACS::DISABLED;
528 YACS::StatesForNode effectiveState=getEffectiveState();
529 switch(effectiveState)
533 case YACS::TOACTIVATE:
536 return YACS::DISABLED;
540 return node->getState();
544 //! Return the color associated to a state
546 * \param state : the node state
547 * \return the associated color
549 std::string Node::getColorState(YACS::StatesForNode state) const
559 case YACS::TOACTIVATE:
561 case YACS::ACTIVATED:
578 //! Dump to the input stream a dot representation of the node
580 * \param os : the input stream
582 void Node::writeDot(std::ostream &os) const
584 os << getId() << "[fillcolor=\"" ;
585 YACS::StatesForNode state=getEffectiveState();
586 os << getColorState(state);
587 os << "\" label=\"" << getImplementation() << "Node:" ;
588 os << getQualifiedName() <<"\"];\n";
591 //! same as Node::getName() in most cases, but differs for children of switch
593 * used by writeDot to distinguish children of switch, by adding a prefix to the name.
594 * prefix is built on case id.
597 std::string Node::getQualifiedName() const
600 return _father->getMyQualifiedName(this);
604 //! return node instance identifiant, unique for each node instance
606 * node instance identifiant is used to check if to nodes pointers refers to the same instance
613 //! Sets the given state for node.
614 /*! It is strongly recommended to use this function if you want to
615 * change the state of the node, instead of direct access to _state field (_state = ...).
617 void Node::setState(YACS::StatesForNode theState)
619 DEBTRACE("Node::setState: " << getName() << " " << theState);
621 // emit notification to all observers registered with the dispatcher on any change of the node's state
625 std::vector<std::pair<std::string,int> > Node::getDPLScopeInfo(ComposedNode *gfn)
627 std::vector< std::pair<std::string,int> > ret;
629 ComposedNode *work(getFather());
630 while(work!=gfn && work!=0)
632 DynParaLoop *workc(dynamic_cast<DynParaLoop *>(work));
635 std::pair<std::string,int> p(gfn->getChildName(workc),workc->getBranchIDOfNode(work2));
639 work=work->getFather();
645 * Method called by the Executor only if the executor is sensitive of scope of DynParaLoop.
646 * This method is virtual and empty because by default nothing is done.
648 void Node::applyDPLScope(ComposedNode *gfn)
652 //! emit notification to all observers registered with the dispatcher
654 * The dispatcher is unique and can be obtained by getDispatcher()
656 void Node::sendEvent(const std::string& event)
658 DEBTRACE("Node::sendEvent " << event);
659 Dispatcher* disp=Dispatcher::getDispatcher();
660 disp->dispatch(this,event);
663 //! emit notification to all observers registered with the dispatcher
665 * The dispatcher is unique and can be obtained by getDispatcher()
667 void Node::sendEvent2(const std::string& event, void *something)
669 Dispatcher* disp=Dispatcher::getDispatcher();
670 disp->dispatch2(this,event,something);
674 * For use only when loading a previously saved execution
677 void YACS::ENGINE::StateLoader(Node* node, YACS::StatesForNode state)
679 node->setState(state);
682 //! indicates if the node is valid (returns 1) or not (returns 0)
684 * This method is useful when editing a schema. It has no meaning in execution.
685 * When a node is edited, its modified method must be called so when isValid is called, its state
686 * is updated (call to edUpdateState) before returning the validity check
692 if(_state > YACS::INVALID)
698 //! update the status of the node
700 * Only useful when editing a schema
701 * Do nothing in base Node : to implement in derived classes
703 void Node::edUpdateState()
705 DEBTRACE("Node::edUpdateState(): " << _modified);
709 //! returns a string that contains an error report if the node is in error
713 std::string Node::getErrorReport()
715 if(getState()==YACS::DISABLED)
716 return "<error node= "+getName()+ "state= DISABLED/>\n";
718 YACS::StatesForNode effectiveState=getEffectiveState();
720 DEBTRACE("Node::getErrorReport: " << getName() << " " << effectiveState << " " << _errorDetails);
721 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR &&
722 effectiveState != YACS::FAILED && effectiveState != YACS::INTERNALERR)
725 std::string report="<error node= " ;
726 report=report + getName() ;
727 switch(effectiveState)
730 report=report+" state= INVALID";
733 report=report+" state= ERROR";
736 report=report+" state= FAILED";
738 case YACS::INTERNALERR:
739 report=report+" state= INTERNALERR";
744 report=report + ">\n" ;
745 report=report+_errorDetails;
746 report=report+"\n</error>";
750 //! returns a string that contains the name of the container log file if it exists
752 * Do nothing here. To subclass
754 std::string Node::getContainerLog()
759 //! Sets Node in modified state and its father if it exists
763 void Node::modified()
765 DEBTRACE("Node::modified() " << getName());
771 //! Put this node into TOLOAD state when possible
775 void Node::ensureLoading()
777 if(_state == YACS::READY)
778 setState(YACS::TOLOAD);
781 //! Return the name of a state
785 std::string Node::getStateName(YACS::StatesForNode state)
787 static NodeStateNameMap nodeStateNameMap;
788 return nodeStateNameMap[state];
791 //! Stop all pending activities of the node
793 * This method should be called when a Proc is finished and must be deleted from the YACS server
795 void Node::shutdown(int level)
800 //! Clean the node in case of not clean exit
802 * This method should be called on a control-C or sigterm
804 void Node::cleanNodes()
808 //! Reset the node state depending on the parameter level
809 void Node::resetState(int level)
811 DEBTRACE("Node::resetState " << getName() << "," << level << "," << _state);
812 if(_state==YACS::ERROR || _state==YACS::FAILED || _state==YACS::ACTIVATED)
814 setState(YACS::READY);
815 InGate* inGate = getInGate();
816 std::list<OutGate*> backlinks = inGate->getBackLinks();
817 for (std::list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
819 Node* fromNode = (*io)->getNode();
820 if(fromNode->getState() == YACS::DONE)
822 inGate->setPrecursorDone(*io);