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