Salome HOME
Update version to 3.1.0a
[modules/superv.git] / src / Supervision / Graph_Impl.cxx
index 3af8ebd8201379064293c8b7cb6353d1bbb49ff1..b6aee77e1bd8cb40d387be9bf2718c5316d2d8e0 100644 (file)
@@ -34,6 +34,8 @@ using namespace std;
 
 //#include "utilities.h"
 
+#include "SALOME_Container_i.hxx"
+
 #include "StreamGraph_Impl.hxx"
 
 #include "DataFlowEditor_DataFlow.hxx"
@@ -46,11 +48,7 @@ static void CreateEditor( CORBA::ORB_ptr orb ,
                           const SUPERV::KindOfNode aKindOfNode ,
                           string & dbgfile ,
                           GraphEditor::DataFlow **  aDataFlowEditor ) {
-//    string dbgfile = "/tmp/" ;
   bool aXmlFile = false ;
-  dbgfile = "/tmp/" ;
-  dbgfile += instanceName ;
-  dbgfile += "_" ;
   int lenname = strlen( aDataFlowName ) ;
   char * theDataFlowName = new char [ lenname+1 ] ;
   strcpy( theDataFlowName , aDataFlowName ) ;
@@ -72,12 +70,28 @@ static void CreateEditor( CORBA::ORB_ptr orb ,
       strcpy( theDataFlowName , &aDataFlowName[ 0 ] ) ;
     }
   }
+  
   string theDataFlowInstanceName = theDataFlowName ;
-//  if ( !aXmlFile ) {
-//    theDataFlowInstanceName = theAutomaton->GraphInstanceName( theDataFlowName ) ;
-//  }
+
+  // asv : 16.11.04 : creation of log file in /tmp/logs/$USER dir. 
+  // "/tmp/logs/$USER" was created by  runSalome.py -> orbmodule.py.
+  dbgfile = "/tmp/logs/" ;
+  dbgfile += getenv( "USER" ) ;
+  dbgfile += "/" ;
+  dbgfile += instanceName ;
+  dbgfile += "_" ;
   dbgfile += theDataFlowInstanceName ;
   dbgfile = dbgfile + "_" + theAutomaton->DbgFileNumber() + ".log" ;
+  FILE* f = fopen ( dbgfile.c_str(), "a" );
+  if ( f ) { // check if file can be opened for writing
+    fclose( f );
+  } 
+  else { // if file can't be opened - use a guaranteed temp file name
+    char* aTempNam = tempnam( NULL, NULL );
+    dbgfile = aTempNam;
+    free ( aTempNam );
+  }
+
   SALOME_NamingService * NamingService = new SALOME_NamingService( orb ) ;
   *aDataFlowEditor = new GraphEditor::DataFlow( orb , NamingService ,
                                                 theDataFlowInstanceName.c_str() , dbgfile.c_str() ,
@@ -94,29 +108,43 @@ static void CreateExecutor( CORBA::ORB_ptr orb ,
                           const SUPERV::KindOfNode aKindOfNode ,
                           string & dbgfile ,
                           GraphExecutor::DataFlow **  aDataFlowExecutor ) {
-//    string dbgfile = "/tmp/" ;
-  dbgfile = "/tmp/" ;
-  dbgfile += instanceName ;
-  dbgfile += "_" ;
   int lenname = strlen( aDataFlowName ) ;
   char * theDataFlowName = new char [ lenname+1 ] ;
   strcpy( theDataFlowName , aDataFlowName ) ;
   if ( aDataFlowName ) {
     strcpy( theDataFlowName , &aDataFlowName[ 0 ] ) ;
   }
+
   string theDataFlowInstanceName = theDataFlowName ;
+
+  // asv : 16.11.04 : creation of log file in /tmp/logs/$USER dir. 
+  // "/tmp/logs/$USER" was created by  runSalome.py -> orbmodule.py.
+  dbgfile = "/tmp/logs/" ;
+  dbgfile += getenv( "USER" ) ;
+  dbgfile += "/" ;
+  dbgfile += instanceName ;
+  dbgfile += "_" ;
   dbgfile += theDataFlowInstanceName ;
   dbgfile = dbgfile + "_" + theAutomaton->DbgFileNumber() ;
-
   ostringstream astr ;
   astr << theAutomaton->ExecNumber() ;
   dbgfile += astr.str() ;
   dbgfile += string( "_Exec.log" ) ;
+  FILE* f = fopen ( dbgfile.c_str(), "a" );
+  if ( f ) { // check if file can be opened for writing
+    fclose( f );
+  } 
+  else { // if file can't be opened - use a guaranteed temp file name
+    char* aTempNam = tempnam( NULL, NULL );
+    dbgfile = aTempNam;
+    free ( aTempNam );
+  }
 
   SALOME_NamingService * NamingService = new SALOME_NamingService( orb ) ;
   *aDataFlowExecutor = new GraphExecutor::DataFlow( orb , NamingService ,
                                                     theDataFlowInstanceName.c_str() , dbgfile.c_str() ,
                                                     aKindOfNode ) ;
+
   MESSAGE( "CreateExecutor " << theDataFlowName << " " << theDataFlowInstanceName << " created with "
            << dbgfile.c_str() ) ;
 
@@ -226,9 +254,9 @@ void Graph_Impl::destroy() {
     }
   }
   _poa->deactivate_object(*_id) ;
-  CORBA::release(_poa) ;
+//  CORBA::release(_poa) ;
   delete(_id) ;
-  _thisObj->_remove_ref();
+//  _thisObj->_remove_ref();
   endService( "Graph_Impl::destroy" );
 }
 
