Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_UseCaseBuilder.cxx
1 //  File   : SALOMEDSImpl_UseCaseBuilder.cxx
2 //  Author : Sergey RUIN
3 //  Module : SALOME
4
5
6 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
7 #include "SALOMEDSImpl_SObject.hxx"
8 #include "SALOMEDSImpl_SComponent.hxx"
9 #include "SALOMEDSImpl_Study.hxx"
10 #include "SALOMEDSImpl_Attributes.hxx"
11
12 #include <TDF_Label.hxx>
13 #include <TDF_Tool.hxx>
14 #include <TDF_Data.hxx>
15 #include <TDF_AttributeList.hxx>
16 #include <TDF_ListIteratorOfAttributeList.hxx>
17 #include <TCollection_AsciiString.hxx>
18 #include <TDF_ChildIterator.hxx>
19
20 using namespace std;
21
22 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
23 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
24
25
26 #define USE_CASE_LABEL_TAG           2
27 #define USE_CASE_GUID                "AA43BB12-D9CD-11d6-945D-0050DA506788"
28
29
30 //============================================================================
31 /*! Function : constructor
32  *  Purpose  :
33  */
34 //============================================================================
35 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(const Handle(TDocStd_Document)& theDocument)
36 :_doc(theDocument)
37 {
38   if(_doc.IsNull()) return;
39   
40   TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
41   if(!aLabel.FindAttribute(Standard_GUID(USE_CASE_GUID), _root)) {
42     _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, Standard_GUID(USE_CASE_GUID));
43   }
44  
45   Handle(SALOMEDSImpl_AttributeReference) aRef;
46   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
47     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
48   }  
49
50   Handle(SALOMEDSImpl_AttributeName) aNameAttr;
51   if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttr)) { 
52     aNameAttr = SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases"); 
53   }  
54 }
55
56 //============================================================================
57 /*! Function : destructor
58  *  Purpose  :
59  */
60 //============================================================================
61 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
62 {
63 }
64
65
66 //============================================================================
67 /*! Function : Append
68  *  Purpose  : 
69  */
70 //============================================================================
71 bool SALOMEDSImpl_UseCaseBuilder::Append(const Handle(SALOMEDSImpl_SObject)& theObject)
72 {
73   if(_root.IsNull() || theObject.IsNull()) return false;
74
75   TDF_Label aLabel = theObject->GetLabel();
76   if(aLabel.IsNull()) return false;
77
78   Handle(SALOMEDSImpl_AttributeTreeNode) aNode, aCurrentNode;
79   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
80   aNode->Remove();
81
82   Handle(SALOMEDSImpl_AttributeReference) aRef;
83   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
84     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
85   }  
86
87   TDF_Label aCurrent = aRef->Get();
88   if(aCurrent.IsNull() || !aCurrent.FindAttribute(_root->ID(), aCurrentNode)) 
89     aCurrentNode = _root;
90
91   aCurrentNode->Append(aNode);
92
93   return true;
94 }
95
96  //============================================================================
97 /*! Function : Remove
98  *  Purpose  :
99  */
100 //============================================================================
101 bool SALOMEDSImpl_UseCaseBuilder::Remove(const Handle(SALOMEDSImpl_SObject)& theObject)
102 {
103   if(_root.IsNull() || theObject.IsNull()) return false;
104
105   TDF_Label aLabel = theObject->GetLabel();   
106   if(aLabel.IsNull()) return false;
107
108   Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
109   if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
110
111   aNode->Remove();
112
113   TDF_AttributeList aList;
114   aList.Append(aNode);
115
116   Handle(SALOMEDSImpl_AttributeReference) aRef;
117   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
118     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
119   }  
120   TDF_Label aCurrent = aRef->Get();
121
122   SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, Standard_True);
123   for(; aChildItr.More(); aChildItr.Next()) 
124     aList.Append(aChildItr.Value());
125
126   TDF_ListIteratorOfAttributeList anIterator(aList);
127   for(; anIterator.More(); anIterator.Next()) {
128     if(anIterator.Value()->Label() ==  aCurrent) { //The current node is removed
129       aRef->Set(_root->Label()); //Reset the current node to the root
130     }
131     anIterator.Value()->Label().ForgetAttribute(_root->ID());
132   }
133
134   return true;
135 }
136
137
138 //============================================================================
139 /*! Function : AppendTo
140  *  Purpose  :
141  */
142 //============================================================================
143 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const Handle(SALOMEDSImpl_SObject)& theFather, 
144                                            const Handle(SALOMEDSImpl_SObject)& theObject)
145 {
146   if(_root.IsNull() || theFather.IsNull() || theObject.IsNull()) return false;
147
148   TDF_Label aFatherLabel = theFather->GetLabel(), aLabel = theObject->GetLabel();
149   Handle(SALOMEDSImpl_AttributeTreeNode) aFather, aNode;
150   
151   if(aFatherLabel.IsNull()) return false;
152   if(!aFatherLabel.FindAttribute(_root->ID(), aFather)) return false;
153
154   if(aLabel.IsNull()) return false;
155   if(!aLabel.FindAttribute(_root->ID(), aNode)) {
156     aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
157   }
158
159   aNode->Remove();
160
161   return aFather->Append(aNode);
162 }
163
164 //============================================================================
165 /*! Function : InsertBefore
166  *  Purpose  :
167  */
168 //============================================================================
169 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const Handle(SALOMEDSImpl_SObject)& theFirst, 
170                                                const Handle(SALOMEDSImpl_SObject)& theNext)
171 {
172   if(_root.IsNull() || theFirst.IsNull() || theNext.IsNull()) return false;
173
174   TDF_Label aFirstLabel = theFirst->GetLabel(), aLabel= theNext->GetLabel();
175   Handle(SALOMEDSImpl_AttributeTreeNode) aFirstNode, aNode;
176   
177   if(aFirstLabel.IsNull()) return false;
178   if(aFirstLabel.FindAttribute(_root->ID(), aFirstNode)) {
179     aFirstNode->Remove();
180     aFirstLabel.ForgetAttribute(aFirstNode->ID());
181   }
182
183   aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
184   
185   if(aLabel.IsNull()) return false;
186   if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
187
188   aFirstNode->Remove();
189
190   return aNode->InsertBefore(aFirstNode);
191 }
192
193
194 //============================================================================
195 /*! Function : SetCurrentObject
196  *  Purpose  :
197  */
198 //============================================================================
199 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const Handle(SALOMEDSImpl_SObject)& theObject)
200 {
201   if(_root.IsNull() || theObject.IsNull()) return false;
202
203   TDF_Label aLabel = theObject->GetLabel();
204   Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
205   if(aLabel.IsNull()) return false;
206   if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
207
208
209   Handle(SALOMEDSImpl_AttributeReference) aRef;
210   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
211     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());  
212   }
213   
214   aRef->Set(aNode->Label());
215
216   return true;
217 }
218
219 //============================================================================
220 /*! Function : SetRootCurrent
221  *  Purpose  :
222  */
223 //============================================================================
224 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
225 {
226   if(_root.IsNull()) return false;
227    
228   Handle(SALOMEDSImpl_AttributeReference) aRef;
229   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) 
230     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
231
232   aRef->Set(_root->Label());
233   return true;
234 }
235
236 //============================================================================
237 /*! Function : HasChildren
238  *  Purpose  :
239  */
240 //============================================================================
241 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const Handle(SALOMEDSImpl_SObject)& theObject)
242 {
243   if(_root.IsNull()) return false;
244
245   TDF_Label aLabel;
246   if (theObject.IsNull()) aLabel = _root->Label();
247   else 
248     aLabel = theObject->GetLabel(); 
249   if(aLabel.IsNull()) return false;
250
251   Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
252   if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
253
254   return !(aNode->GetFirst().IsNull());
255 }
256
257 //============================================================================
258 /*! Function : SetName
259  *  Purpose  :
260  */
261 //============================================================================
262 bool SALOMEDSImpl_UseCaseBuilder::SetName(const TCollection_AsciiString& theName) {
263   if(_root.IsNull()) return false;
264
265   Handle(SALOMEDSImpl_AttributeName) aNameAttrib;
266
267   if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttrib))
268     aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
269   else    
270     aNameAttrib->SetValue(theName);
271     
272   return true;
273 }
274
275
276 //============================================================================
277 /*! Function : GetCurrentObject
278  *  Purpose  :
279  */
280 //============================================================================
281 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
282 {
283   if(_root.IsNull()) return NULL;
284
285   Handle(SALOMEDSImpl_AttributeReference) aRef;
286   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
287     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
288   }  
289   TDF_Label aCurrent = aRef->Get();  
290   if(aCurrent.IsNull()) return NULL;
291
292   return SALOMEDSImpl_Study::SObject(aCurrent);
293 }
294
295 //============================================================================
296 /*! Function : GetName
297  *  Purpose  :
298  */
299 //============================================================================
300 TCollection_AsciiString SALOMEDSImpl_UseCaseBuilder::GetName() 
301 {
302   TCollection_AsciiString aString;
303   if(_root.IsNull()) return aString;
304
305   Handle(SALOMEDSImpl_AttributeName) aName;
306   if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aName)) return aString;
307   aString = TCollection_AsciiString(aName->Value());
308   return aString;
309 }
310
311 //============================================================================ 
312 /*! Function :  IsUseCase
313  *  Purpose  :  
314  */ 
315 //============================================================================ 
316 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const Handle(SALOMEDSImpl_SObject)& theObject)
317 {
318   if(theObject.IsNull()) return false;
319   TDF_Label aFather, aLabel = theObject->GetLabel(); 
320   aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
321   if(aLabel.Father() == aFather) return true;
322   return false;
323 }
324
325 //============================================================================ 
326 /*! Function : NewUseCase 
327  *  Purpose  :  
328  */ 
329 //============================================================================ 
330 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::AddUseCase(const TCollection_AsciiString& theName)
331 {
332   Standard_GUID aBasicGUID(USE_CASE_GUID);
333
334   //Create a use cases structure if it not exists 
335
336   Handle(SALOMEDSImpl_AttributeTreeNode) aFatherNode, aNode;
337   Handle(SALOMEDSImpl_AttributeInteger) anInteger;
338   Handle(SALOMEDSImpl_AttributeReference) aRef;
339
340   TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
341
342   if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
343     aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
344   }
345  
346   if(!aRef->Get().FindAttribute(aBasicGUID, aFatherNode)) {
347     aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
348   }
349
350   if(!_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID(), anInteger)) {
351     anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
352   }
353
354   //Create a new use case
355   anInteger->SetValue(anInteger->Value()+1);
356   TDF_Label aChild = aLabel.FindChild(anInteger->Value());
357   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
358   aNode->Remove();
359   aFatherNode->Append(aNode);
360   SALOMEDSImpl_AttributeName::Set(aChild, theName);
361
362   return SALOMEDSImpl_Study::SObject(aChild);
363 }
364
365 //============================================================================
366 /*! Function : GetUseCaseIterator
367  *  Purpose  : Creates a new UseCase iterator, if anObject is null all use cases are iterated 
368  */
369 //============================================================================
370 Handle(SALOMEDSImpl_UseCaseIterator) 
371 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const Handle(SALOMEDSImpl_SObject)& theObject) 
372 {
373   TDF_Label aLabel;
374
375   if(!theObject.IsNull()) {
376     aLabel = theObject->GetLabel(); //Iterate only sub tree in the use case
377   }
378   else {
379     aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
380   }
381
382   return new SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false); 
383 }
384
385
386 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetSObject(const TCollection_AsciiString& theEntry)
387 {
388    TDF_Label aLabel;    
389    TDF_Tool::Label(_doc->GetData(), theEntry, aLabel);
390    return SALOMEDSImpl_Study::SObject(aLabel);    
391 }