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