Salome HOME
New implementation of SALOMEDSImpl package based on DF data structure instead of...
[modules/kernel.git] / src / SALOMEDSImpl / testDS.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:    testDS.cxx
21 //Author:  Sergey RUIN
22
23 #include <stdio.h>
24 #include <iostream> 
25 #include <vector>
26 #include <string>
27
28 #include "DF_Document.hxx"
29 #include "DF_Attribute.hxx"
30 #include "DF_Label.hxx"
31 #include "DF_ChildIterator.hxx"
32
33 #include "SALOMEDSImpl_Attributes.hxx"
34 #include "SALOMEDSImpl_StudyManager.hxx"
35 #include "SALOMEDSImpl_Study.hxx"
36 #include "SALOMEDSImpl_StudyBuilder.hxx"
37 #include "SALOMEDSImpl_SObject.hxx"
38 #include "SALOMEDSImpl_SComponent.hxx"
39 #include "SALOMEDSImpl_AttributeParameter.hxx"
40 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
41 #include "SALOMEDSImpl_UseCaseIterator.hxx"
42
43 //#include "SALOMEDSImpl_.hxx"
44
45 using namespace std;
46
47 int main (int argc, char * argv[])
48 {
49   cout << "Test started " << endl;
50
51   SALOMEDSImpl_StudyManager* aSM = new SALOMEDSImpl_StudyManager();
52   cout << "Manager is created " << endl;
53   SALOMEDSImpl_Study* aStudy = aSM->NewStudy("SRN");
54   cout << "Study with id = " << aStudy->StudyId() << " is created " << endl; 
55
56   cout << "Check the study lock, locking"   << endl;
57   aStudy->SetStudyLock("SRN");
58   cout << "Is study locked = " << aStudy->IsStudyLocked() << endl;
59   vector<string> ids = aStudy->GetLockerID();
60   for(int i = 0; i<ids.size(); i++)
61     cout << "Get study locker : " << ids[i] << endl;
62   aStudy->UnLockStudy("SRN");
63   cout << "Is study locked = " << aStudy->IsStudyLocked()  << endl;
64
65   SALOMEDSImpl_StudyBuilder* aBuilder = aStudy->NewBuilder();
66   cout << "StudyBuilder is created " << endl;
67   SALOMEDSImpl_SComponent aSC = aBuilder->NewComponent("TEST");
68   cout << "New component with type " << aSC.ComponentDataType() << " is created " << endl;
69   SALOMEDSImpl_SObject aSO = aBuilder->NewObject(aSC);
70   cout << "New SObject with  ID = " << aSO.GetID() << " is created"  << endl;
71   cout << "An entry of newly created SO is "  <<  aSO.GetLabel().Entry() << endl;
72   SALOMEDSImpl_AttributeIOR* aIORA = SALOMEDSImpl_AttributeIOR::Set(aSO.GetLabel(), "ior1234");
73   cout << "New AttributeIOR is created, it contains " << dynamic_cast<SALOMEDSImpl_AttributeIOR*>(aIORA)->Value() << endl;
74   cout << "Attribute has type: " << aIORA->Type() << " and value: " << aIORA->Save() << endl; 
75   cout << "Just another way to create an attribute: official one :) " << endl;
76   cout << "Is SO null : " << aSO.IsNull()<< endl;
77   DF_Attribute* aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");  
78   SALOMEDSImpl_AttributeName* aRN = dynamic_cast<SALOMEDSImpl_AttributeName*>(aTDFAttr);
79   aRN->SetValue("name_attribute");
80   cout << " The type = " << aRN->Type() << endl;
81   cout << "Attribute has type: " << aRN->Type() << " and value: " << aRN->Save() << endl;   
82   cout << "Check GetObjectPath: " << aStudy->GetObjectPath(aSO) << endl;
83   
84   SALOMEDSImpl_SObject aSubSO = aBuilder->NewObject(aSO);
85   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSubSO, "AttributeIOR");  
86   SALOMEDSImpl_AttributeIOR* aIOR2 = dynamic_cast<SALOMEDSImpl_AttributeIOR*>(aTDFAttr);
87   aIOR2->SetValue("some ior");
88   aBuilder->Addreference(aSubSO, aSO);
89   SALOMEDSImpl_SObject aRefObject;
90   aSubSO.ReferencedObject(aRefObject);
91   cout << "Check reference : ReferencedObject is " << aRefObject.GetID() << endl;
92   cout << "Check : Remove object: " << endl;
93   aBuilder->RemoveObject(aSubSO);
94   cout << "Remove: done" << endl;
95
96   cout << "Try invalid attribute creation" << endl;
97   aTDFAttr = aBuilder->FindOrCreateAttribute(aSubSO, "invalid type");
98   cout << "Address of created attribute : " << aTDFAttr << endl;
99
100   cout << "Check AttributeUserID"   << endl;
101   
102   aTDFAttr = aBuilder->FindOrCreateAttribute(aSubSO, "AttributeUserID");
103   if(aTDFAttr) {
104     cout << "Attribute UserID was created succesfully : id = " << dynamic_cast<SALOMEDSImpl_AttributeUserID*>(aTDFAttr)->Value() << endl;
105   }
106   else cout << "Can't create AttributeUserID"   << endl;
107   
108   string id = "0e1c36e6-379b-4d90-ab3b-17a14310e648";
109   dynamic_cast<SALOMEDSImpl_AttributeUserID*>(aTDFAttr)->SetValue(id);
110   cout << "SetValue id = " << dynamic_cast<SALOMEDSImpl_AttributeUserID*>(aTDFAttr)->Value()  << endl;
111
112   string id2 = "0e1c36e6-379b-4d90-ab3b-18a14310e648";
113   aTDFAttr = aBuilder->FindOrCreateAttribute(aSubSO, "AttributeUserID"+id2);
114   if(aTDFAttr) {
115     cout << "Attribute UserID was created succesfully : id = " << dynamic_cast<SALOMEDSImpl_AttributeUserID*>(aTDFAttr)->Value() << endl;
116   }
117   else cout << "Can't create AttributeUserID"   << endl;
118
119   cout << "Check AttributeTreeNode " << endl;
120   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeTreeNode");  
121   cout << dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aTDFAttr)->Type() << endl;
122   cout << "Check AttributeTreeNode : done " << endl;
123
124   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeParameter");  
125   cout << dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aTDFAttr)->Type() << endl;
126
127   cout << "Check the attributes on SObject" << endl;
128   vector<DF_Attribute*> aSeq = aSO.GetAllAttributes();
129   for(int i = 0; i < aSeq.size(); i++) 
130     cout << "Found: " << dynamic_cast<SALOMEDSImpl_GenericAttribute*>(aSeq[i])->Type() << endl;
131
132
133   cout << "Check UseCase"   << endl;
134   SALOMEDSImpl_UseCaseBuilder* ucb = aStudy->GetUseCaseBuilder();
135   ucb->AddUseCase("use_case1");
136   ucb->AddUseCase("use_case2");
137   SALOMEDSImpl_UseCaseIterator ucitr = ucb->GetUseCaseIterator(SALOMEDSImpl_SObject());
138   ucitr.Init(false);
139   cout << "More? : " << ucitr.More() << endl;
140
141   cout << "Check AttributeParameter "   << endl;
142
143   SALOMEDSImpl_AttributeParameter* AP = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(aTDFAttr);
144
145   cout << "AttributeParameter with type : " << AP->Type() << endl;
146   
147   AP->SetInt("1", 123);
148   cout << "IsSet for int: " << AP->IsSet("1", PT_INTEGER) << " value : " << AP->GetInt("1") << endl;
149   //for(int i = 2; i < 5; i++) AP->SetInt(i, i*i);
150
151   AP->SetReal("1", 123.123);
152   cout << "IsSet for real: " << AP->IsSet("1", PT_REAL) << " value : " << AP->GetReal("1") << endl;
153   //for(int i = 2; i < 5; i++) AP->SetReal(i, 0.1);
154
155   AP->SetString("1", "value is 123.123!");
156   cout << "IsSet for string: " << AP->IsSet("1", PT_STRING) << " value : " << AP->GetString("1") << endl; 
157   /*
158   for(int i = 2; i < 5; i++) {
159     string s((double)(1.0/i));
160     cout << "Setting for " << i << " value : " << s  << endl;
161     AP->SetString(i, s); 
162   }
163   */
164
165   AP->SetBool("1", true);
166   cout << "IsSet for bool: " << AP->IsSet("1", PT_BOOLEAN) << " value : " << AP->GetBool("1") << endl;
167   //for(int i = 2; i < 5; i++) AP->SetBool(i, 0);
168   
169   vector<double> v;
170   v.push_back(111.111);
171   v.push_back(222.22222);
172   v.push_back(333.3333333);
173   AP->SetRealArray("1", v);
174   cout << "IsSet for array: " << AP->IsSet("1", PT_REALARRAY);
175   vector<double> v2 = AP->GetRealArray("1");
176   cout.precision(10);
177   cout << " values :  "; 
178   for(int i = 0; i<v2.size(); i++) cout << v2[i] << " ";
179   cout << endl;
180   
181   v[0] = 211.111;
182   v[1] = 422.22222;
183   v[2] = 633.3333333;
184   AP->SetRealArray("2", v);
185
186   vector<int> vi;
187   vi.push_back(1);
188   vi.push_back(2);
189   AP->SetIntArray("2", vi);
190   
191   vector<string> vs;
192   vs.push_back("hello, ");
193   vs.push_back("world!");
194   AP->SetStrArray("3", vs);        
195
196   string as = AP->Save();
197   cout << "AS = " << as << endl;
198   AP->Load(as);
199   
200   cout << "Restored string with id = 1 is: " << AP->GetString("1") << endl;
201   cout << "Restored int with id = 2 is: " << AP->GetInt("1")  << endl;
202   cout << "Restored real with id = 3 is: " << AP->GetReal("1")  << endl;
203   cout << "Restored bool with id = 1 is: " << AP->GetBool("1")  << endl;
204   
205   v2 = AP->GetRealArray("2");
206   cout << "Restored real array with id = 2 is: ";
207   for(int i = 0; i<v2.size(); i++) cout << v2[i] << " ";
208   cout << endl;
209
210   vi = AP->GetIntArray("2");
211   cout << "Restored int array with id = 2 is: ";
212   for(int i = 0; i<vi.size(); i++) cout << vi[i] << " ";
213   cout << endl;
214   
215   vs = AP->GetStrArray("3");
216   cout << "Restored string array with id = 2 is: ";
217   for(int i = 0; i<vs.size(); i++) cout << vs[i] << " ";
218   cout << endl;
219
220   cout << "Check RemoveID 1 with type PT_INTEGER" << endl;
221   AP->RemoveID("1", PT_INTEGER);
222   cout << "IsSet with id = 1, type = PT_INTEGER : " << AP->IsSet("1", PT_INTEGER)  << endl;
223   cout << "Check RemoveID is done" << endl;
224   
225   cout << "Check AttributeParameter : done"   << endl;
226
227   
228   SALOMEDSImpl_SComponent tst = aBuilder->NewComponent("TEST2");
229   aSO = aBuilder->NewObject(tst);
230   SALOMEDSImpl_SObject ss = aBuilder->NewObjectToTag(aSO, 3);
231   aBuilder->NewObjectToTag(ss, 1);
232   aBuilder->NewObjectToTag(ss, 3);
233   SALOMEDSImpl_SObject ss2 = aBuilder->NewObjectToTag(aSO, 2);
234   aBuilder->NewObjectToTag(ss, 2);
235
236   SALOMEDSImpl_ChildIterator ci=aStudy->NewChildIterator(tst);
237   for(ci.InitEx(true); ci.More(); ci.Next())
238     cout << "######## " << ci.Value().GetID() << endl;
239
240   DF_ChildIterator dci(tst.GetLabel(), true);
241   for(; dci.More(); dci.Next()) 
242     cout << "###### DF:  " << dci.Value().Entry() << endl;
243
244   cout << "Test finished " << endl;    
245   return 0;
246 }
247