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

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

MARK: Dynamic workflow graph developement. Branch up to date with trunk @1676. Arithmetic filter unified

File size: 8.4 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      StdString color_table[7] = {"black", "red", "blue", "green", "purple", "yellow", "gray"};
76   
77      std::ofstream fs_json;
78      fs_json.open ("graph_data.json", std::fstream::out);
79
80      fs_json << "{\"nodes\":["<<std::endl<<"      ";
81      static bool firstnode=true;
82      static bool firstedge=true;
83   
84      for (auto it=CWorkflowGraph::mapFilters_ptr_with_info->begin(); it != CWorkflowGraph::mapFilters_ptr_with_info->end(); it++)
85      {
86        if(firstnode) 
87        {
88          fs_json << "{\"id\": "<<it->first +1<<", "<<std::endl;
89          firstnode = false;
90        }
91        else
92        {
93          fs_json << ",{\"id\": "<<it->first +1<<", "<<std::endl;
94        }
95        if(it->second.filter_class == 1) // source filter
96          fs_json << "       \"label\": \""<<it->second.filter_name<<"\\n("<<it->second.field_id<<")\", "<<std::endl;
97        else
98          fs_json << "       \"label\": \""<<it->second.filter_name<<"\", "<<std::endl;
99        fs_json << "       \"class\": \""<<it->second.filter_class<<"\", "<<std::endl;
100        fs_json << "       \"filled\": \""<<it->second.filter_filled<<"\", "<<std::endl;
101        fs_json << "       \"type\": \""<<it->second.transform_type<<"\", "<<std::endl;
102        fs_json << "       \"entry\": \""<<it->second.expected_entry_nb<<"\", "<<std::endl;
103        fs_json << "       \"inputs\": \""<<it->second.inputs_complete<<"\", "<<std::endl;
104        fs_json << "       \"tag\": \""<<it->second.filter_tag<<"\", "<<std::endl;
105        if(it->second.clusterID != -1) fs_json << "       \"cid\": \""<<it->second.clusterID<<"\", "<<std::endl;
106        fs_json << "       \"attributes\": \""<<it->second.attributes<<"\"}"<<std::endl<<"      ";
107      }
108      fs_json << "    ],"<<std::endl;
109
110
111      fs_json << " \"edges\" : ["<<std::endl<<"      ";
112
113      for (auto it=CWorkflowGraph::mapFieldToFilters_ptr_with_info->begin(); it != CWorkflowGraph::mapFieldToFilters_ptr_with_info->end(); it++)
114      {
115        if(firstedge)
116        {
117          fs_json << "{\"id\": "<<it->first +1<<", "<<std::endl;
118          firstedge = false;
119        }
120        else
121        {
122          fs_json << ",{\"id\": "<<it->first +1<<", "<<std::endl;
123        }
124        fs_json << "       \"from\": "<<it->second.from+1<<", "<<std::endl;
125        fs_json << "       \"to\": "<<it->second.to+1<<", "<<std::endl;
126        fs_json << "       \"label\": \""<<it->second.field_id<<"\\n"<<it->second.date<<"\", "<<std::endl;
127        // fs_json << "       \"title\": \""<<"Show more information about this field"<<"\", "<<std::endl;
128        // fs_json << "       \"fid\": \""<<it->second.field_id<<"\", "<<std::endl;
129        // fs_json << "       \"fname\": \""<<it->second.field_name<<"\", "<<std::endl;
130        // fs_json << "       \"gid\": \""<<it->second.grid_id<<"\", "<<std::endl;
131        fs_json << "       \"date\": \""<<it->second.date<<"\", "<<std::endl;
132        fs_json << "       \"attributes\": \"id = "<<it->second.field_id<<"</br>"<<it->second.attributes<<"\"}"<<std::endl<<"      ";
133
134      }
135      fs_json << "    ]}"<<std::endl;
136
137
138     
139
140     
141
142      fs_json.close();
143
144
145
146
147      // std::ofstream fs;
148      // fs.open ("graph_with_info.html", std::fstream::out);
149
150      // fs << "<html>" <<std::endl;
151      // fs << "<head>" <<std::endl;
152      // fs << "    <script type=\"text/javascript\" src=\"../../../vis-4.21.0/dist/vis.js\"></script>" <<std::endl;
153      // fs << "    <link href=\"../../../vis-4.21.0/dist/vis.css\" rel=\"stylesheet\" type=\"text/css\" />" <<std::endl <<std::endl;
154      // fs << "    <style type=\"text/css\">"<<std::endl;
155      // fs << "        #mynetwork {"<<std::endl;
156      // fs << "            width: 1000px;"<<std::endl;
157      // fs << "            height: 800px;"<<std::endl;
158      // fs << "            border: 1px solid lightgray"<<std::endl;
159      // fs << "        }"<<std::endl;
160      // fs << "    </style>"<<std::endl;
161      // fs << "</head>"<<std::endl;
162      // fs << "<body>"<<std::endl;
163      // fs << "<div id=\"mynetwork\"></div>"<<std::endl<<std::endl;
164      // fs << "<script type=\"text/javascript\">"<<std::endl;
165
166      // fs << "    var nodes = new vis.DataSet(["<<std::endl;
167      // for(int i=0; i<CWorkflowGraph::filters.size(); i++)
168      // {
169      //   StdString color_table[7] = {"black", "red", "blue", "green", "purple", "yellow", "gray"};
170
171      //   fs << "        {id: "<<i+1<<", label: \'"<<CWorkflowGraph::filters[i]<<"\', title: \'"<<"Show more information about this filter"<<"\'";
172      //   fs <<", color: \'"<<color_table[(*CWorkflowGraph::mapFilters_ptr_with_info)[i].filter_class]<<"\'},"<<std::endl  ;
173      // }
174      // fs << "    ]);"<<std::endl<<std::endl;
175
176
177      // fs << "    var edges = new vis.DataSet(["<<std::endl;
178      // for(int i=0; i<CWorkflowGraph::fieldsToFilters.size(); i++)
179      // {
180      //   fs <<"        {from: "<<CWorkflowGraph::fieldsToFilters[i].first+1<<", to: "<<CWorkflowGraph::fieldsToFilters[i].second+1<<", label: \'"<<CWorkflowGraph::fields[i]<<"\', title: \'"<<"Show more information about this field"<<"\'},"<<std::endl  ;
181      // }
182      // fs << "    ]);"<<std::endl<<std::endl;
183
184 
185      // fs << "    var container = document.getElementById(\'mynetwork\');" <<std::endl<<std::endl;
186      // fs << "    var data = {" <<std::endl;
187      // fs << "        nodes: nodes," <<std::endl;
188      // fs << "        edges: edges" <<std::endl;
189      // fs << "    };" <<std::endl;
190      // fs << "    var options = {"<<std::endl;
191      // fs << "        edges:{"<<std::endl;
192      // fs << "            smooth: false,"<<std::endl;
193      // fs << "            arrows: \'to\',"<<std::endl;
194      // fs << "            color: 'red',"<<std::endl;
195      // fs << "            font:{align: \'middle\'},"<<std::endl;
196      // fs << "        },"<<std::endl;
197      // fs << "        physics:{"<<std::endl;
198      // fs << "            enabled: false,"<<std::endl;
199      // fs << "        },"<<std::endl;
200      // fs << "};" <<std::endl<<std::endl;
201
202      // fs << "    var network = new vis.Network(container, data, options);" <<std::endl<<std::endl;
203
204      // fs << "</script>"<<std::endl;
205      // fs << "</body>"<<std::endl;
206      // fs << "</html>"<<std::endl;
207
208     
209
210      // fs.close();
211         
212    }
213  }
214  CATCH
215
216
217
218  void CGraphviz::showStaticWorkflowGraph()
219  TRY
220  {
221    CWorkflowGraph::showStaticWorkflow();
222  }
223  CATCH
224}
Note: See TracBrowser for help on using the repository browser.