forked from intel/fMBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage_exec_filter.cc
287 lines (244 loc) · 6.21 KB
/
coverage_exec_filter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* fMBT, free Model Based Testing tool
* Copyright (c) 2012, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "coverage_exec_filter.hh"
#include "model.hh"
#include "helper.hh"
#include "history.hh"
std::string Coverage_exec_filter::stringify()
{
return std::string("");
}
void Coverage_exec_filter::mhandler
(std::vector<std::string>& sp,std::vector<std::string>& n,
std::vector<std::string*>& from,std::vector<int>& act,
std::vector<int>& tag)
{
for(unsigned i=0;i<from.size();i++) {
// unescape
remove_force(*from[i]);
int pos=find(sp,*from[i],-1);
if (pos<0) {
pos=model->action_number(*(from[i]));
if (pos>0) {
act.push_back(pos);
} else {
std::vector<int> r;
regexpmatch(*(from[i]),sp,r,false);
if (!r.empty()) {
// Let's dump to actions...
tag.insert(tag.begin(),r.begin(),r.end());
} else {
regexpmatch(*(from[i]),n,r,false);
if (!r.empty()) {
act.insert(act.begin(),r.begin(),r.end());
} else {
status=false;
errormsg=errormsg+"No match for "+from[i]->c_str()+"\n";
}
}
}
} else {
tag.push_back(pos);
}
}
}
void Coverage_exec_filter::set_model(Model* _model)
{
Coverage::set_model(_model);
std::vector<std::string>& sp(model->getSPNames());
std::vector<std::string>& n(model->getActionNames());
mhandler(sp,n,from,start_action,start_tag);
mhandler(sp,n,to,end_action,end_tag);
mhandler(sp,n,drop,rollback_action,rollback_tag);
// Let's handle initial tags
execute(0);
}
bool Coverage_exec_filter::prop_set(std::vector<int> p,int npro,
int* props)
{
for(unsigned i=0;i<p.size();i++) {
for(int j=0;j<npro;j++) {
if (p[i]==props[j]) {
return true;
}
}
}
return false;
}
void Coverage_exec_filter::on_drop(int action,std::vector<int>&p)
{
online=false;
executed.clear();
etime.clear();
}
void Coverage_exec_filter::on_find(int action,std::vector<int>&p)
{
online=false;
executed.clear();
etime.clear();
}
void Coverage_exec_filter::on_start(int action,std::vector<int>&p)
{
online=true;
etime.push_back(History::current_time);
// Let's init....
if (prop_set(start_action,1,&action)) {
on_online(action,p);
}
}
void Coverage_exec_filter::on_online(int action,std::vector<int>&p) {
executed.push_back(std::pair<int,std::vector<int> >(action,p));
etime.push_back(History::current_time);
}
bool Coverage_exec_filter::execute(int action)
{
int* props=NULL;
int npro;
npro=model->getprops(&props);
std::vector<int> p(props,props+npro);
if (online) {
on_online(action,p);
} else {
on_offline(action,p);
}
if (online) {
/* Ok. Let's search for drop. */
if (prop_set(rollback_tag,npro,props) ||
prop_set(rollback_action,1,&action)) {
on_drop(action,p);
} else {
/* No drop? Let's search for to */
if (prop_set(end_tag,npro,props) ||
prop_set(end_action,1,&action)) {
on_find(action,p);
}
}
}
/* Let's search for from */
if (prop_set(start_tag,npro,props) ||
prop_set(start_action,1,&action)) {
if (online) {
on_restart(action,p);
} else {
on_start(action,p);
}
}
return true;
}
void Coverage_exec_filter::ds(std::string* s){
if (s)
delete s;
}
Coverage_exec_filter::~Coverage_exec_filter() {
}
#include "dparse.h"
extern "C" {
extern D_ParserTables parser_tables_filter;
}
extern std::vector<std::string*> *ff,*tt,*dd;
extern int filter_node_size;
class Coverage_from: public Coverage_exec_filter {
public:
Coverage_from(Log& l,std::string params):
Coverage_exec_filter(l,_f,_t,_d), sub(NULL)
{
std::vector<std::string> s;
commalist(params,s);
if (s.size()!=2) {
status=false;
errormsg="coverage from parse error "+params;
return;
}
sub=new_coverage(log,s[1]);
if (sub==NULL) {
status=false;
errormsg="can't create coverage "+s[1];
}
if (!sub->status) {
status=false;
errormsg=sub->errormsg;
}
ff=&_f;
tt=&_t;
dd=&_d;
D_Parser *p = new_D_Parser(&parser_tables_filter, filter_node_size);
remove_force(params);
bool ret=dparse(p,(char*)params.c_str(),strlen(s[0].c_str()));
ret=p->syntax_errors==0 && ret;
status=ret;
if (p->syntax_errors>0) {
errormsg="Syntax error...";
}
free_D_Parser(p);
from=_f;
to=_t;
drop=_d;
}
virtual ~Coverage_from() {
if (sub)
delete sub;
}
virtual void push() {
if (sub)
sub->push();
return Coverage_exec_filter::push();
}
virtual void pop() {
if (sub)
sub->pop();
return Coverage_exec_filter::pop();
}
virtual float getCoverage()
{
if (sub)
return sub->getCoverage();
return Coverage_exec_filter::getCoverage();
}
virtual int fitness(int* actions,int n, float* fitness) {
if (sub)
return sub->fitness(actions,n,fitness);
return Coverage_exec_filter::fitness(actions,n,fitness);
}
virtual void on_online(int action,std::vector<int>&p) {
if (sub) {
sub->execute(action);
}
Coverage_exec_filter::on_online(action,p);
}
virtual void on_start(int action,std::vector<int>&p) {
Coverage_exec_filter::on_start(action,p);
if (sub) {
sub->execute(action);
}
}
virtual void set_model(Model* _model) {
Coverage_exec_filter::set_model(_model);
if (status && sub) {
sub->set_model(model);
if (sub->status==false) {
status=false;
errormsg=errormsg+"Sub model failed*: "+sub->errormsg;
}
}
}
private:
std::vector<std::string*> _f,_t,_d;
Coverage* sub;
};
FACTORY_DEFAULT_CREATOR(Coverage, Coverage_from, "from")