Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.91 KB)

1
//
2
// Created by quynh on 12/13/15.
3
//
4

    
5
#include "parser.h"
6

    
7
void readEdgeFileGraphManager(string filepath, GraphManager &gm) {
8
    // NameVertexMap is to keep track of which router has already been added
9
    ifstream inFile(filepath.c_str());
10

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

    
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
        if (strs.size() == 3) {
19
            string source = strs[0];
20
            string target = strs[1];
21

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

    
25
            // 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

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

    
37
void readJsonGraphManager(string filepath, GraphManager &gm) {
38
    boost::property_tree::ptree pt;
39
    boost::property_tree::read_json(filepath, pt);
40

    
41
    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
        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
    }
52
}
53

    
54
void readComplexJsonGraphManager(string filepath, GraphManager &gm) {
55
    boost::property_tree::ptree pt;
56
    boost::property_tree::read_json(filepath, pt);
57

    
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
        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
    }
69
}