Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROData / HYDROData_PriorityQueue.cxx
index 95e167bd2c7fa8440230efd9d7b8f9c2c4c1c126..29678299dcaa4777bd1ffa53019cff1628ff9f85 100644 (file)
@@ -1,3 +1,20 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <HYDROData_PriorityQueue.h>
 #include <HYDROData_CalculationCase.h>
 #include <TDataStd_Integer.hxx>
 #include <TDF_ChildIterator.hxx>
 
-HYDROData_PriorityQueue::HYDROData_PriorityQueue( HYDROData_CalculationCase* theCalcCase )
+HYDROData_PriorityQueue::HYDROData_PriorityQueue( HYDROData_CalculationCase* theCalcCase,
+                                                  Standard_Integer aTag )
 {
-  myGeomObjects = theCalcCase->GetGeometryObjects();
-  for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
+  myObjects = theCalcCase->GetGeometryObjects();
+  for( int i=myObjects.Lower(), n=myObjects.Upper(); i<=n; i++ )
   {
-    Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( myGeomObjects.Value( i ) );
+    Handle(HYDROData_Entity) anObj = myObjects.Value( i );
     if( !anObj.IsNull() )
     {
       QString anObjName = anObj->GetName();
@@ -19,31 +37,31 @@ HYDROData_PriorityQueue::HYDROData_PriorityQueue( HYDROData_CalculationCase* the
     }
   }
 
-  myRules = GetRules( theCalcCase->Label().FindChild( HYDROData_CalculationCase::DataTag_CustomRules ) );
+  myRules = GetRules( theCalcCase->Label().FindChild( aTag ) );
 }
 
 HYDROData_PriorityQueue::~HYDROData_PriorityQueue()
 {
 }
 
-Handle(HYDROData_Object) HYDROData_PriorityQueue::GetMostPriorityObject( const QStringList& theZoneObjects,
-                                                                         HYDROData_Zone::MergeAltitudesType& theMergeType ) const
+Handle(HYDROData_Entity) HYDROData_PriorityQueue::GetMostPriorityObject( const QStringList& theZoneObjects,
+                                                                         HYDROData_Zone::MergeType& theMergeType ) const
 {
   QStringList aSortedZoneObjects;
-  for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
+  for( int i=myObjects.Lower(), n=myObjects.Upper(); i<=n; i++ )
   {
-    QString aName = myGeomObjects.Value( i )->GetName();
+    QString aName = myObjects.Value( i )->GetName();
     if( theZoneObjects.contains( aName ) )
       aSortedZoneObjects.append( aName );
   }
 
-  Handle(HYDROData_Object) aMostPriorityObj;
+  Handle(HYDROData_Entity) aMostPriorityObj;
   theMergeType = HYDROData_Zone::Merge_UNKNOWN;
   QStringList::const_iterator anIt = aSortedZoneObjects.begin(), aLast = aSortedZoneObjects.end();
   for( ; anIt!=aLast; anIt++ )
   {
-    HYDROData_Zone::MergeAltitudesType aLocalMerge = HYDROData_Zone::Merge_UNKNOWN;
-    Handle(HYDROData_Object) anObj = myNames[*anIt];
+    HYDROData_Zone::MergeType aLocalMerge = HYDROData_Zone::Merge_UNKNOWN;
+    Handle(HYDROData_Entity) anObj = myNames[*anIt];
     if( !anObj.IsNull() )
     {
       if( aMostPriorityObj.IsNull() )
@@ -65,9 +83,9 @@ Handle(HYDROData_Object) HYDROData_PriorityQueue::GetMostPriorityObject( const Q
   return aMostPriorityObj;
 }
 
-bool HYDROData_PriorityQueue::IsMorePriority( const Handle(HYDROData_Object)& theObj1,
-                                              const Handle(HYDROData_Object)& theObj2,
-                                              HYDROData_Zone::MergeAltitudesType& theMergeType ) const
+bool HYDROData_PriorityQueue::IsMorePriority( const Handle(HYDROData_Entity)& theObj1,
+                                              const Handle(HYDROData_Entity)& theObj2,
+                                              HYDROData_Zone::MergeType& theMergeType ) const
 {
   // 1. First we check custom rules
   HYDROData_ListOfRules::const_iterator anIt = myRules.begin(), aLast = myRules.end();
@@ -86,14 +104,14 @@ bool HYDROData_PriorityQueue::IsMorePriority( const Handle(HYDROData_Object)& th
   }
 
   // 2. If no custom rule found, the standard ordering list is applied
-  for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
+  for( int i=myObjects.Lower(), n=myObjects.Upper(); i<=n; i++ )
   {
-    if( myGeomObjects.Value( i )->Label() == theObj1->Label() )
+    if( myObjects.Value( i )->Label() == theObj1->Label() )
     {
       theMergeType = HYDROData_Zone::Merge_Object;
       return true;
     }
-    if( myGeomObjects.Value( i )->Label() == theObj2->Label() )
+    if( myObjects.Value( i )->Label() == theObj2->Label() )
     {
       theMergeType = HYDROData_Zone::Merge_Object;
       return false;
@@ -115,27 +133,42 @@ enum HYDROData_PriorityQueueTag
   Merge_Tag,
 };
 
-void HYDROData_PriorityQueue::AddRule( TDF_Label&                         theRulesLabel,
-                                       const Handle(HYDROData_Object)&    theObject1,
-                                       HYDROData_PriorityType             thePriority,
-                                       const Handle(HYDROData_Object)&    theObject2,
-                                       HYDROData_Zone::MergeAltitudesType theMergeType )
+void HYDROData_PriorityQueue::AddRule( TDF_Label&                      theRulesLabel,
+                                       const Handle(HYDROData_Entity)& theObject1,
+                                       HYDROData_PriorityType          thePriority,
+                                       const Handle(HYDROData_Entity)& theObject2,
+                                       HYDROData_Zone::MergeType       theMergeType )
 {
-  TDF_Label aNewRuleLab = theRulesLabel.NewChild();
+  // Get the last rule index
+  Standard_Integer aRuleIndex = 0;
+  Handle(TDataStd_Integer) anIntVal;
+  if ( theRulesLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) ) {
+    aRuleIndex = anIntVal->Get();
+  }
+
+  TDF_Label aNewRuleLab = theRulesLabel.FindChild( aRuleIndex );
 
   TDF_Label anObj1Lab = aNewRuleLab.FindChild( Object1_Tag );
   Handle(TDataStd_ReferenceList) aRefs = TDataStd_ReferenceList::Set( anObj1Lab );
+  aRefs->SetID(TDataStd_ReferenceList::GetID());
   aRefs->Append( theObject1->Label() );
   
   TDF_Label aPriorityLab = aNewRuleLab.FindChild( Priority_Tag );
-  TDataStd_Integer::Set( aPriorityLab, thePriority );
+  Handle(TDataStd_Integer) anPLAttr = TDataStd_Integer::Set( aPriorityLab, thePriority );
+  anPLAttr->SetID(TDataStd_Integer::GetID());
 
   TDF_Label anObj2Lab = aNewRuleLab.FindChild( Object2_Tag );
   aRefs = TDataStd_ReferenceList::Set( anObj2Lab );
+  aRefs->SetID(TDataStd_ReferenceList::GetID());
   aRefs->Append( theObject2->Label() );
 
   TDF_Label aMergeLab = aNewRuleLab.FindChild( Merge_Tag );
-  TDataStd_Integer::Set( aMergeLab, theMergeType );
+  Handle(TDataStd_Integer) anMLAttr = TDataStd_Integer::Set( aMergeLab, theMergeType );
+  anMLAttr->SetID(TDataStd_Integer::GetID());
+
+  // Increment the last rule index
+  Handle(TDataStd_Integer) anRLAttr = TDataStd_Integer::Set( theRulesLabel, aRuleIndex + 1 );
+  anRLAttr->SetID(TDataStd_Integer::GetID());
 }
 
 HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRulesLabel )
@@ -158,10 +191,10 @@ HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRul
     if( isObj1OK && isPriorityOK && isObj2OK && isMergeOK )
     {
       HYDROData_CustomRule aRule;
-      aRule.Object1 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
+      aRule.Object1 = HYDROData_Iterator::Object( aRefs1->First() );
       aRule.Priority = ( HYDROData_PriorityType ) aPriorityAttr->Get();
-      aRule.Object2 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
-      aRule.MergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();
+      aRule.Object2 = HYDROData_Iterator::Object( aRefs2->First() );
+      aRule.MergeType = ( HYDROData_Zone::MergeType ) aMergeAttr->Get();
       aRules.append( aRule );
     }
   }
@@ -172,6 +205,7 @@ HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRul
 QString HYDROData_PriorityQueue::DumpRules( const TDF_Label& theRulesLab )
 {
   QString aDump = "Rules:\n";
+
   HYDROData_ListOfRules aRules = GetRules( theRulesLab );
   HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
   for( ; anIt!=aLast; anIt++ )
@@ -213,6 +247,8 @@ void HYDROData_PriorityQueue::DumpRulesToPython( const TDF_Label& theRulesLab,
     QString aPriority = anIt->Priority == LESS ? "LESS" : "GREATER";
     QString aMergeType;
 
+    HYDROData_CalculationCase::DataTag aDataTag = HYDROData_CalculationCase::DataTag_CustomRules;
+
     switch( anIt->MergeType )
     {
     case HYDROData_Zone::Merge_UNKNOWN:
@@ -229,8 +265,8 @@ void HYDROData_PriorityQueue::DumpRulesToPython( const TDF_Label& theRulesLab,
       break;
     }
 
-    QString aRule = QString( "%0.AddRule( %1, %2, %3, %4 )" ).
-      arg( theCalcCaseName ).arg( anObj1 ).arg( aPriority ).arg( anObj2 ).arg( aMergeType );
+    QString aRule = QString( "%0.AddRule( %1, %2, %3, %4, %5 )" ).
+      arg( theCalcCaseName ).arg( anObj1 ).arg( aPriority ).arg( anObj2 ).arg( aMergeType ).arg( aDataTag );
 
     theScript << aRule;
   }
@@ -238,10 +274,10 @@ void HYDROData_PriorityQueue::DumpRulesToPython( const TDF_Label& theRulesLab,
 
 bool HYDROData_PriorityQueue::GetRule( const TDF_Label& theRulesLab,
                                        int theIndex, 
-                                       Handle(HYDROData_Object)&           theObject1,
-                                       HYDROData_PriorityType&             thePriority,
-                                       Handle(HYDROData_Object)&           theObject2,
-                                       HYDROData_Zone::MergeAltitudesType& theMergeType )
+                                       Handle(HYDROData_Entity)&  theObject1,
+                                       HYDROData_PriorityType&    thePriority,
+                                       Handle(HYDROData_Entity)&  theObject2,
+                                       HYDROData_Zone::MergeType& theMergeType )
 {
   TDF_Label aRuleLabel = theRulesLab.FindChild( theIndex );
 
@@ -256,10 +292,10 @@ bool HYDROData_PriorityQueue::GetRule( const TDF_Label& theRulesLab,
   bool isOK = isObj1OK && isPriorityOK && isObj2OK && isMergeOK;
   if( isOK )
   {
-    theObject1   = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
+    theObject1   = HYDROData_Iterator::Object( aRefs1->First() );
     thePriority  = ( HYDROData_PriorityType ) aPriorityAttr->Get();
-    theObject2   = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
-    theMergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();
+    theObject2   = HYDROData_Iterator::Object( aRefs2->First() );
+    theMergeType = ( HYDROData_Zone::MergeType ) aMergeAttr->Get();
   }
   return isOK;
 }