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 Handle(HYDROData_Object) aMostPriorityObj;
33 theMergeType = HYDROData_Zone::Merge_Object;
34 QStringList::const_iterator anIt = theZoneObjects.begin(), aLast = theZoneObjects.end();
35 for( ; anIt!=aLast; anIt++ )
37 HYDROData_Zone::MergeAltitudesType aLocalMerge = HYDROData_Zone::Merge_UNKNOWN;
38 Handle(HYDROData_Object) anObj = myNames[*anIt];
40 if( aMostPriorityObj.IsNull() || IsMorePriority( anObj, aMostPriorityObj, aLocalMerge ) )
42 aMostPriorityObj = anObj;
43 theMergeType = aLocalMerge;
46 return aMostPriorityObj;
49 bool HYDROData_PriorityQueue::IsMorePriority( const Handle(HYDROData_Object)& theObj1,
50 const Handle(HYDROData_Object)& theObj2,
51 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
53 // 1. First we check custom rules
54 HYDROData_ListOfRules::const_iterator anIt = myRules.begin(), aLast = myRules.end();
55 for( ; anIt!=aLast; anIt++ )
57 if( anIt->Object1->Label()==theObj1->Label() && anIt->Object2->Label()==theObj2->Label() )
59 theMergeType = anIt->MergeType;
60 return anIt->Priority==GREATER;
62 if( anIt->Object1->Label()==theObj2->Label() && anIt->Object2->Label()==theObj1->Label() )
64 theMergeType = anIt->MergeType;
65 return anIt->Priority==LESS;
69 // 2. If no custom rule found, the standard ordering list is applied
70 for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
72 if( myGeomObjects.Value( i )->Label() == theObj1->Label() )
74 theMergeType = HYDROData_Zone::Merge_Object;
77 if( myGeomObjects.Value( i )->Label() == theObj2->Label() )
79 theMergeType = HYDROData_Zone::Merge_Object;
86 void HYDROData_PriorityQueue::ClearRules( TDF_Label& theRulesLabel )
88 theRulesLabel.ForgetAllAttributes( true );
91 enum HYDROData_PriorityQueueTag
99 void HYDROData_PriorityQueue::AddRule( TDF_Label& theRulesLabel,
100 const Handle(HYDROData_Object)& theObject1,
101 HYDROData_PriorityType thePriority,
102 const Handle(HYDROData_Object)& theObject2,
103 HYDROData_Zone::MergeAltitudesType theMergeType )
105 TDF_Label aNewRuleLab = theRulesLabel.NewChild();
107 TDF_Label anObj1Lab = aNewRuleLab.FindChild( Object1_Tag );
108 Handle(TDataStd_ReferenceList) aRefs = TDataStd_ReferenceList::Set( anObj1Lab );
109 aRefs->Append( theObject1->Label() );
111 TDF_Label aPriorityLab = aNewRuleLab.FindChild( Priority_Tag );
112 TDataStd_Integer::Set( aPriorityLab, thePriority );
114 TDF_Label anObj2Lab = aNewRuleLab.FindChild( Object2_Tag );
115 aRefs = TDataStd_ReferenceList::Set( anObj2Lab );
116 aRefs->Append( theObject2->Label() );
118 TDF_Label aMergeLab = aNewRuleLab.FindChild( Merge_Tag );
119 TDataStd_Integer::Set( aMergeLab, theMergeType );
122 HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRulesLabel )
124 HYDROData_ListOfRules aRules;
126 Handle(TDataStd_ReferenceList) aRefs1, aRefs2;
127 Handle(TDataStd_Integer) aPriorityAttr, aMergeAttr;
129 TDF_ChildIterator anIt( theRulesLabel );
130 for( ; anIt.More(); anIt.Next() )
132 TDF_Label aRuleLabel = anIt.Value();
134 bool isObj1OK = aRuleLabel.FindChild ( Object1_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs1 );
135 bool isPriorityOK = aRuleLabel.FindChild( Priority_Tag ).FindAttribute( TDataStd_Integer::GetID(), aPriorityAttr );
136 bool isObj2OK = aRuleLabel.FindChild ( Object2_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs2 );
137 bool isMergeOK = aRuleLabel.FindChild ( Merge_Tag ). FindAttribute( TDataStd_Integer::GetID(), aMergeAttr );
139 if( isObj1OK && isPriorityOK && isObj2OK && isMergeOK )
141 HYDROData_CustomRule aRule;
142 aRule.Object1 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
143 aRule.Priority = ( HYDROData_PriorityType ) aPriorityAttr->Get();
144 aRule.Object2 = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
145 aRule.MergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();
146 aRules.append( aRule );
153 QString HYDROData_PriorityQueue::DumpRules( const TDF_Label& theRulesLab )
155 QString aDump = "Rules:\n";
156 HYDROData_ListOfRules aRules = GetRules( theRulesLab );
157 HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
158 for( ; anIt!=aLast; anIt++ )
160 QString aRule = anIt->Object1->GetName() + " ";
161 aRule += ( anIt->Priority == LESS ? "<" : ">" ) + QString( " " );
162 aRule += anIt->Object2->GetName() + " ";
164 switch( anIt->MergeType )
166 case HYDROData_Zone::Merge_UNKNOWN:
169 case HYDROData_Zone::Merge_ZMIN:
172 case HYDROData_Zone::Merge_ZMAX:
175 case HYDROData_Zone::Merge_Object:
179 aDump += aRule + "\n";