source: XIOS/dev/dev_trunk_graph/src/workflow_graph.cpp @ 2028

Last change on this file since 2028 was 2028, checked in by yushan, 9 months ago

Graph intermediate commit to a tmp branch.

File size: 12.8 KB
Line 
1#include "workflow_graph.hpp"
2#include "cxios.hpp"
3
4namespace xios
5{
6
7  std::vector<graph_node_object> *CWorkflowGraph::vectorOfNodes_ = 0;
8  std::vector<graph_edge_object> *CWorkflowGraph::vectorOfEdges_ = 0;
9  std::vector<StdString> *CWorkflowGraph::vectorOfContexts_ = 0;
10
11  std::vector<graph_node_object> *CWorkflowGraph::vectorOfNodes_srv_ = 0;
12  std::vector<graph_edge_object> *CWorkflowGraph::vectorOfEdges_srv_ = 0;
13  std::vector<StdString> *CWorkflowGraph::vectorOfContexts_srv_ = 0;
14
15  bool CWorkflowGraph::clientGraphBuilt = false;
16  bool CWorkflowGraph::serverGraphBuilt = false;
17  bool CWorkflowGraph::build_begin = false;
18
19
20  CWorkflowGraph::CWorkflowGraph()
21  { }
22
23
24//******************************************************
25
26  void CWorkflowGraph::outputWorkflowGraph_client_stdout()
27  {
28    std::cout<<"\n\nbuild workflow graph ..."<<std::endl;
29    for(int i=0; i<vectorOfNodes_->size(); i++)
30    {
31      std::cout<<"Node["<<i<<"] is "<<(*vectorOfNodes_)[i].filter_name<<std::endl;
32    }
33 
34    for(int i=0; i<vectorOfEdges_->size(); i++)
35    {
36      std::cout<<"Edge["<<i<<"] from "<<(*vectorOfEdges_)[i].from<<" to "<<(*vectorOfEdges_)[i].to<<std::endl;
37    }
38    std::cout<<"\nend workflow graph ...\n\n"<<std::endl;
39  }
40 
41  void CWorkflowGraph::outputWorkflowGraph_server_stdout()
42  {
43    std::cout<<"\n\nServer side : build workflow graph ..."<<std::endl;
44    for(int i=0; i<vectorOfNodes_srv_->size(); i++)
45    {
46      std::cout<<"Node["<<i<<"] is "<<(*vectorOfNodes_srv_)[i].filter_name<<std::endl;
47    }
48 
49    for(int i=0; i<vectorOfEdges_srv_->size(); i++)
50    {
51      std::cout<<"Edge["<<i<<"] from "<<(*vectorOfEdges_srv_)[i].from<<" to "<<(*vectorOfEdges_srv_)[i].to<<std::endl;
52    }
53    std::cout<<"\nend workflow graph ...\n\n"<<std::endl;
54  }
55
56
57  void CWorkflowGraph::drawWorkFlowGraph_client()
58  TRY
59  {
60    if(vectorOfNodes_ && vectorOfEdges_) 
61    {
62      //outputWorkflowGraph_client_stdout();
63      outputWorkflowGraph_client();
64    }
65    else std::cout<<"graph information not complete"<<std::endl;
66  }
67  CATCH
68 
69
70  void CWorkflowGraph::drawWorkFlowGraph_server()
71  TRY
72  {
73    if(vectorOfNodes_srv_ && vectorOfEdges_srv_) 
74    {
75      //outputWorkflowGraph_server_stdout();
76      outputWorkflowGraph_server();
77    }
78    else std::cout<<"Server side : graph information not complete"<<std::endl;
79
80  }
81  CATCH
82 
83  void CWorkflowGraph::addEdge(int from, int to, CDataPacketPtr packet)
84  TRY
85  {
86    if(from<0) return;
87
88    if(CXios::isClient)
89    {
90      if(!vectorOfEdges_) vectorOfEdges_ = new std::vector<graph_edge_object>;
91      std::string currentContextId = CContext::getCurrent()->getId();
92     
93      graph_edge_object edge_obj;   
94      edge_obj.from = from;
95      edge_obj.to = to;
96      edge_obj.date = packet->date;
97      edge_obj.timestamp = packet->timestamp;
98      edge_obj.field = packet->graphPackage->currentField;
99      edge_obj.show = true;
100     
101      if(vectorOfNodes_->at(from).filter_class == 2) // from pass through filter
102      {
103        edge_obj.label_info = vectorOfNodes_->at(from).label_field_id;
104      }
105     
106      for(int i=0; i<vectorOfContexts_->size(); i++)
107      {
108        if(vectorOfContexts_->at(i) == currentContextId)
109        {
110          edge_obj.context = i;     
111          edge_obj.context_id = currentContextId;     
112          break;
113        }
114      } 
115      edge_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
116     
117      vectorOfEdges_->push_back(edge_obj);
118      std::cout<<"****************** Add Edge from "<<from<<" to "<<to<<std::endl; 
119      vectorOfNodes_->at(from).filter_filled = true;
120    }
121    else
122    {
123      if(!vectorOfEdges_srv_) vectorOfEdges_srv_ = new std::vector<graph_edge_object>;
124      std::string currentContextId = CContext::getCurrent()->getId();
125     
126      graph_edge_object edge_obj;   
127      edge_obj.from = from;
128      edge_obj.to = to;
129      edge_obj.date = packet->date;
130      edge_obj.timestamp = packet->timestamp;
131      edge_obj.field = packet->graphPackage->currentField;
132      edge_obj.show = true;
133      for(int i=0; i<vectorOfContexts_srv_->size(); i++)
134      {
135        if(vectorOfContexts_srv_->at(i) == currentContextId)
136        {
137          edge_obj.context = i;     
138          edge_obj.context_id = currentContextId;     
139          break;
140        }
141      }
142      edge_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
143     
144      vectorOfEdges_srv_->push_back(edge_obj);
145      std::cout<<"****************** Server side : Add Edge from "<<from<<" to "<<to<<std::endl; 
146      vectorOfNodes_srv_->at(from).filter_filled = true;
147
148    }
149  }
150  CATCH
151 
152
153  void CWorkflowGraph::addNode(StdString filterName, int filterClass, bool filterFilled, int entryNb, CDataPacketPtr packet)
154  TRY
155  {
156    if(CXios::isClient)
157    {
158      if(!vectorOfNodes_) vectorOfNodes_ = new std::vector<graph_node_object>;
159      if(!vectorOfContexts_) vectorOfContexts_ = new std::vector<StdString>;
160      std::string currentContextId = CContext::getCurrent()->getId();
161      if ( std::find(vectorOfContexts_->begin(), vectorOfContexts_->end(), currentContextId) == vectorOfContexts_->end() )
162         vectorOfContexts_->push_back(currentContextId);
163     
164     
165      graph_node_object node_obj;   
166      node_obj.filter_name = filterName;
167      node_obj.filter_class = filterClass;
168      node_obj.filter_filled = filterFilled;
169      node_obj.expected_entry_nb = entryNb;
170      node_obj.date = packet->date;
171      node_obj.timestamp = packet->timestamp;
172      for(int i=0; i<vectorOfContexts_->size(); i++)
173      {
174        if(vectorOfContexts_->at(i) == currentContextId)
175        {
176          node_obj.context = i;     
177          node_obj.context_id = currentContextId;     
178          break;
179        }
180      }   
181      node_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
182
183      vectorOfNodes_->push_back(node_obj);
184      std::cout<<"****************** Add node "<<filterName<<std::endl;   
185    }
186    else
187    { 
188      if(!vectorOfNodes_srv_) vectorOfNodes_srv_ = new std::vector<graph_node_object>;
189      if(!vectorOfContexts_srv_) vectorOfContexts_srv_ = new std::vector<StdString>;
190      std::string currentContextId = CContext::getCurrent()->getId();
191      if ( std::find(vectorOfContexts_srv_->begin(), vectorOfContexts_srv_->end(), currentContextId) == vectorOfContexts_srv_->end() )
192         vectorOfContexts_srv_->push_back(currentContextId);
193     
194      graph_node_object node_obj;   
195      node_obj.filter_name = filterName;
196      node_obj.filter_class = filterClass;
197      node_obj.filter_filled = filterFilled;
198      node_obj.expected_entry_nb = entryNb;
199      node_obj.date = packet->date;
200      node_obj.timestamp = packet->timestamp;
201      for(int i=0; i<vectorOfContexts_srv_->size(); i++)
202      {
203        if(vectorOfContexts_srv_->at(i) == currentContextId)
204        {
205          node_obj.context = i;     
206          node_obj.context_id = currentContextId;     
207          break;
208        }
209      } 
210      node_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
211
212      vectorOfNodes_srv_->push_back(node_obj);
213      std::cout<<"******************Server side : Add node "<<filterName<<std::endl; 
214    }
215
216  }
217  CATCH
218
219  int CWorkflowGraph::getNodeSize()
220  TRY
221  {
222    if(CXios::isClient)
223    {
224      return !vectorOfNodes_? 0 : vectorOfNodes_->size();
225    }
226    else
227    {
228      return !vectorOfNodes_srv_? 0 : vectorOfNodes_srv_->size();
229    }
230  }
231  CATCH
232
233
234
235  void CWorkflowGraph::outputWorkflowGraph_client()
236  {
237    int graph_rank;
238    MPI_Comm_rank(MPI_COMM_WORLD, &graph_rank);
239    std::ofstream *outfiles;
240
241    outfiles = new std::ofstream[vectorOfContexts_->size()];
242
243    for(int ctx=0; ctx<vectorOfContexts_->size(); ctx++)
244    {
245      StdString graphFileName="graph_data_"+vectorOfContexts_->at(ctx)+"_client_"+to_string(graph_rank)+".json";
246      outfiles[ctx].open(graphFileName); 
247   
248      outfiles[ctx] << "{\"nodes\":["<<std::endl;
249    }
250    for(int i=0; i<vectorOfNodes_->size(); i++)
251    {
252      int ctx = vectorOfNodes_->at(i).context;
253      if(i!=0) outfiles[ctx] << ",";
254      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
255      outfiles[ctx] << "\"label\":"<<"\""<<vectorOfNodes_->at(i).filter_name<<"\","<<std::endl;
256      outfiles[ctx] << "\"class\":"<<vectorOfNodes_->at(i).filter_class<<","<<std::endl;
257      outfiles[ctx] << "\"filled\":"<<!(vectorOfNodes_->at(i).filter_filled)<<","<<std::endl;
258      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfNodes_->at(i).context_id<<"\","<<std::endl;
259      outfiles[ctx] << "\"entry\":"<<"\""<<vectorOfNodes_->at(i).expected_entry_nb<<"\","<<std::endl;
260      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfNodes_->at(i).attributes<<"\","<<std::endl;
261      outfiles[ctx] << "\"type\":"<<"\"\"}"<<std::endl;
262    } 
263    for(int ctx=0; ctx<vectorOfContexts_->size(); ctx++)
264    {
265      outfiles[ctx] << std::endl<<"],"<<std::endl<<"\"edges\" : ["<<std::endl;
266    }
267    for(int i=0; i<vectorOfEdges_->size(); i++)
268    {
269      int ctx = vectorOfEdges_->at(i).context;
270      if(i!=0) outfiles[ctx] << ",";
271      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
272      outfiles[ctx] << "\"from\":"<<vectorOfEdges_->at(i).from<<","<<std::endl;
273      outfiles[ctx] << "\"to\":"<<vectorOfEdges_->at(i).to<<","<<std::endl;
274      if(vectorOfEdges_->at(i).label_info != "none")
275      {
276        if(vectorOfEdges_->at(i).show) outfiles[ctx] << "\"label\":"<<"\""<<vectorOfEdges_->at(i).label_info<<"\\n"<<vectorOfEdges_->at(i).date<<"\","<<std::endl;
277        else outfiles[ctx] << "\"label\":"<<"\"\\n"<<vectorOfEdges_->at(i).date<<"\","<<std::endl;
278      }
279      else
280      {
281        if(vectorOfEdges_->at(i).show) outfiles[ctx] << "\"label\":"<<"\""<<vectorOfEdges_->at(i).field->getId()<<"\\n"<<vectorOfEdges_->at(i).date<<"\","<<std::endl;
282        else outfiles[ctx] << "\"label\":"<<"\"\\n"<<vectorOfEdges_->at(i).date<<"\","<<std::endl;
283      }
284      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfEdges_->at(i).context_id<<"\","<<std::endl;
285      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfEdges_->at(i).attributes<<"\","<<std::endl;
286      outfiles[ctx] << "\"date\":"<<"\""<<vectorOfEdges_->at(i).date<<"\"}"<<std::endl;
287    }
288    for(int ctx=0; ctx<vectorOfContexts_->size(); ctx++)
289    {
290      outfiles[ctx] << std::endl<<"]}"<<std::endl;
291      outfiles[ctx].close();
292    }
293  }
294 
295
296  void CWorkflowGraph::outputWorkflowGraph_server()
297  {
298    int graph_rank;
299    MPI_Comm_rank(MPI_COMM_WORLD, &graph_rank);
300    std::ofstream *outfiles;
301
302    outfiles = new std::ofstream[vectorOfContexts_srv_->size()];
303
304    for(int ctx=0; ctx<vectorOfContexts_srv_->size(); ctx++)
305    {
306      StdString graphFileName="graph_data_"+vectorOfContexts_srv_->at(ctx)+"_client_"+to_string(graph_rank)+".json";
307      outfiles[ctx].open(graphFileName); 
308   
309      outfiles[ctx] << "{\"nodes\":["<<std::endl;
310    }
311    for(int i=0; i<vectorOfNodes_srv_->size(); i++)
312    {
313      int ctx = vectorOfNodes_srv_->at(i).context;
314      if(i!=0) outfiles[ctx] << ",";
315      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
316      outfiles[ctx] << "\"label\":"<<"\""<<vectorOfNodes_srv_->at(i).filter_name<<"\","<<std::endl;
317      outfiles[ctx] << "\"class\":"<<vectorOfNodes_srv_->at(i).filter_class<<","<<std::endl;
318      outfiles[ctx] << "\"filled\":"<<!(vectorOfNodes_srv_->at(i).filter_filled)<<","<<std::endl;
319      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfNodes_srv_->at(i).context_id<<"\","<<std::endl;
320      outfiles[ctx] << "\"entry\":"<<"\""<<vectorOfNodes_srv_->at(i).expected_entry_nb<<"\","<<std::endl;
321      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfNodes_srv_->at(i).attributes<<"\","<<std::endl;
322      outfiles[ctx] << "\"type\":"<<"\"\"}"<<std::endl;
323    } 
324    for(int ctx=0; ctx<vectorOfContexts_srv_->size(); ctx++)
325    {
326      outfiles[ctx] << std::endl<<"],"<<std::endl<<"\"edges\" : ["<<std::endl;
327    }
328    for(int i=0; i<vectorOfEdges_srv_->size(); i++)
329    {
330      int ctx = vectorOfEdges_srv_->at(i).context;
331      if(i!=0) outfiles[ctx] << ",";
332      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
333      outfiles[ctx] << "\"from\":"<<vectorOfEdges_srv_->at(i).from<<","<<std::endl;
334      outfiles[ctx] << "\"to\":"<<vectorOfEdges_srv_->at(i).to<<","<<std::endl;
335      if(vectorOfEdges_srv_->at(i).show) outfiles[ctx] << "\"label\":"<<"\""<<vectorOfEdges_srv_->at(i).field->getId()<<"\\n"<<vectorOfEdges_srv_->at(i).date<<"\","<<std::endl;
336      else                               outfiles[ctx] << "\"label\":"<<"\"\\n"<<vectorOfEdges_srv_->at(i).date<<"\","<<std::endl;
337      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfEdges_srv_->at(i).context_id<<"\","<<std::endl;
338      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfEdges_srv_->at(i).attributes<<"\","<<std::endl;
339      outfiles[ctx] << "\"date\":"<<"\""<<vectorOfEdges_srv_->at(i).date<<"\"}"<<std::endl;
340    }
341    for(int ctx=0; ctx<vectorOfContexts_srv_->size(); ctx++)
342    {
343      outfiles[ctx] << std::endl<<"]}"<<std::endl;
344      outfiles[ctx].close();
345    }
346  }
347}
348
Note: See TracBrowser for help on using the repository browser.