BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
test_RelTabs.cxx File Reference
#include "GaudiKernel/ContainedObject.h"
#include "GaudiKernel/ObjectList.h"
#include "RelTable/RelTable.h"
#include "RelTable/Relation.h"
#include <iostream>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

class  FakeOne
class  FakeTwo

Functions

int main ()

Function Documentation

◆ main()

int main ( )

Definition at line 44 of file test_RelTabs.cxx.

44 {
45 FakeOne *person1, *person2, *person3;
46 FakeTwo *location1, *location2;
47
48 // First we create some contained object
49 person1 = new FakeOne( "Jack Tripper", 654 );
50 person2 = new FakeOne( "Richard Kanningam", 456 );
51 person3 = new FakeOne( "Dana Scully", 231 );
52
53 location1 = new FakeTwo( "531 Stanford Avenue", 2 );
54 location2 = new FakeTwo( "520 Cowper Street", 3 );
55
56 // Then we relate them
58 new Relation<FakeOne, FakeTwo>( person1, location1, "06/08/2002" );
60 new Relation<FakeOne, FakeTwo>( person2, location1, "06/08/2002" );
62 new Relation<FakeOne, FakeTwo>( person3, location2, "10/08/2002" );
64 new Relation<FakeOne, FakeTwo>( person1, location2, "20/09/2002" );
66 new Relation<FakeOne, FakeTwo>( person2, location1, "09/10/2002" );
67
68 // Using the TDS, we should only require an ObjectList of Relations, ie:
69 // typdef ObjectList< Relation<FakeOne, FakeTwo> > ListRelations;
70 // ListRelations* rels = SmartDataPtr<ListRelations > (SomeSvc(), "/SomePath");
71
72 // We add them to a table of relations
74 tab.init();
75
76 // The table is empty and so the following query should return an emtpy vector
77 std::vector<Relation<FakeOne, FakeTwo>*> empty = tab.getRelByFirst( person1 );
78 std::cout << "Querying and empty table the size of the returned vector is " << empty.size()
79 << std::endl;
80
81 tab.addRelation( rel1 );
82 tab.addRelation( rel2 );
83 tab.addRelation( rel3 );
84 tab.addRelation( rel4 );
85 tab.addRelation( rel5 );
86
87 // Using the TDS, the table is directly initialized by the ObjectList of relations:
88 // RelTable<FakeOne, FakeTwo> tab(rels);
89
90 // Only to verify if the fillStream method works
91 rel1->fillStream( std::cout );
92
93 // Now lets do some queries
94
95 std::vector<Relation<FakeOne, FakeTwo>*>::iterator i;
96
97 // What are all hotels visited by person1 (Jack Tripper)?
98 std::vector<Relation<FakeOne, FakeTwo>*> locs = tab.getRelByFirst( person1 );
99
100 std::cout << std::endl << person1->name << std::endl;
101 for ( i = locs.begin(); i != locs.end(); i++ )
102 {
103 std::cout << "Address: " << ( *i )->getSecond()->address
104 << " Floor: " << ( *i )->getSecond()->floor
105 << " Date : " << ( *i )->getInfos()[0] << std::endl;
106 }
107
108 // And all persons that visited location2 ?
109 std::vector<Relation<FakeOne, FakeTwo>*> pers = tab.getRelBySecond( location2 );
110
111 std::cout << std::endl << location2->address << std::endl;
112 for ( i = pers.begin(); i != pers.end(); i++ )
113 {
114 std::cout << "Name: " << ( *i )->getFirst()->name
115 << " ssn: " << ( *i )->getFirst()->ssn << std::endl;
116 }
117
118 // Now we change a relation already inserted
119
120 locs = tab.getRelByFirst( person3 );
121 for ( i = locs.begin(); i != locs.end(); i++ ) { tab.changeSecond( *i, location1 ); }
122
123 std::cout << std::endl << "persion3 location changed" << std::endl;
124 std::cout << std::endl << location2->address << std::endl;
125
126 pers = tab.getRelBySecond( location1 );
127 for ( i = pers.begin(); i != pers.end(); i++ )
128 {
129 std::cout << "Name: " << ( *i )->getFirst()->name
130 << " ssn: " << ( *i )->getFirst()->ssn << std::endl;
131 }
132
133 locs = tab.getRelByFirst( person1 );
134
135 for ( i = locs.begin(); i != locs.end(); i++ ) { tab.changeFirst( *i, 0 ); }
136
137 std::cout << std::endl << "Jack Tripper set to null" << std::endl;
138
139 // Let's see the result oh the previous query after this change. We have to
140 // take care about null pointers now.
141
142 pers = tab.getRelBySecond( location2 );
143
144 std::cout << "Number of relations with location2 = " << pers.size() << std::endl;
145
146 std::cout << std::endl << location2->address << std::endl;
147 for ( i = pers.begin(); i != pers.end(); i++ )
148 {
149 if ( ( *i )->getFirst() )
150 std::cout << "Name: " << ( *i )->getFirst()->name
151 << " ssn: " << ( *i )->getFirst()->ssn << std::endl;
152 }
153
154 // Now let's remove the two relations with the null pointer
155
156 pers = tab.getRelByFirst( 0 );
157 for ( i = pers.begin(); i != pers.end(); i++ ) { tab.erase( *i ); }
158
159 std::cout << std::endl << "Removed relations with null pointer" << std::endl;
160 std::cout << "Number of relations = " << tab.size() << std::endl;
161 std::cout << std::endl << location2->address << std::endl;
162
163 pers = tab.getRelBySecond( location1 );
164 for ( i = pers.begin(); i != pers.end(); i++ )
165 {
166 std::cout << "Name: " << ( *i )->getFirst()->name
167 << " ssn: " << ( *i )->getFirst()->ssn << std::endl;
168 }
169
170 std::cout << std::endl << person2->name << std::endl;
171
172 unsigned int index;
173 // Now, we verify how many times person2 has been in a location
174 locs = tab.getRelByFirst( person2 );
175 for ( i = locs.begin(); i != locs.end(); i++ )
176 {
177 std::cout << "Address: " << ( *i )->getSecond()->address << std::endl;
178 std::cout << "Floor: " << ( *i )->getSecond()->floor << std::endl;
179 std::cout << "Dates : " << std::endl;
180 for ( index = 0; index < ( *i )->getInfos().size(); index++ )
181 { std::cout << ( *i )->getInfos()[index] << std::endl; }
182 }
183
184 // Using the TDS, one can finally register the collection of relations:
185 // SomeSvc()->registerObject("/Event/MC/RelFakeOneFakeTwo",
186 // tab.getAllRelations());
187
188 return 0;
189}
void changeSecond(Relation< T1, T2 > *rel, T2 *pobj)
std::vector< Relation< T1, T2 > * > getRelBySecond(const T2 *pobj) const
void init()
Initialize the internal pointer to an ObjectList of relations.
unsigned long size() const
This method returns the number of relations in the table.
void erase(Relation< T1, T2 > *rel)
bool addRelation(Relation< T1, T2 > *rel)
std::vector< Relation< T1, T2 > * > getRelByFirst(const T1 *pobj) const
void changeFirst(Relation< T1, T2 > *rel, T1 *pobj)
std::ostream & fillStream(std::ostream &s) const
Fill the ASCII output stream.
std::string name
std::string address