2 #include <HYDROData_PriorityQueue.h>
3 #include <HYDROData_CalculationCase.h>
4 #include <HYDROData_Iterator.h>
5 #include <TDataStd_ReferenceList.hxx>
6 #include <TDataStd_Integer.hxx>
7 #include <TDF_ChildIterator.hxx>
9 HYDROData_PriorityQueue::HYDROData_PriorityQueue( HYDROData_CalculationCase* theCalcCase )
11 myGeomObjects = theCalcCase->GetGeometryObjects();
12 for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
14 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( myGeomObjects.Value( i ) );
17 QString anObjName = anObj->GetName();
18 myNames[anObjName] = anObj;
22 myRules = GetRules( theCalcCase->Label().FindChild( HYDROData_CalculationCase::DataTag_CustomRules ) );
25 HYDROData_PriorityQueue::~HYDROData_PriorityQueue()
29 Handle(HYDROData_Object) HYDROData_PriorityQueue::GetMostPriorityObject( const QStringList& theZoneObjects,
30 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
32 QStringList aSortedZoneObjects;
33 for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
35 QString aName = myGeomObjects.Value( i )->GetName();
36 if( theZoneObjects.contains( aName ) )
37 aSortedZoneObjects.append( aName );
40 Handle(HYDROData_Object) aMostPriorityObj;
41 theMergeType = HYDROData_Zone::Merge_UNKNOWN;
42 QStringList::const_iterator anIt = aSortedZoneObjects.begin(), aLast = aSortedZoneObjects.end();
43 for( ; anIt!=aLast; anIt++ )
45 HYDROData_Zone::MergeAltitudesType aLocalMerge = HYDROData_Zone::Merge_UNKNOWN;
46 Handle(HYDROData_Object) anObj = myNames[*anIt];
49 if( aMostPriorityObj.IsNull() )
51 aMostPriorityObj = anObj;
55 bool isMorePriority = IsMorePriority( anObj, aMostPriorityObj, aLocalMerge );
58 aMostPriorityObj = anObj;
60 if( aLocalMerge != HYDROData_Zone::Merge_UNKNOWN &&
61 ( theMergeType==HYDROData_Zone::Merge_UNKNOWN || isMorePriority ) )
62 theMergeType = aLocalMerge;
65 return aMostPriorityObj;
68 bool HYDROData_PriorityQueue::IsMorePriority( const Handle(HYDROData_Object)& theObj1,
69 const Handle(HYDROData_Object)& theObj2,
70 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
72 // 1. First we check custom rules
73 HYDROData_ListOfRules::const_iterator anIt = myRules.begin(), aLast = myRules.end();
74 for( ; anIt!=aLast; anIt++ )
76 if( anIt->Object1->Label()==theObj1->Label() && anIt->Object2->Label()==theObj2->Label() )
78 theMergeType = anIt->MergeType;
79 return anIt->Priority==GREATER;
81 if( anIt->Object1->Label()==theObj2->Label() && anIt->Object2->Label()==theObj1->Label() )
83 theMergeType = anIt->MergeType;
84 return anIt->Priority==LESS;
88 // 2. If no custom rule found, the standard ordering list is applied
89 for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
91 if( myGeomObjects.Value( i )->Label() == theObj1->Label() )
93 theMergeType = HYDROData_Zone::Merge_Object;
96 if( myGeomObjects.Value( i )->Label() == theObj2->Label() )
98 theMergeType = HYDROData_Zone::Merge_Object;
105 void HYDROData_PriorityQueue::ClearRules( TDF_Label& theRulesLabel )
107 theRulesLabel.ForgetAllAttributes( true );
110 enum HYDROData_PriorityQueueTag
118 void HYDROData_PriorityQueue::AddRule( TDF_Label& theRulesLabel,
119 const Handle(HYDROData_Object)& theObject1,
120 HYDROData_PriorityType thePriority,
121 const Handle(HYDROData_Object)& theObject2,
122 HYDROData_Zone::MergeAltitudesType theMergeType )
124 // Get the last rule index
125 Standard_Integer aRuleIndex = 0;
126 Handle(TDataStd_Integer) anIntVal;
127 if ( theRulesLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) ) {
128 aRuleIndex = anIntVal->Get();
131 TDF_Label aNewRuleLab = theRulesLabel.FindChild( aRuleIndex );
133 TDF_Label anObj1Lab = aNewRuleLab.FindChild( Object1_Tag );
134 Handle(TDataStd_ReferenceList) aRefs = TDataStd_ReferenceList::Set( anObj1Lab );
135 aRefs->Append( theObject1->Label() );
137 TDF_Label aPriorityLab = aNewRuleLab.FindChild( Priority_Tag );
138 TDataStd_Integer::Set( aPriorityLab, thePriority );
140 TDF_Label anObj2Lab = aNewRuleLab.FindChild( Object2_Tag );
141 aRefs = TDataStd_ReferenceList::Set( anObj2Lab );
142 aRefs->Append( theObject2->Label() );
144 TDF_Label aMergeLab = aNewRuleLab.FindChild( Merge_Tag );
145 TDataStd_Integer::Set( aMergeLab, theMergeType );
147 // Increment the last rule index
148 TDataStd_Integer::Set( theRulesLabel, aRuleIndex + 1 );
151 HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRulesLabel )
153 HYDROData_ListOfRules aRules;
155 Handle(TDataStd_ReferenceList) aRefs1, aRefs2;
156 Handle(TDataStd_Integer) aPriorityAttr, aMergeAttr;
158 TDF_ChildIterator anIt( theRulesLabel );
159 for( ; anIt.More(); anIt.Next() )
161 TDF_Label aRuleLabel = anIt.Value();
163 bool isObj1OK = aRuleLabel.FindChild ( Object1_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs1 );
164 bool isPriorityOK = aRuleLabel.FindChild( Priority_Tag ).FindAttribute( TDataStd_Integer::GetID(), aPriorityAttr );
165 bool isObj2OK = aRuleLabel.FindChild ( Object2_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs2 );
166 bool isMergeOK = aRuleLabel.FindChild ( Merge_Tag ). FindAttribute( TDataStd_Integer::GetID(), aMergeAttr );
168 if( isObj1OK && isPriorityOK && isObj2OK && isMergeOK )
170 HYDROData_CustomRule aRule;
171 aRule.Object1 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
172 aRule.Priority = ( HYDROData_PriorityType ) aPriorityAttr->Get();
173 aRule.Object2 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
174 aRule.MergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();
175 aRules.append( aRule );
182 QString HYDROData_PriorityQueue::DumpRules( const TDF_Label& theRulesLab )
184 QString aDump = "Rules:\n";
185 HYDROData_ListOfRules aRules = GetRules( theRulesLab );
186 HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
187 for( ; anIt!=aLast; anIt++ )
189 QString aRule = anIt->Object1->GetName() + " ";
190 aRule += ( anIt->Priority == LESS ? "<" : ">" ) + QString( " " );
191 aRule += anIt->Object2->GetName() + " ";
193 switch( anIt->MergeType )
195 case HYDROData_Zone::Merge_UNKNOWN:
198 case HYDROData_Zone::Merge_ZMIN:
201 case HYDROData_Zone::Merge_ZMAX:
204 case HYDROData_Zone::Merge_Object:
208 aDump += aRule + "\n";
213 void HYDROData_PriorityQueue::DumpRulesToPython( const TDF_Label& theRulesLab,
214 const QString& theCalcCaseName,
215 QStringList& theScript )
217 HYDROData_ListOfRules aRules = GetRules( theRulesLab );
218 HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
219 for( ; anIt!=aLast; anIt++ )
221 QString anObj1 = anIt->Object1->GetObjPyName();
222 QString anObj2 = anIt->Object2->GetObjPyName();
223 QString aPriority = anIt->Priority == LESS ? "LESS" : "GREATER";
226 switch( anIt->MergeType )
228 case HYDROData_Zone::Merge_UNKNOWN:
229 aMergeType = "HYDROData_Zone.Merge_UNKNOWN";
231 case HYDROData_Zone::Merge_ZMIN:
232 aMergeType = "HYDROData_Zone.Merge_ZMIN";
234 case HYDROData_Zone::Merge_ZMAX:
235 aMergeType = "HYDROData_Zone.Merge_ZMAX";
237 case HYDROData_Zone::Merge_Object:
238 aMergeType = "HYDROData_Zone.Merge_Object";
242 QString aRule = QString( "%0.AddRule( %1, %2, %3, %4 )" ).
243 arg( theCalcCaseName ).arg( anObj1 ).arg( aPriority ).arg( anObj2 ).arg( aMergeType );
249 bool HYDROData_PriorityQueue::GetRule( const TDF_Label& theRulesLab,
251 Handle(HYDROData_Object)& theObject1,
252 HYDROData_PriorityType& thePriority,
253 Handle(HYDROData_Object)& theObject2,
254 HYDROData_Zone::MergeAltitudesType& theMergeType )
256 TDF_Label aRuleLabel = theRulesLab.FindChild( theIndex );
258 Handle(TDataStd_ReferenceList) aRefs1, aRefs2;
259 Handle(TDataStd_Integer) aPriorityAttr, aMergeAttr;
261 bool isObj1OK = aRuleLabel.FindChild ( Object1_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs1 );
262 bool isPriorityOK = aRuleLabel.FindChild( Priority_Tag ).FindAttribute( TDataStd_Integer::GetID(), aPriorityAttr );
263 bool isObj2OK = aRuleLabel.FindChild ( Object2_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs2 );
264 bool isMergeOK = aRuleLabel.FindChild ( Merge_Tag ). FindAttribute( TDataStd_Integer::GetID(), aMergeAttr );
266 bool isOK = isObj1OK && isPriorityOK && isObj2OK && isMergeOK;
269 theObject1 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
270 thePriority = ( HYDROData_PriorityType ) aPriorityAttr->Get();
271 theObject2 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
272 theMergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();