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