source: XIOS/trunk/src/filter/binary_arithmetic_filter.cpp @ 1768

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

trunk : bug fix for workflowgraph

File size: 11.9 KB
Line 
1#include "binary_arithmetic_filter.hpp"
2#include "workflow_graph.hpp"
3#include "yacc_var.hpp"
4#include "file.hpp"
5
6
7namespace xios
8{
9  CScalarFieldArithmeticFilter::CScalarFieldArithmeticFilter(CGarbageCollector& gc, const std::string& op, double value)
10    : CFilter(gc, 1, this)
11    , op(operatorExpr.getOpScalarField(op))
12    , value(value)
13  { 
14    expression.assign(*yacc_globalInputText_ptr, 0, yacc_globalInputText_ptr->size()-1);
15  };
16
17  std::tuple<int, int, int> CScalarFieldArithmeticFilter::buildGraph(std::vector<CDataPacketPtr> data)
18  {
19    bool building_graph = this->tag ? data[0]->timestamp >= this->start_graph && data[0]->timestamp <= this->end_graph : false;
20    int unique_filter_id;
21    bool firstround;
22
23    if(building_graph)
24    {
25      CWorkflowGraph::allocNodeEdge();
26
27      size_t filterhash = std::hash<StdString>{}(expression+to_string(data[0]->timestamp)+this->field->getId());
28
29      // first round
30      if(CWorkflowGraph::mapHashFilterID_ptr->find(filterhash) == CWorkflowGraph::mapHashFilterID_ptr->end())
31      {
32        firstround = true;
33        this->filterID = InvalidableObject::filterIdGenerator++;
34        int edgeID = InvalidableObject::edgeIdGenerator++;
35
36        CWorkflowGraph::addNode(this->filterID, "Arithmetic Filter\\n("+expression+")", 3, 1, 0, data[0]);
37        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].filter_tag = this->tag;
38        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = data[0]->distance+1;
39
40
41        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes();
42        if(this->field->file) (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes += "</br>file attributes : </br>" +this->field->file->record4graphXiosAttributes();
43     
44
45        if(CWorkflowGraph::build_begin)
46        {
47          CWorkflowGraph::addEdge(edgeID, this->filterID, data[0]);
48          (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].expected_entry_nb ++;
49
50          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ;
51        }
52        else CWorkflowGraph::build_begin = true;
53
54        (*CWorkflowGraph::mapHashFilterID_ptr)[filterhash] = this->filterID; 
55        unique_filter_id = this->filterID;
56      }
57      // not first round
58      else 
59      {
60        firstround=false;
61        unique_filter_id = (*CWorkflowGraph::mapHashFilterID_ptr)[filterhash];
62        if(data[0]->src_filterID != unique_filter_id)
63        {
64          int edgeID = InvalidableObject::edgeIdGenerator++;
65          CWorkflowGraph::addEdge(edgeID, unique_filter_id, data[0]); 
66          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ; 
67          (*CWorkflowGraph::mapFilters_ptr_with_info)[unique_filter_id].expected_entry_nb ++;
68        }
69      } 
70    }
71
72    return std::make_tuple(building_graph, firstround, unique_filter_id);
73  }
74
75
76  CDataPacketPtr CScalarFieldArithmeticFilter::apply(std::vector<CDataPacketPtr> data)
77  {
78    CDataPacketPtr packet(new CDataPacket);
79    packet->date = data[0]->date;
80    packet->timestamp = data[0]->timestamp;
81    packet->status = data[0]->status;
82
83    std::tuple<int, int, int> graph = buildGraph(data);
84
85    if(std::get<0>(graph)) packet->src_filterID = std::get<2>(graph);
86    if(std::get<0>(graph) && std::get<1>(graph)) packet->distance = data[0]->distance+1;
87    if(std::get<0>(graph) && !std::get<1>(graph)) packet->distance = data[0]->distance;
88
89    packet->field = this->field;
90
91    if (packet->status == CDataPacket::NO_ERROR)
92      packet->data.reference(op(value, data[0]->data));
93
94    return packet;
95  }
96
97  CFieldScalarArithmeticFilter::CFieldScalarArithmeticFilter(CGarbageCollector& gc, const std::string& op, double value)
98    : CFilter(gc, 1, this)
99    , op(operatorExpr.getOpFieldScalar(op))
100    , value(value)
101  { 
102    expression.assign(*yacc_globalInputText_ptr, 0, yacc_globalInputText_ptr->size()-1);
103  };
104
105  std::tuple<int, int, int> CFieldScalarArithmeticFilter::buildGraph(std::vector<CDataPacketPtr> data)
106  {
107    bool building_graph = this->tag ? data[0]->timestamp >= this->start_graph && data[0]->timestamp <= this->end_graph : false;
108    // bool building_graph = true;
109    int unique_filter_id;
110    bool firstround;
111
112    if(building_graph)
113    {
114      CWorkflowGraph::allocNodeEdge();
115
116      size_t filterhash = std::hash<StdString>{}(expression+to_string(data[0]->timestamp)+this->field->getId());
117
118      // first round
119      if(CWorkflowGraph::mapHashFilterID_ptr->find(filterhash) == CWorkflowGraph::mapHashFilterID_ptr->end())
120      {
121        firstround = true;
122        this->filterID = InvalidableObject::filterIdGenerator++;
123        int edgeID = InvalidableObject::edgeIdGenerator++;
124
125        CWorkflowGraph::addNode(this->filterID, "Arithmetic Filter\\n("+expression+")", 3, 1, 0, data[0]);
126        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].filter_tag = this->tag;
127        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = data[0]->distance+1;
128
129
130        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes();
131        if(this->field->file) (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes += "</br>file attributes : </br>" +this->field->file->record4graphXiosAttributes();
132     
133
134        if(CWorkflowGraph::build_begin)
135        {
136          CWorkflowGraph::addEdge(edgeID, this->filterID, data[0]);
137          (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].expected_entry_nb ++;
138
139          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ;
140        }
141        else CWorkflowGraph::build_begin = true;
142
143        (*CWorkflowGraph::mapHashFilterID_ptr)[filterhash] = this->filterID; 
144        unique_filter_id = this->filterID;
145      }
146      // not first round
147      else 
148      {
149        firstround=false;
150        unique_filter_id = (*CWorkflowGraph::mapHashFilterID_ptr)[filterhash];
151        if(data[0]->src_filterID != unique_filter_id)
152        {
153          int edgeID = InvalidableObject::edgeIdGenerator++;
154          CWorkflowGraph::addEdge(edgeID, unique_filter_id, data[0]); 
155          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ; 
156          (*CWorkflowGraph::mapFilters_ptr_with_info)[unique_filter_id].expected_entry_nb ++;
157        }
158      } 
159    }
160
161    return std::make_tuple(building_graph, firstround, unique_filter_id);
162  }
163
164  CDataPacketPtr CFieldScalarArithmeticFilter::apply(std::vector<CDataPacketPtr> data)
165  {
166    CDataPacketPtr packet(new CDataPacket);
167    packet->date = data[0]->date;
168    packet->timestamp = data[0]->timestamp;
169    packet->status = data[0]->status;
170
171    std::tuple<int, int, int> graph = buildGraph(data);
172
173    if(std::get<0>(graph)) packet->src_filterID = std::get<2>(graph);
174    if(std::get<0>(graph) && std::get<1>(graph)) packet->distance = data[0]->distance+1;
175    if(std::get<0>(graph) && !std::get<1>(graph)) packet->distance = data[0]->distance;
176
177    packet->field = this->field;
178
179    if (packet->status == CDataPacket::NO_ERROR)
180      packet->data.reference(op(data[0]->data, value));
181
182    return packet;
183  }
184
185  CFieldFieldArithmeticFilter::CFieldFieldArithmeticFilter(CGarbageCollector& gc, const std::string& op)
186    : CFilter(gc, 2, this)
187    , op(operatorExpr.getOpFieldField(op))
188  { 
189    expression.assign(*yacc_globalInputText_ptr, 0, yacc_globalInputText_ptr->size()-1);
190  };
191
192  std::tuple<int, int, int> CFieldFieldArithmeticFilter::buildGraph(std::vector<CDataPacketPtr> data)
193  {
194    bool building_graph = this->tag ? ((data[0]->timestamp >= this->field->field_graph_start && data[0]->timestamp <= this->field->field_graph_end) && (data[0]->timestamp == data[1]->timestamp)) : false;
195
196    int unique_filter_id;
197
198    bool firstround;
199
200    if(building_graph)
201    { 
202      CWorkflowGraph::allocNodeEdge();
203
204      // std::cout<<"CFieldFieldArithmeticFilter::apply filter tag = "<<this->tag<<std::endl;
205
206      size_t filterhash = std::hash<StdString>{}(expression+to_string(data[0]->timestamp)+this->field->getId());
207
208      // first round
209      if(CWorkflowGraph::mapHashFilterID_ptr->find(filterhash) == CWorkflowGraph::mapHashFilterID_ptr->end())
210      {
211        firstround = true;
212        this->filterID = InvalidableObject::filterIdGenerator++;
213        int edgeID = InvalidableObject::edgeIdGenerator++;
214   
215        CWorkflowGraph::addNode(this->filterID, "Arithmetic Filter\\n("+expression+")", 3, 1, 0, data[0]);
216        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes = this->field->record4graphXiosAttributes();
217        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].distance = data[0]->distance+1;
218
219        if(this->field->file) (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].attributes += "</br>file attributes : </br>" +this->field->file->record4graphXiosAttributes();
220   
221        (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].filter_tag = this->tag;
222        if(CWorkflowGraph::build_begin)
223        {
224
225          CWorkflowGraph::addEdge(edgeID, this->filterID, data[0]);
226          (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].expected_entry_nb ++;
227
228          edgeID = InvalidableObject::edgeIdGenerator++;
229
230          CWorkflowGraph::addEdge(edgeID, this->filterID, data[1]);
231          (*CWorkflowGraph::mapFilters_ptr_with_info)[this->filterID].expected_entry_nb ++;
232
233          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ;
234          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[1]->src_filterID].filter_filled = 0 ;
235        }
236        CWorkflowGraph::build_begin = true;
237
238        (*CWorkflowGraph::mapHashFilterID_ptr)[filterhash] = this->filterID; 
239        unique_filter_id = this->filterID;
240 
241      }
242      // not first round
243      else 
244      {
245        firstround = false;
246        unique_filter_id = (*CWorkflowGraph::mapHashFilterID_ptr)[filterhash];
247        if(data[0]->src_filterID != unique_filter_id)
248        {
249          int edgeID = InvalidableObject::edgeIdGenerator++;
250          CWorkflowGraph::addEdge(edgeID, unique_filter_id, data[0]); 
251          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[0]->src_filterID].filter_filled = 0 ; 
252          (*CWorkflowGraph::mapFilters_ptr_with_info)[unique_filter_id].expected_entry_nb ++;
253        }
254        if(data[1]->src_filterID != unique_filter_id)
255        { 
256          int edgeID = InvalidableObject::edgeIdGenerator++;
257          CWorkflowGraph::addEdge(edgeID, unique_filter_id, data[1]); 
258          (*CWorkflowGraph::mapFilters_ptr_with_info)[data[1]->src_filterID].filter_filled = 0 ;
259          (*CWorkflowGraph::mapFilters_ptr_with_info)[unique_filter_id].expected_entry_nb ++;
260        }
261       
262      } 
263    }
264
265    return std::make_tuple(building_graph, firstround, unique_filter_id);
266  }
267
268  CDataPacketPtr CFieldFieldArithmeticFilter::apply(std::vector<CDataPacketPtr> data)
269  {
270    CDataPacketPtr packet(new CDataPacket);
271    packet->date = data[0]->date;
272    packet->timestamp = data[0]->timestamp;
273
274    std::tuple<int, int, int> graph = buildGraph(data);
275
276    if(std::get<0>(graph)) packet->src_filterID = std::get<2>(graph);
277    if(std::get<0>(graph) && std::get<1>(graph)) packet->distance = data[0]->distance+1;
278    if(std::get<0>(graph) && !std::get<1>(graph)) packet->distance = data[0]->distance;
279   
280    packet->field = this->field;
281   
282
283    if (data[0]->status != CDataPacket::NO_ERROR)
284      packet->status = data[0]->status;
285    else if (data[1]->status != CDataPacket::NO_ERROR)
286      packet->status = data[1]->status;
287    else
288    {
289      packet->status = CDataPacket::NO_ERROR;
290      packet->data.reference(op(data[0]->data, data[1]->data));
291    }
292
293    return packet;
294  }
295
296  StdString CScalarFieldArithmeticFilter::GetName(void)    { return StdString("CScalarFieldArithmeticFilter"); }
297  StdString CFieldScalarArithmeticFilter::GetName(void)    { return StdString("CFieldScalarArithmeticFilter"); }
298  StdString CFieldFieldArithmeticFilter::GetName(void)     { return StdString("CFieldFieldArithmeticFilter"); }
299
300
301} // namespace xios
Note: See TracBrowser for help on using the repository browser.