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

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

Graph intermediate commit to a tmp branch.

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