00001
00002 #include "RelTable/Relation.h"
00003 #include "RelTable/RelTable.h"
00004 #include "GaudiKernel/ContainedObject.h"
00005 #include "GaudiKernel/ObjectList.h"
00006 #include <vector>
00007 #include <iostream>
00008 #include <string>
00009
00010 using Event::RelTable;
00011 using Event::Relation;
00012
00013
00014
00015
00016
00017
00018
00019 static const CLID CLID_FakeOne = 5101;
00020 static const CLID CLID_FakeTwo = 5102;
00021
00022
00023
00024 class FakeOne: public ContainedObject {
00025 public:
00026
00027 virtual const CLID& clID() const { return FakeOne::classID(); }
00028 static const CLID& classID() { return CLID_FakeOne; }
00029
00030 FakeOne(std::string s, int number): name(s), ssn(number) {}
00031
00032
00033 std::string name;
00034
00035 int ssn;
00036 };
00037
00038 class FakeTwo: public ContainedObject {
00039 public:
00040
00041 virtual const CLID& clID() const { return FakeTwo::classID(); }
00042 static const CLID& classID() { return CLID_FakeTwo; }
00043
00044 FakeTwo(std::string add, int fl): address(add), floor(fl) {}
00045
00046
00047 std::string address;
00048
00049 int floor;
00050 };
00051
00052
00053 int main()
00054 {
00055 FakeOne *person1, *person2, *person3;
00056 FakeTwo *location1, *location2;
00057
00058
00059
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
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
00076
00077
00078
00079
00080
00081 RelTable<FakeOne, FakeTwo> tab;
00082 tab.init();
00083
00084
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
00097
00098
00099
00100 rel1->fillStream(std::cout);
00101
00102
00103
00104 std::vector< Relation<FakeOne, FakeTwo>* >::iterator i;
00105
00106
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
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
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
00159
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
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
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
00214
00215
00216
00217 return 0;
00218 }