source: XIOS/dev/dev_trunk_omp/src/graphviz.cpp @ 1679

Last change on this file since 1679 was 1679, checked in by yushan, 5 years ago

MARK: Dynamic workflow graph developement. Branch up to date with trunk @1676. Using vis.js

File size: 6.7 KB
Line 
1#include "graphviz.hpp"
2#include "workflow_graph.hpp"
3
4namespace 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,
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));
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();
74   
75      std::ofstream fs_json;
76      fs_json.open ("graph_data.json", std::fstream::out);
77
78      fs_json << "{ \"node\" : ["<<std::endl;
79   
80      for (auto it=CWorkflowGraph::mapFilters_ptr_with_info->begin(); it != CWorkflowGraph::mapFilters_ptr_with_info->end(); it++)
81      {
82        fs_json << "        {\"id\": "<<it->first +1<<"}, "<<std::endl;
83        fs_json << "            {\"label\": \""<<it->second.filter_name<<"\"}, "<<std::endl  ;
84        fs_json << "            {\"class\": "<<it->second.filter_class<<"}, "<<std::endl  ;
85        fs_json << "            {\"filled\": "<<it->second.filter_filled<<"}, "<<std::endl  ;
86        fs_json << "            {\"entry\": "<<it->second.expected_entry_nb<<"}, "<<std::endl  ;
87        // fs_json << "            {\"date\": \""<<it->second.date<<"\"}, "<<std::endl  ;
88        fs_json << "            {\"type\": \""<<it->second.transform_type<<"\"}, "<<std::endl  ;
89      }
90      fs_json << "    ]}"<<std::endl<<std::endl;
91
92      fs_json << "{ \"edge\" : ["<<std::endl;
93
94      for (auto it=CWorkflowGraph::mapFieldToFilters_ptr_with_info->begin(); it != CWorkflowGraph::mapFieldToFilters_ptr_with_info->end(); it++)
95      {
96        fs_json << "        {\"id\": "<<it->first +1<<"}, "<<std::endl;
97        fs_json << "            {\"from\": "<<it->second.from +1<<"}, "<<std::endl;
98        fs_json << "            {\"to\": "<<it->second.to+1<<"}, "<<std::endl  ;
99        fs_json << "            {\"fid\": \""<<it->second.field_id<<"\"}, "<<std::endl  ;
100        fs_json << "            {\"fname\": \""<<it->second.field_name<<"\"}, "<<std::endl  ;
101        fs_json << "            {\"gid\": \""<<it->second.grid_id<<"\"}, "<<std::endl  ;
102        fs_json << "            {\"date\": \""<<it->second.date<<"\"}, "<<std::endl  ;
103      }
104      fs_json << "    ]}"<<std::endl<<std::endl;
105
106
107     
108
109     
110
111      fs_json.close();
112
113
114
115
116      std::ofstream fs;
117      fs.open ("graph_with_info.html", std::fstream::out);
118
119      fs << "<html>" <<std::endl;
120      fs << "<head>" <<std::endl;
121      fs << "    <script type=\"text/javascript\" src=\"../../../vis-4.21.0/dist/vis.js\"></script>" <<std::endl;
122      fs << "    <link href=\"../../../vis-4.21.0/dist/vis.css\" rel=\"stylesheet\" type=\"text/css\" />" <<std::endl <<std::endl;
123      fs << "    <style type=\"text/css\">"<<std::endl;
124      fs << "        #mynetwork {"<<std::endl;
125      fs << "            width: 1000px;"<<std::endl;
126      fs << "            height: 800px;"<<std::endl;
127      fs << "            border: 1px solid lightgray"<<std::endl;
128      fs << "        }"<<std::endl;
129      fs << "    </style>"<<std::endl;
130      fs << "</head>"<<std::endl;
131      fs << "<body>"<<std::endl;
132      fs << "<div id=\"mynetwork\"></div>"<<std::endl<<std::endl;
133      fs << "<script type=\"text/javascript\">"<<std::endl;
134
135      fs << "    var nodes = new vis.DataSet(["<<std::endl;
136      for(int i=0; i<CWorkflowGraph::filters.size(); i++)
137      {
138        fs << "        {id: "<<i+1<<", label: \'"<<CWorkflowGraph::filters[i]<<"\'},"<<std::endl  ;
139      }
140      fs << "    ]);"<<std::endl<<std::endl;
141
142
143      fs << "    var edges = new vis.DataSet(["<<std::endl;
144      for(int i=0; i<CWorkflowGraph::fieldsToFilters.size(); i++)
145      {
146        fs <<"        {from: "<<CWorkflowGraph::fieldsToFilters[i].first+1<<", to: "<<CWorkflowGraph::fieldsToFilters[i].second+1<<", label: \'"<<CWorkflowGraph::fields[i]<<"\'},"<<std::endl;
147      }
148      fs << "    ]);"<<std::endl<<std::endl;
149
150 
151      fs << "    var container = document.getElementById(\'mynetwork\');" <<std::endl<<std::endl;
152      fs << "    var data = {" <<std::endl;
153      fs << "        nodes: nodes," <<std::endl;
154      fs << "        edges: edges" <<std::endl;
155      fs << "    };" <<std::endl;
156      fs << "    var options = {"<<std::endl;
157      fs << "        edges:{"<<std::endl;
158      fs << "            smooth: false,"<<std::endl;
159      fs << "            arrows: \'to\',"<<std::endl;
160      fs << "            color: 'red',"<<std::endl;
161      fs << "            font:{align: \'middle\'},"<<std::endl;
162      fs << "        },"<<std::endl;
163      fs << "        physics:{"<<std::endl;
164      fs << "            enabled: false,"<<std::endl;
165      fs << "        },"<<std::endl;
166      fs << "};" <<std::endl<<std::endl;
167
168      fs << "    var network = new vis.Network(container, data, options);" <<std::endl<<std::endl;
169
170      fs << "</script>"<<std::endl;
171      fs << "</body>"<<std::endl;
172      fs << "</html>"<<std::endl;
173
174     
175
176      fs.close();
177         
178    }
179  }
180  CATCH
181
182
183
184  void CGraphviz::showStaticWorkflowGraph()
185  TRY
186  {
187    CWorkflowGraph::showStaticWorkflow();
188  }
189  CATCH
190}
Note: See TracBrowser for help on using the repository browser.