@@ -340,6 +368,15 @@ bool Graph_Impl::Export(const char * anXmlFile ) {
   return RetVal ;
 }
 
+char *  Graph_Impl::SavePY( bool importSuperV ) {
+  beginService( "Graph_Impl::SavePY" );
+  ostringstream fstring ;
+  bool RetVal ;
+  RetVal = DataFlowEditor()->SavePY( fstring , importSuperV ) ;
+  fstring  << ends ;
+  endService( "Graph_Impl::SavePY" );
+  return CORBA::string_dup( fstring.str().c_str() );
+}
 SUPERV::CNode_ptr Graph_Impl::CNode( const SALOME_ModuleCatalog::Service &NodeService ) {
   beginService( "Graph_Impl::CNode" );
   SUPERV::CNode_var iobject = SUPERV::CNode::_nil() ;
@@ -494,8 +531,10 @@ SUPERV::LNode_ptr Graph_Impl::LNode( const char * InitName ,
       myEndNode->SetObjRef( SUPERV::ELNode::_duplicate( iendobject ) ) ;
       myNode->SetCoupled( myEndNode->BaseNode()->Name() ) ;
       myEndNode->SetCoupled( myNode->BaseNode()->Name() ) ;
-      Link( myNode->Port( "DoLoop" ) , myEndNode->Port( "DoLoop" ) ) ;
-      Link( myEndNode->Port( "DoLoop" ) , myNode->Port( "InitLoop" ) ) ;
+//JR 25.01.2005  That links are already done in SetCoupled above ...
+//      Link( myNode->Port( "DoLoop" ) , myEndNode->Port( "DoLoop" ) ) ;
+//      Link( myEndNode->Port( "DoLoop" ) , myNode->Port( "InitLoop" ) ) ;
+//      Link( myEndNode->Port( "DoLoop" ) , myNode->Port( "DoLoop" ) ) ;
     }
   }
   anEndOfLoop = SUPERV::ELNode::_duplicate( iendobject ) ;
@@ -786,13 +825,19 @@ SUPERV::Graph_var Graph_Impl::LoadDataFlows( GraphExecutor::DataFlow * aDataFlow
           aMacroGraphExe->CoupledNode( aMacroNode ) ;
           aMacroNode->CoupledNode( aMacroGraphExe ) ;
 
+//JR 04.05.2005 Debug : InPorts values of MacroNodes are like for other nodes
+//                      InPorts values of MacroGraphs of MacroNodes are done in the Executor
+// It was probably to try to debug anything but it was a wrong debug and I do not know what bug
+// ===> folowing code is commented
+#if 0
          // initialize MacroNode's InPorts with corresponding "external" values
          int q ;
          for ( q = 0 ; q < aMacroNode->GetNodeInPortsSize() ; q++ ) {
            const GraphBase::InPort * anInPort = aMacroNode->GetNodeInPort( q ) ;
            if ( anInPort->IsDataConnected() ) {
              const char* aMacroGraphInPortName = aMacroGraph->GetNodeInPort( q )->PortName();
-             if ( !aMacroGraphExecutor->InputOfAny(  aMacroGraphInPortName, *anInPort->GetOutPort()->Value(),false ) ) {
+//JR 30.03.2005              if ( !aMacroGraphExecutor->InputOfAny(  aMacroGraphInPortName, *anInPort->GetOutPort()->Value(),false ) ) {
+             if ( !aMacroGraphExecutor->InputOfAny(  aMacroGraphInPortName, anInPort->GetOutPort()->Value(),false ) ) {
                return SUPERV::Graph::_duplicate( SUPERV::Graph::_nil() ) ;
              }
              else {
@@ -801,7 +846,7 @@ SUPERV::Graph_var Graph_Impl::LoadDataFlows( GraphExecutor::DataFlow * aDataFlow
              }
            }
          } // end of init MacroNode's InPorts
-
+#endif
          aMacroGraphImpl->Run() ;
 
         }
@@ -1135,6 +1180,8 @@ SUPERV::Link_ptr Graph_Impl::Link( SUPERV::Port_ptr OutPort ,
         }
       }
       else {
+        MESSAGE( "Graph_Impl::Link( " << OutPort->Name() << " " << anOutPort << " , "
+                 << InPort->Name() << " " << anInPort << " ) ObjRef already exists" ) ;
         iobject = anOutPort->InPortObjRef( anInPort ) ;
       }
     }
