1 // Copyright (C) 2007-2008 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
22 // File : SALOMEDSImpl_UseCaseBuilder.cxx
23 // Author : Sergey RUIN
26 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
27 #include "SALOMEDSImpl_SObject.hxx"
28 #include "SALOMEDSImpl_SComponent.hxx"
29 #include "SALOMEDSImpl_Study.hxx"
30 #include "SALOMEDSImpl_Attributes.hxx"
32 #include "DF_ChildIterator.hxx"
36 #define USE_CASE_LABEL_TAG 2
37 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
40 //============================================================================
41 /*! Function : constructor
44 //============================================================================
45 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
50 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
51 if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
52 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
55 SALOMEDSImpl_AttributeReference* aRef = NULL;
56 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
57 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
60 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) {
61 SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
65 //============================================================================
66 /*! Function : destructor
69 //============================================================================
70 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
75 //============================================================================
79 //============================================================================
80 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
82 if(!_root || !theObject) return false;
84 DF_Label aLabel = theObject.GetLabel();
85 if(aLabel.IsNull()) return false;
87 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
88 SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
89 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
92 SALOMEDSImpl_AttributeReference* aRef;
93 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
94 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
97 DF_Label aCurrent = aRef->Get();
98 if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID())))
101 aCurrentNode->Append(aNode);
106 //============================================================================
107 /*! Function : Remove
110 //============================================================================
111 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
113 if(!_root || !theObject) return false;
115 DF_Label aLabel = theObject.GetLabel();
116 if(aLabel.IsNull()) return false;
118 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
119 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
123 vector<DF_Attribute*> aList;
124 aList.push_back(aNode);
126 SALOMEDSImpl_AttributeReference* aRef = NULL;
127 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
128 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
131 DF_Label aCurrent = aRef->Get();
133 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
134 for(; aChildItr.More(); aChildItr.Next())
135 aList.push_back(aChildItr.Value());
137 for(int i = 0, len = aList.size(); i<len; i++) {
138 if(aList[i]->Label() == aCurrent) { //The current node is removed
139 aRef->Set(_root->Label()); //Reset the current node to the root
141 aList[i]->Label().ForgetAttribute(_root->ID());
148 //============================================================================
149 /*! Function : AppendTo
152 //============================================================================
153 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather,
154 const SALOMEDSImpl_SObject& theObject)
156 if(!_root || !theFather || !theObject) return false;
158 DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
159 if(aFatherLabel == aLabel) return false;
161 SALOMEDSImpl_AttributeTreeNode *aFather = false, *aNode = false;
163 if(aFatherLabel.IsNull()) return false;
164 if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
166 if(aLabel.IsNull()) return false;
167 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
168 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
173 return aFather->Append(aNode);
176 //============================================================================
177 /*! Function : InsertBefore
180 //============================================================================
181 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst,
182 const SALOMEDSImpl_SObject& theNext)
184 if(!_root || !theFirst || !theNext) return false;
186 DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
187 if(aFirstLabel == aLabel) return false;
189 SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
191 if(aFirstLabel.IsNull()) return false;
192 if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
193 aFirstNode->Remove();
194 aFirstLabel.ForgetAttribute(aFirstNode->ID());
197 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
199 if(aLabel.IsNull()) return false;
200 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
202 aFirstNode->Remove();
204 return aNode->InsertBefore(aFirstNode);
208 //============================================================================
209 /*! Function : SetCurrentObject
212 //============================================================================
213 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
215 if(!_root || !theObject) return false;
217 DF_Label aLabel = theObject.GetLabel();
218 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
219 if(aLabel.IsNull()) return false;
220 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
222 SALOMEDSImpl_AttributeReference* aRef = NULL;
223 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
224 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
227 aRef->Set(aNode->Label());
232 //============================================================================
233 /*! Function : SetRootCurrent
236 //============================================================================
237 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
239 if(!_root) return false;
241 SALOMEDSImpl_AttributeReference* aRef = NULL;
242 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
243 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
245 aRef->Set(_root->Label());
249 //============================================================================
250 /*! Function : HasChildren
253 //============================================================================
254 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
256 if(!_root) return false;
259 if (!theObject) aLabel = _root->Label();
261 aLabel = theObject.GetLabel();
262 if(aLabel.IsNull()) return false;
264 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
265 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
267 return (aNode->GetFirst());
270 //============================================================================
271 /*! Function : SetName
274 //============================================================================
275 bool SALOMEDSImpl_UseCaseBuilder::SetName(const string& theName) {
276 if(!_root) return false;
278 SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
280 if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
281 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
283 aNameAttrib->SetValue(theName);
289 //============================================================================
290 /*! Function : GetCurrentObject
293 //============================================================================
294 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
296 SALOMEDSImpl_SObject so;
297 if(!_root) return so;
299 SALOMEDSImpl_AttributeReference* aRef = NULL;
300 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
301 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
304 DF_Label aCurrent = aRef->Get();
305 if(aCurrent.IsNull()) return so;
307 return SALOMEDSImpl_Study::SObject(aCurrent);
310 //============================================================================
311 /*! Function : GetName
314 //============================================================================
315 string SALOMEDSImpl_UseCaseBuilder::GetName()
318 if(!_root) return aString;
320 SALOMEDSImpl_AttributeName* aName = NULL;
321 if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
322 return aName->Value();
325 //============================================================================
326 /*! Function : IsUseCase
329 //============================================================================
330 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
332 if(!theObject) return false;
333 DF_Label aFather, aLabel = theObject.GetLabel();
334 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
335 if(aLabel.Father() == aFather) return true;
339 //============================================================================
340 /*! Function : NewUseCase
343 //============================================================================
344 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const string& theName)
346 string aBasicGUID(USE_CASE_GUID);
348 //Create a use cases structure if it not exists
350 SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
351 SALOMEDSImpl_AttributeInteger* anInteger = NULL;
352 SALOMEDSImpl_AttributeReference* aRef = NULL;
354 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
356 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
357 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
360 if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
361 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
364 if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
365 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
368 //Create a new use case
369 anInteger->SetValue(anInteger->Value()+1);
370 DF_Label aChild = aLabel.FindChild(anInteger->Value());
371 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
373 aFatherNode->Append(aNode);
374 SALOMEDSImpl_AttributeName::Set(aChild, theName);
376 return SALOMEDSImpl_Study::SObject(aChild);
379 //============================================================================
380 /*! Function : GetUseCaseIterator
381 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
383 //============================================================================
384 SALOMEDSImpl_UseCaseIterator
385 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject)
390 aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
393 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
396 return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
400 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const string& theEntry)
402 DF_Label L = DF_Label::Label(_root->Label(), theEntry);
403 return SALOMEDSImpl_Study::SObject(L);