root / custompackages / graph-parser / src / parser.cpp @ 437fd680
History | View | Annotate | Download (4.64 KB)
1 |
//
|
---|---|
2 |
// Created by quynh on 12/13/15.
|
3 |
//
|
4 |
|
5 |
#include "parser.h" |
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) { |
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 |
ifstream inFile(filePath.c_str()); |
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 |
if (strs.size() == 3) { |
61 |
string source = strs[0]; |
62 |
string target = strs[1]; |
63 |
// TODO: use atof as a way around the error: ‘stof’ was not declared in this scope
|
64 |
// double cost = stof(strs[2]);
|
65 |
double cost = atof(strs[2].c_str()); |
66 |
|
67 |
addLinkToGraph(source, target, cost, g, routers); |
68 |
|
69 |
} |
70 |
} |
71 |
inFile.close(); |
72 |
} |
73 |
|
74 |
void readJson(string filePath, Graph &g) { |
75 |
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; |
81 |
|
82 |
BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("links")) { |
83 |
cout << "X" << endl;
|
84 |
cout << v.second.get_value<std::string>() << " "; |
85 |
string source = v.second.get_child("source").get_value<std::string>(); |
86 |
string target = v.second.get_child("target").get_value<std::string>(); |
87 |
double cost = v.second.get_child("cost").get_value<double>(); |
88 |
|
89 |
|
90 |
addLinkToGraph(source, target, cost, g, routers); |
91 |
} |
92 |
} |
93 |
|
94 |
void readComplexJson(string filePath, Graph &g) { |
95 |
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; |
101 |
|
102 |
BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, pt.get_child("topology")) { |
103 |
cout << "X" << endl;
|
104 |
cout << v.second.get_value<std::string>() << " "; |
105 |
string source = v.second.get_child("lastHopIP").get_value<std::string>(); |
106 |
string target = v.second.get_child("destinationIP").get_value<std::string>(); |
107 |
double cost = v.second.get_child("neighborLinkQuality").get_value<double>(); |
108 |
|
109 |
addLinkToGraph(source, target, cost, g, routers); |
110 |
} |
111 |
} |
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 |
} |