forked from hundlab/LongQt-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasuremanager.cpp
228 lines (205 loc) · 6.66 KB
/
measuremanager.cpp
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
#include "measuremanager.h"
#include "cellutils.h"
#include <QDebug>
MeasureManager::MeasureManager(shared_ptr<Cell> cell): __cell(cell) {};
MeasureManager::~MeasureManager() {
if(this->ofile) {
ofile->close();
}
}
MeasureManager::MeasureManager(const MeasureManager& o) {
this->copy(o);
}
MeasureManager* MeasureManager::clone() {
return new MeasureManager(*this);
}
shared_ptr<Cell> MeasureManager::cell() {
return this->__cell;
}
void MeasureManager::copy(const MeasureManager& o) {
__cell = o.__cell;
variableSelection = o.variableSelection;
__percrepol = o.__percrepol;
last = o.last;
}
void MeasureManager::cell(shared_ptr<Cell> cell) {
if(cell->type() != __cell->type()) {
this->variableSelection.clear();
this->measures.clear();
}
this->__cell = cell;
}
map<string,set<string>> MeasureManager::selection() {
return this->variableSelection;
}
void MeasureManager::selection(map<string,set<string>> sel) {
this->variableSelection = sel;
}
double MeasureManager::percrepol() {
return this->__percrepol;
}
void MeasureManager::percrepol(double percrepol) {
this->__percrepol = percrepol;
}
shared_ptr<Measure> MeasureManager::getMeasure(string varname, set<string> selection) {
if(varsMeas.count(varname)>0) {
string measName = varsMeas.at(varname);
return shared_ptr<Measure>(varMeasCreator.at(measName)(selection));
}
return shared_ptr<Measure>(new Measure(selection));
}
void MeasureManager::addMeasure(string var,set<string> selection) {
variableSelection.insert({var,selection});
}
void MeasureManager::removeMeasure(string var) {
measures.erase(var);
}
void MeasureManager::setupMeasures(string filename) {
this->measures.clear();
if(variableSelection.count("vOld") == 0) {
variableSelection.insert({"vOld",{}});
}
this->removeBad();
this->ofile.reset(new QFile(filename.c_str()));
if (!ofile->open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning("MeasureManager: File could not be opened for writing.");
}
for(auto& sel: variableSelection) {
auto it = measures
.insert({sel.first,this->getMeasure(sel.first,sel.second)}).first;
string nameStr = it->second->getNameString(sel.first);
if(ofile->write(nameStr.c_str())==-1) {
qWarning("MeasureManager: File cound not be written to");
}
}
if(ofile->write("\n")==-1) {
qWarning("MeasureManager: File cound not be written to");
}
}
void MeasureManager::measure(double time) {
bool write = false;
for(auto& m: this->measures) {
bool varWrite = m.second->measure(time, *__cell->vars.at(m.first));
if(m.first=="vOld"&&varWrite) {
write = true;
}
}
if(write) {
this->write();
this->resetMeasures();
}
}
void MeasureManager::writeLast(string filename) {
QFile lastFile(filename.c_str());
if (!lastFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning("MeasureManager: File could not be opened for writing.");
}
for(auto& meas: this->measures) {
string nameStr = meas.second->getNameString(meas.first);
if(lastFile.write(nameStr.c_str())==-1) {
qWarning("MeasureManager: Last file cound not be written to");
}
}
if(lastFile.write("\n")==-1) {
qWarning("MeasureManager: Last file cound not be written to");
}
if(this->last == "") {
this->write(&lastFile);
} else {
if(lastFile.write(last.c_str())==-1) {
qWarning("MeasureManager: Last file cound not be written to");
}
}
lastFile.close();
}
void MeasureManager::write(QFile* file) {
QFile& ofile = file? *file: *this->ofile;
if(!ofile.isOpen()) {
qWarning("MeasureManager: Measure file must be open to be written");
return;
}
this->last = "";
for(auto& meas: measures) {
string valStr = meas.second->getValueString();
last += valStr;
if(ofile.write(valStr.c_str())==-1) {
qWarning("MeasureManager: File cound not be written to");
}
}
last += "\n";
if(ofile.write("\n")==-1) {
qWarning("MeasureManager: File cound not be written to");
}
}
void MeasureManager::clear() {
__percrepol = 50;
variableSelection.clear();
measures.clear();
}
void MeasureManager::close() {
if(this->ofile->isOpen()) {
ofile->close();
}
}
void MeasureManager::resetMeasures() {
for(auto& meas: this->measures) {
meas.second->reset();
}
}
//#############################################################
//read and mvars style file and use it to set measures list
//where varname is the first word on the line
//and all sequential words are properties to measure (the selection)
//#############################################################
bool MeasureManager::readMvarsFile(QXmlStreamReader& xml) {
variableSelection.clear();
set<string> possible_vars = __cell->getVariables();
if(!CellUtils::readNext(xml, "mvars")) return false;
if(xml.readNextStartElement() && xml.name()=="percrepol") {
xml.readNext();
__percrepol = xml.text().toDouble();
xml.skipCurrentElement();
} else {
return true;
}
while(!xml.atEnd() && xml.readNextStartElement() && xml.name()=="mvar"){
string varname = xml.attributes().value("name").toString().toStdString();
set<string> selection;
while(!xml.atEnd() && xml.readNextStartElement() && xml.name()=="property"){
xml.readNext();
string propName = xml.text().toString().toStdString();
selection.insert(propName);
xml.skipCurrentElement();
}
if(possible_vars.count(varname) == 1) {
variableSelection.insert({varname,selection});
}
}
return true;
};
bool MeasureManager::writeMVarsFile(QXmlStreamWriter& xml) {
xml.writeStartElement("mvars");
xml.writeTextElement("percrepol",QString::number(__percrepol));
for(auto& sel: this->variableSelection){
xml.writeStartElement("mvar");
xml.writeAttribute("name",sel.first.c_str());
for(auto& prop: sel.second){
xml.writeTextElement("property",prop.c_str());
}
xml.writeEndElement();
}
xml.writeEndElement();
return 0;
}
void MeasureManager::removeBad() {
list<map<string,set<string>>::iterator> bad;
auto vars = __cell->getVariables();
for(auto it = this->variableSelection.begin(); it != variableSelection.end(); ++it) {
if(vars.count(it->first) != 1) {
bad.push_back(it);
}
}
for(auto& b: bad) {
variableSelection.erase(b);
}
}