#include "RelTable/Relation.h"
#include "RelTable/RelTable.h"
#include "GaudiKernel/ContainedObject.h"
#include "GaudiKernel/ObjectList.h"
#include <vector>
#include <iostream>
#include <string>
Go to the source code of this file.
Classes | |
class | FakeOne |
class | FakeTwo |
Functions | |
int | main () |
Variables | |
static const CLID | CLID_FakeOne = 5101 |
static const CLID | CLID_FakeTwo = 5102 |
int main | ( | ) |
Definition at line 53 of file test_RelTabs.cxx.
References FakeTwo::address, Event::Relation< T1, T2 >::fillStream(), Event::Relation< T1, T2 >::getFirst(), Event::Relation< T1, T2 >::getSecond(), genRecEmupikp::i, and FakeOne::name.
00054 { 00055 FakeOne *person1, *person2, *person3; 00056 FakeTwo *location1, *location2; 00057 00058 00059 // First we create some contained object 00060 person1 = new FakeOne("Jack Tripper", 654); 00061 person2 = new FakeOne("Richard Kanningam", 456); 00062 person3 = new FakeOne("Dana Scully", 231); 00063 00064 location1 = new FakeTwo("531 Stanford Avenue", 2); 00065 location2 = new FakeTwo("520 Cowper Street", 3); 00066 00067 00068 // Then we relate them 00069 Relation<FakeOne, FakeTwo> *rel1 = new Relation<FakeOne, FakeTwo>(person1, location1, "06/08/2002"); 00070 Relation<FakeOne, FakeTwo> *rel2 = new Relation<FakeOne, FakeTwo>(person2, location1, "06/08/2002"); 00071 Relation<FakeOne, FakeTwo> *rel3 = new Relation<FakeOne, FakeTwo>(person3, location2, "10/08/2002"); 00072 Relation<FakeOne, FakeTwo> *rel4 = new Relation<FakeOne, FakeTwo>(person1, location2, "20/09/2002"); 00073 Relation<FakeOne, FakeTwo> *rel5 = new Relation<FakeOne, FakeTwo>(person2, location1, "09/10/2002"); 00074 00075 // Using the TDS, we should only require an ObjectList of Relations, ie: 00076 // typdef ObjectList< Relation<FakeOne, FakeTwo> > ListRelations; 00077 // ListRelations* rels = SmartDataPtr<ListRelations > (SomeSvc(), "/SomePath"); 00078 00079 00080 // We add them to a table of relations 00081 RelTable<FakeOne, FakeTwo> tab; 00082 tab.init(); 00083 00084 // The table is empty and so the following query should return an emtpy vector 00085 std::vector< Relation<FakeOne, FakeTwo>* > empty = tab.getRelByFirst(person1); 00086 std::cout << "Querying and empty table the size of the returned vector is " 00087 << empty.size() << std::endl; 00088 00089 00090 tab.addRelation(rel1); 00091 tab.addRelation(rel2); 00092 tab.addRelation(rel3); 00093 tab.addRelation(rel4); 00094 tab.addRelation(rel5); 00095 00096 // Using the TDS, the table is directly initialized by the ObjectList of relations: 00097 // RelTable<FakeOne, FakeTwo> tab(rels); 00098 00099 // Only to verify if the fillStream method works 00100 rel1->fillStream(std::cout); 00101 00102 // Now lets do some queries 00103 00104 std::vector< Relation<FakeOne, FakeTwo>* >::iterator i; 00105 00106 // What are all hotels visited by person1 (Jack Tripper)? 00107 std::vector< Relation<FakeOne, FakeTwo>* > locs = tab.getRelByFirst(person1); 00108 00109 std::cout << std::endl << person1->name << std::endl; 00110 for (i = locs.begin(); i != locs.end(); i++) 00111 { 00112 std::cout << "Address: " << (*i)->getSecond()->address 00113 << " Floor: " << (*i)->getSecond()->floor 00114 << " Date : " << (*i)->getInfos()[0] << std::endl; 00115 } 00116 00117 // And all persons that visited location2 ? 00118 std::vector< Relation<FakeOne, FakeTwo>* > pers = tab.getRelBySecond(location2); 00119 00120 std::cout << std::endl << location2->address << std::endl; 00121 for (i = pers.begin(); i != pers.end(); i++) 00122 { 00123 std::cout << "Name: " << (*i)->getFirst()->name 00124 << " ssn: " << (*i)->getFirst()->ssn << std::endl; 00125 } 00126 00127 00128 // Now we change a relation already inserted 00129 00130 00131 locs = tab.getRelByFirst(person3); 00132 for (i = locs.begin(); i != locs.end(); i++) 00133 { 00134 tab.changeSecond(*i,location1); 00135 } 00136 00137 std::cout << std::endl << "persion3 location changed" << std::endl; 00138 std::cout << std::endl << location2->address << std::endl; 00139 00140 pers = tab.getRelBySecond(location1); 00141 for (i = pers.begin(); i != pers.end(); i++) 00142 { 00143 std::cout << "Name: " << (*i)->getFirst()->name 00144 << " ssn: " << (*i)->getFirst()->ssn << std::endl; 00145 } 00146 00147 00148 00149 locs = tab.getRelByFirst(person1); 00150 00151 for (i = locs.begin(); i != locs.end(); i++) 00152 { 00153 tab.changeFirst(*i,0); 00154 } 00155 00156 std::cout << std::endl << "Jack Tripper set to null" << std::endl; 00157 00158 // Let's see the result oh the previous query after this change. We have to 00159 // take care about null pointers now. 00160 00161 pers = tab.getRelBySecond(location2); 00162 00163 std::cout << "Number of relations with location2 = " << pers.size() << std::endl; 00164 00165 std::cout << std::endl << location2->address << std::endl; 00166 for (i = pers.begin(); i != pers.end(); i++) 00167 { 00168 if ((*i)->getFirst()) 00169 std::cout << "Name: " << (*i)->getFirst()->name 00170 << " ssn: " << (*i)->getFirst()->ssn << std::endl; 00171 } 00172 00173 00174 // Now let's remove the two relations with the null pointer 00175 00176 pers = tab.getRelByFirst(0); 00177 for (i = pers.begin(); i != pers.end(); i++) 00178 { 00179 tab.erase(*i); 00180 } 00181 00182 std::cout << std::endl << "Removed relations with null pointer" << std::endl; 00183 std::cout << "Number of relations = " << tab.size() << std::endl; 00184 std::cout << std::endl << location2->address << std::endl; 00185 00186 pers = tab.getRelBySecond(location1); 00187 for (i = pers.begin(); i != pers.end(); i++) 00188 { 00189 std::cout << "Name: " << (*i)->getFirst()->name 00190 << " ssn: " << (*i)->getFirst()->ssn << std::endl; 00191 } 00192 00193 00194 00195 std::cout << std::endl << person2->name << std::endl; 00196 00197 00198 unsigned int index; 00199 // Now, we verify how many times person2 has been in a location 00200 locs = tab.getRelByFirst(person2); 00201 for (i = locs.begin(); i != locs.end(); i++) 00202 { 00203 std::cout << "Address: " << (*i)->getSecond()->address << std::endl; 00204 std::cout << "Floor: " << (*i)->getSecond()->floor << std::endl; 00205 std::cout << "Dates : " << std::endl; 00206 for ( index = 0; index < (*i)->getInfos().size(); index++) 00207 { 00208 std::cout << (*i)->getInfos()[index] << std::endl; 00209 } 00210 } 00211 00212 00213 // Using the TDS, one can finally register the collection of relations: 00214 // SomeSvc()->registerObject("/Event/MC/RelFakeOneFakeTwo", 00215 // tab.getAllRelations()); 00216 00217 return 0; 00218 }
const CLID CLID_FakeOne = 5101 [static] |
const CLID CLID_FakeTwo = 5102 [static] |