Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / parser.cpp @ c6961065

History | View | Annotate | Download (3.33 KB)

1 04a9ef10 Quynh PX Nguyen
//
2
// Created by quynh on 12/13/15.
3
//
4
5
#include "parser.h"
6
7 6a12e353 Quynh PX Nguyen
template<typename NameVertexMap>
8
void addLinkToGraph(string s1, string s2, double cost, Graph &g, NameVertexMap &routers) {
9
    // TODO: change routers --> routers_map
10
11
    Vertex v1, v2;
12
    Edge e;
13
14
    typename NameVertexMap::iterator pos;
15
    bool inserted;
16
17
    boost::tie(pos, inserted) = routers.insert(std::make_pair(s1, Vertex()));
18
    if (inserted) {
19
        v1 = boost::add_vertex(g);
20
        routers[s1] = v1;
21
        pos->second = v1;
22
        g[v1].id = s1;
23
        g[v1].name = s1;
24
    } else {
25
        v1 = pos->second;
26
    }
27
28
    boost::tie(pos, inserted) = routers.insert(std::make_pair(s2, Vertex()));
29
    if (inserted) {
30
        v2 = boost::add_vertex(g);
31
        routers[s2] = v2;
32
        pos->second = v2;
33
        g[v2].id = s2;
34
        g[v2].name = s2;
35
    } else {
36
        v2 = pos->second;
37
    }
38
39
    // Add edge (aka. link) connecting 2 vertices
40
    boost::tie(e, inserted) = boost::add_edge(v1, v2, g);
41
    if (inserted) {
42
        g[e].cost = cost;
43
    }
44
}
45
46
void readEdgeFile(string filePath, Graph &g) {
47
    // NameVertexMap is to keep track of which router has already been added
48
    typedef std::map<std::string, Vertex> NameVertexMap;
49
    NameVertexMap routers;
50
51 04a9ef10 Quynh PX Nguyen
    ifstream inFile(filePath);
52
53
    vector<string> strs;
54
    for (string line; getline(inFile, line); /**/) {
55
        boost::split(strs, line, boost::is_any_of(" "));
56
57
        // Cast vector<string> to array<string, 3>
58
        // TODO: this is really crude way to do it.
59
        // TODO: how to copy some element of vector to array
60 6a12e353 Quynh PX Nguyen
        if (strs.size() == 3) {
61
            string source = strs[0];
62
            string target = strs[1];
63
            double cost = stof(strs[2]);
64
65
            addLinkToGraph(source, target, cost, g, routers);
66
67 04a9ef10 Quynh PX Nguyen
        }
68
    }
69
    inFile.close();
70 6a12e353 Quynh PX Nguyen
}
71
72
void readJson(string filePath, Graph &g) {
73
    boost::property_tree::ptree pt;
74
    boost::property_tree::read_json(filePath, pt);
75
76
    // NameVertexMap is to keep track of which router has already been added
77
    typedef std::map<std::string, Vertex> NameVertexMap;
78
    NameVertexMap routers;
79 04a9ef10 Quynh PX Nguyen
80 6a12e353 Quynh PX Nguyen
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("links")) {
81
        cout << "X" << endl;
82
        cout << v.second.get_value<std::string>() << " ";
83
        string source = v.second.get_child("source").get_value<std::string>();
84
        string target = v.second.get_child("target").get_value<std::string>();
85
        double cost = v.second.get_child("cost").get_value<double>();
86
87
88
        addLinkToGraph(source, target, cost, g, routers);
89
    }
90 04a9ef10 Quynh PX Nguyen
}
91 6a12e353 Quynh PX Nguyen
92
void readComplexJson(string filePath, Graph &g) {
93
    boost::property_tree::ptree pt;
94
    boost::property_tree::read_json(filePath, pt);
95
96
    // NameVertexMap is to keep track of which router has already been added
97
    typedef std::map<std::string, Vertex> NameVertexMap;
98
    NameVertexMap routers;
99
100
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("topology")) {
101
        cout << "X" << endl;
102
        cout << v.second.get_value<std::string>() << " ";
103
        string source = v.second.get_child("lastHopIP").get_value<std::string>();
104
        string target = v.second.get_child("destinationIP").get_value<std::string>();
105
        double cost = v.second.get_child("neighborLinkQuality").get_value<double>();
106
107
        addLinkToGraph(source, target, cost, g, routers);
108
    }
109
}