@@ -1350,8 +1397,14 @@ SUPERV::ListOfNodes * Graph_Impl::Nodes() {
   RetVal->Graphs.length(0) ;
   if ( !IsMacro() ) {
     int i ;
+    MESSAGE( DataFlowEditor()->Graph()->GetGraphNodeSize() << " Nodes in the Map and "
+             << DataFlowEditor()->Graph()->GraphNodesSize() << " Nodes int the Vector" ) ;
     for ( i = 0 ; i < DataFlowEditor()->Graph()->GraphNodesSize() ; i++ ) {
       GraphBase::ComputingNode * aNode = DataFlowEditor()->Graph()->GraphNodes( i ) ;
+#if 1
+      MESSAGE( i << ". Vector : " << aNode->Name() << " Map : "
+               << DataFlowEditor()->Graph()->GetGraphNode( aNode->Name() )->Name() ) ;
+#endif
       RetVal = SetNode( RetVal , aNode ) ;
     }
 #if 0
@@ -1447,8 +1500,10 @@ SUPERV::ListOfLinks * Graph_Impl::Links( GraphBase::ComputingNode * theNode ,
 //                        << anOutPort->PortName() << ") --> " << aNode->Name() << "("
 //                        << anInPort->PortName() << ")" ) ;
                 if ( theNode == NULL ||
-                     ( theNode != NULL && !theNode->IsEndSwitchNode() &&
-                       !strcmp( theNode->Name() , aNode->Name() ) ) ) {
+//PAL8521
+//JR 14.02.2005 : Debug : we must see also that links !
+//                     ( theNode != NULL && !theNode->IsEndSwitchNode() &&
+                       !strcmp( theNode->Name() , aNode->Name() ) ) {
                   if ( anInPort->IsLoop() || anOutPort->IsLoop() ||
                        ( aNode->IsEndLoopNode() && !strcmp( aNode->CoupledNode()->Name() ,
                                                             anOutPort->NodeName() ) ) ) {
@@ -1517,7 +1572,9 @@ SUPERV::ListOfLinks * Graph_Impl::Links( GraphBase::ComputingNode * theNode ,
             if ( theNode == NULL ||
                  !strcmp( theNode->Name() , aNode->Name() ) ) {
               if ( !anInPort->IsDataStream() ) {
-                if ( theNode || ( toNode->IsEndSwitchNode() && !aNode->IsSwitchNode() ) ) {
+//PAL8521
+//JR 14.02.2005 : Debug : we must see also that links !
+//                if ( theNode || ( toNode->IsEndSwitchNode() && !aNode->IsSwitchNode() ) ) {
                   if ( anInputParam == NULL ||
                        !strcmp( anInPort->PortName() , anInputParam ) ) {
                     if ( anInPort->IsLoop() || anOutPort->IsLoop() ||
@@ -1564,7 +1621,7 @@ SUPERV::ListOfLinks * Graph_Impl::Links( GraphBase::ComputingNode * theNode ,
 //                             << anOutPort->PortName() << ") --> " << toNode->Name() << "("
 //                             << anInPort->PortName() << ")" << " skipped" ) ;
                  }
-               }
+//             }
              }
            }
          }
@@ -1607,6 +1664,20 @@ Engines::Component_ptr Graph_Impl::ComponentRef( const char * aComputerContainer
   return Engines::Component::_duplicate( objComponent ) ;
 }
 
+char * Graph_Impl::Messages() {
+  beginService( "Graph_Impl::Messages" );
+  string Messages  ;
+  MESSAGE( "Graph_Impl::Messages IsEditing " << DataFlowEditor()->IsEditing() ) ;
+  Messages = DataFlowEditor()->Graph()->Messages() ;
+  DataFlowEditor()->Graph()->ReSetMessages() ;
+  if ( DataFlowExecutor() ) {
+    Messages += DataFlowExecutor()->Graph()->Messages() ;
+    DataFlowExecutor()->Graph()->ReSetMessages() ;
+  }
+  endService( "Graph_Impl::Messages" );
+  return ( CORBA::string_dup( Messages.c_str() ) ) ;
+}
+
 bool Graph_Impl::IsValid() {
 //  beginService( "Graph_Impl::IsValid" );
   bool RetVal = false ;
@@ -1619,7 +1690,10 @@ bool Graph_Impl::IsValid() {
 bool Graph_Impl::IsExecutable() {
 //  beginService( "Graph_Impl::IsExecutable" );
   bool RetVal = false ;
-  if ( !IsMacro() ) {
+
+  // asv : 15.11.04 : added "&& GraphMacroLevel() == 0" -> 
+  // subgraphs are not executable by themselves, RetVal = false..
+  if ( !IsMacro() && DataFlowEditor()->Graph()->GraphMacroLevel() == 0 ) {
     RetVal = DataFlowEditor()->IsExecutable() ;
   }
 //  endService( "Graph_Impl::IsExecutable" );
@@ -1730,13 +1804,10 @@ bool Graph_Impl::Begin() {
       perror("pthread_mutex_lock _MutexExecutorWait") ;
       exit( 0 ) ;
     }
+    DataFlowEditor()->Graph()->ReSetMessages() ; // ==> Only one set of errors messages ...
     if ( DataFlowExecutor() ) {
       MESSAGE( "Graph_Impl::Begin " << DataFlowExecutor()->Threads() << " threads" )
-//      if ( DataFlowExecutor()->Threads() ) {
-//        DataFlowExecutor()->JoinedWait() ;
-//      }
-      delete DataFlowExecutor() ;
-      DataFlowEditor()->Executor( NULL ) ;
+      Editing(); // just in case it was not called before by GUI..
     }
     if ( DataFlowEditor()->IsExecutable() ) {
       DataFlowEditor()->EditedAfterExecution( false ) ;
@@ -1762,70 +1833,59 @@ bool Graph_Impl::Begin() {
   }
   return RetVal ;
 }
-bool Graph_Impl::Run() {
-  beginService( "Graph_Impl::Run" );
+
+/**
+ * run() : was created in order to reduce code dublication, Run() and Start() do the same thing
+ *         except for final call to Executor->Run( bool ) - Run() calls with AndSuspend=false
+ *         and Start() calls with AndSuspend=true flag.  The rest of the actions are the same:
+ *         1. if a MacroNode's coupled graph (MacroLevel>0) -> simply Run() it.
+ *         2. if a Supergraph -> create executor, load dataflow in it (done in Begin()), then - Run() it.
+ */
+bool Graph_Impl::run( const bool andSuspend ) {
   bool RetVal = false ;
+
+  // if Editing or Finished state, and, naturally, 'this' is not a MacroNode. 
   if ( ( DataFlowEditor()->IsEditing() || IsDone() ) && !IsMacro() ) {
-    MESSAGE( "Graph_Impl::Run " << DataFlowEditor()->Graph()->Name() << " " << DataFlowEditor() << " IsEditing" );
+
     if ( DataFlowEditor()->Graph()->GraphMacroLevel() ) {
-      MESSAGE( "Graph_Impl::Run " << DataFlowEditor()->Graph()->Name() << " GraphMacroLevel "
-               << DataFlowEditor()->Graph()->GraphMacroLevel() ) ;
-      RetVal = true ;
+      // MacroGraph's Executor was created in recursive function LoadDataflows(Executor), called from
+      // Begin() of Supergraph.  See Begin() and LoadDataflows(Executor) for details.
+      RetVal = true ; 
     }
     else {
-      MESSAGE( "Graph_Impl::Run " << DataFlowEditor()->Graph()->Name() << " GraphMacroLevel "
-               << DataFlowEditor()->Graph()->GraphMacroLevel() << " --> Begin()" ) ;
+      // Create Executor instance and its datamodel
       RetVal = Begin() ;
     }
     if ( RetVal ) {
-      MESSAGE( "Graph_Impl::Run " );
+      // Set Executing flag to avoid edition operations during execution, EditedAfterExecution=false
       DataFlowEditor()->Executing() ;
-      RetVal = CNode_Impl::Run() ;
+      DataFlowEditor()->EditedAfterExecution( false ); 
+
+      // THE MAIN RUN METHOD
+      RetVal = DataFlowExecutor()->Run( andSuspend );
     }
   }
-// It is necessary to reload the dataflow because of ports with input initial values changed
-// during execution to values of outport of EndLoopNode or GOTONode
-//  else if ( !IsMacro() && IsDone() ) {
-//    MESSAGE( "Graph_Impl::Run " << DataFlowEditor()->Graph()->Name() << " " << DataFlowEditor()
-//             << " IsNOTEditing --> CNode_Impl::ReRun" );
-//    DataFlowEditor()->Executing() ;
-//    RetVal = CNode_Impl::ReRun() ;
-//  }
+  return RetVal ;
+
+}
+
+/**
+ * Run() - called on "Execute" command
+ */
+bool Graph_Impl::Run() {
+  beginService( "Graph_Impl::Run" );
+  bool RetVal = run( /*andSuspend=*/false ) ;
   MESSAGE( "Graph_Impl::Run " << DataFlowEditor()->Graph()->Name() << " RetVal " << RetVal ) ;
   endService( "Graph_Impl::Run" );
   return RetVal ;
 }
+
+/**
+ * Run() - called on "Execute step-by-step" command
+ */
 bool Graph_Impl::Start() {
   beginService( "Graph_Impl::Start" );
-  bool RetVal = false ;
-  if ( ( DataFlowEditor()->IsEditing() || IsDone() ) && !IsMacro() ) {
-    MESSAGE( "Graph_Impl::Start " << DataFlowEditor()->Graph()->Name() << " " << DataFlowEditor() << " IsEditing" );
-    if ( DataFlowEditor()->Graph()->GraphMacroLevel() ) {
-      MESSAGE( "Graph_Impl::Start " << DataFlowEditor()->Graph()->Name() << " GraphMacroLevel "
-               << DataFlowEditor()->Graph()->GraphMacroLevel() ) ;
-      RetVal = true ;
-    }
-    else {
-      MESSAGE( "Graph_Impl::Start " << DataFlowEditor()->Graph()->Name() << " GraphMacroLevel "
-               << DataFlowEditor()->Graph()->GraphMacroLevel() << " --> Begin()" ) ;
-      RetVal = Begin() ;
-    }
-    if ( RetVal ) {
-      // Added by NKV to fix PAL6222 --> to be confirm by JR ; JR confirmed
-      DataFlowEditor()->Executing() ;
-      // End of NKV's correction
-      MESSAGE( "Graph_Impl::(Re)Start " );
-      RetVal = ReStart() ;
-    }
-  }
-// It is necessary to reload the dataflow because of ports with input initial values changed
-// during execution to values of outport of EndLoopNode or GOTONode
-//  else if ( !IsMacro() && IsDone() ) {
-//    MESSAGE( "Graph_Impl::Start " << DataFlowEditor()->Graph()->Name() << " " << DataFlowEditor()
-//             << " IsNOTEditing --> CNode_Impl::ReStart" );
-//    DataFlowEditor()->Executing() ;
-//    RetVal = CNode_Impl::ReStart() ;
-//  }
+  bool RetVal = run( /*andSuspend=*/true ) ;
   MESSAGE( "Graph_Impl::Start " << DataFlowEditor()->Graph()->Name() << " RetVal " << RetVal ) ;
   endService( "Graph_Impl::Start" );
   return RetVal ;
@@ -1909,6 +1969,7 @@ bool Graph_Impl::Event( SUPERV::CNode_out aNode ,
   }
   anEvent = theEvent ;
   aState = theState ;
+
   return RetVal ;
 }
 
@@ -2464,5 +2525,13 @@ SUPERV::StreamGraph_ptr Graph_Impl::ToStreamGraph() {
   return SUPERV::StreamGraph::_duplicate( iobject ) ;
 }
 
-
+/**
+ * Destroy Executor and use only Editor and its data model
+ */
+void Graph_Impl::Editing() {
+  if ( DataFlowEditor() && DataFlowExecutor() ) {
+    delete DataFlowExecutor() ;
+    DataFlowEditor()->Executor( NULL );
+  }
+}