1 // Copyright (C) 2006-2014 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 "InputPort.hxx"
22 #include "OutputPort.hxx"
23 #include "InPropertyPort.hxx"
24 #include "ComposedNode.hxx"
25 #include "Dispatcher.hxx"
26 #include "InputDataStreamPort.hxx"
27 #include "OutputDataStreamPort.hxx"
31 #include "YacsTrace.hxx"
33 using namespace YACS::ENGINE;
36 /*! \class YACS::ENGINE::Node
37 * \brief Base class for all nodes
42 const char Node::SEP_CHAR_IN_PORT[]=".";
45 std::map<int,Node*> Node::idMap;
47 NodeStateNameMap::NodeStateNameMap()
49 insert(make_pair(YACS::READY, "READY"));
50 insert(make_pair(YACS::TOLOAD, "TOLOAD"));
51 insert(make_pair(YACS::LOADED, "LOADED"));
52 insert(make_pair(YACS::TOACTIVATE, "TOACTIVATE"));
53 insert(make_pair(YACS::ACTIVATED, "ACTIVATED"));
54 insert(make_pair(YACS::DESACTIVATED, "DESACTIVATED"));
55 insert(make_pair(YACS::DONE, "DONE"));
56 insert(make_pair(YACS::SUSPENDED, "SUSPENDED"));
57 insert(make_pair(YACS::LOADFAILED, "LOADFAILED"));
58 insert(make_pair(YACS::EXECFAILED, "EXECFAILED"));
59 insert(make_pair(YACS::PAUSE, "PAUSE"));
60 insert(make_pair(YACS::INTERNALERR, "INTERNALERR"));
61 insert(make_pair(YACS::DISABLED, "DISABLED"));
62 insert(make_pair(YACS::FAILED, "FAILED"));
63 insert(make_pair(YACS::ERROR, "ERROR"));
67 Node::Node(const std::string& name):_name(name),_inGate(this),_outGate(this),_father(0),_state(YACS::READY),
68 _implementation(Runtime::RUNTIME_ENGINE_INTERACTION_IMPL_NAME),_modified(1)
70 // Should be protected by lock ??
74 // Every node has an InPropertyPort
75 _inPropertyPort = new InPropertyPort("__InPropertyPort__Node__YACS_", this, Runtime::_tc_propvec);
78 Node::Node(const Node& other, ComposedNode *father):_inGate(this),_outGate(this),_name(other._name),_father(father),
79 _state(YACS::READY),_implementation(other._implementation),
80 _propertyMap(other._propertyMap),_modified(1)
85 // Every node has an InPropertyPort
86 _inPropertyPort = new InPropertyPort("__InPropertyPort__Node__YACS_", this, Runtime::_tc_propvec);
91 delete _inPropertyPort;
95 * initialisation of all input and output ports and gates, for execution
98 void Node::init(bool start)
102 if(_state == YACS::DISABLED)
104 exDisabledState(); // to refresh propagation of DISABLED state
107 setState(YACS::READY);
110 Node *Node::clone(ComposedNode *father, bool editionOnly) const
112 Node *ret=simpleClone(father,editionOnly);
113 ret->performDuplicationOfPlacement(*this);
117 //! Change the name of the node
119 * raise an exception if the name is already used in the scope of its father
120 * \param name : the new name
122 void Node::setName(const std::string& name)
126 if(_father->isNameAlreadyUsed(name))
128 if ( _father->getChildByName(name) != this )
130 std::string what("Name ");
132 what+=" already exists in the scope of "; what+=_father->getName();
133 throw Exception(what);
141 * get the set of all nodes connected to the outGate
144 set<Node *> Node::getOutNodes() const
147 set<InGate *> inGates=_outGate.edSetInGate();
148 for(set<InGate *>::iterator iter=inGates.begin();iter!=inGates.end();iter++)
149 ret.insert((*iter)->getNode());
153 bool Node::exIsControlReady() const
155 return _inGate.exIsReady();
158 //! Update the node state
160 * \note : Update the '_state' attribute.
161 * Typically called by 'this->_inGate' when 'this->_inGate' is ready.
163 * Called by InGate::exNotifyFromPrecursor
165 void Node::exUpdateState()
167 if(_state==YACS::DISABLED)return;
168 if(_inGate.exIsReady())
169 setState(YACS::TOACTIVATE);
172 //! Notify this node that its execution has failed
174 * The node goes in FAILED state and
175 * propagate the notification through the outGate port
178 void Node::exFailedState()
180 DEBTRACE( "Node::exFailedState: " << getName() );
181 setState(YACS::FAILED);
182 _outGate.exNotifyFailed();
185 //! Notify this node that it has been disabled
187 * The node goes in DISABLED state and
188 * propagate the notification through the outGate port
191 void Node::exDisabledState()
193 DEBTRACE( "Node::exDisabledState: " << getName() );
194 setState(YACS::DISABLED);
195 _outGate.exNotifyDisabled();
198 InPort *Node::getInPort(const std::string& name) const throw(YACS::Exception)
203 ret=getInputPort(name);
207 ret=getInputDataStreamPort(name);
213 Node::getInPropertyPort() const throw(YACS::Exception)
215 return _inPropertyPort;
219 Node::getInputPort(const std::string& name) const throw(YACS::Exception)
221 if (name == "__InPropertyPort__Node__YACS_")
222 return _inPropertyPort;
225 std::string what("Node::getInputPort : the port with name "); what+=name; what+=" does not exist on the current level";
226 throw Exception(what);
231 * \note: Contrary to getOutputPort method, this method returns the output port at highest level, possible.
232 * That is to say in some ComposedNode, like ForEachLoop or Switch, an outport inside 'this' is seen differently than the true outport.
234 OutPort *Node::getOutPort(const std::string& name) const throw(YACS::Exception)
239 ret=getOutputPort(name);
243 ret=getOutputDataStreamPort(name);
248 std::list<InPort *> Node::getSetOfInPort() const
251 list<InputPort *> data=getSetOfInputPort();
252 ret.insert(ret.end(),data.begin(),data.end());
253 list<InputDataStreamPort *> ds=getSetOfInputDataStreamPort();
254 ret.insert(ret.end(),ds.begin(),ds.end());
258 std::list<OutPort *> Node::getSetOfOutPort() const
261 list<OutputPort *> data=getSetOfOutputPort();
262 ret.insert(ret.end(),data.begin(),data.end());
263 list<OutputDataStreamPort *> ds=getSetOfOutputDataStreamPort();
264 ret.insert(ret.end(),ds.begin(),ds.end());
269 * gets a set of the composed nodes that constitute the ascendancy of this node, starting from root
270 * or from a particular ancestor
271 * \b WARNING : returned set is not sorted !
272 * @param levelToStop composed node which is the oldest ancestor required
273 * @return ascendancy, direct father first in set.
276 std::list<ComposedNode *> Node::getAllAscendanceOf(ComposedNode *levelToStop) const
278 list<ComposedNode *> ret;
279 if(this==levelToStop)
281 for(ComposedNode *iter=_father;iter!=levelToStop && iter!=0; iter=iter->_father)
286 bool Node::operator>(const Node& other) const
288 const ComposedNode *iter=other._father;
289 while(iter!=0 && iter!=this)
294 bool Node::operator<(const Node& other) const
296 const ComposedNode *iter=_father;
297 while(iter!=0 && iter!=(&other))
299 return iter==(&other);
303 * @return Implementation of node: C++, Python, CORBA...
304 * _implementation is set by a derived class in a Runtime
305 * it normally applies only to elementaryNodes and it is used by Ports to select Data Converters.
306 * Potential problem with Ports attached to composed Nodes...
309 string Node::getImplementation() const
311 return _implementation;
314 //! Becomes deprecated soon. Replaced by ComposedNode::CheckConsistency.
315 set<InputPort *> Node::edGetSetOfUnitializedInputPort() const
317 set<InputPort *> setOfUnitializedInputPort;
318 list<InputPort *> allOfInputPorts=getSetOfInputPort();
319 for(list<InputPort *>::const_iterator iter=allOfInputPorts.begin();iter!=allOfInputPorts.end();iter++)
321 if ( ! (*iter)->edIsInitialized() )
322 setOfUnitializedInputPort.insert(*iter);
324 return setOfUnitializedInputPort;
327 //! Becomes deprecated soon. Replaced by ComposedNode::CheckConsistency.
328 bool Node::edAreAllInputPortInitialized() const
330 set<InputPort *> setOfUnitializedInputPort = edGetSetOfUnitializedInputPort();
331 return ( setOfUnitializedInputPort.size() == 0);
335 * Called typically by Bloc to notify failure on potentially next nodes on the same scope of 'this'
337 void Node::exForwardFailed()
339 _outGate.exNotifyFailed();
343 * Called typically by Bloc to activate potentially next nodes on the same scope of 'this'
345 void Node::exForwardFinished()
347 DEBTRACE("Node::exForwardFinished");
348 _outGate.exNotifyDone();
352 * Called typically by ComposedNode to correctly update DF/CF/DS links
354 void Node::edDisconnectAllLinksWithMe()
356 _inGate.edDisconnectAllLinksToMe();
357 _outGate.edDisconnectAllLinksFromMe();
360 Proc *Node::getProc()
364 return _father->getProc();
367 const Proc * Node::getProc() const
371 return _father->getProc();
374 ComposedNode *Node::getRootNode() const throw(YACS::Exception)
377 throw Exception("No root node");
378 ComposedNode *iter=_father;
381 return (ComposedNode *)iter;
385 * checks validity of ports name, that must not contain a particular character '?'
386 * USAGE NOT CLEAR, not used so far, when are those characters set ?
389 void Node::checkValidityOfPortName(const std::string& name) throw(YACS::Exception)
391 if(name.find(SEP_CHAR_IN_PORT, 0 )!=string::npos)
393 string what("Port name "); what+=name; what+="not valid because it contains character "; what+=SEP_CHAR_IN_PORT;
394 throw Exception(what);
399 * @note : Check that 'node1' and 'node2' have exactly the same father
400 * @exception : If 'node1' and 'node2' have NOT exactly the same father
402 ComposedNode *Node::checkHavingCommonFather(Node *node1, Node *node2) throw(YACS::Exception)
404 if(node1!=0 && node2!=0)
406 if(node1->_father==node2->_father)
407 return node1->_father;
409 throw Exception("check failed : nodes have not the same father");
412 const std::string Node::getId() const
414 std::string id=getRootNode()->getName();
415 if(getRootNode() != this)
416 id= id+'.'+ getRootNode()->getChildName(this);
417 string::size_type debut =id.find_first_of('.');
418 while(debut != std::string::npos){
420 debut=id.find_first_of('.',debut);
425 void Node::setProperty(const std::string& name, const std::string& value)
427 DEBTRACE("Node::setProperty " << name << " " << value);
428 _propertyMap[name]=value;
431 std::string Node::getProperty(const std::string& name)
433 std::map<std::string,std::string>::iterator it=_propertyMap.find(name);
435 if(it != _propertyMap.end())
438 return _father->getProperty(name);
443 std::map<std::string,std::string> Node::getProperties()
445 std::map<std::string,std::string> amap=_propertyMap;
448 std::map<std::string,std::string> fatherMap=_father->getProperties();
449 amap.insert(fatherMap.begin(),fatherMap.end());
455 void Node::setProperties(std::map<std::string,std::string> properties)
457 _propertyMap.clear();
458 _propertyMap=properties;
461 //! Return the node state in the context of its father
463 * \return the effective node state
465 * The node state is stored in a private attribute _state.
466 * This state is relative to its father state : a node with a
467 * TOACTIVATE state with a father node in a READY state is not
468 * to activate. Its effective state is only READY.
469 * This method returns the effective state of the node taking
470 * into account that of its father.
472 YACS::StatesForNode Node::getEffectiveState() const
474 if(!_father) //the root node
476 if(_state==YACS::DISABLED)
477 return YACS::DISABLED;
478 return _father->getEffectiveState(this);
481 //! Return the effective state of a node in the context of this one (its father)
483 * \param node: the node which effective state is queried
484 * \return the effective node state
486 YACS::StatesForNode Node::getEffectiveState(const Node* node) const
488 if(node->getState()==YACS::DISABLED)
489 return YACS::DISABLED;
491 YACS::StatesForNode effectiveState=getEffectiveState();
492 switch(effectiveState)
496 case YACS::TOACTIVATE:
499 return YACS::DISABLED;
503 return node->getState();
507 //! Return the color associated to a state
509 * \param state : the node state
510 * \return the associated color
512 std::string Node::getColorState(YACS::StatesForNode state) const
522 case YACS::TOACTIVATE:
524 case YACS::ACTIVATED:
541 //! Dump to the input stream a dot representation of the node
543 * \param os : the input stream
545 void Node::writeDot(std::ostream &os) const
547 os << getId() << "[fillcolor=\"" ;
548 YACS::StatesForNode state=getEffectiveState();
549 os << getColorState(state);
550 os << "\" label=\"" << getImplementation() << "Node:" ;
551 os << getQualifiedName() <<"\"];\n";
554 //! same as Node::getName() in most cases, but differs for children of switch
556 * used by writeDot to distinguish children of switch, by adding a prefix to the name.
557 * prefix is built on case id.
560 std::string Node::getQualifiedName() const
563 return _father->getMyQualifiedName(this);
567 //! return node instance identifiant, unique for each node instance
569 * node instance identifiant is used to check if to nodes pointers refers to the same instance
576 //! Sets the given state for node.
577 /*! It is strongly recommended to use this function if you want to
578 * change the state of the node, instead of direct access to _state field (_state = ...).
580 void Node::setState(YACS::StatesForNode theState)
582 DEBTRACE("Node::setState: " << getName() << " " << theState);
584 // emit notification to all observers registered with the dispatcher on any change of the node's state
588 //! emit notification to all observers registered with the dispatcher
590 * The dispatcher is unique and can be obtained by getDispatcher()
592 void Node::sendEvent(const std::string& event)
594 DEBTRACE("Node::sendEvent " << event);
595 Dispatcher* disp=Dispatcher::getDispatcher();
596 disp->dispatch(this,event);
600 * For use only when loading a previously saved execution
603 void YACS::ENGINE::StateLoader(Node* node, YACS::StatesForNode state)
605 node->setState(state);
608 //! indicates if the node is valid (returns 1) or not (returns 0)
610 * This method is useful when editing a schema. It has no meaning in execution.
611 * When a node is edited, its modified method must be called so when isValid is called, its state
612 * is updated (call to edUpdateState) before returning the validity check
618 if(_state > YACS::INVALID)
624 //! update the status of the node
626 * Only useful when editing a schema
627 * Do nothing in base Node : to implement in derived classes
629 void Node::edUpdateState()
631 DEBTRACE("Node::edUpdateState(): " << _modified);
635 //! returns a string that contains an error report if the node is in error
639 std::string Node::getErrorReport()
641 if(getState()==YACS::DISABLED)
642 return "<error node= "+getName()+ "state= DISABLED/>\n";
644 YACS::StatesForNode effectiveState=getEffectiveState();
646 DEBTRACE("Node::getErrorReport: " << getName() << " " << effectiveState << " " << _errorDetails);
647 if(effectiveState != YACS::INVALID && effectiveState != YACS::ERROR &&
648 effectiveState != YACS::FAILED && effectiveState != YACS::INTERNALERR)
651 std::string report="<error node= " ;
652 report=report + getName() ;
653 switch(effectiveState)
656 report=report+" state= INVALID";
659 report=report+" state= ERROR";
662 report=report+" state= FAILED";
664 case YACS::INTERNALERR:
665 report=report+" state= INTERNALERR";
670 report=report + ">\n" ;
671 report=report+_errorDetails;
672 report=report+"\n</error>";
676 //! returns a string that contains the name of the container log file if it exists
678 * Do nothing here. To subclass
680 std::string Node::getContainerLog()
685 //! Sets Node in modified state and its father if it exists
689 void Node::modified()
691 DEBTRACE("Node::modified() " << getName());
697 //! Put this node into TOLOAD state when possible
701 void Node::ensureLoading()
703 if(_state == YACS::READY)
704 setState(YACS::TOLOAD);
707 //! Return the name of a state
711 std::string Node::getStateName(YACS::StatesForNode state)
713 static NodeStateNameMap nodeStateNameMap;
714 return nodeStateNameMap[state];
717 //! Stop all pending activities of the node
719 * This method should be called when a Proc is finished and must be deleted from the YACS server
721 void Node::shutdown(int level)
726 //! Clean the node in case of not clean exit
728 * This method should be called on a control-C or sigterm
730 void Node::cleanNodes()
734 //! Reset the node state depending on the parameter level
735 void Node::resetState(int level)
737 DEBTRACE("Node::resetState " << getName() << "," << level << "," << _state);
738 if(_state==YACS::ERROR || _state==YACS::FAILED)
740 setState(YACS::READY);
741 InGate* inGate = getInGate();
742 std::list<OutGate*> backlinks = inGate->getBackLinks();
743 for (std::list<OutGate*>::iterator io = backlinks.begin(); io != backlinks.end(); io++)
745 Node* fromNode = (*io)->getNode();
746 if(fromNode->getState() == YACS::DONE)
748 inGate->setPrecursorDone(*io);