forked from intel/fMBT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_model.cc
102 lines (89 loc) · 2.43 KB
/
adapter_model.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
/*
* fMBT, free Model Based Testing tool
* Copyright (c) 2011, 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 "adapter_model.hh"
#include "helper.hh"
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include "random.hh"
Adapter_model::Adapter_model(Log& l,const std::string& params) :
Adapter::Adapter(l)
{
model = new_model(log,params);
model->init();
model->reset();
r=Random::default_random();
}
bool Adapter_model::init()
{
std::vector<std::string>& mactions=model->getActionNames();
adapter2model.resize(actions->size());
for(unsigned i = 0; i < actions->size(); i++) {
adapter2model[i]=find(mactions, (*actions)[i]);
}
model2adapter.resize(mactions.size());
for(unsigned i = 0; i < mactions.size(); i++) {
model2adapter[i]=find(*actions, mactions[i]);
}
return true;
}
std::string Adapter_model::stringify()
{
std::ostringstream t(std::ios::out | std::ios::binary);
std::string s=model->stringify();
t << "model:" << capsulate(s) << std::endl;
return t.str();
}
/* adapter can execute.. */
void Adapter_model::execute(std::vector<int> &action)
{
log.push("Adapter_model");
log.print("<action type=\"input\" name=\"%s\"/>",
getUActionName(action[0]));
if (!model->execute(adapter2model[action[0]])) {
action[0]=0;
}
log.pop();
}
Adapter_model::~Adapter_model()
{
if (r)
r->unref();
}
int Adapter_model::observe(std::vector<int> &action,bool block)
{
int* act,*iact;
int actions=-model->getIActions(&iact)+model->getActions(&act);
int i=1+r->rand()%actions;
int a;
if (!actions) {
return false;
}
a=-1;
while (i) {
a++;
while (!model->is_output(act[a])) {
i--;
}
}
action.resize(1);
action[0]=act[a];
return true;
}
FACTORY_DEFAULT_CREATOR(Adapter, Adapter_model, "model")