Revision 162e1bda custompackages/graph-parser/src/parser.cpp

View differences:

custompackages/graph-parser/src/parser.cpp
4 4

  
5 5
#include "parser.h"
6 6

  
7
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].label = 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].label = 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) {
7
void readEdgeFileGraphManager(string filepath, GraphManager &gm) {
47 8
    // 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
    ifstream inFile(filePath.c_str());
9
    ifstream inFile(filepath.c_str());
52 10

  
53 11
    vector<string> strs;
54 12
    for (string line; getline(inFile, line); /**/) {
55
        boost::split(strs, line, boost::is_any_of(" "));
13
        boost::split(strs, line, boost::is_any_of(" "), boost::token_compress_on);
56 14

  
57 15
        // Cast vector<string> to array<string, 3>
58 16
        // TODO: this is really crude way to do it.
......
60 18
        if (strs.size() == 3) {
61 19
            string source = strs[0];
62 20
            string target = strs[1];
21

  
22
            GraphManager::VertexProperties vp1 = GraphManager::VertexProperties(source, source);
23
            GraphManager::VertexProperties vp2 = GraphManager::VertexProperties(target, target);
24

  
63 25
            // TODO: use atof as a way around the error: ‘stof’ was not declared in this scope
64 26
            // double cost = stof(strs[2]);
65 27
            double cost = atof(strs[2].c_str());
66 28

  
67
            addLinkToGraph(source, target, cost, g, routers);
68

  
29
            GraphManager::EdgeProperties ep = GraphManager::EdgeProperties(cost);
30
            gm.AddEdge(vp1, vp2, ep);
69 31
        }
70 32
    }
33
    gm.ResetVerticesAndEdgesIndexMap();
71 34
    inFile.close();
72 35
}
73 36

  
74
void readJson(string filePath, Graph &g) {
37
void readJsonGraphManager(string filepath, GraphManager &gm) {
75 38
    boost::property_tree::ptree pt;
76
    boost::property_tree::read_json(filePath, pt);
77

  
78
    // NameVertexMap is to keep track of which router has already been added
79
    typedef std::map<std::string, Vertex> NameVertexMap;
80
    NameVertexMap routers;
39
    boost::property_tree::read_json(filepath, pt);
81 40

  
82 41
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("links")) {
83
        cout << "X" << endl;
84 42
        cout << v.second.get_value<std::string>() << " ";
85 43
        string source = v.second.get_child("source").get_value<std::string>();
86 44
        string target = v.second.get_child("target").get_value<std::string>();
87 45
        double cost = v.second.get_child("cost").get_value<double>();
88 46

  
89

  
90
        addLinkToGraph(source, target, cost, g, routers);
47
        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);
91 51
    }
92 52
}
93 53

  
94
void readComplexJson(string filePath, Graph &g) {
54
void readComplexJsonGraphManager(string filepath, GraphManager &gm) {
95 55
    boost::property_tree::ptree pt;
96
    boost::property_tree::read_json(filePath, pt);
97

  
98
    // NameVertexMap is to keep track of which router has already been added
99
    typedef std::map<std::string, Vertex> NameVertexMap;
100
    NameVertexMap routers;
56
    boost::property_tree::read_json(filepath, pt);
101 57

  
102 58
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("topology")) {
103
        cout << "X" << endl;
104 59
        cout << v.second.get_value<std::string>() << " ";
105 60
        string source = v.second.get_child("lastHopIP").get_value<std::string>();
106 61
        string target = v.second.get_child("destinationIP").get_value<std::string>();
107 62
        double cost = v.second.get_child("neighborLinkQuality").get_value<double>();
108 63

  
109
        addLinkToGraph(source, target, cost, g, routers);
64
        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);
110 68
    }
111 69
}
112

  
113
void readEdgeFileGraphManager(string filePath, GraphManager &gm) {
114
    // NameVertexMap is to keep track of which router has already been added
115
    ifstream inFile(filePath.c_str());
116

  
117
    vector<string> strs;
118
    for (string line; getline(inFile, line); /**/) {
119
        boost::split(strs, line, boost::is_any_of(" "));
120

  
121
        // Cast vector<string> to array<string, 3>
122
        // TODO: this is really crude way to do it.
123
        // TODO: how to copy some element of vector to array
124
        if (strs.size() == 3) {
125
            string source = strs[0];
126
            string target = strs[1];
127

  
128
            GraphManager::VertexProperties vp1 = GraphManager::VertexProperties(source, source);
129
            GraphManager::VertexProperties vp2 = GraphManager::VertexProperties(target, target);
130

  
131
            // TODO: use atof as a way around the error: ‘stof’ was not declared in this scope
132
            // double cost = stof(strs[2]);
133
            double cost = atof(strs[2].c_str());
134

  
135
            GraphManager::EdgeProperties ep = GraphManager::EdgeProperties(cost);
136

  
137
            gm.AddEdge(vp1, vp2, ep);
138
        }
139
    }
140
    gm.ResetVerticesAndEdgesIndexMap();
141
    inFile.close();
142
}

Also available in: Unified diff