1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_UseCaseBuilder.cxx
24 // Author : Sergey RUIN
27 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
28 #include "SALOMEDSImpl_SObject.hxx"
29 #include "SALOMEDSImpl_SComponent.hxx"
30 #include "SALOMEDSImpl_Study.hxx"
31 #include "SALOMEDSImpl_Attributes.hxx"
33 #include "DF_ChildIterator.hxx"
35 #define USE_CASE_LABEL_TAG 2
36 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
39 //============================================================================
40 /*! Function : constructor
43 //============================================================================
44 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
49 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
50 if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
51 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
54 SALOMEDSImpl_AttributeReference* aRef = NULL;
55 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
56 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
59 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) {
60 SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
64 //============================================================================
65 /*! Function : destructor
68 //============================================================================
69 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
74 //============================================================================
78 //============================================================================
79 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
81 if(!_root || !theObject) return false;
83 DF_Label aLabel = theObject.GetLabel();
84 if(aLabel.IsNull()) return false;
86 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
87 SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
88 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
91 SALOMEDSImpl_AttributeReference* aRef;
92 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
93 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
96 DF_Label aCurrent = aRef->Get();
97 if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID())))
100 aCurrentNode->Append(aNode);
105 //============================================================================
106 /*! Function : Remove
109 //============================================================================
110 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
112 if(!_root || !theObject) return false;
114 DF_Label aLabel = theObject.GetLabel();
115 if(aLabel.IsNull()) return false;
117 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
118 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
122 std::vector<DF_Attribute*> aList;
123 aList.push_back(aNode);
125 SALOMEDSImpl_AttributeReference* aRef = NULL;
126 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
127 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
130 DF_Label aCurrent = aRef->Get();
132 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
133 for(; aChildItr.More(); aChildItr.Next())
134 aList.push_back(aChildItr.Value());
136 for(int i = 0, len = aList.size(); i<len; i++) {
137 if(aList[i]->Label() == aCurrent) { //The current node is removed
138 aRef->Set(_root->Label()); //Reset the current node to the root
140 aList[i]->Label().ForgetAttribute(_root->ID());
147 //============================================================================
148 /*! Function : AppendTo
151 //============================================================================
152 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather,
153 const SALOMEDSImpl_SObject& theObject)
155 if(!_root || !theFather || !theObject) return false;
157 DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
158 if(aFatherLabel == aLabel) return false;
160 SALOMEDSImpl_AttributeTreeNode *aFather = false, *aNode = false;
162 if(aFatherLabel.IsNull()) return false;
163 if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
165 if(aLabel.IsNull()) return false;
166 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
167 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
172 return aFather->Append(aNode);
175 //============================================================================
176 /*! Function : InsertBefore
179 //============================================================================
180 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst,
181 const SALOMEDSImpl_SObject& theNext)
183 if(!_root || !theFirst || !theNext) return false;
185 DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
186 if(aFirstLabel == aLabel) return false;
188 SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
190 if(aFirstLabel.IsNull()) return false;
191 if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
192 aFirstNode->Remove();
193 aFirstLabel.ForgetAttribute(aFirstNode->ID());
196 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
198 if(aLabel.IsNull()) return false;
199 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
201 aFirstNode->Remove();
203 return aNode->InsertBefore(aFirstNode);
207 //============================================================================
208 /*! Function : SetCurrentObject
211 //============================================================================
212 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
214 if(!_root || !theObject) return false;
216 DF_Label aLabel = theObject.GetLabel();
217 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
218 if(aLabel.IsNull()) return false;
219 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
221 SALOMEDSImpl_AttributeReference* aRef = NULL;
222 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
223 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
226 aRef->Set(aNode->Label());
231 //============================================================================
232 /*! Function : SetRootCurrent
235 //============================================================================
236 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
238 if(!_root) return false;
240 SALOMEDSImpl_AttributeReference* aRef = NULL;
241 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
242 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
244 aRef->Set(_root->Label());
248 //============================================================================
249 /*! Function : HasChildren
252 //============================================================================
253 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
255 if(!_root) return false;
258 if (!theObject) aLabel = _root->Label();
260 aLabel = theObject.GetLabel();
261 if(aLabel.IsNull()) return false;
263 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
264 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
266 return (aNode->GetFirst());
269 //============================================================================
270 /*! Function : SetName
273 //============================================================================
274 bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
275 if(!_root) return false;
277 SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
279 if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
280 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
282 aNameAttrib->SetValue(theName);
288 //============================================================================
289 /*! Function : GetCurrentObject
292 //============================================================================
293 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
295 SALOMEDSImpl_SObject so;
296 if(!_root) return so;
298 SALOMEDSImpl_AttributeReference* aRef = NULL;
299 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
300 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
303 DF_Label aCurrent = aRef->Get();
304 if(aCurrent.IsNull()) return so;
306 return SALOMEDSImpl_Study::SObject(aCurrent);
309 //============================================================================
310 /*! Function : GetName
313 //============================================================================
314 std::string SALOMEDSImpl_UseCaseBuilder::GetName()
317 if(!_root) return aString;
319 SALOMEDSImpl_AttributeName* aName = NULL;
320 if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
321 return aName->Value();
324 //============================================================================
325 /*! Function : IsUseCase
328 //============================================================================
329 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
331 if(!theObject) return false;
332 DF_Label aFather, aLabel = theObject.GetLabel();
333 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
334 if(aLabel.Father() == aFather) return true;
338 //============================================================================
339 /*! Function : NewUseCase
342 //============================================================================
343 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
345 std::string aBasicGUID(USE_CASE_GUID);
347 //Create a use cases structure if it not exists
349 SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
350 SALOMEDSImpl_AttributeInteger* anInteger = NULL;
351 SALOMEDSImpl_AttributeReference* aRef = NULL;
353 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
355 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
356 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
359 if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
360 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
363 if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
364 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
367 //Create a new use case
368 anInteger->SetValue(anInteger->Value()+1);
369 DF_Label aChild = aLabel.FindChild(anInteger->Value());
370 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
372 aFatherNode->Append(aNode);
373 SALOMEDSImpl_AttributeName::Set(aChild, theName);
375 return SALOMEDSImpl_Study::SObject(aChild);
378 //============================================================================
379 /*! Function : GetUseCaseIterator
380 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
382 //============================================================================
383 SALOMEDSImpl_UseCaseIterator
384 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject)
389 aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
392 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
395 return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
399 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
401 DF_Label L = DF_Label::Label(_root->Label(), theEntry);
402 return SALOMEDSImpl_Study::SObject(L);