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 TDF_Label aNewRuleLab = theRulesLabel.NewChild();
126 TDF_Label anObj1Lab = aNewRuleLab.FindChild( Object1_Tag );
127 Handle(TDataStd_ReferenceList) aRefs = TDataStd_ReferenceList::Set( anObj1Lab );
128 aRefs->Append( theObject1->Label() );
130 TDF_Label aPriorityLab = aNewRuleLab.FindChild( Priority_Tag );
131 TDataStd_Integer::Set( aPriorityLab, thePriority );
133 TDF_Label anObj2Lab = aNewRuleLab.FindChild( Object2_Tag );
134 aRefs = TDataStd_ReferenceList::Set( anObj2Lab );
135 aRefs->Append( theObject2->Label() );
137 TDF_Label aMergeLab = aNewRuleLab.FindChild( Merge_Tag );
138 TDataStd_Integer::Set( aMergeLab, theMergeType );
141 HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRulesLabel )
143 HYDROData_ListOfRules aRules;
145 Handle(TDataStd_ReferenceList) aRefs1, aRefs2;
146 Handle(TDataStd_Integer) aPriorityAttr, aMergeAttr;
148 TDF_ChildIterator anIt( theRulesLabel );
149 for( ; anIt.More(); anIt.Next() )
151 TDF_Label aRuleLabel = anIt.Value();
153 bool isObj1OK = aRuleLabel.FindChild ( Object1_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs1 );
154 bool isPriorityOK = aRuleLabel.FindChild( Priority_Tag ).FindAttribute( TDataStd_Integer::GetID(), aPriorityAttr );
155 bool isObj2OK = aRuleLabel.FindChild ( Object2_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs2 );
156 bool isMergeOK = aRuleLabel.FindChild ( Merge_Tag ). FindAttribute( TDataStd_Integer::GetID(), aMergeAttr );
158 if( isObj1OK && isPriorityOK && isObj2OK && isMergeOK )
160 HYDROData_CustomRule aRule;
161 aRule.Object1 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
162 aRule.Priority = ( HYDROData_PriorityType ) aPriorityAttr->Get();
163 aRule.Object2 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
164 aRule.MergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();
165 aRules.append( aRule );
172 QString HYDROData_PriorityQueue::DumpRules( const TDF_Label& theRulesLab )
174 QString aDump = "Rules:\n";
175 HYDROData_ListOfRules aRules = GetRules( theRulesLab );
176 HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
177 for( ; anIt!=aLast; anIt++ )
179 QString aRule = anIt->Object1->GetName() + " ";
180 aRule += ( anIt->Priority == LESS ? "<" : ">" ) + QString( " " );
181 aRule += anIt->Object2->GetName() + " ";
183 switch( anIt->MergeType )
185 case HYDROData_Zone::Merge_UNKNOWN:
188 case HYDROData_Zone::Merge_ZMIN:
191 case HYDROData_Zone::Merge_ZMAX:
194 case HYDROData_Zone::Merge_Object:
198 aDump += aRule + "\n";
203 void HYDROData_PriorityQueue::DumpRulesToPython( const TDF_Label& theRulesLab,
204 const QString& theCalcCaseName,
205 QStringList& theScript )
207 HYDROData_ListOfRules aRules = GetRules( theRulesLab );
208 HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
209 for( ; anIt!=aLast; anIt++ )
211 QString anObj1 = anIt->Object1->GetObjPyName();
212 QString anObj2 = anIt->Object2->GetObjPyName();
213 QString aPriority = anIt->Priority == LESS ? "LESS" : "GREATER";
216 switch( anIt->MergeType )
218 case HYDROData_Zone::Merge_UNKNOWN:
219 aMergeType = "HYDROData_Zone.Merge_UNKNOWN";
221 case HYDROData_Zone::Merge_ZMIN:
222 aMergeType = "HYDROData_Zone.Merge_ZMIN";
224 case HYDROData_Zone::Merge_ZMAX:
225 aMergeType = "HYDROData_Zone.Merge_ZMAX";
227 case HYDROData_Zone::Merge_Object:
228 aMergeType = "HYDROData_Zone.Merge_Object";
232 QString aRule = QString( "%0.AddRule( %1, %2, %3, %4 )" ).
233 arg( theCalcCaseName ).arg( anObj1 ).arg( aPriority ).arg( anObj2 ).arg( aMergeType );
239 int HYDROData_PriorityQueue::GetRulesCount( const TDF_Label& theRulesLab )
241 return theRulesLab.NbChildren();
244 bool HYDROData_PriorityQueue::GetRule( const TDF_Label& theRulesLab,
246 Handle(HYDROData_Object)& theObject1,
247 HYDROData_PriorityType& thePriority,
248 Handle(HYDROData_Object)& theObject2,
249 HYDROData_Zone::MergeAltitudesType& theMergeType )
251 TDF_Label aRuleLabel = theRulesLab.FindChild( theIndex );
253 Handle(TDataStd_ReferenceList) aRefs1, aRefs2;
254 Handle(TDataStd_Integer) aPriorityAttr, aMergeAttr;
256 bool isObj1OK = aRuleLabel.FindChild ( Object1_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs1 );
257 bool isPriorityOK = aRuleLabel.FindChild( Priority_Tag ).FindAttribute( TDataStd_Integer::GetID(), aPriorityAttr );
258 bool isObj2OK = aRuleLabel.FindChild ( Object2_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs2 );
259 bool isMergeOK = aRuleLabel.FindChild ( Merge_Tag ). FindAttribute( TDataStd_Integer::GetID(), aMergeAttr );
261 bool isOK = isObj1OK && isPriorityOK && isObj2OK && isMergeOK;
264 theObject1 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
265 thePriority = ( HYDROData_PriorityType ) aPriorityAttr->Get();
266 theObject2 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
267 theMergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();