[1704] | 1 | #include "graphviz.hpp" |
---|
| 2 | #include "workflow_graph.hpp" |
---|
| 3 | |
---|
| 4 | namespace xios |
---|
| 5 | { |
---|
| 6 | |
---|
| 7 | CGraphviz::CGraphviz() |
---|
| 8 | { } |
---|
| 9 | |
---|
| 10 | /*! |
---|
| 11 | * |
---|
| 12 | */ |
---|
| 13 | void CGraphviz::buildWorkflowGraphDot() |
---|
| 14 | TRY |
---|
| 15 | { |
---|
| 16 | if (CWorkflowGraph::mapFieldToFilters_ptr_with_info !=0 && !CWorkflowGraph::mapFieldToFilters_ptr_with_info->empty()) |
---|
| 17 | { |
---|
| 18 | CWorkflowGraph::buildStaticWorkflow(); |
---|
| 19 | |
---|
| 20 | typedef boost::property<boost::edge_name_t, std::string> EdgeProperty; |
---|
| 21 | typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProperty> Graph; |
---|
| 22 | |
---|
| 23 | // Input 1: nodes (=filters) |
---|
| 24 | vector<StdString>& nodes = CWorkflowGraph::filters; |
---|
| 25 | const int nbNodes = nodes.size(); |
---|
| 26 | |
---|
| 27 | // Input 2: edges (=fields) |
---|
| 28 | vector<StdString>& edges = CWorkflowGraph::fields; |
---|
| 29 | |
---|
| 30 | // Input 3: nodes to edges connectivity |
---|
| 31 | vector<pair<int, int> >& nodesToEdges = CWorkflowGraph::fieldsToFilters; |
---|
| 32 | const int nbEdges = nodesToEdges.size(); |
---|
| 33 | |
---|
| 34 | // Declare a graph object, adding the edges and edge properties |
---|
| 35 | Graph g(nbNodes); |
---|
| 36 | for (int j = 0; j < nbEdges; ++j) |
---|
| 37 | { |
---|
| 38 | graph_traits<Graph>::edge_descriptor e; |
---|
| 39 | bool inserted; |
---|
| 40 | boost::tie(e, inserted) = boost::add_edge(nodesToEdges[j].first, nodesToEdges[j].second, edges[j], g); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | std::for_each(vertices(g).first, vertices(g).second, exercise_vertex<Graph>(g)); |
---|
| 44 | |
---|
| 45 | std::map<std::string,std::string> graph_attr, vertex_attr, edge_attr; |
---|
| 46 | graph_attr["size"] = "5,5"; |
---|
| 47 | graph_attr["rankdir"] = "LR"; |
---|
| 48 | graph_attr["ratio"] = "fill"; |
---|
| 49 | vertex_attr["shape"] = "record"; |
---|
| 50 | vertex_attr["width"] = "2.2"; |
---|
| 51 | vertex_attr["fontsize"] = "16"; |
---|
| 52 | |
---|
| 53 | const std::string filename = "graph.dot"; |
---|
| 54 | std::ofstream file (filename.c_str()); |
---|
| 55 | |
---|
| 56 | boost::write_graphviz(file, g, |
---|
[1877] | 57 | boost::make_label_writer(&nodes[0]), |
---|
| 58 | boost::make_label_writer(get(edge_name, g)), |
---|
| 59 | boost::make_graph_attributes_writer(graph_attr, vertex_attr, edge_attr)); |
---|
[1704] | 60 | |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | CATCH |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | void CGraphviz::buildWorkflowGraphVisjs_with_info() |
---|
| 69 | TRY |
---|
| 70 | { |
---|
| 71 | if (CWorkflowGraph::mapFilters_ptr_with_info !=0 && !CWorkflowGraph::mapFilters_ptr_with_info->empty()) |
---|
| 72 | { |
---|
| 73 | CWorkflowGraph::buildStaticWorkflow_with_info(); |
---|
[1877] | 74 | int nbGraphContext = CWorkflowGraph::mapContext_ptr->size(); |
---|
[1704] | 75 | |
---|
| 76 | StdString color_table[7] = {"black", "red", "blue", "green", "purple", "yellow", "gray"}; |
---|
| 77 | |
---|
[1877] | 78 | std::ofstream fs_json[nbGraphContext]; |
---|
| 79 | |
---|
| 80 | for (std::unordered_map<StdString, int>::const_iterator it = CWorkflowGraph::mapContext_ptr->begin(); it != CWorkflowGraph::mapContext_ptr->end(); ++it) |
---|
| 81 | { |
---|
| 82 | fs_json[it->second].open ("graph_data_"+it->first+".json", std::fstream::out); |
---|
| 83 | fs_json[it->second] << "{\"nodes\":["<<std::endl<<" "; |
---|
| 84 | } |
---|
[1704] | 85 | |
---|
[1877] | 86 | static bool firstnodes[5]; |
---|
| 87 | static bool firstedges[5]; |
---|
| 88 | |
---|
| 89 | for(int i=0; i<nbGraphContext; i++) |
---|
| 90 | { |
---|
| 91 | firstnodes[i]=true; |
---|
| 92 | firstedges[i]=true; |
---|
| 93 | } |
---|
| 94 | |
---|
[1704] | 95 | for (auto it=CWorkflowGraph::mapFilters_ptr_with_info->begin(); it != CWorkflowGraph::mapFilters_ptr_with_info->end(); it++) |
---|
| 96 | { |
---|
[1877] | 97 | int index = (*CWorkflowGraph::mapContext_ptr)[it->second.node_context_id]; |
---|
| 98 | if(firstnodes[index]) |
---|
[1704] | 99 | { |
---|
[1877] | 100 | fs_json[index] << "{\"id\": "<<it->first +1<<", "<<std::endl; |
---|
| 101 | firstnodes[index] = false; |
---|
[1704] | 102 | } |
---|
| 103 | else |
---|
| 104 | { |
---|
[1877] | 105 | fs_json[index] << ",{\"id\": "<<it->first +1<<", "<<std::endl; |
---|
[1704] | 106 | } |
---|
| 107 | if(it->second.filter_class == 1) // source filter |
---|
[1877] | 108 | fs_json[index] << " \"label\": \""<<it->second.filter_name<<"\\n("<<it->second.field_id<<")\", "<<std::endl; |
---|
[1704] | 109 | else |
---|
[1877] | 110 | fs_json[index] << " \"label\": \""<<it->second.filter_name<<"\", "<<std::endl; |
---|
| 111 | fs_json[index] << " \"class\": "<<it->second.filter_class<<", "<<std::endl; |
---|
| 112 | fs_json[index] << " \"filled\": "<<it->second.filter_filled<<", "<<std::endl; |
---|
| 113 | fs_json[index] << " \"type\": \""<<it->second.transform_type<<"\", "<<std::endl; |
---|
| 114 | fs_json[index] << " \"entry\": "<<it->second.expected_entry_nb<<", "<<std::endl; |
---|
| 115 | fs_json[index] << " \"inputs\": "<<it->second.inputs_complete<<", "<<std::endl; |
---|
| 116 | fs_json[index] << " \"tag\": "<<it->second.filter_tag<<", "<<std::endl; |
---|
| 117 | fs_json[index] << " \"cid\": "<<it->second.clusterID<<", "<<std::endl; |
---|
| 118 | fs_json[index] << " \"distance\": "<<it->second.distance<<", "<<std::endl; |
---|
| 119 | fs_json[index] << " \"context\": \""<<it->second.node_context_id<<"\", "<<std::endl; |
---|
| 120 | fs_json[index] << " \"attributes\": \""<<it->second.attributes<<"\"}"<<std::endl<<" "; |
---|
[1704] | 121 | } |
---|
[1877] | 122 | for(int i=0; i<nbGraphContext; i++) |
---|
| 123 | { |
---|
| 124 | fs_json[i] << " ],"<<std::endl; |
---|
| 125 | fs_json[i] << " \"edges\" : ["<<std::endl<<" "; |
---|
| 126 | } |
---|
[1704] | 127 | |
---|
| 128 | for (auto it=CWorkflowGraph::mapFieldToFilters_ptr_with_info->begin(); it != CWorkflowGraph::mapFieldToFilters_ptr_with_info->end(); it++) |
---|
| 129 | { |
---|
[1877] | 130 | int index = (*CWorkflowGraph::mapContext_ptr)[it->second.edge_context_id]; |
---|
| 131 | if(firstedges[index]) |
---|
[1704] | 132 | { |
---|
[1877] | 133 | fs_json[index] << "{\"id\": "<<it->first +1<<", "<<std::endl; |
---|
| 134 | firstedges[index] = false; |
---|
[1704] | 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
[1877] | 138 | fs_json[index] << ",{\"id\": "<<it->first +1<<", "<<std::endl; |
---|
[1704] | 139 | } |
---|
[1877] | 140 | fs_json[index] << " \"from\": "<<it->second.from+1<<", "<<std::endl; |
---|
| 141 | fs_json[index] << " \"to\": "<<it->second.to+1<<", "<<std::endl; |
---|
| 142 | fs_json[index] << " \"label\": \""<<it->second.field_id<<"\\n"<<it->second.date<<"\", "<<std::endl; |
---|
[1704] | 143 | // fs_json << " \"title\": \""<<"Show more information about this field"<<"\", "<<std::endl; |
---|
| 144 | // fs_json << " \"fid\": \""<<it->second.field_id<<"\", "<<std::endl; |
---|
| 145 | // fs_json << " \"fname\": \""<<it->second.field_name<<"\", "<<std::endl; |
---|
| 146 | // fs_json << " \"gid\": \""<<it->second.grid_id<<"\", "<<std::endl; |
---|
[1877] | 147 | fs_json[index] << " \"date\": \""<<it->second.date<<"\", "<<std::endl; |
---|
| 148 | fs_json[index] << " \"context\": \""<<it->second.edge_context_id<<"\", "<<std::endl; |
---|
| 149 | fs_json[index] << " \"attributes\": \"id = "<<it->second.field_id<<"</br>"<<it->second.attributes<<"\"}"<<std::endl<<" "; |
---|
[1704] | 150 | } |
---|
[1877] | 151 | for(int i=0; i<nbGraphContext; i++) |
---|
| 152 | { |
---|
| 153 | fs_json[i] << " ]}"<<std::endl; |
---|
| 154 | fs_json[i].close(); |
---|
| 155 | } |
---|
[1704] | 156 | } |
---|
| 157 | } |
---|
| 158 | CATCH |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | void CGraphviz::showStaticWorkflowGraph() |
---|
| 163 | TRY |
---|
| 164 | { |
---|
| 165 | CWorkflowGraph::showStaticWorkflow(); |
---|
| 166 | } |
---|
| 167 | CATCH |
---|
| 168 | } |
---|