BOSS 6.6.4.p01
BESIII Offline Software System
Loading...
Searching...
No Matches
test_RelTabs.cxx
Go to the documentation of this file.
1
2#include "RelTable/Relation.h"
3#include "RelTable/RelTable.h"
4#include "GaudiKernel/ContainedObject.h"
5#include "GaudiKernel/ObjectList.h"
6#include <vector>
7#include <iostream>
8#include <string>
9
10using Event::RelTable;
11using Event::Relation;
12
13
14
15
16
17// First I define two dummy classes to be used to test a relational table
18
19static const CLID CLID_FakeOne = 5101;
20static const CLID CLID_FakeTwo = 5102;
21
22
23
24class FakeOne: public ContainedObject {
25public:
26
27 virtual const CLID& clID() const { return FakeOne::classID(); }
28 static const CLID& classID() { return CLID_FakeOne; }
29
30 FakeOne(std::string s, int number): name(s), ssn(number) {}
31
32 // Name
33 std::string name;
34 // Social Securuty Number
35 int ssn;
36};
37
38class FakeTwo: public ContainedObject {
39public:
40
41 virtual const CLID& clID() const { return FakeTwo::classID(); }
42 static const CLID& classID() { return CLID_FakeTwo; }
43
44 FakeTwo(std::string add, int fl): address(add), floor(fl) {}
45
46 // Address of an Hotel
47 std::string address;
48 // Floor in the Hotel
49 int floor;
50};
51
52
53int main()
54{
55 FakeOne *person1, *person2, *person3;
56 FakeTwo *location1, *location2;
57
58
59 // First we create some contained object
60 person1 = new FakeOne("Jack Tripper", 654);
61 person2 = new FakeOne("Richard Kanningam", 456);
62 person3 = new FakeOne("Dana Scully", 231);
63
64 location1 = new FakeTwo("531 Stanford Avenue", 2);
65 location2 = new FakeTwo("520 Cowper Street", 3);
66
67
68 // Then we relate them
69 Relation<FakeOne, FakeTwo> *rel1 = new Relation<FakeOne, FakeTwo>(person1, location1, "06/08/2002");
70 Relation<FakeOne, FakeTwo> *rel2 = new Relation<FakeOne, FakeTwo>(person2, location1, "06/08/2002");
71 Relation<FakeOne, FakeTwo> *rel3 = new Relation<FakeOne, FakeTwo>(person3, location2, "10/08/2002");
72 Relation<FakeOne, FakeTwo> *rel4 = new Relation<FakeOne, FakeTwo>(person1, location2, "20/09/2002");
73 Relation<FakeOne, FakeTwo> *rel5 = new Relation<FakeOne, FakeTwo>(person2, location1, "09/10/2002");
74
75 // Using the TDS, we should only require an ObjectList of Relations, ie:
76 // typdef ObjectList< Relation<FakeOne, FakeTwo> > ListRelations;
77 // ListRelations* rels = SmartDataPtr<ListRelations > (SomeSvc(), "/SomePath");
78
79
80 // We add them to a table of relations
82 tab.init();
83
84 // The table is empty and so the following query should return an emtpy vector
85 std::vector< Relation<FakeOne, FakeTwo>* > empty = tab.getRelByFirst(person1);
86 std::cout << "Querying and empty table the size of the returned vector is "
87 << empty.size() << std::endl;
88
89
90 tab.addRelation(rel1);
91 tab.addRelation(rel2);
92 tab.addRelation(rel3);
93 tab.addRelation(rel4);
94 tab.addRelation(rel5);
95
96 // Using the TDS, the table is directly initialized by the ObjectList of relations:
97 // RelTable<FakeOne, FakeTwo> tab(rels);
98
99 // Only to verify if the fillStream method works
100 rel1->fillStream(std::cout);
101
102 // Now lets do some queries
103
104 std::vector< Relation<FakeOne, FakeTwo>* >::iterator i;
105
106 // What are all hotels visited by person1 (Jack Tripper)?
107 std::vector< Relation<FakeOne, FakeTwo>* > locs = tab.getRelByFirst(person1);
108
109 std::cout << std::endl << person1->name << std::endl;
110 for (i = locs.begin(); i != locs.end(); i++)
111 {
112 std::cout << "Address: " << (*i)->getSecond()->address
113 << " Floor: " << (*i)->getSecond()->floor
114 << " Date : " << (*i)->getInfos()[0] << std::endl;
115 }
116
117 // And all persons that visited location2 ?
118 std::vector< Relation<FakeOne, FakeTwo>* > pers = tab.getRelBySecond(location2);
119
120 std::cout << std::endl << location2->address << std::endl;
121 for (i = pers.begin(); i != pers.end(); i++)
122 {
123 std::cout << "Name: " << (*i)->getFirst()->name
124 << " ssn: " << (*i)->getFirst()->ssn << std::endl;
125 }
126
127
128 // Now we change a relation already inserted
129
130
131 locs = tab.getRelByFirst(person3);
132 for (i = locs.begin(); i != locs.end(); i++)
133 {
134 tab.changeSecond(*i,location1);
135 }
136
137 std::cout << std::endl << "persion3 location changed" << std::endl;
138 std::cout << std::endl << location2->address << std::endl;
139
140 pers = tab.getRelBySecond(location1);
141 for (i = pers.begin(); i != pers.end(); i++)
142 {
143 std::cout << "Name: " << (*i)->getFirst()->name
144 << " ssn: " << (*i)->getFirst()->ssn << std::endl;
145 }
146
147
148
149 locs = tab.getRelByFirst(person1);
150
151 for (i = locs.begin(); i != locs.end(); i++)
152 {
153 tab.changeFirst(*i,0);
154 }
155
156 std::cout << std::endl << "Jack Tripper set to null" << std::endl;
157
158 // Let's see the result oh the previous query after this change. We have to
159 // take care about null pointers now.
160
161 pers = tab.getRelBySecond(location2);
162
163 std::cout << "Number of relations with location2 = " << pers.size() << std::endl;
164
165 std::cout << std::endl << location2->address << std::endl;
166 for (i = pers.begin(); i != pers.end(); i++)
167 {
168 if ((*i)->getFirst())
169 std::cout << "Name: " << (*i)->getFirst()->name
170 << " ssn: " << (*i)->getFirst()->ssn << std::endl;
171 }
172
173
174 // Now let's remove the two relations with the null pointer
175
176 pers = tab.getRelByFirst(0);
177 for (i = pers.begin(); i != pers.end(); i++)
178 {
179 tab.erase(*i);
180 }
181
182 std::cout << std::endl << "Removed relations with null pointer" << std::endl;
183 std::cout << "Number of relations = " << tab.size() << std::endl;
184 std::cout << std::endl << location2->address << std::endl;
185
186 pers = tab.getRelBySecond(location1);
187 for (i = pers.begin(); i != pers.end(); i++)
188 {
189 std::cout << "Name: " << (*i)->getFirst()->name
190 << " ssn: " << (*i)->getFirst()->ssn << std::endl;
191 }
192
193
194
195 std::cout << std::endl << person2->name << std::endl;
196
197
198 unsigned int index;
199 // Now, we verify how many times person2 has been in a location
200 locs = tab.getRelByFirst(person2);
201 for (i = locs.begin(); i != locs.end(); i++)
202 {
203 std::cout << "Address: " << (*i)->getSecond()->address << std::endl;
204 std::cout << "Floor: " << (*i)->getSecond()->floor << std::endl;
205 std::cout << "Dates : " << std::endl;
206 for ( index = 0; index < (*i)->getInfos().size(); index++)
207 {
208 std::cout << (*i)->getInfos()[index] << std::endl;
209 }
210 }
211
212
213 // Using the TDS, one can finally register the collection of relations:
214 // SomeSvc()->registerObject("/Event/MC/RelFakeOneFakeTwo",
215 // tab.getAllRelations());
216
217 return 0;
218}
XmlRpcServer s
Definition: HelloServer.cpp:11
void changeSecond(Relation< T1, T2 > *rel, T2 *pobj)
Definition: RelTable.h:243
std::vector< Relation< T1, T2 > * > getRelBySecond(const T2 *pobj) const
Definition: RelTable.h:191
void init()
Initialize the internal pointer to an ObjectList of relations.
Definition: RelTable.h:38
unsigned long size() const
This method returns the number of relations in the table.
Definition: RelTable.h:256
void erase(Relation< T1, T2 > *rel)
Definition: RelTable.h:218
bool addRelation(Relation< T1, T2 > *rel)
Definition: RelTable.h:143
std::vector< Relation< T1, T2 > * > getRelByFirst(const T1 *pobj) const
Definition: RelTable.h:163
void changeFirst(Relation< T1, T2 > *rel, T1 *pobj)
Definition: RelTable.h:230
std::ostream & fillStream(std::ostream &s) const
Fill the ASCII output stream.
Definition: Relation.h:109
static const CLID & classID()
virtual const CLID & clID() const
std::string name
FakeOne(std::string s, int number)
static const CLID & classID()
FakeTwo(std::string add, int fl)
std::string address
virtual const CLID & clID() const
int main()