1 // Copyright (C) 2007-2013 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"
37 #define USE_CASE_LABEL_TAG 2
38 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
41 // comparator to sort use case nodes in ascending order
43 bool operator()( SALOMEDSImpl_SObject firstSO, SALOMEDSImpl_SObject secondSO ) const {
44 std::string firstName, secondName;
45 SALOMEDSImpl_SObject refSO;
46 firstSO.ReferencedObject(refSO) ?
47 firstName = refSO.GetName() :
48 firstName = firstSO.GetName();
49 secondSO.ReferencedObject(refSO) ?
50 secondName = refSO.GetName() :
51 secondName = secondSO.GetName();
52 return firstName < secondName;
56 // comparator to sort use case nodes in descending order
58 bool operator()( SALOMEDSImpl_SObject firstSO, SALOMEDSImpl_SObject secondSO ) const {
59 std::string firstName, secondName;
60 SALOMEDSImpl_SObject refSO;
61 firstSO.ReferencedObject(refSO) ?
62 firstName = refSO.GetName() :
63 firstName = firstSO.GetName();
64 secondSO.ReferencedObject(refSO) ?
65 secondName = refSO.GetName() :
66 secondName = secondSO.GetName();
67 return firstName > secondName;
72 //============================================================================
73 /*! Function : constructor
76 //============================================================================
77 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
82 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
83 if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
84 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
87 SALOMEDSImpl_AttributeReference* aRef = NULL;
88 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
89 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
92 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) {
93 SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
97 //============================================================================
98 /*! Function : destructor
101 //============================================================================
102 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
107 //============================================================================
108 /*! Function : Append
111 //============================================================================
112 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
114 if(!_root || !theObject) return false;
116 DF_Label aLabel = theObject.GetLabel();
117 if(aLabel.IsNull()) return false;
119 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
120 SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
121 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
124 SALOMEDSImpl_AttributeReference* aRef;
125 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
126 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
129 DF_Label aCurrent = aRef->Get();
130 if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID())))
131 aCurrentNode = _root;
133 aCurrentNode->Append(aNode);
135 // Mantis issue 0020136: Drag&Drop in OB
136 theObject.GetStudy()->addSO_Notification(theObject);
141 //============================================================================
142 /*! Function : Remove
145 //============================================================================
146 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
148 if(!_root || !theObject) return false;
150 DF_Label aLabel = theObject.GetLabel();
151 if(aLabel.IsNull()) return false;
153 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
154 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
158 std::vector<DF_Attribute*> aList;
159 aList.push_back(aNode);
161 SALOMEDSImpl_AttributeReference* aRef = NULL;
162 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
163 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
166 DF_Label aCurrent = aRef->Get();
168 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
169 for(; aChildItr.More(); aChildItr.Next())
170 aList.push_back(aChildItr.Value());
172 for(int i = 0, len = aList.size(); i<len; i++) {
173 if(aList[i]->Label() == aCurrent) { //The current node is removed
174 aRef->Set(_root->Label()); //Reset the current node to the root
176 aList[i]->Label().ForgetAttribute(_root->ID());
183 //============================================================================
184 /*! Function : AppendTo
187 //============================================================================
188 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather,
189 const SALOMEDSImpl_SObject& theObject)
191 if(!_root || !theFather || !theObject) return false;
193 DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
194 if(aFatherLabel == aLabel) return false;
196 SALOMEDSImpl_AttributeTreeNode *aFather = NULL, *aNode = NULL;
198 if(aFatherLabel.IsNull()) return false;
199 if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
201 if(aLabel.IsNull()) return false;
202 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
203 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
208 bool ret = aFather->Append(aNode);
210 // Mantis issue 0020136: Drag&Drop in OB
211 theObject.GetStudy()->addSO_Notification(theObject);
216 //============================================================================
217 /*! Function : InsertBefore
220 //============================================================================
221 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst,
222 const SALOMEDSImpl_SObject& theNext)
224 if(!_root || !theFirst || !theNext) return false;
226 DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
227 if(aFirstLabel == aLabel) return false;
229 SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
231 if(aFirstLabel.IsNull()) return false;
232 if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
233 aFirstNode->Remove();
234 aFirstLabel.ForgetAttribute(aFirstNode->ID());
237 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
239 if(aLabel.IsNull()) return false;
240 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
242 aFirstNode->Remove();
244 bool ret = aNode->InsertBefore(aFirstNode);
246 // Mantis issue 0020136: Drag&Drop in OB
247 theFirst.GetStudy()->addSO_Notification(theFirst);
253 //============================================================================
254 /*! Function : SetCurrentObject
257 //============================================================================
258 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
260 if(!_root || !theObject) return false;
262 DF_Label aLabel = theObject.GetLabel();
263 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
264 if(aLabel.IsNull()) return false;
265 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
267 SALOMEDSImpl_AttributeReference* aRef = NULL;
268 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
269 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
272 aRef->Set(aNode->Label());
277 //============================================================================
278 /*! Function : SetRootCurrent
281 //============================================================================
282 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
284 if(!_root) return false;
286 SALOMEDSImpl_AttributeReference* aRef = NULL;
287 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
288 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
290 aRef->Set(_root->Label());
294 //============================================================================
295 /*! Function : HasChildren
298 //============================================================================
299 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
301 if(!_root) return false;
304 if (!theObject) aLabel = _root->Label();
306 aLabel = theObject.GetLabel();
307 if(aLabel.IsNull()) return false;
309 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
310 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
312 return (aNode->GetFirst());
315 //============================================================================
316 /*! Function : SortChildren
319 //============================================================================
320 bool SALOMEDSImpl_UseCaseBuilder::SortChildren(const SALOMEDSImpl_SObject& theObject, bool theAscendingOrder)
322 if(!_root) return false;
325 if (!theObject) aLabel = _root->Label();
327 aLabel = theObject.GetLabel();
328 if(aLabel.IsNull()) return false;
330 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
331 if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
333 std::list<SALOMEDSImpl_SObject> aNodeSOs;
334 for (SALOMEDSImpl_AttributeTreeNode* aChildNode=aNode->GetFirst(); aChildNode; aChildNode=aChildNode->GetNext() ) {
335 SALOMEDSImpl_SObject aSO = SALOMEDSImpl_Study::SObject(aChildNode->Label());
337 aNodeSOs.push_back(aSO);
340 if (aNodeSOs.empty()) return false;
342 //sort items by names in ascending/descending order
343 theAscendingOrder ? aNodeSOs.sort( AscSortSOs() ) : aNodeSOs.sort( DescSortSOs() );
345 std::list<SALOMEDSImpl_SObject>::iterator it;
346 for (it=aNodeSOs.begin(); it!=aNodeSOs.end(); ++it) {
347 AppendTo(aNode->GetSObject(), *it);
353 //============================================================================
354 /*! Function : GetFather
357 //============================================================================
358 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetFather(const SALOMEDSImpl_SObject& theObject)
360 SALOMEDSImpl_SObject so;
361 if (!_root || !theObject) return so;
363 DF_Label aLabel = theObject.GetLabel();
364 if (aLabel.IsNull()) return so;
366 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
367 if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return so;
369 SALOMEDSImpl_AttributeTreeNode* aFatherNode = aNode->GetFather();
370 if (!aFatherNode) return so;
372 return aFatherNode->GetSObject();
375 //============================================================================
376 /*! Function : SetName
379 //============================================================================
380 bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
381 if(!_root) return false;
383 SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
385 if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
386 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
388 aNameAttrib->SetValue(theName);
394 //============================================================================
395 /*! Function : GetCurrentObject
398 //============================================================================
399 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
401 SALOMEDSImpl_SObject so;
402 if(!_root) return so;
404 SALOMEDSImpl_AttributeReference* aRef = NULL;
405 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
406 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
409 DF_Label aCurrent = aRef->Get();
410 if(aCurrent.IsNull()) return so;
412 return SALOMEDSImpl_Study::SObject(aCurrent);
415 //============================================================================
416 /*! Function : GetName
419 //============================================================================
420 std::string SALOMEDSImpl_UseCaseBuilder::GetName()
423 if(!_root) return aString;
425 SALOMEDSImpl_AttributeName* aName = NULL;
426 if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
427 return aName->Value();
430 //============================================================================
431 /*! Function : IsUseCase
434 //============================================================================
435 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
437 if(!theObject) return false;
438 DF_Label aFather, aLabel = theObject.GetLabel();
439 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
440 if(aLabel.Father() == aFather) return true;
444 //============================================================================
445 /*! Function : IsUseCaseNode
448 //============================================================================
449 bool SALOMEDSImpl_UseCaseBuilder::IsUseCaseNode(const SALOMEDSImpl_SObject& theObject)
451 if(!_root) return false;
454 if (!theObject) aLabel = _root->Label();
456 aLabel = theObject.GetLabel();
457 if(aLabel.IsNull()) return false;
459 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
460 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
465 //============================================================================
466 /*! Function : NewUseCase
469 //============================================================================
470 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
472 std::string aBasicGUID(USE_CASE_GUID);
474 //Create a use cases structure if it not exists
476 SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
477 SALOMEDSImpl_AttributeInteger* anInteger = NULL;
478 SALOMEDSImpl_AttributeReference* aRef = NULL;
480 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
482 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
483 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
486 if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
487 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
490 if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
491 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
494 //Create a new use case
495 anInteger->SetValue(anInteger->Value()+1);
496 DF_Label aChild = aLabel.FindChild(anInteger->Value());
497 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
499 aFatherNode->Append(aNode);
500 SALOMEDSImpl_AttributeName::Set(aChild, theName);
502 return SALOMEDSImpl_Study::SObject(aChild);
505 //============================================================================
506 /*! Function : GetUseCaseIterator
507 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
509 //============================================================================
510 SALOMEDSImpl_UseCaseIterator
511 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject)
516 aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
519 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
522 return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
526 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
528 DF_Label L = DF_Label::Label(_root->Label(), theEntry);
529 return SALOMEDSImpl_Study::SObject(L);