| 1 | #include "NetworkReader.h" |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Constructor |
|---|
| 5 | * @param networkFileName input xml file |
|---|
| 6 | */ |
|---|
| 7 | NetworkReader::NetworkReader(const char * xml) { |
|---|
| 8 | ldsIndex = 0; |
|---|
| 9 | loadLines(xml); |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | LOOP * NetworkReader::parseLoop(TiXmlElement * loopElem) { |
|---|
| 13 | LOOP *loop = new LOOP; |
|---|
| 14 | |
|---|
| 15 | TiXmlElement *subLoopElem = loopElem->FirstChildElement(); |
|---|
| 16 | loop->loopID = atoi(subLoopElem->GetText()); |
|---|
| 17 | cout << "LOOPID: " << subLoopElem->GetText() << endl; |
|---|
| 18 | subLoopElem = subLoopElem->NextSiblingElement(); |
|---|
| 19 | loop->loop_loc = (char *) subLoopElem->GetText(); |
|---|
| 20 | subLoopElem = subLoopElem->NextSiblingElement(); |
|---|
| 21 | loop->vol = atoi(subLoopElem->GetText()); |
|---|
| 22 | subLoopElem = subLoopElem->NextSiblingElement(); |
|---|
| 23 | loop->occ = atof(subLoopElem->GetText()); |
|---|
| 24 | subLoopElem = subLoopElem->NextSiblingElement(); |
|---|
| 25 | loop->spd = atof(subLoopElem->GetText()); |
|---|
| 26 | |
|---|
| 27 | return loop; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Parses a station xml element into an "STATION" |
|---|
| 32 | * |
|---|
| 33 | * @param stationElem the station xml element |
|---|
| 34 | * @param the parent line |
|---|
| 35 | * @return the new station |
|---|
| 36 | */ |
|---|
| 37 | STATION * NetworkReader::parseStation(TiXmlElement *stationElem, FEP_LINE *line) { |
|---|
| 38 | STATION *station = new STATION; |
|---|
| 39 | |
|---|
| 40 | TiXmlElement *stationSubElem = stationElem->FirstChildElement(); |
|---|
| 41 | station->lds = atol(stationSubElem->GetText()); |
|---|
| 42 | line->lds.push_back(station->lds); |
|---|
| 43 | line->ldsIndex.push_back(ldsIndex++); |
|---|
| 44 | stationSubElem = stationSubElem->NextSiblingElement(); |
|---|
| 45 | station->line_num = atoi(stationSubElem->GetText()); |
|---|
| 46 | stationSubElem = stationSubElem->NextSiblingElement(); |
|---|
| 47 | station->drop = atoi(stationSubElem->GetText()); |
|---|
| 48 | stationSubElem = stationSubElem->NextSiblingElement(); |
|---|
| 49 | stationSubElem = stationSubElem->NextSiblingElement(); // skip location |
|---|
| 50 | stationSubElem = stationSubElem->NextSiblingElement(); // skip postmile |
|---|
| 51 | stationSubElem = stationSubElem->NextSiblingElement(); // skip direction |
|---|
| 52 | stationSubElem = stationSubElem->NextSiblingElement(); // skip freeway |
|---|
| 53 | station->MlTotVol = atoi(stationSubElem->GetText()); |
|---|
| 54 | stationSubElem = stationSubElem->NextSiblingElement(); |
|---|
| 55 | station->OppTotVol = atoi(stationSubElem->GetText()); |
|---|
| 56 | |
|---|
| 57 | station->pos = 0; // NOT SURE WHY WE NEED THIS? |
|---|
| 58 | |
|---|
| 59 | // Add loops to station |
|---|
| 60 | TiXmlElement *loopElem = stationSubElem->NextSiblingElement()->FirstChildElement(); |
|---|
| 61 | for (loopElem; loopElem; loopElem = loopElem->NextSiblingElement()) { |
|---|
| 62 | LOOP *loop = parseLoop(loopElem); |
|---|
| 63 | station->loops.push_back(loop); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | // Data pack ATMS message |
|---|
| 67 | station->length = station->loops.size() * 2 + CONTROL_DATA_LEN; |
|---|
| 68 | cout << station->loops.size() << endl; |
|---|
| 69 | station->dataPack = DataPacker::packData(station); |
|---|
| 70 | |
|---|
| 71 | return station; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Parses a "Line" xml element into an FEP_LINE |
|---|
| 76 | * @param lineElem the xml element |
|---|
| 77 | * @return FEP_LINE |
|---|
| 78 | */ |
|---|
| 79 | FEP_LINE * NetworkReader::parseLine(TiXmlElement * lineElem) { |
|---|
| 80 | FEP_LINE *line = new FEP_LINE; |
|---|
| 81 | |
|---|
| 82 | TiXmlElement *lineSubElem = lineElem->FirstChildElement(); |
|---|
| 83 | line->lineNum = atoi(lineSubElem->GetText()); |
|---|
| 84 | cout << "Line: " << line->lineNum << endl; |
|---|
| 85 | lineSubElem = lineSubElem->NextSiblingElement(); |
|---|
| 86 | line->count = atoi(lineSubElem->GetText()); |
|---|
| 87 | lineSubElem = lineSubElem->NextSiblingElement(); |
|---|
| 88 | line->schedule = atoi(lineSubElem->GetText()); |
|---|
| 89 | lineSubElem = lineSubElem->NextSiblingElement(); |
|---|
| 90 | line->lineInfo = atoi(lineSubElem->GetText()); |
|---|
| 91 | lineSubElem = lineSubElem->NextSiblingElement(); |
|---|
| 92 | line->systemKey = atol(lineSubElem->GetText()); |
|---|
| 93 | lineSubElem = lineSubElem->NextSiblingElement(); |
|---|
| 94 | line->globalSeq = atol(lineSubElem->GetText()); |
|---|
| 95 | lineSubElem = lineSubElem->NextSiblingElement(); |
|---|
| 96 | line->schedleSeq = atol(lineSubElem->GetText()); |
|---|
| 97 | |
|---|
| 98 | TiXmlElement *stationsElem = lineSubElem->NextSiblingElement(); |
|---|
| 99 | TiXmlElement *stationElem = stationsElem->FirstChildElement(); |
|---|
| 100 | for (stationElem; stationElem; stationElem = stationElem->NextSiblingElement()) { |
|---|
| 101 | stations.push_back(parseStation(stationElem, line)); |
|---|
| 102 | } |
|---|
| 103 | return line; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Loads FEPLines from a specified xml file |
|---|
| 108 | * @param networkFileName the input xml file |
|---|
| 109 | */ |
|---|
| 110 | void NetworkReader::loadLines(const char * xml) { |
|---|
| 111 | // Load network xml file |
|---|
| 112 | TiXmlDocument doc; |
|---|
| 113 | doc.Parse((const char*)xml, 0, TIXML_ENCODING_UTF8); |
|---|
| 114 | |
|---|
| 115 | // grab <Network> element |
|---|
| 116 | TiXmlHandle hDoc(&doc); |
|---|
| 117 | TiXmlElement *networkElem = hDoc.FirstChildElement().Element(); |
|---|
| 118 | |
|---|
| 119 | // grab first <Line> element |
|---|
| 120 | TiXmlElement *lineElem = networkElem->FirstChildElement(); |
|---|
| 121 | |
|---|
| 122 | // iterate through each line element to create FEP_LINE list |
|---|
| 123 | for (lineElem; lineElem; lineElem = lineElem->NextSiblingElement()) { |
|---|
| 124 | lines.push_back(parseLine(lineElem)); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** |
|---|
| 129 | * Getter for lines |
|---|
| 130 | * @return List of FEP_LINES |
|---|
| 131 | */ |
|---|
| 132 | vector<FEP_LINE*> NetworkReader::getLines() { |
|---|
| 133 | |
|---|
| 134 | return lines; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * Getter for stations |
|---|
| 139 | * @return List of STATIONs |
|---|
| 140 | */ |
|---|
| 141 | vector<STATION*> NetworkReader::getStations() { |
|---|
| 142 | |
|---|
| 143 | return stations; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | NetworkReader::~NetworkReader() { |
|---|
| 147 | |
|---|
| 148 | } |
|---|