Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / parser.cpp @ 6575aa2e

History | View | Annotate | Download (2.91 KB)

1 04a9ef10 Quynh PX Nguyen
//
2
// Created by quynh on 12/13/15.
3
//
4
5
#include "parser.h"
6
7 162e1bda Quynh PX Nguyen
void readEdgeFileGraphManager(string filepath, GraphManager &gm) {
8 6a12e353 Quynh PX Nguyen
    // NameVertexMap is to keep track of which router has already been added
9 162e1bda Quynh PX Nguyen
    ifstream inFile(filepath.c_str());
10 04a9ef10 Quynh PX Nguyen
11
    vector<string> strs;
12
    for (string line; getline(inFile, line); /**/) {
13 6575aa2e Quynh PX Nguyen
        boost::split(strs, line, boost::is_any_of(" ,"), boost::token_compress_on);
14 04a9ef10 Quynh PX Nguyen
15
        // Cast vector<string> to array<string, 3>
16
        // TODO: this is really crude way to do it.
17
        // TODO: how to copy some element of vector to array
18 6a12e353 Quynh PX Nguyen
        if (strs.size() == 3) {
19
            string source = strs[0];
20
            string target = strs[1];
21 162e1bda Quynh PX Nguyen
22
            GraphManager::VertexProperties vp1 = GraphManager::VertexProperties(source, source);
23
            GraphManager::VertexProperties vp2 = GraphManager::VertexProperties(target, target);
24
25 ee0dd796 Quynh PX Nguyen
            // TODO: use atof as a way around the error: ‘stof’ was not declared in this scope
26
            // double cost = stof(strs[2]);
27
            double cost = atof(strs[2].c_str());
28 6a12e353 Quynh PX Nguyen
29 162e1bda Quynh PX Nguyen
            GraphManager::EdgeProperties ep = GraphManager::EdgeProperties(cost);
30
            gm.AddEdge(vp1, vp2, ep);
31 04a9ef10 Quynh PX Nguyen
        }
32
    }
33 162e1bda Quynh PX Nguyen
    gm.ResetVerticesAndEdgesIndexMap();
34 04a9ef10 Quynh PX Nguyen
    inFile.close();
35 6a12e353 Quynh PX Nguyen
}
36
37 162e1bda Quynh PX Nguyen
void readJsonGraphManager(string filepath, GraphManager &gm) {
38 6a12e353 Quynh PX Nguyen
    boost::property_tree::ptree pt;
39 162e1bda Quynh PX Nguyen
    boost::property_tree::read_json(filepath, pt);
40 04a9ef10 Quynh PX Nguyen
41 6a12e353 Quynh PX Nguyen
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("links")) {
42
        cout << v.second.get_value<std::string>() << " ";
43
        string source = v.second.get_child("source").get_value<std::string>();
44
        string target = v.second.get_child("target").get_value<std::string>();
45
        double cost = v.second.get_child("cost").get_value<double>();
46
47 162e1bda Quynh PX Nguyen
        GraphManager::VertexProperties vp1 = GraphManager::VertexProperties(source, source);
48
        GraphManager::VertexProperties vp2 = GraphManager::VertexProperties(target, target);
49
        GraphManager::EdgeProperties ep = GraphManager::EdgeProperties(cost);
50
        gm.AddEdge(vp1, vp2, ep);
51 6a12e353 Quynh PX Nguyen
    }
52 04a9ef10 Quynh PX Nguyen
}
53 6a12e353 Quynh PX Nguyen
54 162e1bda Quynh PX Nguyen
void readComplexJsonGraphManager(string filepath, GraphManager &gm) {
55 6a12e353 Quynh PX Nguyen
    boost::property_tree::ptree pt;
56 162e1bda Quynh PX Nguyen
    boost::property_tree::read_json(filepath, pt);
57 6a12e353 Quynh PX Nguyen
58
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("topology")) {
59
        cout << v.second.get_value<std::string>() << " ";
60
        string source = v.second.get_child("lastHopIP").get_value<std::string>();
61
        string target = v.second.get_child("destinationIP").get_value<std::string>();
62
        double cost = v.second.get_child("neighborLinkQuality").get_value<double>();
63
64 162e1bda Quynh PX Nguyen
        GraphManager::VertexProperties vp1 = GraphManager::VertexProperties(source, source);
65
        GraphManager::VertexProperties vp2 = GraphManager::VertexProperties(target, target);
66
        GraphManager::EdgeProperties ep = GraphManager::EdgeProperties(cost);
67
        gm.AddEdge(vp1, vp2, ep);
68 6a12e353 Quynh PX Nguyen
    }
69 cb770240 Quynh PX Nguyen
}